Advertisement
Guest User

Untitled

a guest
Apr 8th, 2016
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 37.16 KB | None | 0 0
  1. <?php
  2. /*****************************************************************************
  3. *
  4. * GlobalBackendcentreonbroker.php - backend class for handling object and state
  5. * information stored in the Centreon Broker database.
  6. *
  7. * Copyright (c) 2004-2011 NagVis Project (Contact: info@nagvis.org)
  8. *
  9. * License:
  10. *
  11. * This program is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License version 2 as
  13. * published by the Free Software Foundation.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with this program; if not, write to the Free Software
  22. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  23. *
  24. *****************************************************************************/
  25.  
  26. /**
  27. * @author Maximilien Bersoult <mbersoult@merethis.com>
  28. */
  29. class GlobalBackendcentreonbroker implements GlobalBackendInterface {
  30. private $_CORE = null;
  31. private $_backendId = 0;
  32. private $_dbname;
  33. private $_dbuser;
  34. private $_dbpass;
  35. private $_dbhost;
  36. private $_dbport;
  37. private $_dbinstancename;
  38.  
  39. private $_dbh;
  40.  
  41. private $_instanceId = 0;
  42. private $_cacheHostId = array();
  43. private $_cacheServiceId = array();
  44. private $_cacheHostAck = array();
  45.  
  46. private static $_validConfig = array(
  47. 'dbhost' => array(
  48. 'must' => 1,
  49. 'editable' => 1,
  50. 'default' => 'localhost',
  51. 'match' => MATCH_STRING_NO_SPACE
  52. ),
  53. 'dbport' => array(
  54. 'must' => 0,
  55. 'editable' => 1,
  56. 'default' => '3306',
  57. 'match' => MATCH_INTEGER
  58. ),
  59. 'dbname' => array(
  60. 'must' => 1,
  61. 'editable' => 1,
  62. 'default' => 'centreon_storage',
  63. 'match' => MATCH_STRING_NO_SPACE
  64. ),
  65. 'dbuser' => array(
  66. 'must' => 1,
  67. 'editable' => 1,
  68. 'default' => 'centreon',
  69. 'match' => MATCH_STRING_NO_SPACE
  70. ),
  71. 'dbpass' => array(
  72. 'must' => 0,
  73. 'editable' => 1,
  74. 'default' => '',
  75. 'match' => MATCH_STRING_NO_SPACE
  76. ),
  77. 'dbinstancename' => array(
  78. 'must' => 0,
  79. 'editable' => 1,
  80. 'default' => 'default',
  81. 'match' => MATCH_STRING_NO_SPACE
  82. )
  83. );
  84.  
  85. /**
  86. * Constructor
  87. *
  88. * @author Maximilien Bersoult <mbersoult@merethis.com>
  89. * @param GlobalCore $CORE The instance of NagVis
  90. * @param int $backendId The backend ID
  91. */
  92. public function __construct($CORE, $backendId) {
  93. $this->_CORE = $CORE;
  94. $this->_backendId = $backendId;
  95.  
  96. $this->_dbname = cfg('backend_'.$backendId, 'dbname');
  97. $this->_dbuser = cfg('backend_'.$backendId, 'dbuser');
  98. $this->_dbpass = cfg('backend_'.$backendId, 'dbpass');
  99. $this->_dbhost = cfg('backend_'.$backendId, 'dbhost');
  100. $this->_dbport = cfg('backend_'.$backendId, 'dbport');
  101. $this->_dbinstancename = cfg('backend_'.$backendId, 'dbinstancename');
  102.  
  103. $this->connectToDb();
  104. $this->loadInstanceId();
  105. }
  106.  
  107. /**
  108. * Return the valid config for this backend
  109. *
  110. * @author Maximilien Bersoult <mbersoult@merethis.com>
  111. * @return array
  112. */
  113. static public function getValidConfig() {
  114. return self::$_validConfig;
  115. }
  116.  
  117. /**
  118. * Get the list of objects
  119. *
  120. * @author Maximilien Bersoult <mbersoult@merethis.com>
  121. * @param string $type The object type
  122. * @param string $name1Pattern The object name (host name or hostgroup name or servicegroup name)
  123. * @param string $name2Pattern Service name for a object type service
  124. * @return array
  125. * @throws BackendException *
  126. */
  127. public function getObjects($type, $name1Pattern = '', $name2Pattern = '') {
  128. $ret = array();
  129. switch ($type) {
  130. case 'host':
  131. $queryGetObject = 'SELECT host_id, 0 as service_id, name as name1, "" as name2
  132. FROM hosts
  133. WHERE enabled = 1';
  134. if ($name1Pattern != '') {
  135. $queryGetObject .= ' AND name = %s';
  136. }
  137. break;
  138. case 'service':
  139. $queryGetObject = 'SELECT s.host_id, s.service_id, h.name as name1, s.description as name2
  140. FROM services s, hosts h
  141. WHERE h.enabled =1
  142. AND s.enabled = 1
  143. AND h.name = %s
  144. AND h.host_id = s.host_id';
  145. if ('' !== $name2Pattern) {
  146. $queryGetObject .= ' AND s.description = %s';
  147. }
  148. break;
  149. case 'hostgroup':
  150. $queryGetObject = 'SELECT 0 as host_id, 0 as service_id, name as name1, "" as name2
  151. FROM hostgroups
  152. WHERE 1 = 1';
  153. if ($name1Pattern != '') {
  154. $queryGetObject .= ' AND name = %s';
  155. }
  156. break;
  157. case 'servicegroup':
  158. $queryGetObject = 'SELECT 0 as host_id, 0 as service_id, name as name1, "" as name2
  159. FROM servicegroups
  160. WHERE 1 = 1';
  161. if ($name1Pattern != '') {
  162. $queryGetObject .= ' name = %s';
  163. }
  164. break;
  165. default:
  166. return array();
  167. }
  168. /* Add instance id, enabled and order */
  169. if ($this->_instanceId != 0) {
  170. $queryGetObject .= ' AND instance_id = ' . $this->_instanceId;
  171. }
  172. $queryGetObject .= ' ORDER BY name1, name2';
  173.  
  174. if ('' !== $name2Pattern) {
  175. $queryGetObject = sprintf($queryGetObject, $this->_dbh->quote($name1Pattern), $this->_dbh->quote($name2Pattern), $this->_instanceId);
  176. }
  177. if ('' !== $name1Pattern) {
  178. $queryGetObject = sprintf($queryGetObject, $this->_dbh->quote($name1Pattern), $this->_instanceId);
  179. }
  180.  
  181. try {
  182. $stmt = $this->_dbh->query($queryGetObject);
  183. } catch (PDOException $e) {
  184. throw new BackendException(l('errorGettingObject', array('BACKENDID' => $this->_backendId, 'ERROR' => $e->getMessage())));
  185. }
  186.  
  187. while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
  188. /* Set cache */
  189. if (0 != $row['host_id']) {
  190. $this->_cacheHostId[$row['name1']] = $row['host_id'];
  191. if (0 != $row['service_id']) {
  192. $this->_cacheServiceId[$row['name1']][$row['name2']] = $row['service_id'];
  193. }
  194. }
  195. /* Set table */
  196. $ret[] = array('name1' => $row['name1'], 'name2' => $row['name2']);
  197. }
  198.  
  199. return $ret;
  200. }
  201.  
  202. /**
  203. * Get host state object
  204. *
  205. * @param type $objects
  206. * @param type $options
  207. * @param type $filters
  208. * @return array
  209. */
  210. public function getHostState($objects, $options, $filters) {
  211. $queryGetHostState = 'SELECT
  212. h.alias,
  213. h.name,
  214. h.address,
  215. h.statusmap_image,
  216. h.notes,
  217. h.check_command,
  218. h.perfdata,
  219. h.last_check,
  220. h.next_check,
  221. h.state_type,
  222. h.check_attempt as current_check_attempt,
  223. h.max_check_attempts,
  224. h.last_state_change,
  225. h.last_hard_state,
  226. h.last_hard_state_change,
  227. h.checked as has_been_checked,
  228. h.state as current_state,
  229. h.output,
  230. h.acknowledged as problem_has_been_acknowledged,
  231. d.start_time as downtime_start,
  232. d.end_time as downtime_end,
  233. d.author as downtime_author,
  234. d.comment_data as downtime_data
  235. FROM hosts h
  236. LEFT JOIN downtimes d
  237. ON (d.host_id = h.host_id AND d.service_id IS NULL AND d.start_time < UNIX_TIMESTAMP() AND d.end_time > UNIX_TIMESTAMP() AND d.deletion_time IS NULL)
  238. WHERE (d.downtime_id IS NULL OR d.downtime_id IN (
  239. SELECT MAX(d.downtime_id) as downtime_id
  240. FROM downtimes d where d.host_id = h.host_id AND d.service_id IS NULL AND d.start_time < UNIX_TIMESTAMP() AND d.end_time > UNIX_TIMESTAMP() AND d.deletion_time IS NULL
  241. )
  242. )
  243. AND h.enabled = 1 AND (%s)';
  244. if ($this->_instanceId != 0) {
  245. $queryGetHostState .= ' AND h.instance_id = ' . $this->_instanceId;
  246. }
  247. $queryGetHostState = sprintf($queryGetHostState, $this->parseFilter($objects, $filters));
  248.  
  249. try {
  250. $stmt = $this->_dbh->query($queryGetHostState);
  251. } catch (PDOException $e) {
  252. throw new BackendException(l('errorGettingHostState', array('BACKENDID' => $this->_backendId, 'ERROR' => $e->getMessage())));
  253. }
  254.  
  255. $listStates = array();
  256. while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
  257. /* Modifiy for downtime */
  258. if (false === isset($row['downtime_start']) || '' === $row['downtime_start']) {
  259. unset($row['downtime_start']);
  260. unset($row['downtime_end']);
  261. unset($row['downtime_author']);
  262. unset($row['downtime_data']);
  263. } else {
  264. $row['in_downtime'] = 1;
  265. }
  266. /* Modify state */
  267. /* Only Hard */
  268. if ($options & 1) {
  269. if ($row['state_type'] == '0') {
  270. $row['current_state'] = $row['last_hard_state'];
  271. }
  272. }
  273. /* Unchecked state */
  274. if ($row['has_been_checked'] == '0' || $row['current_state'] == '') {
  275. $row['state'] = 'UNCHECKED';
  276. $row['output'] = l('hostIsPending', Array('HOST' => $row['name']));
  277. } else {
  278. switch ($row['current_state']) {
  279. case '0':
  280. $row['state'] = 'UP';
  281. unset($row['problem_has_been_acknowledged']);
  282. break;
  283. case '1':
  284. $row['state'] = 'DOWN';
  285. break;
  286. case '2':
  287. $row['state'] = 'UNREACHABLE';
  288. break;
  289. case '3':
  290. $row['state'] = 'UNKNOWN';
  291. break;
  292. default:
  293. $row['state'] = 'UNKNOWN';
  294. $row['output'] = 'GlobalBackendcentreonbroker::getHostState: Undefined state!';
  295. break;
  296. }
  297. }
  298. $listStates[$row['name']] = $row;
  299. }
  300. return $listStates;
  301. }
  302.  
  303. public function getServiceState($objects, $options, $filters) {
  304. $queryGetServiceState = 'SELECT
  305. h.host_id,
  306. h.name,
  307. h.address,
  308. s.checked as has_been_checked,
  309. s.description as service_description,
  310. s.display_name,
  311. s.display_name as alias,
  312. s.notes,
  313. s.check_command,
  314. s.perfdata,
  315. s.output,
  316. s.state as current_state,
  317. s.last_check,
  318. s.next_check,
  319. s.state_type,
  320. s.check_attempt as current_check_attempt,
  321. s.max_check_attempts,
  322. s.last_state_change,
  323. s.last_hard_state_change,
  324. s.acknowledged as problem_has_been_acknowledged,
  325. d.start_time as downtime_start,
  326. d.end_time as downtime_end,
  327. d.author as downtime_author,
  328. d.comment_data as downtime_data
  329. FROM services s
  330. LEFT JOIN hosts h
  331. ON s.host_id=h.host_id
  332. LEFT JOIN downtimes d
  333. ON (d.host_id = h.host_id AND d.service_id=s.service_id AND d.start_time < UNIX_TIMESTAMP() AND d.end_time > UNIX_TIMESTAMP() AND d.deletion_time IS NULL)
  334. WHERE (d.downtime_id IS NULL OR d.downtime_id IN (
  335. SELECT MAX(d.downtime_id) as downtime_id
  336. FROM downtimes d where d.host_id = h.host_id AND d.service_id = s.service_id AND d.start_time < UNIX_TIMESTAMP() AND d.end_time > UNIX_TIMESTAMP() AND d.deletion_time IS NULL
  337. )
  338. )
  339. AND s.host_id = h.host_id AND s.enabled = 1 AND h.enabled = 1
  340. AND (%s)';
  341. if ($this->_instanceId != 0) {
  342. $queryGetServiceState .= ' AND h.instance_id = ' . $this->_instanceId;
  343. }
  344. $queryGetServiceState = sprintf($queryGetServiceState, $this->parseFilter($objects, $filters));
  345.  
  346. try {
  347. $stmt = $this->_dbh->query($queryGetServiceState);
  348. } catch (PDOException $e) {
  349. throw new BackendException(l('errorGettingServiceState', array('BACKENDID' => $this->_backendId, 'ERROR' => $e->getMessage())));
  350. }
  351.  
  352. $listStates = array();
  353. while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
  354. /* Define key */
  355. $specific = false;
  356. $key = $row['name'];
  357. if (isset($objects[$key . '~~' . $row['service_description']])) {
  358. $key = $key . '~~' . $row['service_description'];
  359. $specific = true;
  360. }
  361. /* Modifiy for downtime */
  362. if (false === isset($row['downtime_start']) || '' === $row['downtime_start']) {
  363. unset($row['downtime_start']);
  364. unset($row['downtime_end']);
  365. unset($row['downtime_author']);
  366. unset($row['downtime_data']);
  367. } else {
  368. $row['in_downtime'] = 1;
  369. }
  370. /* Modify state */
  371. /* Only Hard */
  372. if ($options & 1) {
  373. if ($row['state_type'] == '0') {
  374. $row['current_state'] = $row['last_hard_state'];
  375. }
  376. }
  377. /* Get host ack */
  378. if ($row['problem_has_been_acknowledged'] != 1) {
  379. $row['problem_has_been_acknowledged'] = $this->getHostAckByHost($row['host_id']);
  380. }
  381. unset($row['host_id']);
  382.  
  383. /* Unchecked state */
  384. if ($row['has_been_checked'] == '0' || $row['current_state'] == '') {
  385. $row['state'] = 'PENDING';
  386. $row['output'] = l('serviceNotChecked', Array('SERVICE' => $row['service_description']));
  387. } else {
  388. switch ($row['current_state']) {
  389. case '0':
  390. $row['state'] = 'OK';
  391. unset($row['problem_has_been_acknowledged']);
  392. break;
  393. case '1':
  394. $row['state'] = 'WARNING';
  395. break;
  396. case '2':
  397. $row['state'] = 'CRITICAL';
  398. break;
  399. case '3':
  400. $row['state'] = 'UNKNOWN';
  401. break;
  402. default:
  403. $row['state'] = 'UNKNOWN';
  404. $row['output'] = 'GlobalBackendcentreonbroker::getHostState: Undefined state!';
  405. break;
  406. }
  407. }
  408. if ($specific) {
  409. $listStates[$key] = $row;
  410. } else {
  411. if (!isset($listStates[$key])) {
  412. $listStates[$key] = array();
  413. }
  414. $listStates[$key][] = $row;
  415. }
  416. }
  417. return $listStates;
  418. }
  419.  
  420. public function getHostStateCounts($objects, $options, $filters) {
  421. if($options & 1) {
  422. $stateAttr = 'IF((s.state_type = 0), s.last_hard_state, s.state)';
  423. } else {
  424. $stateAttr = 's.state';
  425. }
  426. $queryCount = 'SELECT
  427. h.name,
  428. h.alias,
  429. SUM(IF(s.checked=0,1,0)) AS pending,
  430. SUM(IF(('.$stateAttr.'=0 AND s.checked!=0 AND s.scheduled_downtime_depth=0 AND h.scheduled_downtime_depth=0),1,0)) AS ok,
  431. SUM(IF(('.$stateAttr.'=0 AND s.checked!=0 AND (s.scheduled_downtime_depth!=0 OR h.scheduled_downtime_depth!=0)),1,0)) AS ok_downtime,
  432. SUM(IF(('.$stateAttr.'=1 AND s.checked!=0 AND s.scheduled_downtime_depth=0 AND h.scheduled_downtime_depth=0 AND s.acknowledged=0 AND h.acknowledged=0),1,0)) AS warning,
  433. SUM(IF(('.$stateAttr.'=1 AND s.checked!=0 AND (s.scheduled_downtime_depth!=0 OR h.scheduled_downtime_depth!=0)),1,0)) AS warning_downtime,
  434. SUM(IF(('.$stateAttr.'=1 AND s.checked!=0 AND (s.acknowledged=1 OR h.acknowledged=1)),1,0)) AS warning_ack,
  435. SUM(IF(('.$stateAttr.'=2 AND s.checked!=0 AND s.scheduled_downtime_depth=0 AND h.scheduled_downtime_depth=0) AND s.acknowledged=0 AND h.acknowledged=0,1,0)) AS critical,
  436. SUM(IF(('.$stateAttr.'=2 AND s.checked!=0 AND (s.scheduled_downtime_depth!=0 OR h.scheduled_downtime_depth!=0)),1,0)) AS critical_downtime,
  437. SUM(IF(('.$stateAttr.'=2 AND s.checked!=0 AND (s.acknowledged=1 OR h.acknowledged=1)),1,0)) AS critical_ack,
  438. SUM(IF(('.$stateAttr.'=3 AND s.checked!=0 AND s.scheduled_downtime_depth=0 AND h.scheduled_downtime_depth=0 AND s.acknowledged=0 AND h.acknowledged=0),1,0)) AS unknown,
  439. SUM(IF(('.$stateAttr.'=3 AND s.checked!=0 AND (s.scheduled_downtime_depth!=0 OR h.scheduled_downtime_depth!=0)),1,0)) AS unknown_downtime,
  440. SUM(IF(('.$stateAttr.'=3 AND s.checked!=0 AND (s.acknowledged=1 OR h.acknowledged=1)),1,0)) AS unknown_ack
  441. FROM hosts h, services s
  442. WHERE h.host_id = s.host_id AND h.enabled = 1 AND s.enabled = 1
  443. AND (%s)';
  444. if ($this->_instanceId != 0) {
  445. $queryCount .= ' AND h.instance_id = ' . $this->_instanceId;
  446. }
  447. $queryCount .= ' GROUP BY h.name';
  448. $queryCount = sprintf($queryCount, $this->parseFilter($objects, $filters));
  449.  
  450. try {
  451. $stmt = $this->_dbh->query($queryCount);
  452. } catch (PDOException $e) {
  453. throw new BackendException(l('errorGettingHostStateCount', array('BACKENDID' => $this->_backendId, 'ERROR' => $e->getMessage())));
  454. }
  455.  
  456. $counts = array();
  457. while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
  458. $counts[$row['name']] = array(
  459. 'details' => array('alias' => $row['alias']),
  460. 'counts' => array(
  461. 'UNCHECKED' => array(
  462. 'normal' => intval($row['pending']),
  463. ),
  464. 'OK' => array(
  465. 'normal' => intval($row['ok']),
  466. 'downtime' => intval($row['ok_downtime']),
  467. ),
  468. 'WARNING' => array(
  469. 'normal' => intval($row['warning']),
  470. 'ack' => intval($row['warning_ack']),
  471. 'downtime' => intval($row['warning_downtime']),
  472. ),
  473. 'CRITICAL' => array(
  474. 'normal' => intval($row['critical']),
  475. 'ack' => intval($row['critical_ack']),
  476. 'downtime' => intval($row['critical_downtime']),
  477. ),
  478. 'UNKNOWN' => array(
  479. 'normal' => intval($row['unknown']),
  480. 'ack' => intval($row['unknown_ack']),
  481. 'downtime' => intval($row['unknown_downtime']),
  482. )
  483. )
  484. );
  485. }
  486. return $counts;
  487. }
  488.  
  489. public function getHostgroupStateCounts($objects, $options, $filters) {
  490. if($options & 1) {
  491. $stateAttr = 'IF((h.state_type = 0), h.last_hard_state, h.state)';
  492. } else {
  493. $stateAttr = 'h.state';
  494. }
  495. $queryCount = 'SELECT
  496. hg.name,
  497. hg.alias,
  498. SUM(IF(h.checked=0,1,0)) AS unchecked,
  499. SUM(IF(('.$stateAttr.'=0 AND h.checked!=0 AND h.scheduled_downtime_depth=0),1,0)) AS up,
  500. SUM(IF(('.$stateAttr.'=0 AND h.checked!=0 AND h.scheduled_downtime_depth!=0),1,0)) AS up_downtime,
  501. SUM(IF(('.$stateAttr.'=1 AND h.checked!=0 AND h.scheduled_downtime_depth=0 AND h.acknowledged=0),1,0)) AS down,
  502. SUM(IF(('.$stateAttr.'=1 AND h.checked!=0 AND h.scheduled_downtime_depth!=0),1,0)) AS down_downtime,
  503. SUM(IF(('.$stateAttr.'=1 AND h.checked!=0 AND h.acknowledged=1),1,0)) AS down_ack,
  504. SUM(IF(('.$stateAttr.'=2 AND h.checked!=0 AND h.scheduled_downtime_depth=0 AND h.acknowledged=0),1,0)) AS unreachable,
  505. SUM(IF(('.$stateAttr.'=2 AND h.checked!=0 AND h.scheduled_downtime_depth!=0),1,0)) AS unreachable_downtime,
  506. SUM(IF(('.$stateAttr.'=2 AND h.checked!=0 AND h.acknowledged=1),1,0)) AS unreachable_ack,
  507. SUM(IF(('.$stateAttr.'=3 AND h.checked!=0 AND h.scheduled_downtime_depth=0 AND h.acknowledged=0),1,0)) AS unknown,
  508. SUM(IF(('.$stateAttr.'=3 AND h.checked!=0 AND h.scheduled_downtime_depth!=0),1,0)) AS unknown_downtime,
  509. SUM(IF(('.$stateAttr.'=3 AND h.checked!=0 AND h.acknowledged=1),1,0)) AS unknown_ack
  510. FROM hostgroups hg, hosts_hostgroups hhg, hosts h
  511. WHERE hhg.hostgroup_id = hg.hostgroup_id
  512. AND hhg.host_id = h.host_id
  513. AND h.enabled = 1
  514. AND (%s)';
  515. if ($this->_instanceId != 0) {
  516. $queryCount .= ' AND h.instance_id = ' . $this->_instanceId;
  517. }
  518. $queryCount .= ' GROUP BY hg.name';
  519. $queryCount = sprintf($queryCount, $this->parseFilter($objects, $filters, 'hg'));
  520.  
  521. try {
  522. $stmt = $this->_dbh->query($queryCount);
  523. } catch (PDOException $e) {
  524. throw new BackendException(l('errorGettinggetHostgroupStateCounts', array('BACKENDID' => $this->_backendId, 'ERROR' => $e->getMessage())));
  525. }
  526.  
  527. $counts = array();
  528. while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
  529. $counts[$row['name']] = array(
  530. 'details' => array('alias' => $row['alias']),
  531. 'counts' => array(
  532. 'UNCHECKED' => array(
  533. 'normal' => intval($row['unchecked']),
  534. ),
  535. 'UP' => array(
  536. 'normal' => intval($row['up']),
  537. 'downtime' => intval($row['up_downtime']),
  538. ),
  539. 'DOWN' => array(
  540. 'normal' => intval($row['down']),
  541. 'ack' => intval($row['down_ack']),
  542. 'downtime' => intval($row['down_downtime']),
  543. ),
  544. 'UNREACHABLE' => array(
  545. 'normal' => intval($row['unreachable']),
  546. 'ack' => intval($row['unreachable_ack']),
  547. 'downtime' => intval($row['unreachable_downtime']),
  548. )
  549. )
  550. );
  551. }
  552. if ($options & 2) {
  553. return $counts;
  554. }
  555.  
  556. if ($options & 1) {
  557. $stateAttr = 'IF((s.state_type = 0), s.last_hard_state, s.state)';
  558. } else {
  559. $stateAttr = 's.state';
  560. }
  561. $queryCount = 'SELECT
  562. hg.name,
  563. hg.alias,
  564. SUM(IF(s.checked=0,1,0)) AS pending,
  565. SUM(IF(('.$stateAttr.'=0 AND s.checked!=0 AND s.scheduled_downtime_depth=0),1,0)) AS ok,
  566. SUM(IF(('.$stateAttr.'=0 AND s.checked!=0 AND s.scheduled_downtime_depth!=0),1,0)) AS ok_downtime,
  567. SUM(IF(('.$stateAttr.'=1 AND s.checked!=0 AND s.scheduled_downtime_depth=0 AND s.acknowledged=0),1,0)) AS warning,
  568. SUM(IF(('.$stateAttr.'=1 AND s.checked!=0 AND s.scheduled_downtime_depth!=0),1,0)) AS warning_downtime,
  569. SUM(IF(('.$stateAttr.'=1 AND s.checked!=0 AND s.acknowledged=1),1,0)) AS warning_ack,
  570. SUM(IF(('.$stateAttr.'=2 AND s.checked!=0 AND s.scheduled_downtime_depth=0 AND s.acknowledged=0),1,0)) AS critical,
  571. SUM(IF(('.$stateAttr.'=2 AND s.checked!=0 AND s.scheduled_downtime_depth!=0),1,0)) AS critical_downtime,
  572. SUM(IF(('.$stateAttr.'=2 AND s.checked!=0 AND s.acknowledged=1),1,0)) AS critical_ack,
  573. SUM(IF(('.$stateAttr.'=3 AND s.checked!=0 AND s.scheduled_downtime_depth=0 AND s.acknowledged=0),1,0)) AS unknown,
  574. SUM(IF(('.$stateAttr.'=3 AND s.checked!=0 AND s.scheduled_downtime_depth!=0),1,0)) AS unknown_downtime,
  575. SUM(IF(('.$stateAttr.'=3 AND s.checked!=0 AND s.acknowledged=1),1,0)) AS unknown_ack
  576. FROM hostgroups hg, hosts_hostgroups hhg, services s, hosts h
  577. WHERE hhg.hostgroup_id = hg.hostgroup_id
  578. AND hhg.host_id = s.host_id
  579. AND hhg.host_id = h.host_id
  580. AND h.enabled = 1
  581. AND s.enabled = 1
  582. AND (%s)';
  583. if ($this->_instanceId != 0) {
  584. $queryCount .= ' AND h.instance_id = ' . $this->_instanceId;
  585. }
  586. $queryCount .= ' GROUP BY hg.name';
  587. $queryCount = sprintf($queryCount, $this->parseFilter($objects, $filters, 'hg'));
  588.  
  589. try {
  590. $stmt = $this->_dbh->query($queryCount);
  591. } catch (PDOException $e) {
  592. throw new BackendException(l('errorGettinggetHostgroupStateCounts', array('BACKENDID' => $this->_backendId, 'ERROR' => $e->getMessage())));
  593. }
  594.  
  595. while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
  596. $counts[$row['name']]['counts']['PENDING']['normal'] = intval($row['pending']);
  597. $counts[$row['name']]['counts']['OK']['normal'] = intval($row['ok']);
  598. $counts[$row['name']]['counts']['OK']['downtime'] = intval($row['ok_downtime']);
  599. $counts[$row['name']]['counts']['WARNING']['normal'] = intval($row['warning']);
  600. $counts[$row['name']]['counts']['WARNING']['ack'] = intval($row['warning_ack']);
  601. $counts[$row['name']]['counts']['WARNING']['downtime'] = intval($row['warning_downtime']);
  602. $counts[$row['name']]['counts']['CRITICAL']['normal'] = intval($row['critical']);
  603. $counts[$row['name']]['counts']['CRITICAL']['ack'] = intval($row['critical_ack']);
  604. $counts[$row['name']]['counts']['CRITICAL']['downtime'] = intval($row['critical_downtime']);
  605. $counts[$row['name']]['counts']['UNKNOWN']['normal'] = intval($row['unknown']);
  606. $counts[$row['name']]['counts']['UNKNOWN']['ack'] = intval($row['unknown_ack']);
  607. $counts[$row['name']]['counts']['UNKNOWN']['downtime'] = intval($row['unknown_downtime']);
  608. }
  609. return $counts;
  610. }
  611.  
  612. public function getServicegroupStateCounts($objects, $options, $filters) {
  613. if($options & 1) {
  614. $stateAttr = 'IF((s.state_type = 0), s.last_hard_state, s.state)';
  615. } else {
  616. $stateAttr = 's.state';
  617. }
  618. $queryCount = 'SELECT
  619. sg.name,
  620. sg.alias,
  621. SUM(IF(s.checked=0,1,0)) AS pending,
  622. SUM(IF(('.$stateAttr.'=0 AND s.checked!=0 AND s.scheduled_downtime_depth=0),1,0)) AS ok,
  623. SUM(IF(('.$stateAttr.'=0 AND s.checked!=0 AND s.scheduled_downtime_depth!=0),1,0)) AS ok_downtime,
  624. SUM(IF(('.$stateAttr.'=1 AND s.checked!=0 AND s.scheduled_downtime_depth=0 AND s.acknowledged=0),1,0)) AS warning,
  625. SUM(IF(('.$stateAttr.'=1 AND s.checked!=0 AND s.scheduled_downtime_depth!=0),1,0)) AS warning_downtime,
  626. SUM(IF(('.$stateAttr.'=1 AND s.checked!=0 AND s.acknowledged=1),1,0)) AS warning_ack,
  627. SUM(IF(('.$stateAttr.'=2 AND s.checked!=0 AND s.scheduled_downtime_depth=0 AND s.acknowledged=0),1,0)) AS critical,
  628. SUM(IF(('.$stateAttr.'=2 AND s.checked!=0 AND s.scheduled_downtime_depth!=0),1,0)) AS critical_downtime,
  629. SUM(IF(('.$stateAttr.'=2 AND s.checked!=0 AND s.acknowledged=1),1,0)) AS critical_ack,
  630. SUM(IF(('.$stateAttr.'=3 AND s.checked!=0 AND s.scheduled_downtime_depth=0 AND s.acknowledged=0),1,0)) AS unknown,
  631. SUM(IF(('.$stateAttr.'=3 AND s.checked!=0 AND s.scheduled_downtime_depth!=0),1,0)) AS unknown_downtime,
  632. SUM(IF(('.$stateAttr.'=3 AND s.checked!=0 AND s.acknowledged=1),1,0)) AS unknown_ack
  633. FROM servicegroups sg, services_servicegroups ssg, services s
  634. WHERE ssg.servicegroup_id = sg.servicegroup_id
  635. AND ssg.service_id = s.service_id
  636. AND s.enabled = 1
  637. AND (%s) GROUP BY sg.name';
  638. $queryCount = sprintf($queryCount, $this->parseFilter($objects, $filters, 'sg'));
  639.  
  640. try {
  641. $stmt = $this->_dbh->query($queryCount);
  642. } catch (PDOException $e) {
  643. throw new BackendException(l('errorGettingServicegroupStateCounts', array('BACKENDID' => $this->_backendId, 'ERROR' => $e->getMessage())));
  644. }
  645.  
  646. $counts = array();
  647. while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
  648. $counts[$row['name']] = array(
  649. 'details' => array('alias' => $row['alias']),
  650. 'counts' => array(
  651. 'PENDING' => array(
  652. 'normal' => intval($row['pending']),
  653. ),
  654. 'OK' => array(
  655. 'normal' => intval($row['ok']),
  656. 'downtime' => intval($row['ok_downtime']),
  657. ),
  658. 'WARNING' => array(
  659. 'normal' => intval($row['warning']),
  660. 'ack' => intval($row['warning_ack']),
  661. 'downtime' => intval($row['warning_downtime']),
  662. ),
  663. 'CRITICAL' => array(
  664. 'normal' => intval($row['critical']),
  665. 'ack' => intval($row['critical_ack']),
  666. 'downtime' => intval($row['critical_downtime']),
  667. ),
  668. 'UNKNOWN' => array(
  669. 'normal' => intval($row['unknown']),
  670. 'ack' => intval($row['unknown_ack']),
  671. 'downtime' => intval($row['unknown_downtime']),
  672. )
  673. )
  674. );
  675. }
  676.  
  677. return $counts;
  678. }
  679.  
  680. public function getHostNamesWithNoParent() {
  681. $queryNoParents = 'SELECT name
  682. FROM hosts
  683. WHERE enabled = 1 AND host_id NOT IN (SELECT host_id
  684. FROM hosts_hosts_parents)';
  685. if ($this->_instanceId != 0) {
  686. $queryNoParents .= ' AND instance_id = ' . $this->_instanceId;
  687. }
  688.  
  689. try {
  690. $stmt = $this->_dbh->query($queryNoParents);
  691. } catch (PDOException $e) {
  692. throw new BackendException(l('errorGettingHostNamesWithNoParent', array('BACKENDID' => $this->_backendId, 'ERROR' => $e->getMessage())));
  693. }
  694.  
  695. $noParents = array();
  696. while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
  697. $noParents[] = $row['name'];
  698. }
  699. return $noParents;
  700. }
  701.  
  702. public function getDirectChildNamesByHostName($hostname) {
  703. $queryGetChilds = 'SELECT h.name
  704. FROM hosts h, hosts_hosts_parents hp
  705. WHERE h.host_id = hp.child_id
  706. AND h.enabled = 1
  707. AND hp.parent_id IN (SELECT host_id
  708. FROM hosts
  709. WHERE name = %s)';
  710. if ($this->_instanceId != 0) {
  711. $queryGetChilds .= ' AND h.instance_id = ' . $this->_instanceId;
  712. }
  713. $queryGetChilds = sprintf($queryGetChilds, $this->_dbh->quote($hostname));
  714.  
  715. try {
  716. $stmt = $this->_dbh->query($queryGetChilds);
  717. } catch (PDOException $e) {
  718. throw new BackendException(l('errorGettingDirectChildNamesByHostName', array('BACKENDID' => $this->_backendId, 'ERROR' => $e->getMessage())));
  719. }
  720.  
  721. $childs = array();
  722. while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
  723. $childs[] = $row['name'];
  724. }
  725. return $childs;
  726. }
  727.  
  728. public function getDirectParentNamesByHostName($hostname) {
  729. $queryGetParents = 'SELECT h.name
  730. FROM hosts h, hosts_hosts_parents hp
  731. WHERE h.host_id = hp.parent_id
  732. AND h.enabled = 1
  733. AND hp.child_id IN (SELECT host_id
  734. FROM hosts
  735. WHERE name = "%s")';
  736. if ($this->_instanceId != 0) {
  737. $queryGetParents .= ' AND h.instance_id = ' . $this->_instanceId;
  738. }
  739. $queryGetParents = sprintf($queryGetParents, $this->_dbh->quote($hostname));
  740.  
  741. try {
  742. $stmt = $this->_dbh->query($queryGetParents);
  743. } catch (PDOException $e) {
  744. throw new BackendException(l('errorGettingDirectParentNamesByHostName', array('BACKENDID' => $this->_backendId, 'ERROR' => $e->getMessage())));
  745. }
  746.  
  747. $parents = array();
  748. while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
  749. $parents[] = $row['name'];
  750. }
  751. return $parents;
  752. }
  753.  
  754. private function getHostAckByHost($hostId) {
  755. if (isset($this->_cacheHostAck[$hostId])) {
  756. return $this->_cacheHostAck[$hostId];
  757. }
  758. $queryAck = 'SELECT acknowledged
  759. FROM hosts
  760. WHERE enabled = 1 AND host_id = ' . $hostId;
  761.  
  762. try {
  763. $stmt = $this->_dbh->query($queryAck);
  764. } catch (PDOException $e) {
  765. throw new BackendException(l('errorGettingHostAck', array('BACKENDID' => $this->_backendId, 'ERROR' => $e->getMessage())));
  766. }
  767.  
  768. $row = $stmt->fetch(PDO::FETCH_ASSOC);
  769. if (false === $row) {
  770. return 0;
  771. }
  772.  
  773. $return = 0;
  774. if (isset($row['acknowledged']) && $row['acknowledged'] == '1') {
  775. $return = 1;
  776. }
  777. $this->_cacheHostAck[$hostId] = $return;
  778. return $this->_cacheHostAck[$hostId];
  779. }
  780.  
  781. private function parseFilter($objects, $filters, $tableAlias = 'h') {
  782. $listKeys = array(
  783. 'host_name',
  784. 'host_groups',
  785. 'service_groups',
  786. 'hostgroup_name',
  787. 'group_name',
  788. 'groups',
  789. 'servicegroup_name',
  790. 'service_description'
  791. );
  792. $allFilters = array();
  793. foreach ($objects as $object) {
  794. $objFilters = array();
  795. /* Filters */
  796. foreach ($filters as $filter) {
  797. if (false === in_array($filter['key'], $listKeys)) {
  798. throw new BackendException('Invalid filter key ('.$filter['key'].')');
  799. }
  800. if ($filter['op'] == '>=') {
  801. $op = '=';
  802. } else {
  803. $op = $filter['op'];
  804. }
  805. if ($filter['key'] == 'service_description') {
  806. $key = 's.description';
  807. $val = $object[0]->getServiceDescription();
  808. } else {
  809. $key = $tableAlias . '.name';
  810. $val = $object[0]->getName();
  811. }
  812. $objFilters[] = $key . ' ' . $op . ' ' . $this->_dbh->quote($val);
  813. }
  814.  
  815.  
  816. $allFilters[] = join(' AND ', $objFilters);
  817. }
  818. return join(' OR ', $allFilters);
  819. }
  820.  
  821. /**
  822. * Connection to the Centreon Broker database
  823. *
  824. * @author Maximilien Bersoult <mbersoult@merethis.com>
  825. * @throws BackendConnectionProblem
  826. */
  827. private function connectToDb() {
  828. if (false === extension_loaded('mysql')) {
  829. throw new BackendConnectionProblem(l('mysqlNotSupported', array('BACKENDID', $this->_backendId)));
  830. }
  831. $fullhost = 'host=' . $this->_dbhost;
  832. if ('' != $this->_dbport) {
  833. $fullhost .= ';port=' . $this->_dbport;
  834. }
  835. if ('' != $this->_dbname) {
  836. $fullhost .= ';dbname=' . $this->_dbname;
  837. }
  838. try {
  839. $this->_dbh = new PDO('mysql:' . $fullhost, $this->_dbuser, $this->_dbpass, array(PDO::ATTR_PERSISTENT => false,
  840. PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8"));
  841. $this->_dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  842. } catch (PDOException $e) {
  843. throw new BackendConnectionProblem(l('errorConnectingMySQL', Array('BACKENDID' => $this->backendId,'MYSQLERR' => $e->getMessage())));
  844. }
  845. }
  846.  
  847. /**
  848. * Load the instance id
  849. *
  850. * @author Maximilien Bersoult <mbersoult@merethis.com>
  851. * @throws BackendException
  852. */
  853. private function loadInstanceId() {
  854. try {
  855. $stmt = $this->_dbh->prepare("SELECT instance_id
  856. FROM instances
  857. WHERE name = :name");
  858. $stmt->bindParam(':name', $this->_dbinstancename, PDO::PARAM_STR);
  859. $stmt->execute();
  860. $row = $stmt->fetch(PDO::FETCH_ASSOC);
  861. } catch (PDOException $e ) {
  862. throw new BackendException('errorLoadingInstanceId', array('BACKENDID' => $this->_backendId, 'ERROR' => $e->getMessage()));
  863. }
  864.  
  865. if (isset($row['instance_id'])) {
  866. $this->_instanceId = $row['instance_id'];
  867. }
  868. }
  869. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement