#!/usr/bin/perl
###############################################################################
# #
# IPFire.org - A linux based firewall #
# Copyright (C) 2008 Michael Tremer & Christian Schmidt #
# #
# This program is free software: you can redistribute it and/or modify #
# it under the terms of the GNU General Public License as published by #
# the Free Software Foundation, either version 3 of the License, or #
# (at your option) any later version. #
# #
# This program is distributed in the hope that it will be useful, #
# but WITHOUT ANY WARRANTY; without even the implied warranty of #
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
# GNU General Public License for more details. #
# #
# You should have received a copy of the GNU General Public License #
# along with this program. If not, see . #
# #
###############################################################################
# This skript will migrate all old rrd files with ADSL Optimizer Style to a
# custom IPFire one only collecting byte count for the QoS classes
# Fore testing purpose i recommend to copy your rrd files to /tmp and change
# the rrddir, then you are able to test the migration without problems
# Migration will take all classes rrd into count
my $rrddir = "/var/log/rrd";
my @files = `cd $rrddir && ls class_*.rrd`;
# Ff migration was already done we will skip this to avoid errors
if ( -e "$rrddir/migrated" ){print "Already migrated rrd files -> exit.\n";exit 1;}
# Stop collectd and qos to ensure that no one further write to the rrds
system("/etc/init.d/collectd stop");
system("/usr/local/bin/qosctrl stop");
foreach (@files){
chomp($_);
# Dump the whole rrd file to human readable xml format into an array
my @lines = `rrdtool dump $rrddir/$_`;
# to ensure only needed raw data is extracted we will use a marker
my $fromhere = 0;
# because every rrd hase the same header we can use a general one deleting
# lastupdate and lastvalue they will be set the first time rrd is written
# after migration
my @newlines = " 0003
10
bytes
COUNTER
20
0.0000000000e+00
NaN
0
AVERAGE
1
5.0000000000e-01
NaN
NaN
0
";
foreach (@lines){
# if database content line is found we will start to extract the values
if ( $_ =~ /\/ ){
$fromhere = 1;next;
}
# if database content is finished we will stop to extract the values
if ( $_ =~ /\<\/database\>/ ){
$fromhere = 0;next;
}
# if extraction is not set we will skip this line else we will extract
# only the first row and drop all the other ones, the new raw line
# will be written to and array
if ( $fromhere eq "0" ){
next;
}else{
my @t = split(//,$_);
push(@newlines,$t[0]."".$t[1]."\n");
}
}
# Add default footer to the array so a valid rrd xml file will be created
push(@newlines,"
");
# Now write the whole array to an xml file
open(DATEI, ">/tmp/rrd.xml") || die "Unable to create temp file";
print DATEI @newlines;
close(DATEI);
# Delete the old rrd file and restore a new one with content from the xml file
system("rm -f $rrddir/$_");
system("rrdtool restore -f /tmp/rrd.xml $rrddir/$_");
print "$_ ... resized\n";
}
# Now we can restart the collection
system("/etc/init.d/collectd start");
system("/usr/local/bin/qosctrl start");
# Finaly we will delete unneeded evt files and touch the migration file
system("rm -f $rrddir/*.evt");
system("touch $rrddir/migrated");
exit 0;