]>
Commit | Line | Data |
---|---|---|
1 | #!/usr/bin/perl | |
2 | ############################################################################### | |
3 | # # | |
4 | # IPFire.org - A linux based firewall # | |
5 | # Copyright (C) 2008 Michael Tremer & Christian Schmidt # | |
6 | # # | |
7 | # This program is free software: you can redistribute it and/or modify # | |
8 | # it under the terms of the GNU General Public License as published by # | |
9 | # the Free Software Foundation, either version 3 of the License, or # | |
10 | # (at your option) any later version. # | |
11 | # # | |
12 | # This program is distributed in the hope that it will be useful, # | |
13 | # but WITHOUT ANY WARRANTY; without even the implied warranty of # | |
14 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # | |
15 | # GNU General Public License for more details. # | |
16 | # # | |
17 | # You should have received a copy of the GNU General Public License # | |
18 | # along with this program. If not, see <http://www.gnu.org/licenses/>. # | |
19 | # # | |
20 | ############################################################################### | |
21 | ||
22 | # This skript will migrate all old rrd files with ADSL Optimizer Style to a | |
23 | # custom IPFire one only collecting byte count for the QoS classes | |
24 | ||
25 | # Fore testing purpose i recommend to copy your rrd files to /tmp and change | |
26 | # the rrddir, then you are able to test the migration without problems | |
27 | # Migration will take all classes rrd into count | |
28 | ||
29 | my $rrddir = "/var/log/rrd"; | |
30 | my @files = `cd $rrddir && ls class_*.rrd`; | |
31 | ||
32 | # Ff migration was already done we will skip this to avoid errors | |
33 | if ( -e "$rrddir/migrated" ){print "Already migrated rrd files -> exit.\n";exit 1;} | |
34 | ||
35 | # Stop collectd and qos to ensure that no one further write to the rrds | |
36 | system("/etc/init.d/collectd stop"); | |
37 | system("/usr/local/bin/qosctrl stop"); | |
38 | ||
39 | foreach (@files){ | |
40 | chomp($_); | |
41 | ||
42 | # Dump the whole rrd file to human readable xml format into an array | |
43 | my @lines = `rrdtool dump $rrddir/$_`; | |
44 | ||
45 | # to ensure only needed raw data is extracted we will use a marker | |
46 | my $fromhere = 0; | |
47 | ||
48 | # because every rrd hase the same header we can use a general one deleting | |
49 | # lastupdate and lastvalue they will be set the first time rrd is written | |
50 | # after migration | |
51 | my @newlines = "<!-- Round Robin Database Dump --><rrd> <version> 0003 </version> | |
52 | <step> 10 </step> <!-- Seconds --> | |
53 | <lastupdate> </lastupdate> | |
54 | ||
55 | <ds> | |
56 | <name> bytes </name> | |
57 | <type> COUNTER </type> | |
58 | <minimal_heartbeat> 20 </minimal_heartbeat> | |
59 | <min> 0.0000000000e+00 </min> | |
60 | <max> NaN </max> | |
61 | ||
62 | <!-- PDP Status --> | |
63 | <last_ds> </last_ds> | |
64 | <value> </value> | |
65 | <unknown_sec> 0 </unknown_sec> | |
66 | </ds> | |
67 | ||
68 | <!-- Round Robin Archives --> <rra> | |
69 | <cf> AVERAGE </cf> | |
70 | <pdp_per_row> 1 </pdp_per_row> <!-- 10 seconds --> | |
71 | ||
72 | <params> | |
73 | <xff> 5.0000000000e-01 </xff> | |
74 | </params> | |
75 | <cdp_prep> | |
76 | <ds> | |
77 | <primary_value> </primary_value> | |
78 | <secondary_value> NaN </secondary_value> | |
79 | <value> NaN </value> | |
80 | <unknown_datapoints> 0 </unknown_datapoints> | |
81 | </ds> | |
82 | </cdp_prep> | |
83 | <database> | |
84 | "; | |
85 | foreach (@lines){ | |
86 | ||
87 | # if database content line is found we will start to extract the values | |
88 | if ( $_ =~ /\<database\>/ ){ | |
89 | $fromhere = 1;next; | |
90 | } | |
91 | # if database content is finished we will stop to extract the values | |
92 | if ( $_ =~ /\<\/database\>/ ){ | |
93 | $fromhere = 0;next; | |
94 | } | |
95 | # if extraction is not set we will skip this line else we will extract | |
96 | # only the first row and drop all the other ones, the new raw line | |
97 | # will be written to and array | |
98 | if ( $fromhere eq "0" ){ | |
99 | next; | |
100 | }else{ | |
101 | my @t = split(/<v>/,$_); | |
102 | push(@newlines,$t[0]."<v>".$t[1]."</row>\n"); | |
103 | } | |
104 | } | |
105 | # Add default footer to the array so a valid rrd xml file will be created | |
106 | push(@newlines," </database> | |
107 | </rra> | |
108 | </rrd>"); | |
109 | # Now write the whole array to an xml file | |
110 | open(DATEI, ">/tmp/rrd.xml") || die "Unable to create temp file"; | |
111 | print DATEI @newlines; | |
112 | close(DATEI); | |
113 | ||
114 | # Delete the old rrd file and restore a new one with content from the xml file | |
115 | system("rm -f $rrddir/$_"); | |
116 | system("rrdtool restore -f /tmp/rrd.xml $rrddir/$_"); | |
117 | print "$_ ... resized\n"; | |
118 | } | |
119 | ||
120 | # Now we can restart the collection | |
121 | system("/etc/init.d/collectd start"); | |
122 | system("/usr/local/bin/qosctrl start"); | |
123 | ||
124 | # Finaly we will delete unneeded evt files and touch the migration file | |
125 | system("rf $rrddir/*.evt"); | |
126 | system("touch $rrddir/migrated"); | |
127 | exit 0; |