PageRenderTime 56ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/zabbix-2.0.1/frontends/php/include/events.inc.php

#
PHP | 371 lines | 299 code | 46 blank | 26 comment | 44 complexity | 504de23e7cf41f6d18dc855fad6c9b2e MD5 | raw file
Possible License(s): GPL-2.0, AGPL-1.0
  1. <?php
  2. /*
  3. ** Zabbix
  4. ** Copyright (C) 2000-2011 Zabbix SIA
  5. **
  6. ** This program is free software; you can redistribute it and/or modify
  7. ** it under the terms of the GNU General Public License as published by
  8. ** the Free Software Foundation; either version 2 of the License, or
  9. ** (at your option) any later version.
  10. **
  11. ** This program is distributed in the hope that it will be useful,
  12. ** but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. ** GNU General Public License for more details.
  15. **
  16. ** You should have received a copy of the GNU General Public License
  17. ** along with this program; if not, write to the Free Software
  18. ** Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  19. **/
  20. ?>
  21. <?php
  22. function event_source2str($sourceid) {
  23. switch ($sourceid) {
  24. case EVENT_SOURCE_TRIGGERS:
  25. return _('Triggers');
  26. case EVENT_SOURCE_DISCOVERY:
  27. return _('Discovery');
  28. default:
  29. return _('Unknown');
  30. }
  31. }
  32. function get_tr_event_by_eventid($eventid) {
  33. $sql = 'SELECT e.*,t.triggerid,t.description,t.expression,t.priority,t.status,t.type'.
  34. ' FROM events e,triggers t'.
  35. ' WHERE e.eventid='.$eventid.
  36. ' AND e.object='.EVENT_OBJECT_TRIGGER.
  37. ' AND t.triggerid=e.objectid';
  38. return DBfetch(DBselect($sql));
  39. }
  40. function get_events_unacknowledged($db_element, $value_trigger = null, $value_event = null, $ack = false) {
  41. $elements = array('hosts' => array(), 'hosts_groups' => array(), 'triggers' => array());
  42. get_map_elements($db_element, $elements);
  43. if (empty($elements['hosts_groups']) && empty($elements['hosts']) && empty($elements['triggers'])) {
  44. return 0;
  45. }
  46. $config = select_config();
  47. $options = array(
  48. 'nodeids' => get_current_nodeid(),
  49. 'output' => API_OUTPUT_SHORTEN,
  50. 'monitored' => 1,
  51. 'skipDependent' => 1,
  52. 'limit' => $config['search_limit'] + 1
  53. );
  54. if (!is_null($value_trigger)) {
  55. $options['filter'] = array('value' => $value_trigger);
  56. }
  57. if (!empty($elements['hosts_groups'])) {
  58. $options['groupids'] = array_unique($elements['hosts_groups']);
  59. }
  60. if (!empty($elements['hosts'])) {
  61. $options['hostids'] = array_unique($elements['hosts']);
  62. }
  63. if (!empty($elements['triggers'])) {
  64. $options['triggerids'] = array_unique($elements['triggers']);
  65. }
  66. $triggerids = API::Trigger()->get($options);
  67. $options = array(
  68. 'countOutput' => 1,
  69. 'triggerids' => zbx_objectValues($triggerids, 'triggerid'),
  70. 'filter' => array(
  71. 'value_changed' => TRIGGER_VALUE_CHANGED_YES,
  72. 'value' => is_null($value_event) ? array(TRIGGER_VALUE_TRUE, TRIGGER_VALUE_FALSE) : $value_event,
  73. 'acknowledged' => $ack ? 1 : 0
  74. ),
  75. 'object' => EVENT_OBJECT_TRIGGER,
  76. 'nopermissions' => 1
  77. );
  78. return API::Event()->get($options);
  79. }
  80. function get_next_event($currentEvent, array $eventList = array(), $showUnknown = false) {
  81. $nextEvent = false;
  82. foreach ($eventList as $event) {
  83. // check only the events belonging to the same object
  84. // find the event with the smallest eventid but greater than the current event id
  85. if ($event['object'] == $currentEvent['object'] && bccomp($event['objectid'], $currentEvent['objectid']) == 0
  86. && (bccomp($event['eventid'], $currentEvent['eventid']) === 1
  87. && (!$nextEvent || bccomp($event['eventid'], $nextEvent['eventid']) === -1))) {
  88. $nextEvent = $event;
  89. }
  90. }
  91. if ($nextEvent) {
  92. return $nextEvent;
  93. }
  94. $sql = 'SELECT e.*'.
  95. ' FROM events e'.
  96. ' WHERE e.objectid='.$currentEvent['objectid'].
  97. ' AND e.eventid>'.$currentEvent['eventid'].
  98. ' AND e.object='.$currentEvent['object'].
  99. ($showUnknown ? '' : ' AND e.value_changed='.TRIGGER_VALUE_CHANGED_YES).
  100. ' ORDER BY e.object,e.objectid,e.eventid';
  101. return DBfetch(DBselect($sql, 1));
  102. }
  103. function make_event_details($event, $trigger) {
  104. $config = select_config();
  105. $table = new CTableInfo();
  106. $table->addRow(array(_('Event'), expand_trigger_description_by_data(array_merge($trigger, $event), ZBX_FLAG_EVENT)));
  107. $table->addRow(array(_('Time'), zbx_date2str(_('d M Y H:i:s'), $event['clock'])));
  108. if ($config['event_ack_enable']) {
  109. $ack = getEventAckState($event, true);
  110. $table->addRow(array(_('Acknowledged'), $ack));
  111. }
  112. return $table;
  113. }
  114. function make_small_eventlist($startEvent) {
  115. $config = select_config();
  116. $table = new CTableInfo();
  117. $table->setHeader(array(
  118. _('Time'),
  119. _('Status'),
  120. _('Duration'),
  121. _('Age'),
  122. $config['event_ack_enable'] ? _('Ack') : null, // if we need to chow acks
  123. _('Actions')
  124. ));
  125. $clock = $startEvent['clock'];
  126. $options = array(
  127. 'triggerids' => $startEvent['objectid'],
  128. 'eventid_till' => $startEvent['eventid'],
  129. 'output' => API_OUTPUT_EXTEND,
  130. 'select_acknowledges' => API_OUTPUT_COUNT,
  131. 'sortfield' => 'eventid',
  132. 'sortorder' => ZBX_SORT_DOWN,
  133. 'limit' => 20
  134. );
  135. $events = API::Event()->get($options);
  136. $sortFields = array(
  137. array('field' => 'clock', 'order' => ZBX_SORT_DOWN),
  138. array('field' => 'eventid', 'order' => ZBX_SORT_DOWN)
  139. );
  140. CArrayHelper::sort($events, $sortFields);
  141. foreach ($events as $event) {
  142. $lclock = $clock;
  143. $duration = zbx_date2age($lclock, $event['clock']);
  144. $clock = $event['clock'];
  145. if (bccomp($startEvent['eventid'],$event['eventid']) == 0 && $nextevent = get_next_event($event, $events, true)) {
  146. $duration = zbx_date2age($nextevent['clock'], $clock);
  147. }
  148. elseif (bccomp($startEvent['eventid'], $event['eventid']) == 0) {
  149. $duration = zbx_date2age($clock);
  150. }
  151. $eventStatusSpan = new CSpan(trigger_value2str($event['value']));
  152. // add colors and blinking to span depending on configuration and trigger parameters
  153. addTriggerValueStyle(
  154. $eventStatusSpan,
  155. $event['value'],
  156. $event['clock'],
  157. $event['acknowledged']
  158. );
  159. $ack = getEventAckState($event, true);
  160. $actions = get_event_actions_stat_hints($event['eventid']);
  161. $table->addRow(array(
  162. new CLink(
  163. zbx_date2str(_('d M Y H:i:s'), $event['clock']),
  164. 'tr_events.php?triggerid='.$event['objectid'].'&eventid='.$event['eventid'],
  165. 'action'
  166. ),
  167. $eventStatusSpan,
  168. $duration,
  169. zbx_date2age($event['clock']),
  170. $config['event_ack_enable'] ? $ack : null,
  171. $actions
  172. ));
  173. }
  174. return $table;
  175. }
  176. function make_popup_eventlist($eventid, $trigger_type, $triggerid) {
  177. $config = select_config();
  178. $table = new CTableInfo();
  179. // if acknowledges are turned on, we show 'ack' column
  180. if ($config['event_ack_enable']) {
  181. $table->setHeader(array(_('Time'), _('Status'), _('Duration'), _('Age'), _('Ack')));
  182. }
  183. else {
  184. $table->setHeader(array(_('Time'), _('Status'), _('Duration'), _('Age')));
  185. }
  186. $table->setAttribute('style', 'width: 400px;');
  187. $options = array(
  188. 'output' => API_OUTPUT_EXTEND,
  189. 'triggerids' => $triggerid,
  190. 'eventid_till' => $eventid,
  191. 'filter' => array(
  192. 'object' => EVENT_OBJECT_TRIGGER,
  193. 'value_changed' => TRIGGER_VALUE_CHANGED_YES
  194. ),
  195. 'nopermissions' => 1,
  196. 'select_acknowledges' => API_OUTPUT_COUNT,
  197. 'sortfield' => 'eventid',
  198. 'sortorder' => ZBX_SORT_DOWN,
  199. 'limit' => ZBX_WIDGET_ROWS
  200. );
  201. $db_events = API::Event()->get($options);
  202. $lclock = time();
  203. foreach ($db_events as $event) {
  204. $duration = zbx_date2age($lclock, $event['clock']);
  205. $lclock = $event['clock'];
  206. $eventStatusSpan = new CSpan(trigger_value2str($event['value']));
  207. // add colors and blinking to span depending on configuration and trigger parameters
  208. addTriggerValueStyle($eventStatusSpan, $event['value'], $event['clock'], $event['acknowledged']);
  209. $ack = getEventAckState($event, false, false);
  210. $table->addRow(array(
  211. zbx_date2str(_('d M Y H:i:s'), $event['clock']),
  212. $eventStatusSpan,
  213. $duration,
  214. zbx_date2age($event['clock']),
  215. $ack
  216. ));
  217. }
  218. return $table;
  219. }
  220. function getEventAckState($event, $isBackurl = false, $isLink = true, $params = array()) {
  221. $config = select_config();
  222. global $page;
  223. if (!$config['event_ack_enable']) {
  224. return null;
  225. }
  226. if ($event['value_changed'] == TRIGGER_VALUE_CHANGED_NO) {
  227. return SPACE;
  228. }
  229. if ($isLink) {
  230. if ($isBackurl) {
  231. $backurl = '&backurl='.$page['file'];
  232. }
  233. else {
  234. $backurl = '';
  235. }
  236. $additionalParams = '';
  237. foreach ($params as $key => $value) {
  238. $additionalParams .= '&'.$key.'='.$value;
  239. }
  240. if ($event['acknowledged'] == 0) {
  241. $ack = new CLink(_('No'), 'acknow.php?eventid='.$event['eventid'].'&triggerid='.$event['objectid'].$backurl.$additionalParams, 'disabled');
  242. }
  243. else {
  244. $ackLink = new CLink(_('Yes'), 'acknow.php?eventid='.$event['eventid'].'&triggerid='.$event['objectid'].$backurl.$additionalParams, 'enabled');
  245. $ackLinkHints = make_acktab_by_eventid($event);
  246. if (!empty($ackLinkHints)) {
  247. $ackLink->setHint($ackLinkHints, '', '', false);
  248. }
  249. $ack = array($ackLink, ' ('.(is_array($event['acknowledges']) ? count($event['acknowledges']) : $event['acknowledges']).')');
  250. }
  251. }
  252. else {
  253. if ($event['acknowledged'] == 0) {
  254. $ack = new CSpan(_('No'), 'on');
  255. }
  256. else {
  257. $ack = array(new CSpan(_('Yes'), 'off'), ' ('.(is_array($event['acknowledges']) ? count($event['acknowledges']) : $event['acknowledges']).')');
  258. }
  259. }
  260. return $ack;
  261. }
  262. function getLastEvents($options) {
  263. if (!isset($options['limit'])) {
  264. $options['limit'] = 15;
  265. }
  266. $triggerOptions = array(
  267. 'filter' => array(),
  268. 'skipDependent' => 1,
  269. 'selectHosts' => array('hostid', 'host'),
  270. 'output' => API_OUTPUT_EXTEND,
  271. 'sortfield' => 'lastchange',
  272. 'sortorder' => ZBX_SORT_DOWN,
  273. 'limit' => $options['limit']
  274. );
  275. $eventOptions = array(
  276. 'output' => API_OUTPUT_EXTEND,
  277. 'filter' => array(
  278. 'object' => EVENT_OBJECT_TRIGGER,
  279. 'value_changed' => TRIGGER_VALUE_CHANGED_YES
  280. ),
  281. 'sortfield' => 'eventid',
  282. 'sortorder' => ZBX_SORT_DOWN,
  283. 'limit' => $options['limit']
  284. );
  285. if (isset($options['nodeids'])) {
  286. $triggerOptions['nodeids'] = $options['nodeids'];
  287. }
  288. if (isset($options['priority'])) {
  289. $triggerOptions['filter']['priority'] = $options['priority'];
  290. }
  291. if (isset($options['monitored'])) {
  292. $triggerOptions['monitored'] = $options['monitored'];
  293. }
  294. if (isset($options['lastChangeSince'])) {
  295. $triggerOptions['lastChangeSince'] = $options['lastChangeSince'];
  296. $eventOptions['time_from'] = $options['lastChangeSince'];
  297. }
  298. if (isset($options['value'])) {
  299. $triggerOptions['filter']['value'] = $options['value'];
  300. $eventOptions['value'] = $options['value'];
  301. }
  302. // triggers
  303. $triggers = API::Trigger()->get($triggerOptions);
  304. $triggers = zbx_toHash($triggers, 'triggerid');
  305. // events
  306. $eventOptions['triggerids'] = zbx_objectValues($triggers, 'triggerid');
  307. $events = API::Event()->get($eventOptions);
  308. $sortClock = array();
  309. $sortEvent = array();
  310. foreach ($events as $enum => $event) {
  311. if (!isset($triggers[$event['objectid']])) {
  312. continue;
  313. }
  314. $events[$enum]['trigger'] = $triggers[$event['objectid']];
  315. $events[$enum]['host'] = reset($events[$enum]['trigger']['hosts']);
  316. $sortClock[$enum] = $event['clock'];
  317. $sortEvent[$enum] = $event['eventid'];
  318. //expanding description for the state where event was
  319. $merged_event = array_merge($event, $triggers[$event['objectid']]);
  320. $events[$enum]['trigger']['description'] = expand_trigger_description_by_data($merged_event, ZBX_FLAG_EVENT);
  321. }
  322. array_multisort($sortClock, SORT_DESC, $sortEvent, SORT_DESC, $events);
  323. return $events;
  324. }
  325. ?>