Rev 77 | 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 |
2 | |||
3 | /* |
||
4 | * VNag - Nagios Framework for PHP |
||
5 | * Developed by Daniel Marschall, ViaThinkSoft <www.viathinksoft.com> |
||
6 | * Licensed under the terms of the Apache 2.0 license |
||
7 | * |
||
77 | daniel-mar | 8 | * Revision 2023-10-13 |
11 | daniel-mar | 9 | */ |
10 | |||
11 | // TODO: Also let the user decide other parameters like -4/-6 and number of pings/timeout etc. |
||
12 | |||
13 | # --- |
||
14 | |||
15 | declare(ticks=1); |
||
16 | |||
17 | # --- |
||
18 | |||
19 | class PingCheck extends VNag { |
||
28 | daniel-mar | 20 | protected $argHostname = null; |
11 | daniel-mar | 21 | |
22 | public function __construct() { |
||
23 | parent::__construct(); |
||
24 | |||
25 | $this->registerExpectedStandardArguments('Vhtwc'); |
||
26 | |||
27 | $this->getHelpManager()->setPluginName('check_ping'); |
||
79 | daniel-mar | 28 | $this->getHelpManager()->setVersion('2023-10-13'); |
11 | daniel-mar | 29 | $this->getHelpManager()->setShortDescription('This plugin performs a simple ping.'); |
30 | $this->getHelpManager()->setCopyright('Copyright (C) 2011-$CURYEAR$ Daniel Marschall, ViaThinkSoft.'); |
||
31 | $this->getHelpManager()->setSyntax('$SCRIPTNAME$ -H hostname'); |
||
32 | $this->getHelpManager()->setFootNotes('If you encounter bugs, please contact ViaThinkSoft at www.viathinksoft.com'); |
||
33 | |||
34 | $this->addExpectedArgument($this->argHostname = new VNagArgument('H', 'hostname', VNagArgument::VALUE_REQUIRED, 'hostname', 'Hostname or IP address to be pinged', null)); |
||
35 | } |
||
36 | |||
37 | protected function cbRun($optional_args=array()) { |
||
38 | $host = $this->argHostname->getValue(); |
||
77 | daniel-mar | 39 | if (is_null($host)) throw new VNagException("Please enter a hostname"); |
11 | daniel-mar | 40 | |
41 | $this->outputHTML("<h2>Host ".htmlentities($host)."</h2>\n\n", true); |
||
42 | |||
43 | if (self::is_ipv6($host)) { |
||
44 | if (self::is_windows()) { |
||
45 | // Windows |
||
46 | exec("ping -6 -n 4 ".escapeshellarg($host), $outary, $code); |
||
47 | } else { |
||
48 | // Linux / MAC OSX (uses bash) |
||
49 | exec("ping6 -c 4 -- ".escapeshellarg($host).' 2>&1', $outary, $code); |
||
50 | } |
||
51 | } else { |
||
52 | if (self::is_windows()) { |
||
53 | // Windows |
||
54 | exec("ping -n 4 ".escapeshellarg($host), $outary, $code); |
||
55 | } else { |
||
56 | // Linux / MAC OSX (uses bash) |
||
57 | exec("ping -c 4 -- ".escapeshellarg($host).' 2>&1', $outary, $code); |
||
58 | } |
||
59 | } |
||
60 | $execresult = implode("\n", $outary); |
||
61 | $execresult = trim($execresult); |
||
62 | |||
63 | // We could also work with $code, but it might not work under Windows then |
||
64 | if ($execresult == '') { |
||
77 | daniel-mar | 65 | throw new VNagException('Could not launch ping executable.'); |
11 | daniel-mar | 66 | } |
67 | |||
68 | if ($code != 0) { |
||
69 | $this->setStatus(VNag::STATUS_WARNING); |
||
70 | $this->setHeadline("Ping failed", true); |
||
71 | $this->outputHTML('<p><font color="red" size="+2">Ping failed</font></p>', true); |
||
72 | } else { |
||
73 | $this->setStatus(VNag::STATUS_OK); |
||
74 | $this->outputHTML('<p><font color="green" size="+2">Ping OK</font></p>', true); |
||
75 | } |
||
76 | |||
13 | daniel-mar | 77 | $this->outputHTML('<pre>'.htmlentities($execresult).'</pre>', true); |
11 | daniel-mar | 78 | } |
79 | |||
80 | private static function is_windows() { |
||
81 | // There is a problem with this variant: |
||
82 | // Windows 9x does not contain the %OS% environment variable |
||
83 | if (strtolower(getenv('OS')) == 'windows_nt') return true; |
||
84 | |||
85 | if (defined('PHP_OS') && (PHP_OS == 'win')) return true; |
||
86 | |||
87 | if (getenv('windir') != '') return true; |
||
88 | |||
89 | return false; |
||
90 | } |
||
91 | |||
92 | private static function is_ipv6($host) { |
||
93 | // Quick'n'Dirty |
||
94 | return strpos($host, ':') !== false; |
||
95 | } |
||
96 | |||
97 | } |