| 1 | package YATG::Store::Disk; | ||
| 2 | { | ||
| 3 | $YATG::Store::Disk::VERSION = '5.140510'; | ||
| 4 | } | ||
| 5 | |||
| 6 | use strict; | ||
| 7 | use warnings FATAL => 'all'; | ||
| 8 | |||
| 9 | use Time::Local; | ||
| 10 | use Tie::File::FixedRecLen::Store; | ||
| 11 | |||
| 12 | sub store { | ||
| 13 | my ($config, $stamp, $results) = @_; | ||
| 14 | my $root = $config->{yatg}->{disk_root}; | ||
| 15 | |||
| 16 | # results look like this: | ||
| 17 | # $results->{host}->{leaf}->{port} = {value} | ||
| 18 | |||
| 19 | foreach my $device (keys %$results) { | ||
| 20 | eval { mkdir "$root/$device" }; | ||
| 21 | |||
| 22 | foreach my $leaf (keys %{$results->{$device}}) { | ||
| 23 | my %data = %{$results->{$device}->{$leaf}}; | ||
| 24 | |||
| 25 | foreach my $port (keys %data) { | ||
| 26 | (my $mport = $port) =~ s/[^A-Za-z0-9]/./g; | ||
| 27 | |||
| 28 | my @files = grep {m/\d{4}-\d\d-\d\d_\d\d-\d\d-\d\dZ?,\d+$/} | ||
| 29 | glob("$root/$device/$leaf/$mport/*"); | ||
| 30 | |||
| 31 | my (@store, $filename, $offset); | ||
| 32 | |||
| 33 | if (scalar @files == 0) { | ||
| 34 | eval { mkdir "$root/$device/$leaf" }; | ||
| 35 | eval { mkdir "$root/$device/$leaf/$mport" }; | ||
| 36 | |||
| 37 | my ($sec,$min,$hour,$mday, | ||
| 38 | $mon,$year,$wday,$yday,$isdst) = gmtime($stamp); | ||
| 39 | $filename = sprintf "%04d-%02d-%02d_%02d-%02d-%02d", | ||
| 40 | ($year+1900), ($mon+1), $mday, $hour, $min, $sec; | ||
| 41 | |||
| 42 | $filename .= 'Z,' . $config->{yatg}->{interval}; | ||
| 43 | $offset = 0; | ||
| 44 | } | ||
| 45 | else { | ||
| 46 | $filename = (sort @files)[-1]; | ||
| 47 | $filename =~ s#$root/$device/$leaf/$mport/##; | ||
| 48 | $filename | ||
| 49 | =~ m/^(\d{4})-(\d\d)-(\d\d)_(\d\d)-(\d\d)-(\d\d)(Z?),(\d+)$/; | ||
| 50 | |||
| 51 | my $base_time = $7 ? | ||
| 52 | timegm($6,$5,$4,$3,$2-1,$1-1900) : | ||
| 53 | timelocal($6,$5,$4,$3,$2-1,$1-1900); | ||
| 54 | my $interval = $8; | ||
| 55 | |||
| 56 | $offset = (($stamp - $base_time) / $interval); | ||
| 57 | } | ||
| 58 | |||
| 59 | tie @store, 'Tie::File::FixedRecLen::Store', | ||
| 60 | "$root/$device/$leaf/$mport/$filename", | ||
| 61 | record_length => 20; | ||
| 62 | next unless tied @store; | ||
| 63 | |||
| 64 | eval { $store[$offset] = $data{$port} }; | ||
| 65 | untie @store; | ||
| 66 | } | ||
| 67 | } | ||
| 68 | } | ||
| 69 | |||
| 70 | return 1; | ||
| 71 | } | ||
| 72 | |||
| 73 | 1; | ||
| 74 | |||
| 75 | # ABSTRACT: Back-end module to store polled data to disk | ||
| 76 | |||
| 77 | |||
| 78 | __END__ | ||
| 79 | =pod | ||
| 80 | |||
| 81 | =head1 NAME | ||
| 82 | |||
| 83 | YATG::Store::Disk - Back-end module to store polled data to disk | ||
| 84 | |||
| 85 | =head1 VERSION | ||
| 86 | |||
| 87 | version 5.140510 | ||
| 88 | |||
| 89 | =head1 DESCRIPTION | ||
| 90 | |||
| 91 | This module implements part of a callback handler used to store SNMP data to | ||
| 92 | disk quickly, although not necessarily compactly. Disk is cheaper than CPU, | ||
| 93 | after all. | ||
| 94 | |||
| 95 | Our recommendation is that disk-based storage only be used with Interface | ||
| 96 | Indexed SNMP OIDs. The module will work with other results but retrieval will | ||
| 97 | be a bit messier, and we have not really tested it. | ||
| 98 | |||
| 99 | Given a location on your filesystem, each result of the poll of each SNMP OID | ||
| 100 | is written to a file. There is one file per OID per "port" per device. In this | ||
| 101 | context "port" might be a real network interface such as C<GigabitEthernet5/1> | ||
| 102 | or an index, if say the values are CPU loads and there are a few CPUs. | ||
| 103 | |||
| 104 | Input to the module is a data structure with SNMP poll results, like so: | ||
| 105 | |||
| 106 | $results->{ip}->{leaf}->{port} = {value} | ||
| 107 | |||
| 108 | And then the C<value> will get written to a file: | ||
| 109 | |||
| 110 | $root/ip/leaf/port/$timestamp,$interval | ||
| 111 | |||
| 112 | Here, C<$root> is set in the configuration (see below). The port name is | ||
| 113 | munged to translate non-alphanumeric characters to a dot (so it's | ||
| 114 | filesystem-safe on common OSes). The C<$timestamp> is set when the file is | ||
| 115 | created (if it's the first storage for this ip/leaf/port combination), and the | ||
| 116 | C<$interval> is read from the C<yatg_updater> configuration as the SNMP | ||
| 117 | polling interval. | ||
| 118 | |||
| 119 | With this data encoded in the filename and path, the content of the file is | ||
| 120 | each value on its own line. The timestamp of the first line in the file is | ||
| 121 | that of the filename, and each subsequent line is an C<$interval> in the | ||
| 122 | future from that. The file is padded out when there are missing data values. | ||
| 123 | |||
| 124 | Most of this you don't really need to worry about, because you get data back | ||
| 125 | using the L<YATG::Retrieve::Disk> module. | ||
| 126 | |||
| 127 | =head1 REQUIREMENTS | ||
| 128 | |||
| 129 | Install the following additional modules to use this plugin: | ||
| 130 | |||
| 131 | =over 4 | ||
| 132 | |||
| 133 | =item * | ||
| 134 | |||
| 135 | L<Tile::File::FixedRecLen> | ||
| 136 | |||
| 137 | =item * | ||
| 138 | |||
| 139 | L<integer> | ||
| 140 | |||
| 141 | =item * | ||
| 142 | |||
| 143 | L<Time::Local> | ||
| 144 | |||
| 145 | =item * | ||
| 146 | |||
| 147 | L<Fcntl> | ||
| 148 | |||
| 149 | =back | ||
| 150 | |||
| 151 | =head1 CONFIGURATION | ||
| 152 | |||
| 153 | The only configuration you need to provide is for the file path of the | ||
| 154 | C<$root> for data storage. Set this in the main configuration, the default | ||
| 155 | being C</var/lib/yatg>: | ||
| 156 | |||
| 157 | yatg: | ||
| 158 | disk_root: '/tmp/yatg' | ||
| 159 | |||
| 160 | Note that the user under which the C<yatg_updater> process runs must be able | ||
| 161 | to create files and directories and write to them in this location. | ||
| 162 | |||
| 163 | =head1 SEE ALSO | ||
| 164 | |||
| 165 | =over 4 | ||
| 166 | |||
| 167 | =item L<Tie::File::FixedRecLen::Store> | ||
| 168 | |||
| 169 | =back | ||
| 170 | |||
| 171 | =head1 AUTHOR | ||
| 172 | |||
| 173 | Oliver Gorwits <oliver@cpan.org> | ||
| 174 | |||
| 175 | =head1 COPYRIGHT AND LICENSE | ||
| 176 | |||
| 177 | This software is copyright (c) 2014 by University of Oxford. | ||
| 178 | |||
| 179 | This is free software; you can redistribute it and/or modify it under | ||
| 180 | the same terms as the Perl 5 programming language system itself. | ||
| 181 | |||
| 182 | =cut | ||
| 183 |