|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * Class sspmod_perun_NagiosStatusConnector |
| 5 | + * |
| 6 | + * @author Pavel Vyskocil <[email protected]> |
| 7 | + */ |
| 8 | +class sspmod_perun_NagiosStatusConnector extends sspmod_perun_StatusConnector |
| 9 | +{ |
| 10 | + const NAGIOS_URL = "status.nagios.url"; |
| 11 | + const NAGIOS_CERT_PATH = "status.nagios.certificate_path"; |
| 12 | + const NAGIOS_CERT_PASSWORD = "status.nagios.certificate_password"; |
| 13 | + const NAGIOS_CA_PATH = "status.nagios.ca_path"; |
| 14 | + const NAGIOS_PEER_VERIFY = "status.nagios.peer_verification"; |
| 15 | + |
| 16 | + private $url; |
| 17 | + private $certPath; |
| 18 | + private $certPassword; |
| 19 | + private $caPath; |
| 20 | + private $peerVerification; |
| 21 | + |
| 22 | + /** |
| 23 | + * NagiosStatusConnector constructor. |
| 24 | + */ |
| 25 | + public function __construct() |
| 26 | + { |
| 27 | + parent::__construct(); |
| 28 | + |
| 29 | + $this->url = $this->configuration->getString(self::NAGIOS_URL, ""); |
| 30 | + $this->certPath = $this->configuration->getString(self::NAGIOS_CERT_PATH, ""); |
| 31 | + $this->certPassword = $this->configuration->getString(self::NAGIOS_CERT_PASSWORD, ""); |
| 32 | + $this->caPath = $this->configuration->getString(self::NAGIOS_CA_PATH, ""); |
| 33 | + $this->peerVerification = $this->configuration->getBoolean(self::NAGIOS_PEER_VERIFY, false); |
| 34 | + |
| 35 | + if (empty($this->url)) { |
| 36 | + throw new Exception("Required option '" . self::NAGIOS_URL . "' is empty!"); |
| 37 | + } elseif (empty($this->certPath)) { |
| 38 | + throw new Exception("Required option '" . self::NAGIOS_CERT_PATH . "' is empty!"); |
| 39 | + } elseif (empty($this->caPath)) { |
| 40 | + throw new Exception("Required option '" . self::NAGIOS_CA_PATH . "' is empty!"); |
| 41 | + } |
| 42 | + } |
| 43 | + |
| 44 | + |
| 45 | + public function getStatus() |
| 46 | + { |
| 47 | + $result = array(); |
| 48 | + $serviceStatuses = array(); |
| 49 | + |
| 50 | + $ch = curl_init(); |
| 51 | + curl_setopt($ch, CURLOPT_URL, $this->url); |
| 52 | + curl_setopt($ch, CURLOPT_VERBOSE, true); |
| 53 | + curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, $this->peerVerification); |
| 54 | + curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); |
| 55 | + curl_setopt($ch, CURLOPT_SSLCERT, $this->certPath); |
| 56 | + curl_setopt($ch, CURLOPT_CAPATH, $this->caPath); |
| 57 | + curl_setopt($ch, CURLOPT_SSLKEYPASSWD, $this->certPassword); |
| 58 | + |
| 59 | + $response = curl_exec($ch); |
| 60 | + |
| 61 | + if ($response === false) { |
| 62 | + \SimpleSAML\Logger::error(curl_error($ch)); |
| 63 | + } |
| 64 | + |
| 65 | + curl_close($ch); |
| 66 | + |
| 67 | + $jsonResponse = json_decode($response, true); |
| 68 | + |
| 69 | + if (isset($jsonResponse['status']['service_status'])) { |
| 70 | + $serviceStatuses = $jsonResponse['status']['service_status']; |
| 71 | + } |
| 72 | + |
| 73 | + foreach ($serviceStatuses as $serviceStatus){ |
| 74 | + $status = array(); |
| 75 | + $status['name'] = $serviceStatus['service_display_name']; |
| 76 | + $status['status'] = $serviceStatus['status']; |
| 77 | + array_push($result, $status); |
| 78 | + } |
| 79 | + |
| 80 | + return $result; |
| 81 | + |
| 82 | + } |
| 83 | + |
| 84 | +} |
0 commit comments