fopen tager kun den sidste
Hej der ude. Jeg er igang med at lege med en lille kort applikation til nagios, jeg er desvære bare stadig i begynder stadiet inden for php.Jeg skal have en fil kaldet marker.php til at loade forskellige informationer fra hosts.cfg men lige nu henter den kun den sidste og ikke de 2 første informationer osse. Kan være det er lidt nemmere at se sig ud af i koden:
Hosts.cfg:
define host {
host_name test1
alias test1
address 001.001.001.001
check_command check-host-alive
use WRT-54GL
notes notes latlng:56.618356,8.188022
register 1
}
define host {
host_name test2
alias test2
address 002.002.002.002
check_command check-host-alive
use WRT-54GL
notes notes latlng:55.025175,9.409651
register 1
}
define host {
host_name test3
alias test3
address 003.003.003.003
check_command check-host-alive
use WRT-54GL
notes notes latlng:55.850638,9.846175
register 1
}
----------------------------------------
marker.php:
<?
if (file_exists("/usr/local/nagios/etc/objects/moire.cfg")) {
$hosts_file = "/usr/local/nagios/etc/objects/moire.cfg";
} else {
$hosts_file = "/usr/local/nagios/share/nagmap/hosts.cfg";
}
$fp = fopen($hosts_file,"r");
$comment = ";";
include("status.php");
$s = nagmap_status();
#echo "<pre>";
#print_r($s);
while (!feof($fp)) {
$line = trim(fgets($fp));
if ($line && !ereg("^$comment", $line)) {
if (ereg("^define host{", $line)) {
$i++;
// $host[$i] = $i;
} elseif (!ereg("}",$line)) {
$line = trim($line);
$line = preg_replace('/\s+/', ' ', $line);
$pieces = explode(" ", $line, 2);
$option = trim($pieces[0]);
$value = trim($pieces[1]);
$data[$i][$option] = $value;
}
}
}
unset($i);
#echo '<pre>';
#print_r($data);
#hosts definition
foreach ($data as $host) {
$nagios_host_name = $host["host_name"];
foreach ($host as $option => $value) {
$host["host_name"] = str_replace('-','_',$host["host_name"]);
$host["host_name"] = str_replace('.','_',$host["host_name"]);
if (($option == "notes") && (ereg("latlng",$value))) {
$value = explode(":",$value);
$value = $value[1];
$option = "latlng";
};
if (($option != "latlng") && ($option != "nagios_host_name") && (ereg("-",$value))) {
$value = str_replace('-','_',$value);
$value = str_replace('.','_',$value);
if(($option == "parents") OR ($option == "host_name")) {
$value = "x".$value;
};
};
$hosts["x".$host["host_name"]]["nagios_host_name"] = $nagios_host_name;
$hosts["x".$host["host_name"]][$option] = $value;
};
};
fclose($fp);
//echo "<pre>";
//print_r($hosts);
//put markers and bubbles
foreach ($hosts as $h) {
if ((isset($h["latlng"])) and (isset($h["host_name"]))) {
// position the host to the map
echo("var ".$h["host_name"]."_pos = new google.maps.LatLng(".$h["latlng"].");\n");
// display different icons for the host (according to the status in nagios
if (($h["use"] == "wifi_hotspot") && ($s[$h["nagios_host_name"]]["plugin_output"] == "OK")) {
echo('var '.$h["host_name"]."_mark = new google.maps.Marker({".
"\nposition: ".$h["host_name"]."_pos,".
"\nicon: 'http://www.google.com/mapfiles/marker_white.png',".
"\nmap: map,".
"\ntitle: \"".$h["nagios_host_name"]."\"".
"});"."\n\n");
} elseif ($s[$h["nagios_host_name"]]["plugin_output"] == "OK") {
echo('var '.$h["host_name"]."_mark = new google.maps.Marker({".
"\nposition: ".$h["host_name"]."_pos,".
"\nicon: 'http://www.google.com/mapfiles/marker_green.png',".
"\nmap: map,".
"\ntitle: \"".$h["nagios_host_name"]."\"".
"});"."\n\n");
} else {
echo('var '.$h["host_name"]."_mark = new google.maps.Marker({".
"\nposition: ".$h["host_name"]."_pos,".
"\nicon: 'http://www.google.com/mapfiles/marker.png',".
"\nmap: map,".
"\ntitle: \"".$h["nagios_host_name"]."\"".
"});"."\n\n");
};
//generate google maps info bubble
$info = '<div class=\"bubble\"><b>'.$h["nagios_host_name"]."</b><br>Type: ".$h["use"]
.'<br>Address: '.$h["address"]
.'<br>Parents: '.$h["parents"]
.'<br>Status: '.$s[$h["nagios_host_name"]]["plugin_output"]
.'<br><a href=\"/nagios/cgi-bin/statusmap.cgi\?host='.$h["nagios_host_name"].'\">Nagios map page</a>'
.'<br><a href=\"/nagios/cgi-bin/extinfo.cgi\?type=1\&host='.$h["nagios_host_name"].'\">Nagios host page</a>';
$links = '<br><a href=\"../cgi-bin/smokeping.cgi?target=LAN.'.$h["nagios_host_name"].'\">Smokeping statistics</a>'
.'<br><a href=\"../devices/modules/mrtg_uptime/workdir/'.$h["nagios_host_name"].'.html\">Uptime Graph</a>';
if ($nagmap_bubble_links == 1) {
$info = $info.$links
.'<br><span style=\"font-size: 7pt\">Ninki Nagios</span>'
.'</div>';
} else {
$info = $info
.'<br><span style=\"font-size: 7pt\">Ninki Nagios</span>'
.'</div>';
};
echo("var ".$h["host_name"]."_mark_infowindow = new google.maps.InfoWindow({
content: '$info'
})\n");
echo("google.maps.event.addListener(".$h["host_name"]."_mark, 'click', function() {
".$h["host_name"]."_mark_infowindow.open(map,".$h["host_name"]."_mark);
});\n\n");
};
};
#echo ("<pre>");
#print_r($hosts);
//create parent connection links
#foreach ($hosts as $h) {
# if ((isset($h["parents"]) AND (isset($h["latlng"])) AND (isset($hosts[$h["parents"]]["latlng"])))) {
# echo("\nvar ".$h["host_name"].'_to_'.$h["parents"]." = new google.maps.Polyline({\n".
# "path: [".$h["host_name"].'_pos,'.$h["parents"]."_pos],\n".
# "strokeColor: \"#ee1212\",\n".
# "strokeOpacity: 0.9,\n".
# "strokeWeight: 2});\n");
# echo ($h["host_name"].'_to_'.$h["parents"].".setMap(map);\n\n");
# };
#};
?>
Håber der er nogen der kan hjælpe.