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f/c2QVO29LEsxD
ePkfikCPJLfwNqNOYGpoUm7HQc1C60tHHPCbkMwEy1+J3GfIr+q8sHmFcxGLsqAg2
LECUGs5Xa/Z2wY87sdqBpYZXx0yHxfnWYWR/ixCtOcoTvUSKdqRxHZJAVdzY8gteb
aIXX0w1JZ6Hj9AS978aCF3Y833KtgTlyUXzD6JKVddYkpc33zK7yHJUy+QSOxYKIe
fo4TzHZtydviNY3vjE7aDiOO9tBGfW8ZmVNmwcicSdLUYJuEMgCWsH0hpIVSAw6pD
QMRu59uTvROscljpySe2DBmYaBTKKIkaDMuRQ/r15/qd5TsRQH6FjjCfvUdK+2zFI
Q==
</ViaThinkSoftSignature> */ ?>
<?php
/*
* VNag - Nagios Framework for PHP
* Developed by Daniel Marschall, ViaThinkSoft <www.viathinksoft.com>
* Licensed under the terms of the Apache 2.0 license
*
* Revision 2023-10-13
*/
// TODO: Also let the user decide other parameters like -4/-6 and number of pings/timeout etc.
# ---
declare(ticks=1);
# ---
class PingCheck extends VNag {
protected $argHostname = null;
public function __construct() {
parent::__construct();
$this->registerExpectedStandardArguments('Vhtwc');
$this->getHelpManager()->setPluginName('check_ping');
$this->getHelpManager()->setVersion('1.0');
$this->getHelpManager()->setShortDescription('This plugin performs a simple ping.');
$this->getHelpManager()->setCopyright('Copyright (C) 2011-$CURYEAR$ Daniel Marschall, ViaThinkSoft.');
$this->getHelpManager()->setSyntax('$SCRIPTNAME$ -H hostname');
$this->getHelpManager()->setFootNotes('If you encounter bugs, please contact ViaThinkSoft at www.viathinksoft.com');
$this->addExpectedArgument($this->argHostname = new VNagArgument('H', 'hostname', VNagArgument::VALUE_REQUIRED, 'hostname', 'Hostname or IP address to be pinged', null));
}
protected function cbRun
($optional_args=array()) {
$host = $this->argHostname->getValue();
if (is_null($host)) throw new VNagException
("Please enter a hostname");
$this->outputHTML("<h2>Host ".htmlentities($host)."</h2>\n\n", true);
if (self::is_ipv6($host)) {
if (self::is_windows()) {
// Windows
} else {
// Linux / MAC OSX (uses bash)
}
} else {
if (self::is_windows()) {
// Windows
} else {
// Linux / MAC OSX (uses bash)
}
}
$execresult = implode("\n", $outary);
$execresult = trim($execresult);
// We could also work with $code, but it might not work under Windows then
if ($execresult == '') {
throw new VNagException('Could not launch ping executable.');
}
if ($code != 0) {
$this->setStatus(VNag::STATUS_WARNING);
$this->setHeadline("Ping failed", true);
$this->outputHTML('<p><font color="red" size="+2">Ping failed</font></p>', true);
} else {
$this->setStatus(VNag::STATUS_OK);
$this->outputHTML('<p><font color="green" size="+2">Ping OK</font></p>', true);
}
$this->outputHTML('<pre>'.htmlentities($execresult).'</pre>', true);
}
private static function is_windows() {
// There is a problem with this variant:
// Windows 9x does not contain the %OS% environment variable
if (defined('PHP_OS') && (PHP_OS == 'win')) return true;
if (getenv('windir') != '') return true;
return false;
}
private static function is_ipv6($host) {
// Quick'n'Dirty
return strpos($host, ':') !== false;
}
}