Nagios Ping Tool & Nagios QL
Tags: Blog, nagios, PHP
Recently I had a request from rex to modify my nagios ping tool (Official Nagios Exchange page); he wanted to use the tool with his nagios configuration.
Rex appeared to be using Nagios QL (Which I believe to be a nagios management tool) , now QL handles nagios config slightly differently . The Ping Tool reads the nagios hosts.cfg file, and turns it into a couple of arrays to use for ping & display, with QL they generate multiple hosts.cfg files, one for each hosts.
Perhaps a short way of saying it, Rex needed to read in multiple hosts.cfg files from a directory, and below is the hack to do it
Simply copy & past the below code into a file called readhosts.php, replace your readhosts.php with the new one , and set the variable $hostfilepath in config.php, something like $hostfilepath = “/hosts”; (assuming /hosts is where you keep the files
) should do the job.
<?php
#This page reads in the hosts described in the config file to an array ready for use later
if ($hostfilepath == "" ) {
?>
<h1> Please update your config file</h1>
<h2> You need to add the line $hostfilepath = "/path/to/host/config/files"; to config.php </h2>
<?php
exit;
}
#We need a counter to increment for the file array.
$counter = 0;
# A second counter is used for the array of actual hosts
$counter2 = 0;
if (is_dir($hostfilepath)) {
if ($dh = opendir($hostfilepath)) {
while (($file = readdir($dh)) !== false) {
$firstchar = substr($file,0,1);
if ( $firstchar !== "." ) {
$full_file_path = "$hostfilepath/$file";
$lines = file($full_file_path);
make_host_array();
#echo "$full_file_path<br>";
}
}
closedir($dh);
}
}
function make_host_array() {
global $lines,$ipaddress,$node_name,$counter,$counter2;
foreach ($lines as $line_num => $line) {
$current = trim($line);
if (preg_match ("/addres/", "$current")) {
$keywords = preg_split ("/[\s,]+/", "$current");
$ipaddress[$counter] = $keywords[1];
$counter++;
}
if (preg_match ("/host_name/", "$current")) {
$keywords = preg_split ("/[\s,]+/", "$current");
$node_name[$counter2] = $keywords[1];
$counter2++;
}
}
#print_r($ipaddress);
#print_r($node_name);
}
?>
