Merge branch 'collectd-5.0'
[collectd.git] / contrib / migrate-4-5.px
1 #!/usr/bin/perl
2
3 # collectd - contrib/migrate-4-5.px
4 # Copyright (C) 2010  Florian Forster
5 #
6 # Permission is hereby granted, free of charge, to any person obtaining a
7 # copy of this software and associated documentation files (the "Software"),
8 # to deal in the Software without restriction, including without limitation
9 # the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 # and/or sell copies of the Software, and to permit persons to whom the
11 # Software is furnished to do so, subject to the following conditions:
12 #
13 # The above copyright notice and this permission notice shall be included in
14 # all copies or substantial portions of the Software.
15 #
16 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 # FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 # DEALINGS IN THE SOFTWARE.
23 #
24 # Authors:
25 #   Florian Forster <octo at collectd.org>
26
27 use strict;
28 use warnings;
29
30 use Getopt::Long ('GetOptions');
31 use Data::Dumper ();
32 use File::Basename ('dirname');
33
34 our $InDir = '/var/lib/collectd';
35 our $RRDtool = 'rrdtool';
36
37 our %TypesCounterToDerive = # {{{
38 (
39   apache_bytes => ["count"],
40   apache_requests => ["count"],
41   arc_counts => ["demand_data", "demand_metadata", "prefetch_data", "prefetch_metadata"],
42   arc_l2_bytes => ["read", "write"],
43   ath_stat => ["value"],
44   compression => ["uncompressed", "compressed"],
45   connections => ["value"],
46   cpu => ["value"],
47   current => ["value"],
48   disk_merged => ["read", "write"],
49   disk_octets => ["read", "write"],
50   disk_ops => ["read", "write"],
51   disk_ops_complex => ["value"],
52   disk_time => ["read", "write"],
53   dns_answer => ["value"],
54   dns_notify => ["value"],
55   dns_octets => ["queries", "responses"],
56   dns_opcode => ["value"],
57   dns_qtype => ["value"],
58   dns_query => ["value"],
59   dns_question => ["value"],
60   dns_rcode => ["value"],
61   dns_reject => ["value"],
62   dns_request => ["value"],
63   dns_resolver => ["value"],
64   dns_response => ["value"],
65   dns_transfer => ["value"],
66   dns_update => ["value"],
67   dns_zops => ["value"],
68   fscache_stat => ["value"],
69   fork_rate => ["value"],
70   http_request_methods => ["count"],
71   http_requests => ["count"],
72   http_response_codes => ["count"],
73   if_collisions => ["value"],
74   if_dropped => ["rx", "tx"],
75   if_errors => ["rx", "tx"],
76   if_multicast => ["value"],
77   if_octets => ["rx", "tx"],
78   if_packets => ["rx", "tx"],
79   if_rx_errors => ["value"],
80   if_tx_errors => ["value"],
81   io_octets => ["rx", "tx"],
82   io_packets => ["rx", "tx"],
83   ipt_bytes => ["value"],
84   ipt_packets => ["value"],
85   irq => ["value"],
86   memcached_command => ["value"],
87   memcached_octets => ["rx", "tx"],
88   memcached_ops => ["value"],
89   mysql_commands => ["value"],
90   mysql_handler => ["value"],
91   mysql_locks => ["value"],
92   mysql_log_position => ["value"],
93   mysql_octets => ["rx", "tx"],
94   nfs_procedure => ["value"],
95   nginx_requests => ["value"],
96   node_octets => ["rx", "tx"],
97   node_stat => ["value"],
98   operations => ["value"],
99   pg_blks => ["value"],
100   pg_n_tup_c => ["value"],
101   pg_scan => ["value"],
102   pg_xact => ["value"],
103   protocol_counter => ["value"],
104   ps_cputime => ["user", "syst"],
105   ps_pagefaults => ["minflt", "majflt"],
106   ps_code => ["value"],
107   ps_data => ["value"],
108   serial_octets => ["rx", "tx"],
109   swap_io => ["value"],
110   virt_cpu_total => ["ns"],
111   virt_vcpu => ["ns"],
112   vmpage_action => ["value"],
113   vmpage_faults => ["minflt", "majflt"],
114   vmpage_io => ["in", "out"],
115 ); # }}} %TypesCounterToDerive
116
117 our %TypesRenameDataSource = # {{{
118 (
119   absolute => "count",
120   apache_bytes => "count",
121   apache_connections => "count",
122   apache_idle_workers => "count",
123   apache_requests => "count",
124   apache_scoreboard => "count",
125   conntrack => "entropy",
126   contextswitch => "contextswitches",
127   delay => "seconds",
128   entropy => "entropy",
129   file_size => "bytes",
130   frequency => "frequency",
131   frequency_offset => "ppm",
132   http_request_methods => "count",
133   http_requests => "count",
134   http_response_codes => "count",
135   percent => "percent",
136   ping => "ping",
137   records => "count",
138   time_dispersion => "seconds",
139   timeleft => "timeleft",
140   time_offset => "seconds",
141   users => "users",
142   virt_cpu_total => "ns",
143   virt_vcpu => "ns",
144 ); # }}} %TypesRenameDataSource
145
146 sub handle_file # {{{
147 {
148   my @path = @_;
149   my $path = join ('/', @path);
150
151   if (!($path =~ m/\.rrd$/))
152   {
153     return;
154   }
155
156   my $tmp = pop (@path);
157   $tmp =~ s/\.rrd$//;
158   my ($type, $type_inst) = split (m/-/, $tmp, 2);
159   $type_inst ||= '';
160
161   $tmp = pop (@path);
162   my ($plugin, $plugin_inst) = split (m/-/, $tmp, 2);
163   $plugin_inst ||= '';
164
165   if ($TypesRenameDataSource{$type})
166   {
167     my $old_ds = $TypesRenameDataSource{$type};
168     print "$RRDtool tune \"$path\" --data-source-rename ${old_ds}:value\n";
169   }
170
171   if ($TypesCounterToDerive{$type})
172   {
173     my $ds_names = $TypesCounterToDerive{$type};
174     
175     for (@$ds_names)
176     {
177       my $name = $_;
178       print "$RRDtool tune \"$path\" --data-source-type ${name}:DERIVE --minimum ${name}:0 --maximum ${name}:U\n";
179     }
180   }
181
182   if ((($plugin eq 'df') || ($plugin eq 'interface'))
183     && (!$plugin_inst) && ($type_inst))
184   {
185     my $dir = join ('/', @path);
186     print "mv \"$path\" \"$dir/$plugin-$type_inst/$type.rrd\"\n";
187   }
188 } # }}} sub handle_file
189
190 sub scan_dir # {{{
191 {
192   my @dir_parts = @_;
193   my $dir_str = join ('/', @dir_parts);
194   my $dh;
195
196   opendir ($dh, $dir_str) || die;
197   while (my $entry = readdir ($dh))
198   {
199     my $entry_path = "$dir_str/$entry";
200
201     if ($entry =~ m/^\./)
202     {
203       next;
204     }
205
206     if (-d $entry_path)
207     {
208       scan_dir (@dir_parts, $entry);
209     }
210     elsif (-f $entry_path)
211     {
212       handle_file (@dir_parts, $entry);
213     }
214   }
215   closedir ($dh);
216 } # }}} sub scan_dir
217
218 sub exit_usage # {{{
219 {
220   print STDERR <<EOF;
221 migrate-4-5.px [OPTIONS]
222
223 Valid options are:
224
225   --indir <dir>      Source directory
226                      Default: $InDir
227   --rrdtool <path>   Path to the RRDtool binary
228                      Default: $RRDtool
229
230 EOF
231   exit (1);
232 } # }}} sub exit_usage
233
234 GetOptions ("indir|i=s" => \$InDir,
235         "rrdtool=s" => \$RRDtool,
236         "help|h" => \&exit_usage) or exit_usage ();
237
238 scan_dir ($InDir);
239
240 # vim: set sw=2 sts=2 et fdm=marker :