Merge branch 'pull/collectd-4.1' into collectd-4.1
[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         hddtemp => sub { $_ = shift; $_->{'plugin'} = 'hddtemp'; $_->{'type'} = 'temperature'; $_->{'type_instance'} = $_->{'type_instance'}; $_; },
99         if_errors => sub { $_ = shift; $_->{'type_instance'} = $_->{'plugin_instance'}; $_->{'plugin_instance'} = undef; $_; },
100         if_packets => sub { $_ = shift; $_->{'type_instance'} = $_->{'plugin_instance'}; $_->{'plugin_instance'} = undef; $_; },
101         nfs2_procedures => sub { $_ = shift; @$_{qw(plugin plugin_instance type type_instance)} = ('nfs', 'v2' . $_->{'type_instance'}, 'nfs_procedure', undef); $_; },
102         nfs3_procedures => sub { $_ = shift; @$_{qw(plugin plugin_instance type type_instance)} = ('nfs', 'v3' . $_->{'type_instance'}, 'nfs_procedure', undef); $_; },
103         partition => sub { $_ = shift; $_->{'plugin'} = 'disk'; $_; },
104         processes => sub { $_ = shift; $_->{'type'} = 'ps_state'; $_; },
105         traffic => sub { $_ = shift; $_->{'plugin'} =~ s/^traffic$/interface/; @$_{qw(plugin_instance type)} = (undef, 'if_octets'); $_; }
106 );
107
108 our %TypeSplit =
109 (
110         cpu => { from => [qw(user nice syst idle wait)], to => 'value', type_instance => [qw(user nice system idle wait)] },
111         memory => { from => [qw(used free buffers cached)], to => 'value', type_instance => [qw(used free buffered cached)] },
112         nfs3_procedures => { from => [qw(null getattr lookup access readlink
113                 read write create mkdir symlink mknod remove rmdir rename link
114                 readdir readdirplus fsstat fsinfo pathconf commit)], to => 'value' },
115         nfs2_procedures => { from => [qw(create fsstat getattr link lookup
116                 mkdir null read readdir readlink remove rename rmdir root
117                 setattr symlink wrcache write)], to => 'value' },
118         processes => { from => [qw(running sleeping zombies stopped paging blocked)], to => 'value' },
119         swap => { from => [qw(cached free used resv)], to => 'value', type_instance => [qw(cached free used reserved)] }
120 );
121
122 our %TypeRename =
123 (
124         traffic => { from => [qw(incoming outgoing)], to => [qw(rx tx)] }
125 );
126
127 GetOptions ("indir|i=s" => \$InDir,
128         "outdir|o=s" => \$OutDir,
129         "hostname=s" => \$Hostname) or exit_usage ();
130
131 die "No such directory: $InDir" if (!-d $InDir);
132
133 our @Files = ();
134 our %OutDirs = ();
135
136 @Files = find_files ();
137
138 for (@Files)
139 {
140         my $orig_filename = $_;
141         my $orig = parse_file ($orig_filename);
142         my $dest = translate_file ($orig);
143         my $dest_filename = get_filename ($dest);
144
145         my $dest_directory = dirname ($dest_filename);
146         if (!exists ($OutDirs{$dest_directory}))
147         {
148                 print "[ -d '$OutDir/$dest_directory' ] || mkdir -p '$OutDir/$dest_directory'\n";
149                 $OutDirs{$dest_directory} = 1;
150         }
151
152         if (($orig->{'type'} eq 'disk') || ($orig->{'type'} eq 'partition'))
153         {
154                 special_disk ($orig_filename, $orig, $dest_filename, $dest);
155         }
156         elsif (exists ($TypeSplit{$orig->{'type'}}))
157         {
158                 my $src_dses = $TypeSplit{$orig->{'type'}}->{'from'};
159                 my $dst_ds = $TypeSplit{$orig->{'type'}}->{'to'};
160                 my $type_instances = exists ($TypeSplit{$orig->{'type'}}->{'type_instance'})
161                         ? $TypeSplit{$orig->{'type'}}->{'type_instance'}
162                         : $TypeSplit{$orig->{'type'}}->{'from'};
163
164                 for (my $i = 0; $i < @$src_dses; $i++)
165                 {
166                         my $src_ds = $src_dses->[$i];
167                         $dest->{'type_instance'} = $type_instances->[$i];
168                         $dest_filename = get_filename ($dest);
169                         print "./extractDS.px -i '$InDir/$orig_filename' -s '$src_ds' -o '$OutDir/$dest_filename' -d '$dst_ds'\n";
170                 }
171         }
172         elsif (exists ($TypeRename{$orig->{'type'}}))
173         {
174                 my $src_dses = $TypeRename{$orig->{'type'}}->{'from'};
175                 my $dst_dses = $TypeRename{$orig->{'type'}}->{'to'};
176                 my @sed_prog = ();
177
178                 for (my $i = 0; $i < @$src_dses; $i++)
179                 {
180                         push (@sed_prog, 's/^' . $src_dses->[$i] . '$/' . $dst_dses->[$i] . '/g;');
181                 }
182
183                 print "rrdtool dump '$InDir/$orig_filename' | sed -e '" . join (' ', @sed_prog) . "' | rrdtool restore - '$OutDir/$dest_filename'\n";
184         }
185         else
186         {
187                 print "cp '$InDir/$orig_filename' '$OutDir/$dest_filename'\n";
188         }
189 }
190
191 exit (0);
192
193 sub translate_file
194 {
195         my $orig = shift;
196         my $dest = {};
197         %$dest = %$orig;
198
199         if (defined ($TypeTranslate{$orig->{'type'}}))
200         {
201                 $TypeTranslate{$orig->{'type'}}->($dest);
202         }
203
204         return ($dest);
205 } # translate_file
206
207 sub get_filename
208 {
209         my $args = shift;
210         my $filename = $args->{'host'}
211         . '/' . $args->{'plugin'} . (defined ($args->{'plugin_instance'}) ? '-'.$args->{'plugin_instance'} : '')
212         . '/' . $args->{'type'} . (defined ($args->{'type_instance'}) ? '-'.$args->{'type_instance'} : '') . '.rrd';
213
214         return ($filename);
215 }
216
217 sub parse_file
218 {
219         my $fullname = shift;
220         my @parts = split ('/', $fullname);
221
222         my $filename;
223
224         my $host;
225         my $plugin;
226         my $plugin_instance;
227         my $type;
228         my $type_instance;
229
230         $filename = pop (@parts);
231
232         if ($filename =~ m/^([^-]+)(?:-(.*))?\.rrd$/)
233         {
234                 $type = $1;
235                 $type_instance = $2;
236         }
237         else
238         {
239                 return;
240         }
241
242         if (@parts)
243         {
244                 my $dirname = pop (@parts);
245                 my $regex_str = join ('|', keys (%Subdirs));
246                 if ($dirname =~ m/^($regex_str)(?:-(.*))?$/)
247                 {
248                         $plugin = $1;
249                         $plugin_instance = $2;
250                 }
251                 else
252                 {
253                         push (@parts, $dirname);
254                 }
255         }
256         if (!$plugin)
257         {
258                 $plugin = $type;
259         }
260
261         if (@parts)
262         {
263                 $host = pop (@parts);
264         }
265         else
266         {
267                 $host = $Hostname;
268         }
269
270         return
271         ({
272                 host => $host,
273                 plugin => $plugin,
274                 plugin_instance => $plugin_instance,
275                 type => $type,
276                 type_instance => $type_instance
277         });
278 } # parse_file
279
280 sub find_files
281 {
282         my $reldir = @_ ? shift : '';
283         my $absdir = $InDir . ($reldir ? "/$reldir" : '');
284
285         my $dh;
286
287         my @files = ();
288         my @dirs = ();
289
290         opendir ($dh, $absdir) or die ("opendir ($absdir): $!");
291         while (my $file = readdir ($dh))
292         {
293                 next if ($file =~ m/^\./);
294                 next if (-l "$absdir/$file");
295                 if (-d "$absdir/$file")
296                 {
297                         push (@dirs, ($reldir ? "$reldir/" : '') . $file);
298                 }
299                 elsif ($file =~ m/\.rrd$/)
300                 {
301                         push (@files, ($reldir ? "$reldir/" : '') . $file);
302                 }
303         }
304         closedir ($dh);
305
306         for (my $i = 0; $i < @dirs; $i++)
307         {
308                 push (@files, find_files ($dirs[$i]));
309         }
310
311         return (@files);
312 } # find_files
313
314 {my $cache;
315 sub _special_disk_instance
316 {
317         my $orig_instance = shift;
318
319         if (!defined ($cache))
320         {
321                 my $fh;
322                 open ($fh, "< /proc/diskstats") or die ("open (/proc/diststats): $!");
323
324                 $cache = {};
325                 while (my $line = <$fh>)
326                 {
327                         chomp ($line);
328                         my @fields = split (' ', $line);
329                         $cache->{$fields[0] . '-' . $fields[1]} = $fields[2];
330                 }
331                 close ($fh);
332         }
333
334         return (defined ($cache->{$orig_instance})
335                 ? $cache->{$orig_instance}
336                 : $orig_instance);
337 }}
338
339 sub special_disk
340 {
341         my $orig_filename = shift;
342         my $orig = shift;
343         my $dest_filename = shift;
344         my $dest = shift;
345         my $dest_directory;
346
347         $dest->{'type_instance'} = undef;
348         $dest->{'plugin_instance'} = _special_disk_instance ($orig->{'type_instance'});
349         if ($dest->{'plugin_instance'} eq $orig->{'type_instance'})
350         {
351                 print qq(echo "You may need to rename these files" >&2\n);
352         }
353
354         $dest->{'type'} = 'disk_merged';
355         $dest_filename = get_filename ($dest);
356
357         $dest_directory = dirname ($dest_filename);
358         if (!exists ($OutDirs{$dest_directory}))
359         {
360                 print "[ -d '$OutDir/$dest_directory' ] || mkdir -p '$OutDir/$dest_directory'\n";
361                 $OutDirs{$dest_directory} = 1;
362         }
363
364         print "./extractDS.px -i '$InDir/$orig_filename' -s 'rmerged' -s 'wmerged' -o '$OutDir/$dest_filename' -d 'read' -d 'write'\n";
365
366         $dest->{'type'} = 'disk_octets';
367         $dest_filename = get_filename ($dest);
368         print "./extractDS.px -i '$InDir/$orig_filename' -s 'rbytes' -s 'wbytes' -o '$OutDir/$dest_filename' -d 'read' -d 'write'\n";
369
370         $dest->{'type'} = 'disk_ops';
371         $dest_filename = get_filename ($dest);
372         print "./extractDS.px -i '$InDir/$orig_filename' -s 'rcount' -s 'wcount' -o '$OutDir/$dest_filename' -d 'read' -d 'write'\n";
373
374         $dest->{'type'} = 'disk_time';
375         $dest_filename = get_filename ($dest);
376         print "./extractDS.px -i '$InDir/$orig_filename' -s 'rtime' -s 'wtime' -o '$OutDir/$dest_filename' -d 'read' -d 'write'\n";
377 }
378
379 sub exit_usage
380 {
381         print <<EOF;
382 Usage: $0 [-i indir] [-o outdir] [--hostname myhostname]
383 EOF
384         exit (1);
385 }