6 use Getopt::Long ('GetOptions');
8 use File::Basename ('dirname');
10 our $InDir = '/var/lib/collectd';
11 our $OutDir = '/tmp/collectd-4';
12 our $Hostname = 'localhost';
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 ! ! ! !
24 # ! ! cpufreq ! x ! ! !
25 # ! battery ! current ! ! x ! !
26 # ! ntpd ! delay ! 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 ! ! ! !
39 # ! apcups ! load_percent ! ! ! !
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 ! ! !
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 ! ! !
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
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 # +------------+----------------------+----+----+----+
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'); $_; }
109 cpu => { from => [qw(user nice syst idle wait)], to => 'value', type_instance => [qw(user nice system idle wait)] },
110 nfs3_procedures => { from => [qw(null getattr lookup access readlink
111 read write create mkdir symlink mknod remove rmdir rename link
112 readdir readdirplus fsstat fsinfo pathconf commit)], to => 'value' },
113 nfs2_procedures => { from => [qw(create fsstat getattr link lookup
114 mkdir null read readdir readlink remove rename rmdir root
115 setattr symlink wrcache write)], to => 'value' },
116 processes => { from => [qw(running sleeping zombies stopped paging blocked)], to => 'value' },
117 swap => { from => [qw(cached free used resv)], to => 'value', type_instance => [qw(cached free used reserved)] }
122 traffic => { from => [qw(incoming outgoing)], to => [qw(rx tx)] }
125 GetOptions ("indir|i=s" => \$InDir,
126 "outdir|o=s" => \$OutDir,
127 "hostname=s" => \$Hostname) or exit (1);
129 die "No such directory: $InDir" if (!-d $InDir);
132 mkdir ($OutDir) or die ("mkdir ($OutDir): $!");
134 die "Not a directory: $OutDir" if (!-d $OutDir);
139 @Files = find_files ();
143 my $orig_filename = $_;
144 my $orig = parse_file ($orig_filename);
145 my $dest = translate_file ($orig);
146 my $dest_filename = get_filename ($dest);
148 my $dest_directory = dirname ($dest_filename);
149 if (!exists ($OutDirs{$dest_directory}))
151 print "[ -d '$OutDir/$dest_directory' ] || mkdir -p '$OutDir/$dest_directory'\n";
152 $OutDirs{$dest_directory} = 1;
155 if (exists ($TypeSplit{$orig->{'type'}}))
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'};
163 for (my $i = 0; $i < @$src_dses; $i++)
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";
171 elsif (exists ($TypeRename{$orig->{'type'}}))
173 my $src_dses = $TypeRename{$orig->{'type'}}->{'from'};
174 my $dst_dses = $TypeRename{$orig->{'type'}}->{'to'};
177 for (my $i = 0; $i < @$src_dses; $i++)
179 push (@sed_prog, 's/^' . $src_dses->[$i] . '$/' . $dst_dses->[$i] . '/g;');
182 print "rrdtool dump '$InDir/$orig_filename' | sed -e '" . join (' ', @sed_prog) . "' | rrdtool restore - '$OutDir/$dest_filename'\n";
186 print "cp '$InDir/$orig_filename' '$OutDir/$dest_filename'\n";
198 if (defined ($TypeTranslate{$orig->{'type'}}))
200 $TypeTranslate{$orig->{'type'}}->($dest);
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';
218 my $fullname = shift;
219 my @parts = split ('/', $fullname);
229 $filename = pop (@parts);
231 if ($filename =~ m/^([^-]+)(?:-(.*))?\.rrd$/)
243 my $dirname = pop (@parts);
244 my $regex_str = join ('|', keys (%Subdirs));
245 if ($dirname =~ m/^($regex_str)(?:-(.*))?$/)
248 $plugin_instance = $2;
252 push (@parts, $dirname);
262 $host = pop (@parts);
273 plugin_instance => $plugin_instance,
275 type_instance => $type_instance
281 my $reldir = @_ ? shift : '';
282 my $absdir = $InDir . ($reldir ? "/$reldir" : '');
289 opendir ($dh, $absdir) or die ("opendir ($absdir): $!");
290 while (my $file = readdir ($dh))
292 next if ($file =~ m/^\./);
293 next if (-l "$absdir/$file");
294 if (-d "$absdir/$file")
296 push (@dirs, ($reldir ? "$reldir/" : '') . $file);
298 elsif ($file =~ m/\.rrd$/)
300 push (@files, ($reldir ? "$reldir/" : '') . $file);
305 for (my $i = 0; $i < @dirs; $i++)
307 push (@files, find_files ($dirs[$i]));
316 my %file_dest = %file_orig;
318 $file_dest{'plugin_instance'} = $file_dest{'type_instance'}