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
{
8 const MAX_EVENTS
= 10000; /**< Pagination limit for events retrieved from HTTP API. Hardcoded, deal with it */
13 * Means to translate options back and forth between Report_options
14 * terms and HTTP API parameters. Handles both input and output translation.
16 static $http_api_options = array(
17 'alert_types' => array(
24 'state_types' => array(
31 'host_states' => array(
40 'service_states' => array(
53 * Select report options' properties to include and adjust
54 * them for the HTTP API
56 * @param $options array = false
58 function __construct($options = false)
60 parent
::__construct(false); // allright, this is a bit hackish, but parent's constructor tries to set options
61 // but we're not actually ready for that yet (see below? we modify the properties which are used by set_options
63 // whitelist properties to use, reuse the previous definitions
64 $this->properties
= array_intersect_key(
73 'service_description',
81 $this->properties
['include_comments'] = array(
84 'description' => "Include events' comments"
86 $this->properties
['alert_types']['description'] = _('Show events for this kind of objects');
87 $this->properties
['host_states']['description'] = _('Limit the result set to a certain kind of host states');
88 $this->properties
['service_states']['description'] = _('Limit the result set to a certain kind of service states');
89 $this->properties
['state_types']['description'] = _('Restrict events based on which state the event is in (soft vs hard)');
91 $limit = $this->limit
= (int) Op5Config
::instance()->getConfig('http_api.report.limit');
92 if($limit > self
::MAX_EVENTS ||
$limit < 1) {
93 $this->limit
= self
::MAX_EVENTS
;
94 $limit = $this->limit
."; you can decrease this value in http_api.yml";
96 $limit .= "; you can increase this value in http_api.yml";
98 $this->properties
['limit'] = array(
100 'default' => $this->limit
,
101 'description' => 'Include at most this many events (between 1 and '.$limit.')'
103 $this->properties
['offset'] = array(
106 'description' => 'Skip the first <em>offset</em> events matching the rest of the query, well suited for pagination'
110 // finally make the call which *can not* be set in parent::__construct() until all properties and
111 // other boilerplate is set up
112 $this->set_options($options);
117 * Listen for "http api" options/properties, instead of "report" options
119 * @param $input array = false
122 static function discover_options($input = false)
132 if(isset($options['start_time']) && !isset($options['end_time'])) {
133 $options['end_time'] = time();
135 if(isset($options['start_time']) ||
isset($options['end_time'])) {
136 // @todo workaround a nasty bug, implement this in Report_options directly
137 $options['report_period'] = 'custom';
139 if(isset($options['host_name'])) {
140 $options['host_name'] = (array) $options['host_name'];
142 if(isset($options['service_description'])) {
143 $options['service_description'] = (array) $options['service_description'];
146 // translate "all" to valid int-bitmap, for example
147 foreach($options as $key => $value) {
148 if(isset(self
::$http_api_options[$key]) &&
149 isset(self
::$http_api_options[$key]['options']) &&
150 isset(self
::$http_api_options[$key]['options'][$value])
152 $options[$key] = self
::$http_api_options[$key]['options'][$value];
159 * @param $value mixed
160 * @param $type string
163 function format_default($value, $type)
165 if($type == 'bool') {
168 if($type == 'array' ||
$type == 'objsel') {
172 return implode(", ", $value);
174 if($type == 'string' && !$value) {
177 if($type == 'enum') {
180 if($type == 'int' && empty($value) && $value !== 0) {
183 return (string) $value;
187 * Not as forgiving as the parent. (Why is parent forgiving?)
189 * @param $options array
190 * @throws Api_Error_Response
192 function set_options($options) {
193 foreach($options as $name => $value) {
194 if(!$this->set($name, $value)) {
195 throw new Api_Error_Response("Invalid value for option '$name'", 400);
201 * Final step in the "from merlin.report_data row to API-output" process
206 function to_output($row)
209 $type = $row['service_description'] ?
'service' : 'host';
210 $row['event_type'] = Reports_Model
::event_type_to_string($row['event_type'], $type, true);
211 $row['state'] = strtolower(Current_status_Model
::status_text($row['state'], true, $type));
214 $row['in_scheduled_downtime'] = (int) $row['downtime_depth'];
215 unset($row['downtime_depth']);
216 if(isset($row['username'])) {
217 // comments are included and we've got one of them!
218 // let's produce some hierarchy
219 $row['comment'] = array(
220 'username' => $row['username'],
221 'comment' => $row['user_comment'],
222 'timestamp' => $row['comment_timestamp'],
225 unset($row['username'], $row['user_comment'], $row['comment_timestamp']);
231 * @todo be able to throw exceptions here to give feedback of
232 * *which* error we experienced, since, you know, there's at
233 * least one user (you) exposed to this API.. Help yourself
236 * @param $value mixed
239 protected function validate_value($key, &$value)
241 if($key == 'limit') {
243 $value = $this->limit
;
246 if(!is_numeric($value)) {
249 $value = (int) $value;
250 if($value > $this->limit ||
$value < 1) {
255 if($key == 'start_time' && isset($this->options
['end_time']) && $value > $this->options
['end_time']) {
258 if($key == 'end_time' && isset($this->options
['start_time']) && $value < $this->options
['start_time']) {
261 return parent
::validate_value($key, $value);