1 <?php
defined('SYSPATH') OR die('No direct access allowed.');
4 * The report options for the Event type of reports in the HTTP API
6 class HttpApiEvent_options
extends Summary_options
{
7 public static $type = 'httpapievent';
9 const MAX_EVENTS
= 10000; /**< Pagination limit for events retrieved from HTTP API. Hardcoded, deal with it */
14 * Means to translate options back and forth between Report_options
15 * terms and HTTP API parameters. Handles both input and output translation.
17 static $http_api_options = array(
18 'alert_types' => array(
25 'state_types' => array(
32 'host_states' => array(
41 'service_states' => array(
54 * Select report options' properties to include and adjust
55 * them for the HTTP API
57 * @param $options array = false
59 function __construct($options = false)
61 parent
::__construct(false); // allright, this is a bit hackish, but parent's constructor tries to set options
62 // but we're not actually ready for that yet (see below? we modify the properties which are used by set_options
64 // whitelist properties to use, reuse the previous definitions
65 $this->properties
= array_intersect_key(
74 'service_description',
82 $this->properties
['include_comments'] = array(
85 'description' => "Include events' comments"
87 $this->properties
['alert_types']['description'] = _('Show events for this kind of objects');
88 $this->properties
['host_states']['description'] = _('Limit the result set to a certain kind of host states');
89 $this->properties
['service_states']['description'] = _('Limit the result set to a certain kind of service states');
90 $this->properties
['state_types']['description'] = _('Restrict events based on which state the event is in (soft vs hard)');
92 $limit = $this->limit
= (int) Op5Config
::instance()->getConfig('http_api.report.limit');
93 if($limit > self
::MAX_EVENTS ||
$limit < 1) {
94 $this->limit
= self
::MAX_EVENTS
;
95 $limit = $this->limit
."; you can decrease this value in http_api.yml";
97 $limit .= "; you can increase this value in http_api.yml";
99 $this->properties
['limit'] = array(
101 'default' => $this->limit
,
102 'description' => 'Include at most this many events (between 1 and '.$limit.')'
104 $this->properties
['offset'] = array(
107 'description' => 'Skip the first <em>offset</em> events matching the rest of the query, well suited for pagination'
111 // finally make the call which *can not* be set in parent::__construct() until all properties and
112 // other boilerplate is set up
113 $this->set_options($options);
118 * Listen for "http api" options/properties, instead of "report" options
120 * @param $input array = false
123 static function discover_options($input = false)
133 if(isset($options['start_time']) && !isset($options['end_time'])) {
134 $options['end_time'] = time();
136 if(isset($options['start_time']) ||
isset($options['end_time'])) {
137 // @todo workaround a nasty bug, implement this in Report_options directly
138 $options['report_period'] = 'custom';
140 if(isset($options['host_name'])) {
141 $options['host_name'] = (array) $options['host_name'];
143 if(isset($options['service_description'])) {
144 $options['service_description'] = (array) $options['service_description'];
147 // translate "all" to valid int-bitmap, for example
148 foreach($options as $key => $value) {
149 if(isset(self
::$http_api_options[$key]) &&
150 isset(self
::$http_api_options[$key]['options']) &&
151 isset(self
::$http_api_options[$key]['options'][$value])
153 $options[$key] = self
::$http_api_options[$key]['options'][$value];
160 * @param $value mixed
161 * @param $type string
164 function format_default($value, $type)
166 if($type == 'bool') {
169 if($type == 'array' ||
$type == 'objsel') {
173 return implode(", ", $value);
175 if($type == 'string' && !$value) {
178 if($type == 'enum') {
181 if($type == 'int' && empty($value) && $value !== 0) {
184 return (string) $value;
188 * Not as forgiving as the parent. (Why is parent forgiving?)
190 * @param $options array
191 * @throws Api_Error_Response
193 function set_options($options) {
194 foreach($options as $name => $value) {
195 if(!$this->set($name, $value)) {
196 throw new Api_Error_Response("Invalid value for option '$name'", 400);
202 * Final step in the "from merlin.report_data row to API-output" process
207 function to_output($row)
210 $type = $row['service_description'] ?
'service' : 'host';
211 $row['event_type'] = Reports_Model
::event_type_to_string($row['event_type'], $type, true);
212 $row['state'] = strtolower(Current_status_Model
::status_text($row['state'], true, $type));
215 $row['in_scheduled_downtime'] = (int) $row['downtime_depth'];
216 unset($row['downtime_depth']);
217 if(isset($row['username'])) {
218 // comments are included and we've got one of them!
219 // let's produce some hierarchy
220 $row['comment'] = array(
221 'username' => $row['username'],
222 'comment' => $row['user_comment'],
223 'timestamp' => $row['comment_timestamp'],
226 unset($row['username'], $row['user_comment'], $row['comment_timestamp']);
232 * @todo be able to throw exceptions here to give feedback of
233 * *which* error we experienced, since, you know, there's at
234 * least one user (you) exposed to this API.. Help yourself
237 * @param $value mixed
240 protected function validate_value($key, &$value)
242 if($key == 'limit') {
244 $value = $this->limit
;
247 if(!is_numeric($value)) {
250 $value = (int) $value;
251 if($value > $this->limit ||
$value < 1) {
256 if($key == 'start_time' && isset($this->options
['end_time']) && $value > $this->options
['end_time']) {
259 if($key == 'end_time' && isset($this->options
['start_time']) && $value < $this->options
['start_time']) {
262 return parent
::validate_value($key, $value);