1<?php 2/* Icinga Web 2 | (c) 2014 Icinga Development Team | GPLv2+ */ 3 4namespace Icinga\Module\Monitoring\Command\Object; 5 6use InvalidArgumentException; 7use LogicException; 8 9/** 10 * Submit a passive check result for a host or service 11 */ 12class ProcessCheckResultCommand extends ObjectCommand 13{ 14 /** 15 * (non-PHPDoc) 16 * @see \Icinga\Module\Monitoring\Command\Object\ObjectCommand::$allowedObjects For the property documentation. 17 */ 18 protected $allowedObjects = array( 19 self::TYPE_HOST, 20 self::TYPE_SERVICE 21 ); 22 23 /** 24 * Host up 25 */ 26 const HOST_UP = 0; 27 28 /** 29 * Host down 30 */ 31 const HOST_DOWN = 1; 32 33 /** 34 * Host unreachable 35 */ 36 const HOST_UNREACHABLE = 2; // TODO: Icinga 2.x does not support submitting results with this state, yet 37 38 /** 39 * Service ok 40 */ 41 const SERVICE_OK = 0; 42 43 /** 44 * Service warning 45 */ 46 const SERVICE_WARNING = 1; 47 48 /** 49 * Service critical 50 */ 51 const SERVICE_CRITICAL = 2; 52 53 /** 54 * Service unknown 55 */ 56 const SERVICE_UNKNOWN = 3; 57 58 /** 59 * Possible status codes for passive host and service checks 60 * 61 * @var array 62 */ 63 public static $statusCodes = array( 64 self::TYPE_HOST => array( 65 self::HOST_UP, self::HOST_DOWN, self::HOST_UNREACHABLE 66 ), 67 self::TYPE_SERVICE => array( 68 self::SERVICE_OK, self::SERVICE_WARNING, self::SERVICE_CRITICAL, self::SERVICE_UNKNOWN 69 ) 70 ); 71 72 /** 73 * Status code of the host or service check result 74 * 75 * @var int 76 */ 77 protected $status; 78 79 /** 80 * Text output of the host or service check result 81 * 82 * @var string 83 */ 84 protected $output; 85 86 /** 87 * Optional performance data of the host or service check result 88 * 89 * @var string 90 */ 91 protected $performanceData; 92 93 94 /** 95 * Set the status code of the host or service check result 96 * 97 * @param int $status 98 * 99 * @return $this 100 * 101 * @throws LogicException If the object is null 102 * @throws InvalidArgumentException If status is not one of the valid status codes for the object's type 103 */ 104 public function setStatus($status) 105 { 106 if ($this->object === null) { 107 throw new LogicException('You\'re required to call setObject() before calling setStatus()'); 108 } 109 $status = (int) $status; 110 if (! in_array($status, self::$statusCodes[$this->object->getType()])) { 111 throw new InvalidArgumentException(sprintf( 112 'The status code %u you provided is not one of the valid status codes for type %s', 113 $status, 114 $this->object->getType() 115 )); 116 } 117 $this->status = $status; 118 return $this; 119 } 120 121 /** 122 * Get the status code of the host or service check result 123 * 124 * @return int 125 */ 126 public function getStatus() 127 { 128 return $this->status; 129 } 130 131 /** 132 * Set the text output of the host or service check result 133 * 134 * @param string $output 135 * 136 * @return $this 137 */ 138 public function setOutput($output) 139 { 140 $this->output = (string) $output; 141 return $this; 142 } 143 144 /** 145 * Get the text output of the host or service check result 146 * 147 * @return string 148 */ 149 public function getOutput() 150 { 151 return $this->output; 152 } 153 154 /** 155 * Set the performance data of the host or service check result 156 * 157 * @param string $performanceData 158 * 159 * @return $this 160 */ 161 public function setPerformanceData($performanceData) 162 { 163 $this->performanceData = (string) $performanceData; 164 return $this; 165 } 166 167 /** 168 * Get the performance data of the host or service check result 169 * 170 * @return string 171 */ 172 public function getPerformanceData() 173 { 174 return $this->performanceData; 175 } 176} 177