#!/usr/bin/perl -w use XML::Simple; use XML::Parser; if ($ENV{'REQUEST_METHOD'} eq 'POST') { read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'}); } else { $buffer = $ENV{'QUERY_STRING'}; } @pairs = split(/&/, $buffer); foreach $pair (@pairs) { ($name, $value) = split(/=/, $pair); $name =~ tr/+/ /; $name =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg; $value =~ tr/+/ /; $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg; $FORM{$name} = $value; } my $newHost = $FORM{'host'}; my $newMet = $FORM{'metric'}; my $newRedSign = $FORM{'redSign'}; my $newRedVal = $FORM{'redVal'}; my $newRed = $newRedSign . " " . $newRedVal; my $newGreenSign = $FORM{'greenSign'}; my $newGreenVal = $FORM{'greenVal'}; my $newGreen = $newGreenSign . " " . $newGreenVal; $newHost = lc($newHost); my %hostMets; my $confHost; my $confMet; my $confRed; my $confGreen; my $hostcount = 0; my $metscount = 0; my $foundHost = 0; my $foundMet = 0; my $added = 0; sub readConfig { my ($xpat, $elem, %attrs) = @_; if($elem =~ /HOST/){ $confHost = $attrs{'NAME'}; if($confHost eq $newHost){ $foundHost = 1; } $hostMets{HOST}[$hostcount]{NAME} = $confHost; } elsif($elem =~ /METRIC/){ $confMet = $attrs{'NAME'}; $confRed = $attrs{'RED'}; $confGreen = $attrs{'GREEN'}; if(($confMet eq $newMet) && $foundHost){ $hostMets{HOST}[$hostcount]{METRIC}[$metscount]{NAME} = $confMet; $hostMets{HOST}[$hostcount]{METRIC}[$metscount]{RED} = $newRed; $hostMets{HOST}[$hostcount]{METRIC}[$metscount]{GREEN} = $newGreen; $foundMet = 1; $added = 1; } else { $hostMets{HOST}[$hostcount]{METRIC}[$metscount]{NAME} = $confMet; $hostMets{HOST}[$hostcount]{METRIC}[$metscount]{RED} = $confRed; $hostMets{HOST}[$hostcount]{METRIC}[$metscount]{GREEN} = $confGreen; } } } sub HandleEnd { my($xpat, $elem) = @_; if ($elem =~ /HOST/){ if($foundHost && !($foundMet)){ $hostMets{HOST}[$hostcount]{METRIC}[$metscount]{NAME} = $newMet; $hostMets{HOST}[$hostcount]{METRIC}[$metscount]{RED} = $newRed; $hostMets{HOST}[$hostcount]{METRIC}[$metscount]{GREEN} = $newGreen; $foundMet = 1; $added = 1; } $hostcount++; $foundHost = 0; $metscount = 0; } elsif ($elem =~ /METRIC/){ $metscount++; } elsif ($elem =~ /opt/){ if(!($added)){ $hostMets{HOST}[$hostcount]{NAME} = $newHost; $hostMets{HOST}[$hostcount]{METRIC}[$metscount]{NAME} = $newMet; $hostMets{HOST}[$hostcount]{METRIC}[$metscount]{RED} = $newRed; $hostMets{HOST}[$hostcount]{METRIC}[$metscount]{GREEN} = $newGreen; } } } sub HandleChar { } my $configFile = "ScoreProdMetsTest.xml"; my $configReader = new XML::Parser; $configReader->setHandlers ( Start => \&readConfig, End => \&HandleEnd, Char => \&HandleChar ); $configReader->parsefile( $configFile ); my $xsimple = XML::Simple->new(); my $xmlFormat = $xsimple->XMLout(\%hostMets, NoSort => 1, xmldecl => ''); open(FILE, ">outfile.xml"); print FILE $xmlFormat; close(FILE); print "Content-type: text/html\n\n"; print "\n"; print "METRIC ADDED\n"; print "\n"; print "Metric added
\n"; print "\n"; print "\n"; #### Config File Manipulator

Add to Score Config File


Enter the host name (example: scrpropbal800):  *
Enter the name of the metric (Note: it must appear as it is reported by Ganglia):  *

The value is Red when    

The value is Green when     *


* required field