http image ping online check

revision 73af925f470150d9f0047cbe9735eb7cb618def8

raw

ping.php

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

History