PageRenderTime 33ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/www/class/centreonCriticality.class.php

https://gitlab.com/florianocomercial/centreon
PHP | 357 lines | 220 code | 24 blank | 113 comment | 55 complexity | d99edd39dbbce7cf5ef77bc1c6def2ec MD5 | raw file
  1. <?php
  2. /*
  3. * Copyright 2005-2012 Centreon
  4. * Centreon is developped by : Julien Mathis and Romain Le Merlus under
  5. * GPL Licence 2.0.
  6. *
  7. * This program is free software; you can redistribute it and/or modify it under
  8. * the terms of the GNU General Public License as published by the Free Software
  9. * Foundation ; either version 2 of the License.
  10. *
  11. * This program is distributed in the hope that it will be useful, but WITHOUT ANY
  12. * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
  13. * PARTICULAR PURPOSE. See the GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License along with
  16. * this program; if not, see <http://www.gnu.org/licenses>.
  17. *
  18. * Linking this program statically or dynamically with other modules is making a
  19. * combined work based on this program. Thus, the terms and conditions of the GNU
  20. * General Public License cover the whole combination.
  21. *
  22. * As a special exception, the copyright holders of this program give Centreon
  23. * permission to link this program with independent modules to produce an executable,
  24. * regardless of the license terms of these independent modules, and to copy and
  25. * distribute the resulting executable under terms of Centreon choice, provided that
  26. * Centreon also meet, for each linked independent module, the terms and conditions
  27. * of the license of that module. An independent module is a module which is not
  28. * derived from this program. If you modify this program, you may extend this
  29. * exception to your version of the program, but you are not obliged to do so. If you
  30. * do not wish to do so, delete this exception statement from your version.
  31. *
  32. * For more information : contact@centreon.com
  33. *
  34. * SVN : $URL$
  35. * SVN : $Id$
  36. *
  37. */
  38. /**
  39. * Class for managing criticality object
  40. *
  41. * @author Sylvestre Ho
  42. */
  43. class CentreonCriticality {
  44. /**
  45. * @var CentreonDB
  46. */
  47. protected $db;
  48. protected $tree;
  49. public function __construct($db) {
  50. $this->db = $db;
  51. }
  52. /**
  53. * Get data of a criticality object
  54. *
  55. * @param int $critId
  56. * @return array
  57. */
  58. public function getData($critId, $service = false) {
  59. if ($service === false) {
  60. return $this->getDataForHosts($critId);
  61. }
  62. return $this->getDataForServices($critId);
  63. }
  64. /**
  65. * Get data of a criticality object for hosts
  66. *
  67. * @param int $critId
  68. * @return array
  69. */
  70. public function getDataForHosts($critId) {
  71. static $data = array();
  72. if (!isset($data[$critId])) {
  73. $sql = "SELECT hc_id, hc_name, level, icon_id, hc_comment
  74. FROM hostcategories
  75. WHERE level IS NOT NULL
  76. ORDER BY level DESC";
  77. $res = $this->db->query($sql);
  78. while ($row = $res->fetchRow()) {
  79. if (!isset($data[$row['hc_id']])) {
  80. $row['name'] = $row['hc_name'];
  81. $data[$row['hc_id']] = $row;
  82. }
  83. }
  84. }
  85. if (isset($data[$critId])) {
  86. return $data[$critId];
  87. }
  88. return null;
  89. }
  90. /**
  91. * Get data of a criticality object for services
  92. *
  93. * @param int $critId
  94. * @return array
  95. */
  96. public function getDataForServices($critId) {
  97. static $data = array();
  98. if (!isset($data[$critId])) {
  99. $sql = "SELECT sc_id, sc_name, level, icon_id, sc_description
  100. FROM service_categories
  101. WHERE level IS NOT NULL
  102. ORDER BY level DESC";
  103. $res = $this->db->query($sql);
  104. while ($row = $res->fetchRow()) {
  105. if (!isset($data[$row['sc_id']])) {
  106. $row['name'] = $row['sc_name'];
  107. $data[$row['sc_id']] = $row;
  108. }
  109. }
  110. }
  111. if (isset($data[$critId])) {
  112. return $data[$critId];
  113. }
  114. return null;
  115. }
  116. /**
  117. * Get list of criticality
  118. *
  119. * @param string $searchString
  120. * @param string $orderBy
  121. * @param string $sort
  122. * @param int $offset
  123. * @param int $limit
  124. * @paaram bool $service
  125. * @return array
  126. */
  127. public function getList($searchString = null, $orderBy = "level", $sort = 'ASC', $offset = null, $limit = null, $service = false) {
  128. if ($service === false) {
  129. $elements = $this->getListForHosts(
  130. $searchString,
  131. $orderBy,
  132. $sort,
  133. $offset,
  134. $limit
  135. );
  136. } else {
  137. $elements = $this->getListForServices(
  138. $searchString,
  139. $orderBy,
  140. $sort,
  141. $offset,
  142. $limit
  143. );
  144. }
  145. return $elements;
  146. }
  147. /**
  148. * Get real time host criticality id
  149. *
  150. */
  151. public function getRealtimeHostCriticalityId($db, $hostId)
  152. {
  153. static $ids = null;
  154. if (is_null($ids)) {
  155. $sql = "SELECT cvs.host_id, cvs.value as criticality
  156. FROM customvariables cvs
  157. WHERE cvs.name='CRITICALITY_ID'
  158. AND cvs.service_id IS NULL";
  159. $res = $db->query($sql);
  160. $ids = array();
  161. while ($row = $res->fetchRow()) {
  162. $ids[$row['host_id']] = $row['criticality'];
  163. }
  164. }
  165. if (isset($ids[$hostId])) {
  166. return $ids[$hostId];
  167. }
  168. return 0;
  169. }
  170. /**
  171. * Get real time service criticality id
  172. *
  173. */
  174. public function getRealtimeServiceCriticalityId($db, $serviceId)
  175. {
  176. static $ids = null;
  177. if (is_null($ids)) {
  178. $sql = "SELECT cvs.service_id, cvs.value as criticality
  179. FROM customvariables cvs
  180. WHERE cvs.name='CRITICALITY_ID'
  181. AND cvs.service_id IS NOT NULL";
  182. $res = $db->query($sql);
  183. $ids = array();
  184. while ($row = $res->fetchRow()) {
  185. $ids[$row['service_id']] = $row['criticality'];
  186. }
  187. }
  188. if (isset($ids[$serviceId])) {
  189. return $ids[$serviceId];
  190. }
  191. return 0;
  192. }
  193. /**
  194. * Get list of host criticalities
  195. *
  196. * @param type $searchString
  197. * @param type $orderBy
  198. * @param type $sort
  199. * @param type $offset
  200. * @param type $limit
  201. * @return type
  202. */
  203. protected function getListForHosts($searchString = null, $orderBy = "level", $sort = 'ASC', $offset = null, $limit = null) {
  204. $sql = "SELECT hc_id, hc_name, level, icon_id, hc_comment
  205. FROM hostcategories
  206. WHERE level IS NOT NULL ";
  207. if (!is_null($searchString) && $searchString != "") {
  208. $sql .= " AND hc_name LIKE '%".$this->db->escape($searchString)."%' ";
  209. }
  210. if (!is_null($orderBy) && !is_null($sort)) {
  211. $sql .= " ORDER BY $orderBy $sort ";
  212. }
  213. if (!is_null($offset) && !is_null($limit)) {
  214. $sql .= " LIMIT $offset,$limit";
  215. }
  216. $res = $this->db->query($sql);
  217. $elements = array();
  218. while ($row = $res->fetchRow()) {
  219. $elements[$row['hc_id']] = array();
  220. $elements[$row['hc_id']]['hc_name'] = $row['hc_name'];
  221. $elements[$row['hc_id']]['level'] = $row['level'];
  222. $elements[$row['hc_id']]['icon_id'] = $row['icon_id'];
  223. $elements[$row['hc_id']]['comments'] = $row['hc_comment'];
  224. }
  225. return $elements;
  226. }
  227. /**
  228. * Get list of service criticalities
  229. *
  230. * @param type $searchString
  231. * @param type $orderBy
  232. * @param type $sort
  233. * @param type $offset
  234. * @param type $limit
  235. * @return type
  236. */
  237. protected function getListForServices($searchString = null, $orderBy = "level", $sort = 'ASC', $offset = null, $limit = null) {
  238. $sql = "SELECT sc_id, sc_name, level, icon_id, sc_description
  239. FROM service_categories
  240. WHERE level IS NOT NULL ";
  241. if (!is_null($searchString) && $searchString != "") {
  242. $sql .= " AND sc_name LIKE '%".$this->db->escape($searchString)."%' ";
  243. }
  244. if (!is_null($orderBy) && !is_null($sort)) {
  245. $sql .= " ORDER BY $orderBy $sort ";
  246. }
  247. if (!is_null($offset) && !is_null($limit)) {
  248. $sql .= " LIMIT $offset,$limit";
  249. }
  250. $res = $this->db->query($sql);
  251. $elements = array();
  252. while ($row = $res->fetchRow()) {
  253. $elements[$row['sc_id']] = array();
  254. $elements[$row['sc_id']]['sc_name'] = $row['sc_name'];
  255. $elements[$row['sc_id']]['level'] = $row['level'];
  256. $elements[$row['sc_id']]['icon_id'] = $row['icon_id'];
  257. $elements[$row['sc_id']]['description'] = $row['sc_description'];
  258. }
  259. return $elements;
  260. }
  261. /**
  262. * Create a buffer with all criticality informations
  263. *
  264. * @param type service_id
  265. * return array
  266. */
  267. public function criticitiesConfigOnSTpl($service_id) {
  268. global $pearDB, $critCache;
  269. if (!count($this->tree)) {
  270. $request = "SELECT service_id, service_template_model_stm_id FROM service WHERE service_register = '0' AND service_activate = '1' ORDER BY service_template_model_stm_id ASC";
  271. $RES = $pearDB->query($request);
  272. while ($data = $RES->fetchRow()) {
  273. $this->tree[$data['service_id']] = $this->getServiceCriticality($data["service_id"]);
  274. }
  275. }
  276. if (isset($this->tree[$service_id]) && $this->tree[$service_id] != 0) {
  277. return $this->tree[$service_id];
  278. }
  279. return 0;
  280. }
  281. /**
  282. * Get service criticality
  283. *
  284. * @param type service_id
  285. * return array
  286. */
  287. protected function getServiceCriticality($service_id) {
  288. global $pearDB;
  289. if (!isset($service_id) || $service_id == 0) {
  290. return 0;
  291. }
  292. $request = "SELECT service_id, service_template_model_stm_id FROM service WHERE service_register = '0' AND service_activate = '1' AND service_id = $service_id ORDER BY service_template_model_stm_id ASC";
  293. $RES = $pearDB->query($request);
  294. if (isset($RES) && $RES->numRows()) {
  295. while ($data = $RES->fetchRow()) {
  296. $request2 = "select sr.* FROM service_categories_relation sr, service_categories sc WHERE sr.sc_id = sc.sc_id AND sr.service_service_id = '".$data['service_id']."' AND sc.level IS NOT NULL";
  297. $RES2 = $pearDB->query($request2);
  298. if ($RES2->numRows() != 0) {
  299. $criticity = $RES2->fetchRow();
  300. if ($criticity['sc_id'] && isset($criticity['sc_id'])) {
  301. return $criticity["sc_id"];
  302. } else {
  303. return 0;
  304. }
  305. } else {
  306. return $this->getServiceCriticality($data["service_template_model_stm_id"]);
  307. }
  308. }
  309. }
  310. return 0;
  311. }
  312. function getHostTplCriticities($host_id, $cache) {
  313. global $pearDB;
  314. if (!$host_id) {
  315. return NULL;
  316. }
  317. $rq = "SELECT host_tpl_id " .
  318. "FROM host_template_relation " .
  319. "WHERE host_host_id = '".$host_id."' " .
  320. "ORDER BY `order`";
  321. $DBRESULT = $pearDB->query($rq);
  322. while ($row = $DBRESULT->fetchRow()) {
  323. if (isset($cache[$row['host_tpl_id']])) {
  324. return $this->getData($cache[$row['host_tpl_id']], false);
  325. } else {
  326. if ($result_field = $this->getHostTplCriticities($row['host_tpl_id'], $cache)) {
  327. return $result_field;
  328. }
  329. }
  330. }
  331. return NULL;
  332. }
  333. }