Subversion Repositories vnag

Rev

Rev 76 | Go to most recent revision | Blame | Last modification | View Log | RSS feed

  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.  *
  8.  * Revision 2023-10-13
  9.  */
  10.  
  11. declare(ticks=1);
  12.  
  13. class VNagWebReader extends VNag {
  14.         protected $argUrl = null;
  15.         protected $argId = null;
  16.         protected $argBasicAuth = null;
  17.         protected $argPassword = null;
  18.         protected $argSignPubKey = null;
  19.  
  20.         public function __construct() {
  21.                 parent::__construct();
  22.  
  23.                 if ($this->is_http_mode()) {
  24.                         // Don't allow the standard arguments via $_REQUEST
  25.                         $this->registerExpectedStandardArguments('');
  26.                 } else {
  27.                         $this->registerExpectedStandardArguments('Vht');
  28.                 }
  29.                 $this->addExpectedArgument($this->argUrl        = new VNagArgument('u', 'url',        VNagArgument::VALUE_REQUIRED, 'url', 'The URI of the page that contains an embedded machine readable VNag output', null));
  30.                 $this->addExpectedArgument($this->argId         = new VNagArgument('i', 'id',         VNagArgument::VALUE_REQUIRED, 'id', 'The ID (serial or individual name) of the embedded Nagios output. Usually "0" if only one monitor is used without individual names.', '0'));
  31.                 $this->addExpectedArgument($this->argBasicAuth  = new VNagArgument('b', 'basicAuth',  VNagArgument::VALUE_REQUIRED, 'username:password', 'In case the target website requires Basic Auth, please pass username and password, divided by double-colon, into this argument.', null));
  32.                 $this->addExpectedArgument($this->argPassword   = new VNagArgument('p', 'password',   VNagArgument::VALUE_REQUIRED, 'password', 'In case the machine readable VNag output is encrypted, enter the password here.', null));
  33.                 $this->addExpectedArgument($this->argSignPubKey = new VNagArgument('k', 'signPubKey', VNagArgument::VALUE_REQUIRED, 'pemFile', 'In case the machine readable VNag output is signed, enter the filename of the public key (PEM) file here, to verify the signature of the output.', null));
  34.  
  35.                 $this->getHelpManager()->setPluginName('vnag_webreader');
  36.                 $this->getHelpManager()->setVersion('2023-10-13');
  37.                 $this->getHelpManager()->setShortDescription('This plugin reads embedded machine readable VNag output from a website and converts it into a Nagios compatible output format.');
  38.                 $this->getHelpManager()->setCopyright('Copyright (C) 2011-$CURYEAR$ Daniel Marschall, ViaThinkSoft.');
  39.                 $this->getHelpManager()->setSyntax('$SCRIPTNAME$ -u <url> [-i <id>] [-b <username>:<password>] [-k pubKeyFile] [-p <password>]');
  40.                 $this->getHelpManager()->setFootNotes('If you encounter bugs, please contact ViaThinkSoft at www.viathinksoft.com');
  41.         }
  42.  
  43.         protected function cbRun() {
  44.                 $this->argUrl->require();
  45.                 $this->argId->require();
  46.  
  47.                 $url = $this->argUrl->getValue();
  48.                 $this->id = $this->argId->getValue(); // default is '0', so we do not need to check for available()
  49.                 if ($this->argPassword->available()) $this->password_in = $this->argPassword->getValue();
  50.                 if ($this->argSignPubKey->available()) $this->pubkey = $this->argSignPubKey->getValue();
  51.  
  52.                 $header = '';
  53.                 $auth = $this->argBasicAuth->getValue();
  54.                 if (!is_null($auth)) $auth .= sprintf("Authorization: Basic %s\r\n", base64_encode($auth));
  55.                 $header .= sprintf("User-Agent: PHP/%s VNag/%s\r\n", phpversion(), self::VNAG_VERSION);
  56.  
  57.                 $options = array(
  58.                     'http' => array(
  59.                         'method' => 'GET',
  60.                         'header' => $header
  61.                     )
  62.                 );
  63.                 $context = stream_context_create($options);
  64.                 $cont = $this->url_get_contents($url, 60, $context);
  65.                 if ($cont === false) throw new VNagException("Cannot access $url");
  66.  
  67.                 $data = $this->readInvisibleHTML($cont);
  68.  
  69.                 if (!$data) throw new VNagInvalidArgumentException("No monitor with ID \"$this->id\" found at URL $url");
  70.  
  71.                 if (isset($data['text'])) {
  72.                         $this->setHeadline("Special content delivered from the web monitor (see verbose info)");
  73.                         $this->addVerboseMessage($data['text'], VNag::VERBOSITY_SUMMARY); // VERBOSE_SUMMARY is *our* verbosity, not the verbosity of the target monitor
  74.                 } else {
  75.                         foreach ($data['performance_data'] as $perfdata) {
  76.                                 $this->addPerformanceData(VNagPerformanceData::createByString($perfdata));
  77.                         }
  78.  
  79.                         $this->setHeadline($data['headline']);
  80.                         $this->addVerboseMessage($data['verbose_info'], VNag::VERBOSITY_SUMMARY); // VERBOSE_SUMMARY is *our* verbosity, not the verbosity of the target monitor
  81.                 }
  82.  
  83.                 $this->setStatus($data['status'], true);
  84.         }
  85. }
  86.