Несколько скриптов для редактирования rrd.
Часто в cacti для благообразного вида графиков нужно бывает отредактировать rrd базы данных.
Если надо замазать дырку одиночном файле. Список rrd обычный текстовый файли с перечнем rrd файлов.
#!/usr/bin/perl
# Writen by Timur.Nigamov
#remove_hole <rrd file> <hole start time DD-MM-YYYY-HH-MM-SS> <hole end time DD-MM-YYYY-HH-MM-SS>
#
use RRD::Editor ();
use strict;
use Time::Local;
my $date1;my $st_time;my $idx;
my $date;my $row_delta; my $step;
my $row;
my $filename=$ARGV[0];
my $date=$ARGV[1];
my $date1=$ARGV[2];
if ($filename eq "" ) { print "No file\n";exit;}
if ($date eq "" ) { print "No start date\n";exit;}
if ($date1 eq "" ) { print "No end date\n";exit;}
my ($day,$month,$year,$hour,$min,$sec)=split "-",$date;
my ($day1,$month1,$year1,$hour1,$min1,$sec1)=split "-",$date1;
print "$day,$month,$year,$hour,$min,$sec";
$month--;
$month1--;
my $time=timelocal($sec,$min,$hour,$day,$month,$year);
my $time1=timelocal($sec1,$min1,$hour1,$day1,$month1,$year1);
my $time_delta=$time1-$time;
my $save_filename=$filename.".bk";
print "$filename\n";
print "$save_filename\n";
my $rrd=RRD::Editor -> new();
$rrd->open ($filename );
$step=$rrd->minstep();
my $st_row;
my ($end_row,$end_row1);
my ($val,$a,$b);
my $st_row1;my $st_row;my $st;
my @ds_names= $rrd -> DS_names();
my $nums=$rrd->num_RRAs();
for ($idx=0;$idx < $nums;$idx++)
{
$step=$rrd->RRA_step($idx);
($st_time,undef)=$rrd->RRA_el($idx,$ds_names[0],0);
print "################ $idx - step-> $step $time $st_time ################\n";
$st_row=int (($time-$st_time)/$step);
$end_row=($time1-$st_time)/$step;
$end_row1=int($end_row);
if ($end_row1 != 0 ) {$end_row=int($end_row)+1;}
my $numrows = $rrd->RRA_numrows($idx);
print "######## Start -> $st_row End -> $end_row MaxRow=$numrows#######\n";
if ($st_row >= $numrows) {print "Row overlow\n";next;}
if ($end_row >= $numrows) {print "Row overlow\n"; $end_row--; next;}
foreach my $ds (@ds_names)
{
(undef,$a)=$rrd->RRA_el($idx, $ds, $st_row);
(undef,$b)=$rrd->RRA_el($idx, $ds, $end_row);
$row_delta=($end_row-$st_row);
print "###### Row delta $row_delta ###########################################\n";
if ($row_delta == 0) { $st=0; $st_row1=$st_row;}
else { $st=($b-$a)/$row_delta;
$st_row1=$st_row + 1;}
$val=$a+$st;
print "#### $idx $ds $a<->$b ####### $row_delta ########################\n";
for ($row=$st_row1;$row<$end_row;$row++)
{
print "#### read -> ";my(undef,$k)= $rrd->RRA_el($idx, $ds, $row); print "$k\n";
print "#### writing -> $idx $ds -> $row -> $val\n";
$rrd->set_RRA_el($idx,$ds,$row,$val);
$val=$val+$st;
}
}
}
print "\n";
$rrd->save($save_filename);
Если надо устранить пробелы в группе файлов
!/usr/bin/perl
#
#Timur Nigamov (c) 2014
#
use RRD::Editor ();
use strict;
use Time::Local;
my $date1;my $st_time;my $idx;
my $date;my $row_delta; my $step;
my $row;
my $filename;
my $save_filename;
my $date=$ARGV[0];
my $date1=$ARGV[1];
my $list=$ARGV[2];
if ($date eq "" ) { print "No start date\n";&using;exit;}
if ($date1 eq "" ) { print "No end date\n";&using;exit;}
if ($list eq "" ) { print "No list of files \n";&using;exit;}
my ($day,$month,$year,$hour,$min,$sec)=split "-",$date;
my ($day1,$month1,$year1,$hour1,$min1,$sec1)=split "-",$date1;
print "$day,$month,$year,$hour,$min,$sec\n";
$month--;
$month1--;
my $time=timelocal($sec,$min,$hour,$day,$month,$year);
my $time1=timelocal($sec1,$min1,$hour1,$day1,$month1,$year1);
my $time_delta=$time1-$time;
open (F, "< $list") || die "Can't open list files";
my @list=<F>;
close (F);
foreach my $filename (@list)
{
chomp ($filename);
print "*Copy $filename to backup $save_filename \n";
$save_filename=$filename.".bk";
my $rez=`cp -f $filename $save_filename`;
my $rrd=RRD::Editor -> new();
$rrd->open ($filename );
$step=$rrd->minstep();
my $st_row;
my ($end_row,$end_row1);
my ($val,$a,$b);
my $st_row1;my $st_row;my $st;
my @ds_names= $rrd -> DS_names();
my $nums=$rrd->num_RRAs();
for ($idx=0;$idx < $nums;$idx++)
{
$step=$rrd->RRA_step($idx);
($st_time,undef)=$rrd->RRA_el($idx,$ds_names[0],0);
print "################ $idx - step-> $step $time $st_time ################\n";
$st_row=int (($time-$st_time)/$step);
$end_row=($time1-$st_time)/$step;
$end_row1=int($end_row);
if ($end_row1 != 0 ) {$end_row=int($end_row)+1;}
my $numrows = $rrd->RRA_numrows($idx);
print " Start -> $st_row End -> $end_row MaxRow=$numrows\n";
if ($st_row >= $numrows) {print "Row overlow\n";next;}
if ($end_row >= $numrows) {print "Row overlow\n"; $end_row--; next;}
foreach my $ds (@ds_names)
{
(undef,$a)=$rrd->RRA_el($idx, $ds, $st_row);
(undef,$b)=$rrd->RRA_el($idx, $ds, $end_row);
$row_delta=($end_row-$st_row);
print "######Row delta $row_delta\n";
if ($row_delta == 0) { $st=0; $st_row1=$st_row;}
else { $st=($b-$a)/$row_delta;
$st_row1=$st_row + 1;}
$val=$a+$st;
print "#### $idx $ds $a<->$b #### $row_delta ########################\n";
for ($row=$st_row1;$row<$end_row;$row++)
{
print "read -> ";my(undef,$k)= $rrd->RRA_el($idx, $ds, $row); print "$k\n";
print "writing -> $idx $ds -> $row -> $val\n";
$rrd->set_RRA_el($idx,$ds,$row,$val);
$val=$val+$st;
}
}
}
print "\n";
$rrd->save($filename);
}
sub using
{
print << 'EOF';
Script for remove holes in rrd files.
remove_hole_group.pl <DD-MM-YYYY-HH-MM-SS> <DD-MM-YYYY-HH-MM-SS> <file with list files>
EOF
}
exit;
Если надо исправить max и min в rrd файле
#!/usr/bin/perl
#
use strict;
use RRD::Editor;
#
my $rrd;
my $min="";
my $max="";
my $filename="";
($min,$max,$filename) =@ARGV;
if (($min eq "") or ($min eq "-h") or ($min eq "-?")) {&using;exit;}
if ($max eq "") {print "Didn't set maximum\n";&using;exit;}
if ($filename eq "") {print "Can't open file\n";&using;exit}
print "Processing min/max update ---$filename \n";
my $sv_filename=$filename.".bk";
my $rez=`cp $filename $sv_filename`;
$rrd=RRD::Editor -> new();
$rrd->open ($filename);
my @ds_names = $rrd->DS_names();
if (($min eq "")and($max eq "" ))
{
foreach (@ds_names)
{
$min=$rrd->DS_min($_);
$max=$rrd->DS_max($_);
print "Minimum ds name $_: $min\n";
print "Maximum ds name $_: $max\n";
}
exit;
}
if ($max eq "") { $max=$min;$min=0;}
foreach (@ds_names)
{
$rrd->set_DS_min($_, $min);
$rrd->set_DS_max($_, $max);
}
$rrd->save($filename);
$rrd->close();
sub using
{
print << 'EOF';
Script for change maximum and minimum in rrd files
rrd_repair_max.pl <min> <max> <rrd filename>
EOF
}
Часто в cacti для благообразного вида графиков нужно бывает отредактировать rrd базы данных.
Если надо замазать дырку одиночном файле. Список rrd обычный текстовый файли с перечнем rrd файлов.
#!/usr/bin/perl
# Writen by Timur.Nigamov
#remove_hole <rrd file> <hole start time DD-MM-YYYY-HH-MM-SS> <hole end time DD-MM-YYYY-HH-MM-SS>
#
use RRD::Editor ();
use strict;
use Time::Local;
my $date1;my $st_time;my $idx;
my $date;my $row_delta; my $step;
my $row;
my $filename=$ARGV[0];
my $date=$ARGV[1];
my $date1=$ARGV[2];
if ($filename eq "" ) { print "No file\n";exit;}
if ($date eq "" ) { print "No start date\n";exit;}
if ($date1 eq "" ) { print "No end date\n";exit;}
my ($day,$month,$year,$hour,$min,$sec)=split "-",$date;
my ($day1,$month1,$year1,$hour1,$min1,$sec1)=split "-",$date1;
print "$day,$month,$year,$hour,$min,$sec";
$month--;
$month1--;
my $time=timelocal($sec,$min,$hour,$day,$month,$year);
my $time1=timelocal($sec1,$min1,$hour1,$day1,$month1,$year1);
my $time_delta=$time1-$time;
my $save_filename=$filename.".bk";
print "$filename\n";
print "$save_filename\n";
my $rrd=RRD::Editor -> new();
$rrd->open ($filename );
$step=$rrd->minstep();
my $st_row;
my ($end_row,$end_row1);
my ($val,$a,$b);
my $st_row1;my $st_row;my $st;
my @ds_names= $rrd -> DS_names();
my $nums=$rrd->num_RRAs();
for ($idx=0;$idx < $nums;$idx++)
{
$step=$rrd->RRA_step($idx);
($st_time,undef)=$rrd->RRA_el($idx,$ds_names[0],0);
print "################ $idx - step-> $step $time $st_time ################\n";
$st_row=int (($time-$st_time)/$step);
$end_row=($time1-$st_time)/$step;
$end_row1=int($end_row);
if ($end_row1 != 0 ) {$end_row=int($end_row)+1;}
my $numrows = $rrd->RRA_numrows($idx);
print "######## Start -> $st_row End -> $end_row MaxRow=$numrows#######\n";
if ($st_row >= $numrows) {print "Row overlow\n";next;}
if ($end_row >= $numrows) {print "Row overlow\n"; $end_row--; next;}
foreach my $ds (@ds_names)
{
(undef,$a)=$rrd->RRA_el($idx, $ds, $st_row);
(undef,$b)=$rrd->RRA_el($idx, $ds, $end_row);
$row_delta=($end_row-$st_row);
print "###### Row delta $row_delta ###########################################\n";
if ($row_delta == 0) { $st=0; $st_row1=$st_row;}
else { $st=($b-$a)/$row_delta;
$st_row1=$st_row + 1;}
$val=$a+$st;
print "#### $idx $ds $a<->$b ####### $row_delta ########################\n";
for ($row=$st_row1;$row<$end_row;$row++)
{
print "#### read -> ";my(undef,$k)= $rrd->RRA_el($idx, $ds, $row); print "$k\n";
print "#### writing -> $idx $ds -> $row -> $val\n";
$rrd->set_RRA_el($idx,$ds,$row,$val);
$val=$val+$st;
}
}
}
print "\n";
$rrd->save($save_filename);
Если надо устранить пробелы в группе файлов
!/usr/bin/perl
#
#Timur Nigamov (c) 2014
#
use RRD::Editor ();
use strict;
use Time::Local;
my $date1;my $st_time;my $idx;
my $date;my $row_delta; my $step;
my $row;
my $filename;
my $save_filename;
my $date=$ARGV[0];
my $date1=$ARGV[1];
my $list=$ARGV[2];
if ($date eq "" ) { print "No start date\n";&using;exit;}
if ($date1 eq "" ) { print "No end date\n";&using;exit;}
if ($list eq "" ) { print "No list of files \n";&using;exit;}
my ($day,$month,$year,$hour,$min,$sec)=split "-",$date;
my ($day1,$month1,$year1,$hour1,$min1,$sec1)=split "-",$date1;
print "$day,$month,$year,$hour,$min,$sec\n";
$month--;
$month1--;
my $time=timelocal($sec,$min,$hour,$day,$month,$year);
my $time1=timelocal($sec1,$min1,$hour1,$day1,$month1,$year1);
my $time_delta=$time1-$time;
open (F, "< $list") || die "Can't open list files";
my @list=<F>;
close (F);
foreach my $filename (@list)
{
chomp ($filename);
print "*Copy $filename to backup $save_filename \n";
$save_filename=$filename.".bk";
my $rez=`cp -f $filename $save_filename`;
my $rrd=RRD::Editor -> new();
$rrd->open ($filename );
$step=$rrd->minstep();
my $st_row;
my ($end_row,$end_row1);
my ($val,$a,$b);
my $st_row1;my $st_row;my $st;
my @ds_names= $rrd -> DS_names();
my $nums=$rrd->num_RRAs();
for ($idx=0;$idx < $nums;$idx++)
{
$step=$rrd->RRA_step($idx);
($st_time,undef)=$rrd->RRA_el($idx,$ds_names[0],0);
print "################ $idx - step-> $step $time $st_time ################\n";
$st_row=int (($time-$st_time)/$step);
$end_row=($time1-$st_time)/$step;
$end_row1=int($end_row);
if ($end_row1 != 0 ) {$end_row=int($end_row)+1;}
my $numrows = $rrd->RRA_numrows($idx);
print " Start -> $st_row End -> $end_row MaxRow=$numrows\n";
if ($st_row >= $numrows) {print "Row overlow\n";next;}
if ($end_row >= $numrows) {print "Row overlow\n"; $end_row--; next;}
foreach my $ds (@ds_names)
{
(undef,$a)=$rrd->RRA_el($idx, $ds, $st_row);
(undef,$b)=$rrd->RRA_el($idx, $ds, $end_row);
$row_delta=($end_row-$st_row);
print "######Row delta $row_delta\n";
if ($row_delta == 0) { $st=0; $st_row1=$st_row;}
else { $st=($b-$a)/$row_delta;
$st_row1=$st_row + 1;}
$val=$a+$st;
print "#### $idx $ds $a<->$b #### $row_delta ########################\n";
for ($row=$st_row1;$row<$end_row;$row++)
{
print "read -> ";my(undef,$k)= $rrd->RRA_el($idx, $ds, $row); print "$k\n";
print "writing -> $idx $ds -> $row -> $val\n";
$rrd->set_RRA_el($idx,$ds,$row,$val);
$val=$val+$st;
}
}
}
print "\n";
$rrd->save($filename);
}
sub using
{
print << 'EOF';
Script for remove holes in rrd files.
remove_hole_group.pl <DD-MM-YYYY-HH-MM-SS> <DD-MM-YYYY-HH-MM-SS> <file with list files>
EOF
}
exit;
Если надо исправить max и min в rrd файле
#!/usr/bin/perl
#
use strict;
use RRD::Editor;
#
my $rrd;
my $min="";
my $max="";
my $filename="";
($min,$max,$filename) =@ARGV;
if (($min eq "") or ($min eq "-h") or ($min eq "-?")) {&using;exit;}
if ($max eq "") {print "Didn't set maximum\n";&using;exit;}
if ($filename eq "") {print "Can't open file\n";&using;exit}
print "Processing min/max update ---$filename \n";
my $sv_filename=$filename.".bk";
my $rez=`cp $filename $sv_filename`;
$rrd=RRD::Editor -> new();
$rrd->open ($filename);
my @ds_names = $rrd->DS_names();
if (($min eq "")and($max eq "" ))
{
foreach (@ds_names)
{
$min=$rrd->DS_min($_);
$max=$rrd->DS_max($_);
print "Minimum ds name $_: $min\n";
print "Maximum ds name $_: $max\n";
}
exit;
}
if ($max eq "") { $max=$min;$min=0;}
foreach (@ds_names)
{
$rrd->set_DS_min($_, $min);
$rrd->set_DS_max($_, $max);
}
$rrd->save($filename);
$rrd->close();
sub using
{
print << 'EOF';
Script for change maximum and minimum in rrd files
rrd_repair_max.pl <min> <max> <rrd filename>
EOF
}
Комментариев нет:
Отправить комментарий