* Pings a given host and returns a red or green image.\r
* Green when the host is online, red when offline.\r
*\r
+ * Error: ping: socket: Operation not permitted\r
+ * (raw socket required by specified options).\r
+ *\r
+ * Solution:\r
+ * $ chmod +s /path/to/ping\r
+ * Alternative:\r
+ * $ setcap cap_net_raw+ep /path/to/ping\r
+ * Alternative:\r
+ * make it passwordless sudo\r
+ *\r
* Keywords: http image ping online check\r
*\r
* @author Christian Weiske <weiske@mogic.com>\r
$size = intval($_GET['size']);\r
}\r
\r
-exec('ping -c 1 -w 1 -W 1 ' . escapeshellarg($host), $output, $retval);\r
+exec('ping -c 1 -w 1 -W 1 ' . escapeshellarg($host) . ' 2>&1', $output, $retval);\r
if ($retval == 0) {\r
//all fine\r
sendSvg($size, 'fill: green');\r
-} else {\r
- //error\r
+} else if ($retval == 1) {\r
+ //no reply\r
sendSvg($size, 'fill: red');\r
+} else {\r
+ //some other error, e.g. permissions\r
+ header('HTTP/1.0 500 Internal server error');\r
+ header('Content-type: text/plain');\r
+ echo implode("\n", $output) . "\n";\r
}\r
\r
function sendSvg($size, $style)\r
$half = intval($size / 2);\r
header('HTTP/1.0 200 OK');\r
header('Content-type: image/svg+xml');\r
+ echo '<' . '?xml version="1.0"?>' . "\n";\r
echo <<<XML\r
-<?xml version="1.0"?>\r
<svg xmlns="http://www.w3.org/2000/svg" width="$size" height="$size">\r
<circle cx="$half" cy="$half" r="$half" style="$style"/>\r
</svg>\r