// Read all Nagios configuration files into one huge array
foreach ($files as $file) {
$raw_data[$file] = file($file);
}
include("status.php");
//pre-define variables so the E_NOTICES do not show in webserver logs
$javascript = "";
$s = nagmap_status();
$info_msg['status'] = $s;
$i=0;
foreach ($raw_data as $file) {
foreach ($file as $line) {
//remove blank spaces
$line = trim($line);
if ($line && !preg_match("/^;/", $line) && !preg_match("/^#/", $line)) {
//replace many spaces with just one (or tab to one space)
$line = preg_replace('/\s+/', ' ', $line);
$line = preg_replace('/\t+/', ' ', $line);
if ((preg_match("/^define host{/", $line)) OR (preg_match("/^define host {/", $line)) OR (preg_match("/^define hostextinfo {/", $line)) OR (preg_match("/^define hostextinfo{
/", $line))) {
//starting a new host definition
$i++;
} elseif (!preg_match("/}/",$line)) {
//split line to options and values
$pieces = explode(" ", $line, 2);
//get rid of meaningless splits
if (count($pieces)<2) { continue; };
$option = trim($pieces[0]);
$value = trim($pieces[1]);
//remove comments from this line
$value_comm = explode(';', $value);
$data[$i][$option] = $value_comm[0];
}
}
}
}
unset($i);
//hosts definition
foreach ($data as $host) {
if (!empty($host["host_name"])) {
$nagios_host_name = $host["host_name"];
$hostname = trim($host["host_name"]);
$hostname = str_replace('-','_',$hostname);
$hostname = str_replace('.','_',$hostname);
$hostname = str_replace('/','_',$hostname);
$hostname = str_replace('(','_',$hostname);
$hostname = str_replace(')','_',$hostname);
$hostname = str_replace(' ','_',$hostname);
}
//if hostname is empty or hostname starts with exclamation mark, ignore this host
if (empty($hostname) OR (preg_match("/^\\!/", $hostname)) ) {
continue;
};
$hostname = "x".$hostname."x";
$host["host_name"] = $hostname;
foreach ($host as $option => $value) {
if ($option == "parents") {
$value = trim($value);
$value = str_replace('-','_',$value);
$value = str_replace('.','_',$value);
$value = str_replace('/','_',$value);
$value = str_replace('(','_',$value);
$value = str_replace(')','_',$value);
$parents = explode(',', $value);
$value = array();
foreach ($parents as $parent) {
$parent = trim($parent);
$parent = str_replace(' ','_',$parent);
$value[] = "x".$parent."x";
}
}
if (($option == "notes") && (preg_match("/latlng/",$value))) {
$value = explode(" ",$value);
$value = $value[1];
$value = trim($value);
$option = "latlng";
};
if (($option != "latlng") && ($option != "nagios_host_name") && ($option != "parents") && (preg_match("/-/",$value))) {
$value = str_replace('-','_',$value);
$value = str_replace('.','_',$value);
$value = trim($value);
};
$hosts[$hostname]["nagios_host_name"] = $nagios_host_name;
$hosts[$hostname][$option] = $value;
unset($parent, $parents);
};
};