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 */
13 public function setup_properties()
15 parent
::setup_properties();
16 $this->properties
= array_intersect_key(
31 $this->properties
['include_comments'] = array(
34 'description' => "Include events' comments"
36 $this->properties
['alert_types']['options'] = array(
41 $this->properties
['state_types']['options'] = array(
46 $this->properties
['host_states']['options'] = array(
54 $this->properties
['service_states']['options'] = array(
63 $this->properties
['report_period']['default'] = 'custom';
64 $this->properties
['report_period']['options'] = array_combine(
65 array_keys($this->properties
['report_period']['options']),
66 array_keys($this->properties
['report_period']['options']));
68 $limit = $this->limit
= (int) Op5Config
::instance()->getConfig('http_api.report.limit');
69 if($limit > self
::MAX_EVENTS ||
$limit < 1) {
70 $this->limit
= self
::MAX_EVENTS
;
71 $limit = $this->limit
."; you can decrease this value in http_api.yml";
73 $limit .= "; you can increase this value in http_api.yml";
75 $this->properties
['limit'] = array(
77 'default' => $this->limit
,
78 'description' => 'Include at most this many events (between 1 and '.$limit.')'
80 $this->properties
['offset'] = array(
83 'description' => 'Skip the first <em>offset</em> events matching the rest of the query, well suited for pagination'
86 foreach (array('host_name', 'service_description', 'hostgroup', 'servicegroup') as $objtype) {
87 $this->properties
[$objtype] = $this->properties
['objects'];
88 $type = explode('_', $objtype);
89 $this->properties
[$objtype]['description'] = ucfirst($type[0]).'s to include (note: array)';
91 $this->properties
['objects']['generated'] = true;
92 $this->properties
['objects']['default'] = Report_options
::ALL_AUTHORIZED
;
93 $this->properties
['report_type']['generated'] = true;
94 $this->properties
['report_type']['default'] = 'hosts';
102 function format_default($value, $type)
104 if($type == 'bool') {
107 if($type == 'array' ||
$type == 'objsel') {
111 return implode(", ", $value);
113 if($type == 'string' && !$value) {
116 if($type == 'enum') {
119 if($type == 'int' && empty($value) && $value !== 0) {
122 return (string) $value;
126 * Not as forgiving as the parent. (Why is parent forgiving?)
128 * @param $options array
129 * @throws Api_Error_Response
131 function set_options($options) {
132 foreach($options as $name => $value) {
133 if(!$this->set($name, $value)) {
134 throw new Api_Error_Response("Invalid value for option '$name'", 400);
141 * Final step in the "from merlin.report_data row to API-output" process
146 function to_output($row)
149 $type = $row['service_description'] ?
'service' : 'host';
150 $row['event_type'] = Reports_Model
::event_type_to_string($row['event_type'], $type, true);
151 $row['state'] = strtolower(Current_status_Model
::status_text($row['state'], true, $type));
154 $row['in_scheduled_downtime'] = null;
155 unset($row['downtime_depth']);
156 if(isset($row['username'])) {
157 // comments are included and we've got one of them!
158 // let's produce some hierarchy
159 $row['comment'] = array(
160 'username' => $row['username'],
161 'comment' => $row['user_comment'],
162 'timestamp' => $row['comment_timestamp'],
165 unset($row['username'], $row['user_comment'], $row['comment_timestamp']);
171 * @todo be able to throw exceptions here to give feedback of
172 * *which* error we experienced, since, you know, there's at
173 * least one user (you) exposed to this API.. Help yourself
176 * @param $value mixed
179 protected function validate_value($key, &$value)
181 switch ($this->properties
[$key]['type']) {
183 $v = array_search($value, $this->properties
[$key]['options']);
190 if ($key == 'objects' && !is_array($value))
191 $value = array($value);
192 if($key == 'limit') {
194 $value = $this->limit
;
197 if(!is_numeric($value)) {
200 $value = (int) $value;
201 if($value > $this->limit ||
$value < 1) {
206 if($key == 'start_time' && $this['report_period'] == 'custom') {
207 if (!isset($this->options
['end_time']))
208 $this->options
['end_time'] = time();
209 elseif ($value > $this->options
['end_time'])
212 if($key == 'end_time' && isset($this->options
['start_time']) && $value < $this->options
['start_time']) {
215 return parent
::validate_value($key, $value);