Permalink
Browse files

Added option --read to process a list of XML files. Added option --lo…

…g_old_xml to log outdated XML files.
  • Loading branch information...
simonmeggle committed Feb 10, 2015
1 parent 693c546 commit 7510fc9eaa34443d4805a18e4b660e71b5dd0afd
Showing with 82 additions and 36 deletions.
  1. +82 −36 scripts/rrd_convert.pl.in
View
@@ -49,7 +49,7 @@ my %conf = (
);
Getopt::Long::Configure('bundling');
my ( $opt_V, $opt_h, $opt_c, $opt_l, $opt_x, $opt_p, $opt_s );
my ( $opt_V, $opt_h, $opt_c, $opt_l, $opt_x, $opt_p, $opt_s, $opt_o, $opt_r );
# defaults
$opt_x = 1;
@@ -61,14 +61,20 @@ GetOptions(
"l|list_commands" => \$opt_l,
"s|stepwise" => \$opt_s,
"x|no_structure_check" => \$opt_x,
"o|log_old_xml" => \$opt_o,
"d|dry-run" => \$conf{DRY_RUN},
"t|tmp_dir=s" => \$conf{TMP_DIR},
"force" => \$conf{FORCE},
"r|read=s" => \$opt_r,
);
if (defined($opt_r)) {
$opt_c = "ALL";
}
print_help() if $opt_h;
print_help_opt_p() if !$opt_p;
print_help() if !$opt_c and !$opt_l;
print_help() if (!$opt_c and !$opt_l) and (!$opt_r);
print_version() if $opt_V;
if($opt_p){
@@ -101,7 +107,14 @@ main();
sub main{
check_storage_type();
find(\&wanted_xml_files, $conf{RRDPATH});
if ($opt_o) {
create_dir($conf{TMP_DIR});
open(OLDXMLLOG, ">", $conf{TMP_DIR}."/pnp_old_xml_files.list");
print OLDXMLLOG "- generated by rrd_convert.pl - \n";
print OLDXMLLOG "The following XML files are older than $conf{XML_MAX_AGE} seconds:\n\n";
}
build_worklist();
close(OLDXMLLOG);
summary();
if($opt_l){ # List commands and exit
summary_command_list();
@@ -194,41 +207,70 @@ sub check_ds_list{
}
}
sub wanted_xml_files{
sub build_worklist {
if ($opt_r) {
process_xml_files($opt_r);
close XMLLIST;
}else{
find(\&find_xml_files, $conf{RRDPATH});
}
}
# bulk mode, find all XML files
sub find_xml_files{
if(m/.xml$/){
#printf("File: %s\n",$File::Find::name);
my $xmlfile = $File::Find::name;
my ($rrdfile) = $xmlfile =~ /^(.*)\.xml$/;
$rrdfile .= ".rrd";
my $mtime = (stat($xmlfile))[9];
if ( $mtime < $max_age ){
$stats{old_xml}++;
return;
xml2worklist($File::Find::name);
}
}
# file mode, process file with XML filenames
sub process_xml_files{
my $xmllist = shift;
-r $xmllist or die "Cannot open $xmllist: $!";
open XMLLIST, "<$opt_r";
foreach (<XMLLIST>) {
my $file = $_;
chomp($file);
m/.xml$/ && xml2worklist($file);
}
}
sub xml2worklist {
my $xmlfile = shift;
my ($rrdfile) = $xmlfile =~ /^(.*)\.xml$/;
$rrdfile .= ".rrd";
my $mtime = (stat($xmlfile))[9];
if ( $mtime < $max_age ){
$stats{old_xml}++;
if ($opt_o) {
print OLDXMLLOG $xmlfile . ": " . scalar localtime($mtime) ."\n";
}
open(XML, $xmlfile);
while (<XML>) {
if(/TEMPLATE>(.*)</){
my ($t) = split("!",$1);
push(@commands,$t);
if(( defined $opt_c) and ($t =~ /^$opt_c$/)){
if( -e $rrdfile ){
#print "Found: ".$t." in ".$xmlfile."\n";
push(@worklist,$xmlfile);
}else{
$stats{xml_without_rrd}++;
}
}elsif(( defined $opt_c) and ($opt_c eq 'ALL')){
if( -e $rrdfile ){
# Keyword 'ALL' retunrs all XML Files
push(@worklist,$xmlfile);
}else{
$stats{xml_without_rrd}++;
}
return;
}
open(XML, $xmlfile);
while (<XML>) {
if(/TEMPLATE>(.*)</){
my ($t) = split("!",$1);
push(@commands,$t);
if(( defined $opt_c) and ($t =~ /^$opt_c$/)){
if( -e $rrdfile ){
#print "Found: ".$t." in ".$xmlfile."\n";
push(@worklist,$xmlfile);
}else{
$stats{xml_without_rrd}++;
}
}
}
close(XML);
}elsif (( defined $opt_c) and ($opt_c eq 'ALL') ) {
if( -e $rrdfile ){
# Keyword 'ALL' retunrs all XML Files
push(@worklist,$xmlfile);
}else{
$stats{xml_without_rrd}++;
}
}
}
}
close(XML);
}
sub parse_xml_filename{
@@ -490,7 +532,7 @@ sub change_config {
sub check_storage_type{
if($conf{'RRD_STORAGE_TYPE'} eq "MULTIPLE"){
print "RRD_STORAGE_TYPE is already set to ".$conf{'RRD_STORAGE_TYPE'}."\n";
print "RRD_STORAGE_TYPE is already globally set to ".$conf{'RRD_STORAGE_TYPE'}."\n";
}
}
sub check_custom_template {
@@ -511,6 +553,8 @@ sub check_custom_template {
}
sub write_custom_template {
# do not write custom template in --read mode
return if $opt_r;
my $command = $opt_c;
if ( $conf{DRY_RUN} == 1 ){
print "No config check while DRY_RUN = 1\n";
@@ -546,9 +590,11 @@ sub print_help{
print " [ --dry-run ]\n";
print " [ --tmp_dir=<temp directory> ]\n";
print " [ --no_structure_check ]\n";
print " [ --stepwise ]\n";
print " [ --log_old_xml ]\n";
print " [ --stepwise ]\n";
print " [ --read=<file with XML filenames> ]\n";
print "\n";
print "This script is used to switch to RRD_STORAGE_TYPE = MULTIPLE for a given Nagios Check Command\n";
print "This script is used to switch to RRD_STORAGE_TYPE = MULTIPLE for all RRDs/a given Nagios Check Command/single RRDs \n";
print "More info online http://docs.pnp4nagios.org/pnp-0.6/rrd_convert\n";
exit;
}

0 comments on commit 7510fc9

Please sign in to comment.