ca5a562ac742956a9463bc68f49561ed8c01eaea
[paste/332.git] / ping.php
1 <?php\r
2 /**\r
3  * Pings a given host and returns a red or green image.\r
4  * Green when the host is online, red when offline.\r
5  *\r
6  * Keywords: http image ping online check\r
7  *\r
8  * @author Christian Weiske <weiske@mogic.com>\r
9  */\r
10 if (!isset($_GET['host'])) {\r
11     sendError('Host missing');\r
12 }\r
13 $host = $_GET['host'];\r
14 if ($host == '') {\r
15     sendError('Empty host');\r
16 }\r
17 \r
18 if (!isset($_GET['size'])) {\r
19     $size = 100;\r
20 } else {\r
21     $size = intval($_GET['size']);\r
22 }\r
23 \r
24 exec('ping -c 1 -w 1 -W 1 ' . escapeshellarg($host), $output, $retval);\r
25 if ($retval == 0) {\r
26     //all fine\r
27     sendSvg($size, 'fill: green');\r
28 } else {\r
29     //error\r
30     sendSvg($size, 'fill: red');\r
31 }\r
32 \r
33 function sendSvg($size, $style)\r
34 {\r
35     $half = intval($size / 2);\r
36     header('HTTP/1.0 200 OK');\r
37     header('Content-type: image/svg+xml');\r
38     echo <<<XML\r
39 <?xml version="1.0"?>\r
40 <svg xmlns="http://www.w3.org/2000/svg" width="$size" height="$size">\r
41   <circle cx="$half" cy="$half" r="$half" style="$style"/>\r
42 </svg>\r
43 \r
44 XML;\r
45 }\r
46 \r
47 function sendError($msg)\r
48 {\r
49     header('HTTP/1.0 400 Bad Request');\r
50     header('Content-type: text/plain');\r
51     echo $msg . "\n";\r
52     exit(1);\r
53 }\r
54 ?>\r