Subversion Repositories vnag

Rev

Rev 28 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
11 daniel-mar 1
<?php /* <ViaThinkSoftSignature>
77 daniel-mar 2
XADYb11P/nrgh4ZQEY8fC2h7mYMpi0Aj/fKxEEtCNnEiJdFfdPBDrFItkj+/gduXM
3
i3kU6hyR+RFstliaT8F7Yj/r7ta65WnhYw7DAsIRLTT1bC7pXC5BS+a75FQe+Lxeu
4
n9AjjXyuyy+T8S88RSiyRpRSMFn6q16Jd8naGr2IVKZ5T0lKSQBe4AYURLJ3p6U5G
5
cvKLFOerTRMxQs7BiBmJGfmSnk6oU587MxJN5vEvOa5nwpRb/vSWcMActNmZg3moh
6
xfPI1E31tMU9yV0jTAb/2bWEP8+KcysXmPBelti+iAA+QHpuYrwHYVPNFrBAmtOmB
7
/9dCd4jk/ucReEObKhow9JRPrIyJ+8hCAXZovfqKgP+jF3FdlNWypLHcCYsYClfk0
8
iYXTCPRZHTsKwKrDUPDZ+Gksq4ODBeIkkLlIGixCSmVSr8M4VAB2s9H38xPbU+vhg
9
LmgdD1mj+abGMKZnzWjsLdlyuOsRJqnmqZ3QCPhmtRnzpgnYAo5gR/QxvUiPUVMdF
10
KPZJikmEHm/esM9Ia8J7gP+Dv6BLKLTpzJIMzEU9C4Kk3cVlEX5FlqWRwUdwdN+lS
11
LX9IWCFYN504/h5EEeOZz8SxZQxFONfk/mbxTx2yFh4kXetv2GSuk1oAvZIpFbZz3
12
XHNi+t7zhQQJm67lUf/SRYuySfTamEcuA6ueayY2jQh0lFLU9yuH6LYMHoOyP+1d4
13
QJKs9M1KmEvzyX4k5Wbpd2zRJfJiUqnkqYBKDV81yCzf9f8l4NtLHDaoJo6e5w3ut
14
ch4gUlpYNmo8lS7fyhx7u3cpIGYqKpQp/8pSsPvbMisFYjmzPWkrdTnsNxpaqkN9+
15
XKOdmyumj++IDxUrIXQovLJNcT3nAF7+0lG40YigI77YD0jjQV6P4Kxwbh9sRCDjq
16
JmN7aKB4NqvA2DYrtqklvIOl85nKA8+RdXP8Nvaa/5yBvUrWZfHmcHLK6ogQ2Um28
17
ZEAiTB6HUfP94JVA6KXglPN5vQ7rR+gptxJtt82R1ms4VXJo/NPf/c2QVO29LEsxD
18
ePkfikCPJLfwNqNOYGpoUm7HQc1C60tHHPCbkMwEy1+J3GfIr+q8sHmFcxGLsqAg2
19
LECUGs5Xa/Z2wY87sdqBpYZXx0yHxfnWYWR/ixCtOcoTvUSKdqRxHZJAVdzY8gteb
20
aIXX0w1JZ6Hj9AS978aCF3Y833KtgTlyUXzD6JKVddYkpc33zK7yHJUy+QSOxYKIe
21
fo4TzHZtydviNY3vjE7aDiOO9tBGfW8ZmVNmwcicSdLUYJuEMgCWsH0hpIVSAw6pD
22
QMRu59uTvROscljpySe2DBmYaBTKKIkaDMuRQ/r15/qd5TsRQH6FjjCfvUdK+2zFI
23
Q==
11 daniel-mar 24
</ViaThinkSoftSignature> */ ?>
25
<?php
26
 
27
/*
28
 * VNag - Nagios Framework for PHP
29
 * Developed by Daniel Marschall, ViaThinkSoft <www.viathinksoft.com>
30
 * Licensed under the terms of the Apache 2.0 license
31
 *
77 daniel-mar 32
 * Revision 2023-10-13
11 daniel-mar 33
 */
34
 
35
// TODO: Also let the user decide other parameters like -4/-6 and number of pings/timeout etc.
36
 
37
# ---
38
 
39
declare(ticks=1);
40
 
41
# ---
42
 
43
class PingCheck extends VNag {
28 daniel-mar 44
        protected $argHostname = null;
11 daniel-mar 45
 
46
        public function __construct() {
47
                parent::__construct();
48
 
49
                $this->registerExpectedStandardArguments('Vhtwc');
50
 
51
                $this->getHelpManager()->setPluginName('check_ping');
52
                $this->getHelpManager()->setVersion('1.0');
53
                $this->getHelpManager()->setShortDescription('This plugin performs a simple ping.');
54
                $this->getHelpManager()->setCopyright('Copyright (C) 2011-$CURYEAR$ Daniel Marschall, ViaThinkSoft.');
55
                $this->getHelpManager()->setSyntax('$SCRIPTNAME$ -H hostname');
56
                $this->getHelpManager()->setFootNotes('If you encounter bugs, please contact ViaThinkSoft at www.viathinksoft.com');
57
 
58
                $this->addExpectedArgument($this->argHostname = new VNagArgument('H', 'hostname', VNagArgument::VALUE_REQUIRED, 'hostname', 'Hostname or IP address to be pinged', null));
59
        }
60
 
61
        protected function cbRun($optional_args=array()) {
62
                $host = $this->argHostname->getValue();
77 daniel-mar 63
                if (is_null($host)) throw new VNagException("Please enter a hostname");
11 daniel-mar 64
 
65
                $this->outputHTML("<h2>Host ".htmlentities($host)."</h2>\n\n", true);
66
 
67
                if (self::is_ipv6($host)) {
68
                        if (self::is_windows()) {
69
                                // Windows
70
                                exec("ping -6 -n 4 ".escapeshellarg($host), $outary, $code);
71
                        } else {
72
                                // Linux / MAC OSX (uses bash)
73
                                exec("ping6 -c 4 -- ".escapeshellarg($host).' 2>&1', $outary, $code);
74
                        }
75
                } else {
76
                        if (self::is_windows()) {
77
                                // Windows
78
                                exec("ping -n 4 ".escapeshellarg($host), $outary, $code);
79
                        } else {
80
                                // Linux / MAC OSX (uses bash)
81
                                exec("ping -c 4 -- ".escapeshellarg($host).' 2>&1', $outary, $code);
82
                        }
83
                }
84
                $execresult = implode("\n", $outary);
85
                $execresult = trim($execresult);
86
 
87
                // We could also work with $code, but it might not work under Windows then
88
                if ($execresult == '') {
77 daniel-mar 89
                        throw new VNagException('Could not launch ping executable.');
11 daniel-mar 90
                }
91
 
92
                if ($code != 0) {
93
                        $this->setStatus(VNag::STATUS_WARNING);
94
                        $this->setHeadline("Ping failed", true);
95
                        $this->outputHTML('<p><font color="red" size="+2">Ping failed</font></p>', true);
96
                } else {
97
                        $this->setStatus(VNag::STATUS_OK);
98
                        $this->outputHTML('<p><font color="green" size="+2">Ping OK</font></p>', true);
99
                }
100
 
13 daniel-mar 101
                $this->outputHTML('<pre>'.htmlentities($execresult).'</pre>', true);
11 daniel-mar 102
        }
103
 
104
        private static function is_windows() {
105
                // There is a problem with this variant:
106
                // Windows 9x does not contain the %OS% environment variable
107
                if (strtolower(getenv('OS')) == 'windows_nt') return true;
108
 
109
                if (defined('PHP_OS') && (PHP_OS == 'win')) return true;
110
 
111
                if (getenv('windir') != '') return true;
112
 
113
                return false;
114
        }
115
 
116
        private static function is_ipv6($host) {
117
                // Quick'n'Dirty
118
                return strpos($host, ':') !== false;
119
        }
120
 
121
}