Php Help

This support forum board is for support questions relating to Nagios XI, our flagship commercial network monitoring solution.
LuisN
Posts: 40
Joined: Thu May 24, 2012 10:47 am

Php Help

Post by LuisN »

Hey Guys,

I have an array which checks a file for the OID's and the Name of each. when the array is done i get a list of Raids i have and it displays the result

BUT i want to get a graph so i wanted to display at the end of the msg the results as such, | Drive1=0;;; Drive2=0;;; Drive3=0

0 = OK 1=Warn 3 =Crit

Here's my code, i hacked it up from other plugins i have found.

Code: Select all

 $host = $argv[1];
  $warn = $argv[2];
  $crit = $argv[3];
  $type = $argv[4];

  $oid_file = "/usr/lib64/nagios/plugins/oid/host_oid_$type.txt";

  exec("cat $oid_file | grep ':' | cut -d':' -f2", $oid);
  exec("cat $oid_file | grep ':' | cut -d':' -f1", $oid_name);


//print_r($oid);
//print_r($oid_name);

        $msg = "";
        $status = 0;
        for ($i=0; $i<sizeof($oid); $i++){
                $passing_oid=$oid[$i];
                $passing_name=$oid_name[$i];
                $state = "";
                exec("snmpget -v 2c -c public $host $passing_oid | cut -d':' -f4 | sed -e 's/^[ ]*//'", $state);
                //print_r($passing_oid);
                $current_drivestate = str_replace('"','',$state[0]);

                if ($current_drivestate == $crit){
                        $aux = "($current_drivestate)";
                        if ($status < 2)
                                $status = 2;
                }
                else if ($current_drivestate == $warn){
                        $aux = "($current_drivestate)";
                        if ($status < 1)
                                $status = 1;
                }
                else{
                        $aux = "($current_drivestate)";
                }
                //print_r($current_drivestate);

                $msg .= "Status of $passing_name $result <br>";
                //$msg .= "|$end_result";
        }
my result for this would be ,

Status of RaidArray (ok) <br>Status of Drive1 (ok) <br>Status of Drive2 (ok) <br>Status of Drive3 (ok) <br>Status of Drive4 (ok) <br>


BUT at the end of this i want to have | Drive1=0;;; Drive2= etc etc.. how could i accomplish this?
mguthrie
Posts: 4380
Joined: Mon Jun 14, 2010 10:21 am

Re: Php Help

Post by mguthrie »

Something like this:

Code: Select all

$msg = "";
        $perfdata="";  //create a separate string for perfdata
        $status = 0;
        for ($i=0; $i<sizeof($oid); $i++){
                $passing_oid=$oid[$i];
                $passing_name=$oid_name[$i];
                $state = "";
                exec("snmpget -v 2c -c public $host $passing_oid | cut -d':' -f4 | sed -e 's/^[ ]*//'", $state);
                //print_r($passing_oid);
                $current_drivestate = str_replace('"','',$state[0]);

                if ($current_drivestate == $crit){
                        $aux = "($current_drivestate)";
                        if ($status < 2)
                                $status = 2;
                }
                else if ($current_drivestate == $warn){
                        $aux = "($current_drivestate)";
                        if ($status < 1)
                                $status = 1;
                }
                else{
                        $aux = "($current_drivestate)";
                }
                //print_r($current_drivestate);

                $msg .= "Status of $passing_name $result <br>";
                $perfdata .= " $end_result  ";  //don't add the pipe yet, just perfdata string with a space added after
        }
//add pipe and all perfdata after the loop is over
$msg.=" | ".$perfdata; 
LuisN
Posts: 40
Joined: Thu May 24, 2012 10:47 am

Re: Php Help

Post by LuisN »

Guys thanks so much for the help. heres the code i placed after editing the one you pasted.



$msg .= "Status of $passing_name $aux <br>";
$perfdata .= "$passing_name=$status;;; "; //don't add the pipe yet, just perfdata string with a space added after
}
//add pipe and all perfdata after the loop is over
$msg.=" | ".$perfdata;
LuisN
Posts: 40
Joined: Thu May 24, 2012 10:47 am

Re: Php Help

Post by LuisN »

Ignore my last post. i got it working like so..

if ($current_drivestate == $crit){
$aux = "($current_drivestate)";
$perf_drive ="passing_name=2";
if ($status < 2)
$status = 2;
}
else if ($current_drivestate == $warn){
$aux = "($current_drivestate)";
$perf_drive ="$passing_name=1";
if ($status < 1)
$status = 1;
}
else{
$aux = "($current_drivestate)";
$perf_drive = "$passing_name=0";
}
//print_r($current_drivestate);

$msg .= "Status of $passing_name $aux <br>";
$perfdata .= "$perf_drive "; //don't add the pipe yet, just perfdata string with a space added after
}
//add pipe and all perfdata after the loop is over
$msg .=" | ".$perfdata;