Merge remote-tracking branch 'github-tokkee/sh/empty-config'
[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 our $RRDFilter = 'rrd_filter.px';
37
38 our %TypesCounterToDerive = # {{{
39 (
40   apache_bytes => ["count"],
41   apache_requests => ["count"],
42   arc_counts => ["demand_data", "demand_metadata", "prefetch_data", "prefetch_metadata"],
43   arc_l2_bytes => ["read", "write"],
44   ath_stat => ["value"],
45   compression => ["uncompressed", "compressed"],
46   connections => ["value"],
47   cpu => ["value"],
48   current => ["value"],
49   disk_merged => ["read", "write"],
50   disk_octets => ["read", "write"],
51   disk_ops => ["read", "write"],
52   disk_ops_complex => ["value"],
53   disk_time => ["read", "write"],
54   dns_answer => ["value"],
55   dns_notify => ["value"],
56   dns_octets => ["queries", "responses"],
57   dns_opcode => ["value"],
58   dns_qtype => ["value"],
59   dns_query => ["value"],
60   dns_question => ["value"],
61   dns_rcode => ["value"],
62   dns_reject => ["value"],
63   dns_request => ["value"],
64   dns_resolver => ["value"],
65   dns_response => ["value"],
66   dns_transfer => ["value"],
67   dns_update => ["value"],
68   dns_zops => ["value"],
69   fscache_stat => ["value"],
70   fork_rate => ["value"],
71   http_request_methods => ["count"],
72   http_requests => ["count"],
73   http_response_codes => ["count"],
74   if_collisions => ["value"],
75   if_dropped => ["rx", "tx"],
76   if_errors => ["rx", "tx"],
77   if_multicast => ["value"],
78   if_octets => ["rx", "tx"],
79   if_packets => ["rx", "tx"],
80   if_rx_errors => ["value"],
81   if_tx_errors => ["value"],
82   io_octets => ["rx", "tx"],
83   io_packets => ["rx", "tx"],
84   ipt_bytes => ["value"],
85   ipt_packets => ["value"],
86   irq => ["value"],
87   memcached_command => ["value"],
88   memcached_octets => ["rx", "tx"],
89   memcached_ops => ["value"],
90   mysql_commands => ["value"],
91   mysql_handler => ["value"],
92   mysql_locks => ["value"],
93   mysql_log_position => ["value"],
94   mysql_octets => ["rx", "tx"],
95   nfs_procedure => ["value"],
96   nginx_requests => ["value"],
97   node_octets => ["rx", "tx"],
98   node_stat => ["value"],
99   operations => ["value"],
100   pg_blks => ["value"],
101   pg_n_tup_c => ["value"],
102   pg_scan => ["value"],
103   pg_xact => ["value"],
104   protocol_counter => ["value"],
105   ps_cputime => ["user", "syst"],
106   ps_pagefaults => ["minflt", "majflt"],
107   ps_code => ["value"],
108   ps_data => ["value"],
109   serial_octets => ["rx", "tx"],
110   swap_io => ["value"],
111   virt_cpu_total => ["ns"],
112   virt_vcpu => ["ns"],
113   vmpage_action => ["value"],
114   vmpage_faults => ["minflt", "majflt"],
115   vmpage_io => ["in", "out"],
116 ); # }}} %TypesCounterToDerive
117
118 our %TypesRenameDataSource = # {{{
119 (
120   absolute => "count",
121   apache_bytes => "count",
122   apache_connections => "count",
123   apache_idle_workers => "count",
124   apache_requests => "count",
125   apache_scoreboard => "count",
126   conntrack => "entropy",
127   contextswitch => "contextswitches",
128   delay => "seconds",
129   entropy => "entropy",
130   file_size => "bytes",
131   frequency => "frequency",
132   frequency_offset => "ppm",
133   http_request_methods => "count",
134   http_requests => "count",
135   http_response_codes => "count",
136   percent => "percent",
137   ping => "ping",
138   records => "count",
139   time_dispersion => "seconds",
140   timeleft => "timeleft",
141   time_offset => "seconds",
142   users => "users",
143   virt_cpu_total => "ns",
144   virt_vcpu => "ns",
145 ); # }}} %TypesRenameDataSource
146
147 sub handle_file # {{{
148 {
149   my @path = @_;
150   my $path = join ('/', @path);
151
152   if (!($path =~ m/\.rrd$/))
153   {
154     return;
155   }
156
157   my $tmp = pop (@path);
158   $tmp =~ s/\.rrd$//;
159   my ($type, $type_inst) = split (m/-/, $tmp, 2);
160   $type_inst ||= '';
161
162   $tmp = pop (@path);
163   my ($plugin, $plugin_inst) = split (m/-/, $tmp, 2);
164   $plugin_inst ||= '';
165
166   if ($TypesRenameDataSource{$type})
167   {
168     my $old_ds = $TypesRenameDataSource{$type};
169     print "$RRDtool tune \"$path\" --data-source-rename ${old_ds}:value\n";
170   }
171
172   if ($TypesCounterToDerive{$type})
173   {
174     my $ds_names = $TypesCounterToDerive{$type};
175     
176     for (@$ds_names)
177     {
178       my $name = $_;
179       print "$RRDtool tune \"$path\" --data-source-type ${name}:DERIVE --minimum ${name}:0 --maximum ${name}:U\n";
180     }
181   }
182
183   if ((($plugin eq 'df') || ($plugin eq 'interface'))
184     && (!$plugin_inst) && ($type_inst))
185   {
186     my $dir = join ('/', @path);
187     print "mkdir -p \"$dir/$plugin-$type_inst\"\n";
188     if (($plugin eq 'df') and ($type eq 'df'))
189     {
190       print "$RRDFilter --infile=\"$path\" --outfile=\"$dir/$plugin-$type_inst/df_complex-free.rrd\" --map free:value\n";
191       print "$RRDFilter --infile=\"$path\" --outfile=\"$dir/$plugin-$type_inst/df_complex-used.rrd\" --map used:value\n";
192     }
193     else
194     {
195       print "mv \"$path\" \"$dir/$plugin-$type_inst/$type.rrd\"\n";
196     }
197   }
198 } # }}} sub handle_file
199
200 sub scan_dir # {{{
201 {
202   my @dir_parts = @_;
203   my $dir_str = join ('/', @dir_parts);
204   my $dh;
205
206   opendir ($dh, $dir_str) || die;
207   while (my $entry = readdir ($dh))
208   {
209     my $entry_path = "$dir_str/$entry";
210
211     if ($entry =~ m/^\./)
212     {
213       next;
214     }
215
216     if (-d $entry_path)
217     {
218       scan_dir (@dir_parts, $entry);
219     }
220     elsif (-f $entry_path)
221     {
222       handle_file (@dir_parts, $entry);
223     }
224   }
225   closedir ($dh);
226 } # }}} sub scan_dir
227
228 sub exit_usage # {{{
229 {
230   print STDERR <<EOF;
231 migrate-4-5.px [OPTIONS]
232
233 Valid options are:
234
235   --indir <dir>         Source directory
236                         Default: $InDir
237   --rrdtool <path>      Path to the RRDtool binary
238                         Default: $RRDtool
239   --rrdfilter <path>    Path to the rrd_filter.px script
240                         Default: $RRDFilter
241
242 EOF
243   exit (1);
244 } # }}} sub exit_usage
245
246 GetOptions ("indir|i=s" => \$InDir,
247         "rrdtool=s" => \$RRDtool,
248         "rrdfilter=s" => \$RRDFilter,
249         "help|h" => \&exit_usage) or exit_usage ();
250
251 print "#!/bin/bash\n\n";
252
253 scan_dir ($InDir);
254
255 # vim: set sw=2 sts=2 et fdm=marker :