PageRenderTime 58ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/www/class/config-generate/severity.class.php

https://gitlab.com/florianocomercial/centreon
PHP | 256 lines | 188 code | 30 blank | 38 comment | 27 complexity | d0dcdee3779ae328d46ef97be27408c2 MD5 | raw file
  1. <?php
  2. /**
  3. * Copyright 2005-2015 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. */
  35. class Severity extends AbstractObject {
  36. private $use_cache = 1;
  37. private $done_cache = 0;
  38. private $service_severity_cache = array();
  39. private $service_severity_by_name_cache = array();
  40. private $service_linked_cache = array();
  41. private $host_severity_cache = array();
  42. private $host_linked_cache = array();
  43. protected $generate_filename = null;
  44. protected $object_name = null;
  45. protected $stmt_host = null;
  46. protected $stmt_service = null;
  47. protected $stmt_hc_name = null;
  48. public function __construct() {
  49. parent::__construct();
  50. $this->buildCache();
  51. }
  52. private function cacheHostSeverity() {
  53. $stmt = $this->backend_instance->db->prepare("SELECT
  54. hc_name, hc_id, level
  55. FROM hostcategories
  56. WHERE level IS NOT NULL AND hc_activate = '1'
  57. ");
  58. $stmt->execute();
  59. $values = $stmt->fetchAll(PDO::FETCH_ASSOC);
  60. foreach ($values as &$value) {
  61. $this->host_severity_cache[$value['hc_id']] = &$value;
  62. }
  63. }
  64. private function cacheHostSeverityLinked() {
  65. $stmt = $this->backend_instance->db->prepare("SELECT hc_id, host_host_id
  66. FROM hostcategories, hostcategories_relation
  67. WHERE level IS NOT NULL AND hc_activate = '1' AND hostcategories_relation.hostcategories_hc_id = hostcategories.hc_id
  68. ");
  69. $stmt->execute();
  70. foreach ($stmt->fetchAll(PDO::FETCH_ASSOC) as $value) {
  71. if (isset($this->host_linked_cache[$value['host_host_id']])) {
  72. if ($this->host_severity_cache[$value['hc_id']]['level'] < $this->host_severity_cache[$this->host_linked_cache[$value['host_host_id']]]) {
  73. $this->host_linked_cache[$value['host_host_id']] = $value['hc_id'];
  74. }
  75. } else {
  76. $this->host_linked_cache[$value['host_host_id']] = $value['hc_id'];
  77. }
  78. }
  79. }
  80. public function getHostSeverityByHostId($host_id) {
  81. # Get from the cache
  82. if (isset($this->host_linked_cache[$host_id])) {
  83. return $this->host_linked_cache[$host_id];
  84. }
  85. if ($this->done_cache == 1) {
  86. return null;
  87. }
  88. # We get unitary
  89. if (is_null($this->stmt_host)) {
  90. $this->stmt_host = $this->backend_instance->db->prepare("SELECT
  91. hc_id, hc_name, level
  92. FROM hostcategories_relation, hostcategories
  93. WHERE hostcategories_relation.host_host_id = :host_id
  94. AND hostcategories_relation.hostcategories_hc_id = hostcategories.hc_id
  95. AND level IS NOT NULL AND hc_activate = '1'
  96. ORDER BY level DESC
  97. LIMIT 1
  98. ");
  99. }
  100. $this->stmt_host->bindParam(':host_id', $host_id, PDO::PARAM_INT);
  101. $this->stmt_host->execute();
  102. $severity = array_pop($this->stmt_host->fetchAll(PDO::FETCH_ASSOC));
  103. if (is_null($severity)) {
  104. $this->host_linked_cache[$host_id] = null;
  105. return null;
  106. }
  107. $this->host_linked_cache[$service_id] = $severity['hc_id'];
  108. $this->host_severity_cache[$severity['hc_id']] = &$severity;
  109. return $severity['hc_id'];
  110. }
  111. public function getHostSeverityById($hc_id) {
  112. if (is_null($hc_id)) {
  113. return null;
  114. }
  115. if (!isset($this->host_severity_cache[$hc_id])) {
  116. return null;
  117. }
  118. return $this->host_severity_cache[$hc_id];
  119. }
  120. private function cacheServiceSeverity() {
  121. $stmt = $this->backend_instance->db->prepare("SELECT
  122. sc_name, sc_id, level
  123. FROM service_categories
  124. WHERE level IS NOT NULL AND sc_activate = '1'
  125. ");
  126. $stmt->execute();
  127. $values = $stmt->fetchAll(PDO::FETCH_ASSOC);
  128. foreach ($values as &$value) {
  129. $this->service_severity_by_name_cache[$value['sc_name']] = &$value;
  130. $this->service_severity_cache[$value['sc_id']] = &$value;
  131. }
  132. }
  133. private function cacheServiceSeverityLinked() {
  134. $stmt = $this->backend_instance->db->prepare("SELECT service_categories.sc_id, service_service_id
  135. FROM service_categories, service_categories_relation
  136. WHERE level IS NOT NULL AND sc_activate = '1' AND service_categories_relation.sc_id = service_categories.sc_id
  137. ");
  138. $stmt->execute();
  139. foreach ($stmt->fetchAll(PDO::FETCH_ASSOC) as $value) {
  140. if (isset($this->service_linked_cache[$value['service_service_id']])) {
  141. if ($this->service_severity_cache[$value['sc_id']]['level'] < $this->service_severity_cache[$this->service_linked_cache[$value['service_service_id']]]) {
  142. $this->service_linked_cache[$value['service_service_id']] = $value['sc_id'];
  143. }
  144. } else {
  145. $this->service_linked_cache[$value['service_service_id']] = $value['sc_id'];
  146. }
  147. }
  148. }
  149. private function buildCache() {
  150. if ($this->done_cache == 1) {
  151. return 0;
  152. }
  153. $this->cacheHostSeverity();
  154. $this->cacheHostSeverityLinked();
  155. $this->cacheServiceSeverity();
  156. $this->cacheServiceSeverityLinked();
  157. $this->done_cache = 1;
  158. }
  159. public function getServiceSeverityByServiceId($service_id) {
  160. # Get from the cache
  161. if (isset($this->service_linked_cache[$service_id])) {
  162. return $this->service_linked_cache[$service_id];
  163. }
  164. if ($this->done_cache == 1) {
  165. return null;
  166. }
  167. # We get unitary
  168. if (is_null($this->stmt_service)) {
  169. $this->stmt_service = $this->backend_instance->db->prepare("SELECT
  170. service_categories.sc_id, sc_name, level
  171. FROM service_categories_relation, service_categories
  172. WHERE service_categories_relation.service_service_id = :service_id
  173. AND service_categories_relation.sc_id = service_categories.sc_id
  174. AND level IS NOT NULL AND sc_activate = '1'
  175. ORDER BY level DESC
  176. LIMIT 1
  177. ");
  178. }
  179. $this->stmt_service->bindParam(':service_id', $service_id, PDO::PARAM_INT);
  180. $this->stmt_service->execute();
  181. $severity = array_pop($this->stmt_service->fetchAll(PDO::FETCH_ASSOC));
  182. if (is_null($severity)) {
  183. $this->service_linked_cache[$service_id] = null;
  184. return null;
  185. }
  186. $this->service_linked_cache[$service_id] = $severity['sc_id'];
  187. $this->service_severity_by_name_cache[$severity['sc_name']] = &$severity;
  188. $this->service_severity_cache[$severity['sc_id']] = &$severity;
  189. return $severity['sc_id'];
  190. }
  191. public function getServiceSeverityById($sc_id) {
  192. if (is_null($sc_id)) {
  193. return null;
  194. }
  195. if (!isset($this->service_severity_cache[$sc_id])) {
  196. return null;
  197. }
  198. return $this->service_severity_cache[$sc_id];
  199. }
  200. public function getServiceSeverityMappingHostSeverityByName($hc_name) {
  201. if (isset($this->service_severity_by_name_cache[$hc_name])) {
  202. return $this->service_severity_by_name_cache[$hc_name];
  203. }
  204. if ($this->done_cache == 1) {
  205. return null;
  206. }
  207. # We get unitary
  208. if (is_null($this->stmt_hc_name)) {
  209. $this->stmt_hc_name = $this->backend_instance->db->prepare("SELECT
  210. sc_name, sc_id, level
  211. FROM service_categories
  212. WHERE sc_name = :sc_name AND level IS NOT NULL AND sc_activate = '1'
  213. ");
  214. }
  215. $this->stmt_hc_name->bindParam(':sc_name', $hc_name, PDO::PARAM_STR);
  216. $this->stmt_hc_name->execute();
  217. $severity = array_pop($this->stmt_hc_name->fetchAll(PDO::FETCH_ASSOC));
  218. if (is_null($severity)) {
  219. $this->service_severity_by_name_cache[$hc_name] = null;
  220. return null;
  221. }
  222. $this->service_severity_by_name_cache[$hc_name] = &$severity;
  223. $this->service_severity_cache[$hc_name] = &$severity;
  224. return $severity['sc_id'];
  225. }
  226. }