Merge branch 'collectd-3.11'
[collectd.git] / contrib / migrate-3-4.px
1 #!/usr/bin/perl
2
3 use strict;
4 use warnings;
5
6 use Getopt::Long ('GetOptions');
7 use Data::Dumper ();
8 use File::Basename ('dirname');
9
10 our $InDir = '/var/lib/collectd';
11 our $OutDir = '/tmp/collectd-4';
12 our $Hostname = 'localhost';
13
14 # Types:
15 # +------------+----------------------+----+----+----+
16 # ! Subdir     ! Type                 ! ti ! pi ! ex !
17 # +------------+----------------------+----+----+----+
18 # ! apache     ! apache_bytes         !    !    !    !
19 # ! apache     ! apache_requests      !    !    !    !
20 # ! apache     ! apache_scoreboard    ! x  !    !    !
21 # ! battery    ! charge               !    ! x  !    !
22 # ! apcups     ! charge_percent       !    !    !    !
23 # !            ! cpu                  ! x  !    ! x  !
24 # !            ! cpufreq              ! x  !    !    !
25 # ! battery    ! current              !    ! x  !    !
26 # ! ntpd       ! delay                ! x  !    !    !
27 # !            ! df                   ! x  !    !    !
28 # !            ! disk                 ! x  !    !    !
29 # ! dns        ! dns_traffic          !    !    !    !
30 # ! apple_se.. ! fanspeed             ! x  !    !    !
31 # ! mbmon      ! fanspeed             ! x  !    !    !
32 # ! apcups     ! frequency            ! x  !    !    !
33 # ! ntpd       ! frequency_offset     ! x  !    !    !
34 # !            ! hddtemp              ! x  !    !    !
35 # ! interface  ! if_errors            !    ! x  !    !
36 # ! interface  ! if_packets           !    ! x  !    !
37 # !            ! lm_sensors           !    !    !    !
38 # !            ! load                 !    !    !    !
39 # ! apcups     ! load_percent         !    !    !    !
40 # !            ! memory               !    !    !    !
41 # !            ! multimeter           !    !    !    !
42 # ! mysql      ! mysql_commands       ! x  !    !    !
43 # ! mysql      ! mysql_handler        ! x  !    !    !
44 # ! mysql      ! mysql_qcache         !    !    !    !
45 # ! mysql      ! mysql_threads        !    !    !    !
46 # !            ! nfs2_procedures      ! x  !    ! x  !
47 # !            ! nfs3_procedures      ! x  !    ! x  !
48 # ! dns        ! opcode               ! x  !    !    !
49 # !            ! partition            ! x  !    !    !
50 # !            ! ping                 ! x  !    !    !
51 # !            ! processes            !    !    !    !
52 # ! processes  ! ps_count             ! x  !    !    !
53 # ! processes  ! ps_cputime           ! x  !    !    !
54 # ! processes  ! ps_pagefaults        ! x  !    !    !
55 # ! processes  ! ps_rss               ! x  !    !    !
56 # ! dns        ! qtype                ! x  !    !    !
57 # ! dns        ! rcode                ! x  !    !    !
58 # ! (*)        ! sensors              ! x  !    !    !
59 # !            ! serial               ! x  !    !    !
60 # !            ! swap                 !    !    !    !
61 # !            ! tape                 ! x  !    !    !
62 # ! apple_se.. ! temperature          ! x  !    !    !
63 # ! mbmon      ! temperature          ! x  !    !    !
64 # ! ntpd       ! time_dispersion      ! x  !    !    !
65 # ! ntpd       ! time_offset          ! x  !    !    !
66 # ! apcups     ! timeleft             !    !    !    !
67 # !            ! traffic              ! x  !    !    ! ->rx,tx
68 # ! vserver    ! traffic              ! x  ! x  !    ! ->rx.tx
69 # !            ! users                !    !    !    !
70 # ! apucups    ! voltage              ! x  !    !    !
71 # ! battery    ! voltage              !    ! x  !    !
72 # ! mbmon      ! voltage              ! x  !    !    !
73 # ! vserver    ! vs_memory            !    ! x  !    !
74 # ! vserver    ! vs_processes         !    ! x  !    !
75 # ! vserver    ! vs_threads           !    ! x  !    !
76 # !            ! wireless             ! x  !    !    !
77 # +------------+----------------------+----+----+----+
78
79 our %Subdirs =
80 (
81         apache => 0,
82         apcups => 0,
83         apple_sensors => 0,
84         battery => 1,
85         dns => 0,
86         interface => 1,
87         mbmon => 0,
88         mysql => 0,
89         ntpd => 0,
90         processes => 0,
91         sensors => 1,
92         vserver => 1
93 );
94
95 our %TypeTranslate =
96 (
97         cpu => sub { $_ = shift; $_->{'plugin_instance'} = $_->{'type_instance'}; $_->{'type_instance'} = undef; $_; },
98         if_errors => sub { $_ = shift; $_->{'type_instance'} = $_->{'plugin_instance'}; $_->{'plugin_instance'} = undef; $_; },
99         if_packets => sub { $_ = shift; $_->{'type_instance'} = $_->{'plugin_instance'}; $_->{'plugin_instance'} = undef; $_; },
100         nfs2_procedures => sub { $_ = shift; @$_{qw(plugin plugin_instance type type_instance)} = ('nfs', 'v2' . $_->{'type_instance'}, 'nfs_procedure', undef); $_; },
101         nfs3_procedures => sub { $_ = shift; @$_{qw(plugin plugin_instance type type_instance)} = ('nfs', 'v3' . $_->{'type_instance'}, 'nfs_procedure', undef); $_; },
102         partition => sub { $_ = shift; $_->{'plugin'} = 'disk'; $_; },
103         processes => sub { $_ = shift; $_->{'type'} = 'ps_state'; $_; },
104         traffic => sub { $_ = shift; $_->{'plugin'} =~ s/^traffic$/interface/; @$_{qw(plugin_instance type)} = (undef, 'if_octets'); $_; }
105 );
106
107 our %TypeSplit =
108 (
109         cpu => { from => [qw(user nice syst idle wait)], to => 'value', type_instance => [qw(user nice system idle wait)] },
110         memory => { from => [qw(used free buffers cached)], to => 'value', type_instance => [qw(used free buffered cached)] },
111         nfs3_procedures => { from => [qw(null getattr lookup access readlink
112                 read write create mkdir symlink mknod remove rmdir rename link
113                 readdir readdirplus fsstat fsinfo pathconf commit)], to => 'value' },
114         nfs2_procedures => { from => [qw(create fsstat getattr link lookup
115                 mkdir null read readdir readlink remove rename rmdir root
116                 setattr symlink wrcache write)], to => 'value' },
117         processes => { from => [qw(running sleeping zombies stopped paging blocked)], to => 'value' },
118         swap => { from => [qw(cached free used resv)], to => 'value', type_instance => [qw(cached free used reserved)] }
119 );
120
121 our %TypeRename =
122 (
123         traffic => { from => [qw(incoming outgoing)], to => [qw(rx tx)] }
124 );
125
126 GetOptions ("indir|i=s" => \$InDir,
127         "outdir|o=s" => \$OutDir,
128         "hostname=s" => \$Hostname) or exit_usage ();
129
130 die "No such directory: $InDir" if (!-d $InDir);
131
132 our @Files = ();
133 our %OutDirs = ();
134
135 @Files = find_files ();
136
137 for (@Files)
138 {
139         my $orig_filename = $_;
140         my $orig = parse_file ($orig_filename);
141         my $dest = translate_file ($orig);
142         my $dest_filename = get_filename ($dest);
143
144         my $dest_directory = dirname ($dest_filename);
145         if (!exists ($OutDirs{$dest_directory}))
146         {
147                 print "[ -d '$OutDir/$dest_directory' ] || mkdir -p '$OutDir/$dest_directory'\n";
148                 $OutDirs{$dest_directory} = 1;
149         }
150
151         if (($orig->{'type'} eq 'disk') || ($orig->{'type'} eq 'partition'))
152         {
153                 special_disk ($orig_filename, $orig, $dest_filename, $dest);
154         }
155         elsif (exists ($TypeSplit{$orig->{'type'}}))
156         {
157                 my $src_dses = $TypeSplit{$orig->{'type'}}->{'from'};
158                 my $dst_ds = $TypeSplit{$orig->{'type'}}->{'to'};
159                 my $type_instances = exists ($TypeSplit{$orig->{'type'}}->{'type_instance'})
160                         ? $TypeSplit{$orig->{'type'}}->{'type_instance'}
161                         : $TypeSplit{$orig->{'type'}}->{'from'};
162
163                 for (my $i = 0; $i < @$src_dses; $i++)
164                 {
165                         my $src_ds = $src_dses->[$i];
166                         $dest->{'type_instance'} = $type_instances->[$i];
167                         $dest_filename = get_filename ($dest);
168                         print "./extractDS.px -i '$InDir/$orig_filename' -s '$src_ds' -o '$OutDir/$dest_filename' -d '$dst_ds'\n";
169                 }
170         }
171         elsif (exists ($TypeRename{$orig->{'type'}}))
172         {
173                 my $src_dses = $TypeRename{$orig->{'type'}}->{'from'};
174                 my $dst_dses = $TypeRename{$orig->{'type'}}->{'to'};
175                 my @sed_prog = ();
176
177                 for (my $i = 0; $i < @$src_dses; $i++)
178                 {
179                         push (@sed_prog, 's/^' . $src_dses->[$i] . '$/' . $dst_dses->[$i] . '/g;');
180                 }
181
182                 print "rrdtool dump '$InDir/$orig_filename' | sed -e '" . join (' ', @sed_prog) . "' | rrdtool restore - '$OutDir/$dest_filename'\n";
183         }
184         else
185         {
186                 print "cp '$InDir/$orig_filename' '$OutDir/$dest_filename'\n";
187         }
188 }
189
190 exit (0);
191
192 sub translate_file
193 {
194         my $orig = shift;
195         my $dest = {};
196         %$dest = %$orig;
197
198         if (defined ($TypeTranslate{$orig->{'type'}}))
199         {
200                 $TypeTranslate{$orig->{'type'}}->($dest);
201         }
202
203         return ($dest);
204 } # translate_file
205
206 sub get_filename
207 {
208         my $args = shift;
209         my $filename = $args->{'host'}
210         . '/' . $args->{'plugin'} . (defined ($args->{'plugin_instance'}) ? '-'.$args->{'plugin_instance'} : '')
211         . '/' . $args->{'type'} . (defined ($args->{'type_instance'}) ? '-'.$args->{'type_instance'} : '') . '.rrd';
212
213         return ($filename);
214 }
215
216 sub parse_file
217 {
218         my $fullname = shift;
219         my @parts = split ('/', $fullname);
220
221         my $filename;
222
223         my $host;
224         my $plugin;
225         my $plugin_instance;
226         my $type;
227         my $type_instance;
228
229         $filename = pop (@parts);
230
231         if ($filename =~ m/^([^-]+)(?:-(.*))?\.rrd$/)
232         {
233                 $type = $1;
234                 $type_instance = $2;
235         }
236         else
237         {
238                 return;
239         }
240
241         if (@parts)
242         {
243                 my $dirname = pop (@parts);
244                 my $regex_str = join ('|', keys (%Subdirs));
245                 if ($dirname =~ m/^($regex_str)(?:-(.*))?$/)
246                 {
247                         $plugin = $1;
248                         $plugin_instance = $2;
249                 }
250                 else
251                 {
252                         push (@parts, $dirname);
253                 }
254         }
255         if (!$plugin)
256         {
257                 $plugin = $type;
258         }
259
260         if (@parts)
261         {
262                 $host = pop (@parts);
263         }
264         else
265         {
266                 $host = $Hostname;
267         }
268
269         return
270         ({
271                 host => $host,
272                 plugin => $plugin,
273                 plugin_instance => $plugin_instance,
274                 type => $type,
275                 type_instance => $type_instance
276         });
277 } # parse_file
278
279 sub find_files
280 {
281         my $reldir = @_ ? shift : '';
282         my $absdir = $InDir . ($reldir ? "/$reldir" : '');
283
284         my $dh;
285
286         my @files = ();
287         my @dirs = ();
288
289         opendir ($dh, $absdir) or die ("opendir ($absdir): $!");
290         while (my $file = readdir ($dh))
291         {
292                 next if ($file =~ m/^\./);
293                 next if (-l "$absdir/$file");
294                 if (-d "$absdir/$file")
295                 {
296                         push (@dirs, ($reldir ? "$reldir/" : '') . $file);
297                 }
298                 elsif ($file =~ m/\.rrd$/)
299                 {
300                         push (@files, ($reldir ? "$reldir/" : '') . $file);
301                 }
302         }
303         closedir ($dh);
304
305         for (my $i = 0; $i < @dirs; $i++)
306         {
307                 push (@files, find_files ($dirs[$i]));
308         }
309
310         return (@files);
311 } # find_files
312
313 {my $cache;
314 sub _special_disk_instance
315 {
316         my $orig_instance = shift;
317
318         if (!defined ($cache))
319         {
320                 my $fh;
321                 open ($fh, "< /proc/diskstats") or die ("open (/proc/diststats): $!");
322
323                 $cache = {};
324                 while (my $line = <$fh>)
325                 {
326                         chomp ($line);
327                         my @fields = split (' ', $line);
328                         $cache->{$fields[0] . '-' . $fields[1]} = $fields[2];
329                 }
330                 close ($fh);
331         }
332
333         return (defined ($cache->{$orig_instance})
334                 ? $cache->{$orig_instance}
335                 : $orig_instance);
336 }}
337
338 sub special_disk
339 {
340         my $orig_filename = shift;
341         my $orig = shift;
342         my $dest_filename = shift;
343         my $dest = shift;
344         my $dest_directory;
345
346         $dest->{'type_instance'} = undef;
347         $dest->{'plugin_instance'} = _special_disk_instance ($orig->{'type_instance'});
348         if ($dest->{'plugin_instance'} eq $orig->{'type_instance'})
349         {
350                 print qq(echo "You may need to rename these files" >&2\n);
351         }
352
353         $dest->{'type'} = 'disk_merged';
354         $dest_filename = get_filename ($dest);
355
356         $dest_directory = dirname ($dest_filename);
357         if (!exists ($OutDirs{$dest_directory}))
358         {
359                 print "[ -d '$OutDir/$dest_directory' ] || mkdir -p '$OutDir/$dest_directory'\n";
360                 $OutDirs{$dest_directory} = 1;
361         }
362
363         print "./extractDS.px -i '$InDir/$orig_filename' -s 'rmerged' -s 'wmerged' -o '$OutDir/$dest_filename' -d 'read' -d 'write'\n";
364
365         $dest->{'type'} = 'disk_octets';
366         $dest_filename = get_filename ($dest);
367         print "./extractDS.px -i '$InDir/$orig_filename' -s 'rbytes' -s 'wbytes' -o '$OutDir/$dest_filename' -d 'read' -d 'write'\n";
368
369         $dest->{'type'} = 'disk_ops';
370         $dest_filename = get_filename ($dest);
371         print "./extractDS.px -i '$InDir/$orig_filename' -s 'rcount' -s 'wcount' -o '$OutDir/$dest_filename' -d 'read' -d 'write'\n";
372
373         $dest->{'type'} = 'disk_time';
374         $dest_filename = get_filename ($dest);
375         print "./extractDS.px -i '$InDir/$orig_filename' -s 'rtime' -s 'wtime' -o '$OutDir/$dest_filename' -d 'read' -d 'write'\n";
376 }
377
378 sub exit_usage
379 {
380         print <<EOF;
381 Usage: $0 [-i indir] [-o outdir] [--hostname myhostname]
382 EOF
383         exit (1);
384 }