NOC - Network Operations Center - Overview for Nagios

Copy and paste the next code to a file (like index.php) and make sure the location of the status.dat (like /usr/local/nagios/var/status.dat) is correctly defined in the code.

Point your webbrowser to it and voila, you will see all the problems in one view.

--------------------------------------------------------------------------------------------------------------------------------------------------------------

<?php

 $file = fopen("/usr/local/nagios/var/status.dat", "r") or exit("Unable to open file!"); //path to nagios file
 $refreshvalue = 60; //value in seconds to refresh page

 $collastcheck=true; //true/false to show last state change (old=checked) date column in table
 $colhost=true; //true/false to show host column in table
 $colstatusinfo=true; //true/false to show status info/plugin info column in table
 $colservice=true; //true/false to show service type column column in table


 $pagetitle = "Unix4Life Nagios NOC";

 $thedate=date('Y-m-d H:i:s');
 $showthedate=false;
?>
 <html>
 <link rel="shortcut icon" href="/images/favicon.ico">
 <body>
 <body style="background:#000000">
 <font color="white">
 <meta http-equiv="refresh" content="<?php echo($refreshvalue); ?>">
 <title><?php echo($pagetitle); ?></title>
 <style type="text/css">
body
{
        margin:0px;
}

.boldtable, .boldtable TD, .boldtable TH
{
font-family:sans-serif;
font-size:12pt;
font-weight:normal;
color:white;
}

.c{ font-size:15pt;
font-weight:bold;
text-align: left;
font-family:sans-serif;
color:white;
}

</style>

<form name=countdown class=c>

<center>
Unix4Life Nagios NOC - refresh in <input type=text size=2 name=secs>
<?php
 if ($showthedate==true){
  echo($thedate);
 }
?>
</center>

</form>

<center>
<font color=FF0000><b> Red </font> - Critical/Down/Unreachable</b> &nbsp;&nbsp;&nbsp;
<font color=FFF000><b> Yellow </font> - Warning</b> &nbsp;&nbsp;&nbsp;
<font color=FFA500><b> Orange </font> - Unknown</b> &nbsp;&nbsp;&nbsp;
<font color=0080FF><b> Blue </font> - Acknowledged</b> &nbsp;&nbsp;&nbsp;
<font color=808080><b> Grey</font> - Disabled</b> <i><a href="/nocwd.php" style="float: right; color: white;" target="_parent">Show NOC with Disabled Services</a></i>
</center>

<script>
<!--
//
 var milisec=0
 var seconds=<?php echo($refreshvalue); ?>
 document.countdown.secs.value='<?php echo($refreshvalue); ?>'

function display(){
 if (milisec<=0){
    milisec=9
    seconds-=1
 }
 if (seconds<=-1){
    milisec=0
    seconds+=1
 }
 else
    milisec-=1
    document.countdown.secs.value=seconds+"."+milisec+"s"
    setTimeout("display()",100)
}
display()
-->
</script>

<?php

//i may have missed some column displays here so add them in if you need them

if (($collastcheck==true) and ($colhost==true) and ($colstatusinfo==true) and ($colservice==false)){
echo("<table width=75% border=0 class=boldtable align=center>");
}

if (($collastcheck==false) and ($colhost==true) and ($colstatusinfo==true) and ($colservice==false)){
echo("<table width=75% border=0 class=boldtable align=center>");
}

if (($collastcheck==false) and ($colhost==false) and ($colstatusinfo==true) and ($colservice==false)){
echo("<table width=75% border=0 class=boldtable align=center>");
}

if (($collastcheck==false) and ($colhost==true) and ($colstatusinfo==false) and ($colservice==false)){
echo("<table width=75% border=0 class=boldtable align=center>");
}
//This is the Coloumn settings for services and stuff
if (($collastcheck==true) and ($colhost==true) and ($colstatusinfo==true) and ($colservice==true)){
echo("<table width=95% border=0 class=boldtable align=center>");
}

echo("<tr>");
//Column widths
if ($collastcheck==true){
echo("<td width=10%>State Change</td>");
}

if ($colhost==true){
echo("<td width=15%>Host</td>");
}

if ($colstatusinfo==true){
echo("<td width=60%>Status Information</td>");
}

if ($colservice==true){
echo("<td width=20%>Service</td>");
}

echo("</tr>");

function dashdisplay($dashline,$collastcheck,$colhost,$colstatusinfo,$colservice){      //function to display array data into table with colors/font etc

 $dashstatus=substr($dashline,0,strpos($dashline,','));
 $dashline=substr($dashline,strpos($dashline,',')+1,strlen($dashline));
//Here are the colours for the main interface!
  if ($dashstatus==0){
   $dashstatus="UP"; // **** Change the colour from white *****
   $trcolor="000000"; //tr row color
   $fontcolor="000000"; //td text font color
  }

  if ($dashstatus==1){
   $dashstatus="WARNING";//Warning colours
   $trcolor="fff000";  //tr row color
   $fontcolor="black";  //td text font color
   $tdackcolor="0080FF";  //td bgcolor indicating acknowledged items
  }

  if ($dashstatus==2){
   $dashstatus="CRITICAL";
   $trcolor="ff0000";  //tr row color
   $fontcolor="black";  //td text font color
   $tdackcolor="0080FF";  //td bgcolor indicating acknowledged items (i dont think criticals can be acknowledged, put it in anyway)
  }

  if ($dashstatus==3){
   $dashstatus="UNKNOWN";
   $trcolor="ffa500";  //tr row color
   $fontcolor="black";  //td text font color
   $tdackcolor="0080FF";  //td bgcolor indicating acknowledged items (i dont think unknowns can be acknowledged, put it in anyway)
  }

  if ($dashstatus==4){
   $dashstatus="DISABLED";
   $trcolor="808080";  //tr row color
   $fontcolor="black";  //td text font color
   $tdackcolor="0080FF";  //td bgcolor indicating acknowledged items (i dont think disableds can be acknowledged, put it in anyway)
  }


 $dashack=substr($dashline,0,strpos($dashline,','));
 $dashline=substr($dashline,strpos($dashline,',')+1,strlen($dashline));

  if ($dashack==""){ //if somehow the acknowledgement state is blank, set it to 0 (acknowledged)
   $dashack=0;
  }

 $dashdate=substr($dashline,0,strpos($dashline,',')-1);
 $dashline=substr($dashline,strpos($dashline,',')+1,strlen($dashline));

 $dashhost=substr($dashline,0,strpos($dashline,',')-1);
 $dashline=substr($dashline,strpos($dashline,',')+1,strlen($dashline));

 $dashplugin=substr($dashline,0,strrpos($dashline,',')-1);
 $dashline=substr($dashline,strrpos($dashline,',')+1,strlen($dashline));

 $dashservice=$dashline;

  if ($fontcolor!=""){

    if ($colservice==true){
     if ($dashservice==""){
      if ($dashstatus=="DISABLED"){
       echo("<tr width=10px bgcolor=\"808080\">");
      }else{
       echo("<tr width=10px bgcolor=\"FF0000\">");
      }
     }else{
      echo("<tr width=10px bgcolor=\"$trcolor\">");
     }
    }

    if ($collastcheck==true){
     if ($dashack==0){
     echo("<td><font color=\"$fontcolor\">".date("Y-m-d H:i:s",$dashdate)."</font></td>");
     }elseif ($dashack==1){
      echo("<td bgcolor=\"$tdackcolor\"><font color=\"$fontcolor\">".date("Y-m-d H:i:s",$dashdate)."</font></td>");
     }
    }

    if ($colhost==true){
     if ($dashack==0){
      echo("<td><font color=\"$fontcolor\">".$dashhost."</font></td>");
     }elseif ($dashack==1){
     if ($dashservice==""){
      echo("<td bgcolor=\"$tdackcolor\"><font color=\"ff0000\">".$dashhost." (ACK)</font></td>");
      }else{
      echo("<td bgcolor=\"$tdackcolor\"><font color=\"$fontcolor\">".$dashhost."</font></td>");
     }
     }
    }

    if ($colstatusinfo==true){
     if ($dashack==0){
     echo("<td><font color=\"$fontcolor\">".$dashplugin."</font></td>");
     }elseif ($dashack==1){
      echo("<td bgcolor=\"$tdackcolor\"><font color=\"$fontcolor\">".$dashplugin."</font></td>");
     }
    }

    if ($colservice==true){
     if ($dashservice==""){
      if ($dashstatus=="DISABLED"){
       echo("<td><b><font color=\"black\">HOST IS IN BLACKOUT</font></b></td>");
       }elseif ($dashservice==""){
     if ($dashack==0){
        echo("<td><b><font color=\"black\">HOST IS DOWN!!!</font></b></td>");
     }elseif ($dashack==1){
        echo("<td bgcolor=\"$tdackcolor\"><b><font color=\"black\">HOST IS DOWN!!!</font></b></td>");
     }
       }else{
     if ($dashack==0){
        echo("<td><font color=\"$fontcolor\">".$dashservice."</font></td>");
     }elseif ($dashack==1){
      echo("<td bgcolor=\"$tdackcolor\"><font color=\"ff0000\">".$dashservice." (ACK)</font></td>");
     }
       }
      }else{
       if ($dashstatus=="DISABLED"){
        echo("<td><b><font color=\"black\">service is disabled</font></b></td>");
       }else{
     if ($dashack==0){
        echo("<td><font color=\"$fontcolor\">".$dashservice."</font></td>");
     }elseif ($dashack==1){
      echo("<td bgcolor=\"$tdackcolor\"><font color=\"ff0000\">".$dashservice." (ACK)</font></td>");
     }
      }
     }
    }
   echo("</tr>");
  }

 echo("</font>");
 echo("</tr>");
} //end display function


 //arrays for different fields in nagios status.dat
 $hostarray = array();
 $servicearray = array();
 $statearray = array();
 $pluginarray = array();
 $checkarray = array();
 $ackarray = array();
 $disarray = array();

 //arrays for status of hosts/services
 $finaluparray = array();
 $finalunknarray = array();
 $finalwarnarray = array();
 $finalcritarray = array();
 $finaldisarray = array();

 //field to check in nagios status.dat
 $hostname = 'host_name=';
 $hostgroup= 'host_group=';
 $servicedes = 'service_description=';
 $currstate = 'current_state=';
 $pluginout = 'plugin_output=';
 $ackcheck = 'been_acknowledged=';
 $discheck = 'active_checks_enabled=';

//change lastcheck from last_check to last_state_change $lastcheck = 'last_check=';
 $lastcheck = 'last_state_change=';

 //counters for loops
 $hostcount = 0;
 $servicecount = 0;
 $currcount = 0;
 $plugcount = 0;
 $lastcount = 0;
 $discount = 0;
 $disttlcount = 0;
 $ackcount = 0;
 $ttlcount = 0;
 $check = 0;
 $okcount = 0;
 $warncount = 0;
 $critcount = 0;
 $unkncount = 0;

while(!feof($file)){ //begin while through nagios status.dat
 $line = fgets($file);

 //strpos to check for field line by line
  $hostpos = strpos($line,$hostname);
  $servicepos = strpos($line,$servicedes);
  $currpos = strpos($line,$currstate);
  $plugpos = strpos($line,$pluginout);
  $lastpos = strpos($line,$lastcheck);
  $dispos = strpos($line,$discheck);
  $ackpos = strpos($line,$ackcheck);

        if ($hostpos!==false){
         $hostcount++;
         $hostarray[$hostcount]=substr($line,strpos($line,'=')+1,strlen($line));
         $check=1;
        }

        $check=0;

        if ($servicepos!==false){
         $servicecount++;
         $servicearray[$servicecount]=substr($line,strpos($line,'=')+1,strlen($line));
         $check=1;
        }

        $check=0;

        if ($currpos!==false){
         $currcount++;
         $statearray[$currcount]=substr($line,strpos($line,'=')+1,strlen($line));
         $check=1;
        }

        $check=0;

        if ($plugpos!==false){
         if (strpos($line,"long_plugin_output=")===false){
          $plugcount++;
          $pluginarray[$plugcount]=substr($line,strpos($line,'=')+1,strlen($line));
          $check=1;
         }
        }

        $check=0;

        if ($lastpos!==false){
         $lastcount++;
         $checkarray[$lastcount]=substr($line,strpos($line,'=')+1,strlen($line));
         $check=1;
        }

        $check=0;

        if ($servicearray[$servicecount]!=""){ //if the host has a service being checked
        if ($dispos!==false){
         $discount++;
         $disarray[$discount]=substr($line,strpos($line,'=')+1,strlen($line));
         $check=1;
        }

        $check=0;

        if ($ackpos!==false){
         $ackcount++;
         $ackarray[$ackcount]=substr($line,strpos($line,'=')+1,strlen($line));
         $check=1;
        }
        }

        if ($servicearray[$servicecount]==""){ //if the host has no service being checked
         if ($ackpos!==false){
         $ackcount++;
         $ackarray[$ackcount]=substr($line,strpos($line,'=')+1,strlen($line));
         $check=1;
        }

        $check=0;

        if ($dispos!==false){
         $discount++;
         $disarray[$discount]=substr($line,strpos($line,'=')+1,strlen($line));
         $check=1;
         }
        }

        if ($check==1){ //if for final array building
         $ttlcount++;

         if ($disarray[$ttlcount]==1){ //if for active checks being enabled (1)

          if ($statearray[$ttlcount]==0){ //if for state being up/ok (0 for acknowledgements, you dont acknowledge an up service/host)
           $okcount++;
           $finaluparray[$okcount]=$statearray[$ttlcount].",0,".$checkarray[$ttlcount].",".$hostarray[$ttlcount].",".$pluginarray[$ttlcount].",".$servicearray[$servicecount];
          }

          if ($statearray[$ttlcount]==1){ //if for state being warning
           $warncount++;
            if ($ackarray[$ttlcount]==""){
             $ackarray[$ttlcount]=0;
            }

           $finalwarnarray[$warncount]=$statearray[$ttlcount].",".$ackarray[$ttlcount].",".$checkarray[$ttlcount].",".$hostarray[$ttlcount].",".$pluginarray[$ttlcount].",".$servicearray[$servicecount];
         }

         if ($statearray[$ttlcount]==2){ //if for state being critical
          $critcount++;

           if ($ackarray[$ttlcount]==""){
            $ackarray[$ttlcount]="0";
           }

          $finalcritarray[$critcount]=$statearray[$ttlcount].",".$ackarray[$ttlcount].",".$checkarray[$ttlcount].",".$hostarray[$ttlcount].",".$pluginarray[$ttlcount].",".$servicearray[$servicecount];
         }

 

         if ($statearray[$ttlcount]==3){ //if for state being unknown
          $unkncount++;

           if ($ackarray[$ttlcount]==""){
            $ackarray[$ttlcount]="0";
           }

          $finalunknarray[$unkncount]=$statearray[$ttlcount].",".$ackarray[$ttlcount].",".$checkarray[$ttlcount].",".$hostarray[$ttlcount].",".$pluginarray[$ttlcount].",".$servicearray[$servicecount];
         }


        }  //end if for active checks being enabled

         //if active checks are 0 then checking is disabled (0), the 4 represents the disabled state

// The next 4 lines enable the NOC with DISABLED services (Ad Kuijpers 4-3-2020)
// So if you want to NOT display the disabled, then just comment those 4 lines

//       if ($disarray[$ttlcount]==0){
//        $disttlcount++;
//        $finaldisarray[$disttlcount]="4,0,".$checkarray[$ttlcount].",".$hostarray[$ttlcount].",".$pluginarray[$ttlcount].",".$servicearray[$servicecount];
//       }

 

        }  //end if for final array building
}//end while loop through nagios status.dat

fclose($file);

//loops sending arrays and column details to display function
//loops run in order > critical, warnings, unknown, up, disabled

for ($l = 1; $l <= $critcount; $l++) {
   dashdisplay($finalcritarray[$l],$collastcheck,$colhost,$colstatusinfo,$colservice);
}

for ($m = 1; $m <= $warncount; $m++) {
   dashdisplay($finalwarnarray[$m],$collastcheck,$colhost,$colstatusinfo,$colservice);
}

for ($u = 1; $u <= $unkncount; $u++) {
   dashdisplay($finalunknarray[$u],$collastcheck,$colhost,$colstatusinfo,$colservice);
}

for ($o = 1; $o <= $disttlcount; $o++) {
   dashdisplay($finaldisarray[$o],$collastcheck,$colhost,$colstatusinfo,$colservice);
}

echo("<hr>");
echo("</table>");
echo("<hr>");
echo("</body>");
echo("</html>");

?>

 

 

  Donate now via the Paypal button on the top in US Dollar and on the bottom in Euro