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

/www/class/centreonBroker.class.php

https://gitlab.com/florianocomercial/centreon
PHP | 134 lines | 55 code | 8 blank | 71 comment | 13 complexity | 06d7564b368614da084433a79418bb90 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 CentreonBroker
  36. {
  37. private $name;
  38. private $db;
  39. /*
  40. * Constructor class
  41. *
  42. * @access public
  43. * @return object object session
  44. */
  45. public function __construct($db)
  46. {
  47. $this->db = $db;
  48. $this->setBrokerName();
  49. }
  50. /**
  51. * Get Broker engine name
  52. */
  53. private function setBrokerName()
  54. {
  55. $this->name = 'broker';
  56. }
  57. /*
  58. * return broker engine
  59. */
  60. public function getBroker()
  61. {
  62. return "broker";
  63. }
  64. /**
  65. * Execute script
  66. *
  67. * @param string $script
  68. * @param string $action
  69. * @return void
  70. */
  71. protected function execLocalScript($script, $action) {
  72. $scriptD = str_replace("/etc/init.d/", '', $script);
  73. if (file_exists("/etc/systemd/system/") && file_exists("/etc/systemd/system/$scriptD.service")) {
  74. shell_exec("sudo systemctl $action $scriptD");
  75. } else {
  76. exec("ps -edf | grep cbd | grep -v grep", $output, $return_vars);
  77. if (count($output) == 0) {
  78. shell_exec("sudo $script restart");
  79. } else {
  80. shell_exec("sudo $script $action");
  81. }
  82. }
  83. }
  84. /**
  85. * Get Init script
  86. *
  87. * @param string $sql;
  88. * @return string
  89. */
  90. protected function getInitScript($sql) {
  91. $res = $this->db->query($sql);
  92. $row = $res->fetchRow();
  93. $scriptName = "";
  94. if (isset($row['value']) && trim($row['value']) != '') {
  95. $scriptName = trim($row['value']);
  96. }
  97. return $scriptName;
  98. }
  99. /**
  100. * Do action
  101. *
  102. * @param string $action
  103. * @return void
  104. */
  105. protected function doAction($action) {
  106. if ($this->name == 'broker') {
  107. $initScript = $this->getInitScript("SELECT `value` FROM options WHERE `key` = 'broker_correlator_script'");
  108. if ($initScript) {
  109. $this->execLocalScript($initScript, $action);
  110. }
  111. }
  112. }
  113. /**
  114. * Magic method
  115. *
  116. * @param string $name
  117. * @param array $params
  118. * @throws Exception
  119. */
  120. public function __call($name, $params) {
  121. if (!preg_match('/reload|restart|stop|start/', $name)) {
  122. throw new Exception('Unknown method: '.$name);
  123. }
  124. $this->doAction($name);
  125. }
  126. }