PageRenderTime 21ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/contrib/migrate-3-4.px

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