api.watchdog.php 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842
  1. <?php
  2. /**
  3. * Flexible system monitoring aka WatchDog implementation
  4. */
  5. class WatchDog {
  6. /**
  7. * Contains watchdog tasks data
  8. *
  9. * @var array
  10. */
  11. protected $taskData = array();
  12. /**
  13. * Contains old results returned by watchdog tasks
  14. *
  15. * @var array
  16. */
  17. protected $oldResults = array();
  18. /**
  19. * Contains current watchdog run tasks results
  20. *
  21. * @var array
  22. */
  23. protected $curResults = array();
  24. /**
  25. * Contains watchdog configuration as key=>value
  26. *
  27. * @var array
  28. */
  29. protected $settings = array();
  30. /**
  31. * System SMS object placeholder
  32. *
  33. * @var object
  34. */
  35. protected $sms = '';
  36. /**
  37. * System Email object placeholder
  38. *
  39. * @var object
  40. */
  41. protected $email = '';
  42. /**
  43. * One-Punch object placeholder
  44. *
  45. * @var object
  46. */
  47. protected $onePunch = '';
  48. /**
  49. * System Telegram object placeholder
  50. *
  51. * @var object
  52. */
  53. protected $telegram = '';
  54. /**
  55. * Contains stardust process manager instance
  56. *
  57. * @var object
  58. */
  59. protected $processMgr = '';
  60. const PARAM_EX = 'NO_REQUIRED_TASK_PARAM_';
  61. const PARAMFMT_EX = 'WRONG_FORMAT_TASK_PARAM_';
  62. const SETTINGS_EX = 'NO_SETTINGS_LOADED';
  63. const PID_NAME = 'WATCHDOG';
  64. public function __construct() {
  65. //get all current watchdog tasks
  66. $this->loadTasks();
  67. //get all previous polling results
  68. $this->loadOldResults();
  69. //load watchdog settings from database
  70. $this->loadSettings();
  71. //inits process manager
  72. $this->initStarDust();
  73. //init sms class
  74. $this->initSMS();
  75. //init mail class
  76. $this->initEmail();
  77. //init telegram class
  78. $this->initTelegram();
  79. //inits onepunch scripts
  80. $this->initOnePunch();
  81. }
  82. /**
  83. * Returns current watchdog settings
  84. *
  85. * @return array
  86. */
  87. public function getSettings() {
  88. $result = $this->settings;
  89. return ($result);
  90. }
  91. /**
  92. * Loads all previous tasks execution results into private property oldResults
  93. *
  94. * @return void
  95. */
  96. protected function loadOldResults() {
  97. if (!empty($this->taskData)) {
  98. foreach ($this->taskData as $iy => $eachPrevResult) {
  99. $this->oldResults[$eachPrevResult['id']] = $eachPrevResult['oldresult'];
  100. }
  101. }
  102. }
  103. /**
  104. * Loads an active task list from database and pushes it into private property taskData
  105. *
  106. * @return void
  107. */
  108. protected function loadTasks() {
  109. $taskQuery = "SELECT * from `watchdog` WHERE `active`='1';";
  110. $alltasks = simple_queryall($taskQuery);
  111. if (!empty($alltasks)) {
  112. foreach ($alltasks as $iz => $eachTask) {
  113. $this->taskData[$eachTask['id']]['id'] = $eachTask['id'];
  114. $this->taskData[$eachTask['id']]['active'] = $eachTask['active'];
  115. $this->taskData[$eachTask['id']]['name'] = $eachTask['name'];
  116. $this->taskData[$eachTask['id']]['checktype'] = $eachTask['checktype'];
  117. $this->taskData[$eachTask['id']]['param'] = $eachTask['param'];
  118. $this->taskData[$eachTask['id']]['operator'] = $eachTask['operator'];
  119. $this->taskData[$eachTask['id']]['condition'] = $eachTask['condition'];
  120. $this->taskData[$eachTask['id']]['action'] = $eachTask['action'];
  121. $this->taskData[$eachTask['id']]['oldresult'] = $eachTask['oldresult'];
  122. }
  123. }
  124. }
  125. /**
  126. * Gets watchdog settings from database and load it into settings property
  127. *
  128. * @return void
  129. */
  130. protected function loadSettings() {
  131. $alert = zb_StorageGet('WATCHDOG_ALERT');
  132. $phones = zb_StorageGet('WATCHDOG_PHONES');
  133. $emails = zb_StorageGet('WATCHDOG_EMAILS');
  134. $telegramchats = zb_StorageGet('WATCHDOG_TELEGRAM');
  135. $maintenanceMode = zb_StorageGet('WATCHDOG_MAINTENANCE');
  136. $smsSilenceMode = zb_StorageGet('WATCHDOG_SMSSILENCE');
  137. $this->settings['WATCHDOG_ALERT'] = $alert;
  138. $this->settings['WATCHDOG_PHONES'] = $phones;
  139. $this->settings['WATCHDOG_EMAILS'] = $emails;
  140. $this->settings['WATCHDOG_TELEGRAM'] = $telegramchats;
  141. $this->settings['WATCHDOG_MAINTENANCE'] = $maintenanceMode;
  142. $this->settings['WATCHDOG_SMSSILENCE'] = $smsSilenceMode;
  143. if (empty($this->settings['WATCHDOG_ALERT'])) {
  144. throw new Exception(self::SETTINGS_EX);
  145. }
  146. }
  147. /**
  148. * Inits system SMS queue object
  149. *
  150. * @return void
  151. */
  152. protected function initSMS() {
  153. $this->sms = new UbillingSMS();
  154. }
  155. /**
  156. * Inits system email queue object
  157. *
  158. * @return void
  159. */
  160. protected function initEmail() {
  161. $this->email = new UbillingMail();
  162. }
  163. /**
  164. * Inits onepunch object
  165. *
  166. * @return void
  167. */
  168. protected function initOnePunch() {
  169. $this->onePunch = new OnePunch();
  170. }
  171. /**
  172. * Inits system telegram messages queue object
  173. *
  174. * @return void
  175. */
  176. protected function initTelegram() {
  177. $this->telegram = new UbillingTelegram();
  178. }
  179. /**
  180. * Inits process manager
  181. *
  182. * @return void
  183. */
  184. protected function initStarDust() {
  185. $this->processMgr = new StarDust(self::PID_NAME);
  186. }
  187. /**
  188. * stores sms for deffered sending via senddog
  189. *
  190. * @param string $number - number to send sms in internaitional format
  191. * @message string $message in utf8 encoding
  192. *
  193. * @return array
  194. */
  195. public function sendSMS($number, $message) {
  196. $this->sms->sendSMS($this->safeEscapeString($number), $this->safeEscapeString($message), false, 'WATCHDOG');
  197. }
  198. /**
  199. * sends email notification
  200. *
  201. * @param string $email - target email
  202. * @param string $message - message
  203. *
  204. * @return void
  205. */
  206. protected function sendEmail($email, $message) {
  207. $subj = 'Ubilling ' . __('Watchdog');
  208. $message .= ' ' . date("Y-m-d H:i:s");
  209. $this->email->sendEmail($email, $subj, $message, 'WATCHDOG');
  210. }
  211. /**
  212. * ugly hack to dirty input data filtering with multiple DB links
  213. *
  214. * @param $string - string to filter
  215. *
  216. * @return string
  217. */
  218. protected function safeEscapeString($string) {
  219. @$result = preg_replace("#[~@\?\%\/\;=\*\>\<\"\']#Uis", '', $string);
  220. return ($result);
  221. }
  222. /**
  223. * Updates previous poll data in database
  224. *
  225. * @param int $taskID - watchdog task id
  226. * @param string $value - data to set as oldresult
  227. *
  228. * @return void
  229. */
  230. protected function setOldValue($taskID, $value) {
  231. simple_update_field('watchdog', 'oldresult', $value, "WHERE `id`='" . $taskID . "'");
  232. }
  233. /**
  234. * Updates current run task results
  235. *
  236. * @param int $taskID - watchdog task id
  237. * @param string $value - data to set as newresult
  238. *
  239. * @return void
  240. */
  241. protected function setCurValue($taskID, $value) {
  242. $this->curResults[$taskID] = $value;
  243. }
  244. /**
  245. * Execute some action for task
  246. *
  247. * @param int $taskID - id of existing monitoring task to run
  248. *
  249. * @return mixed
  250. */
  251. protected function doAction($taskID = NULL) {
  252. if (!empty($taskID)) {
  253. switch ($this->taskData[$taskID]['checktype']) {
  254. //do the system icmp ping
  255. case 'icmpping':
  256. if (!empty($this->taskData[$taskID]['param'])) {
  257. $result = zb_PingICMP($this->taskData[$taskID]['param']);
  258. $storeValue = ($result) ? 'true' : 'false';
  259. $this->setOldValue($taskID, $storeValue);
  260. $this->setCurValue($taskID, $storeValue);
  261. } else {
  262. throw new Exception(self::PARAM_EX . "ICMPPING");
  263. }
  264. break;
  265. //do the system icmp ping three times with hope of some result
  266. case 'hopeping':
  267. if (!empty($this->taskData[$taskID]['param'])) {
  268. $result = zb_PingICMPHope($this->taskData[$taskID]['param']);
  269. $storeValue = ($result) ? 'true' : 'false';
  270. $this->setOldValue($taskID, $storeValue);
  271. $this->setCurValue($taskID, $storeValue);
  272. } else {
  273. throw new Exception(self::PARAM_EX . "HOPEPING");
  274. }
  275. break;
  276. //get raw http result
  277. case 'httpget':
  278. if (!empty($this->taskData[$taskID]['param'])) {
  279. $httpUrl = $this->taskData[$taskID]['param'];
  280. $result = @file_get_contents($httpUrl);
  281. $result = trim($result);
  282. $this->setOldValue($taskID, $result);
  283. $this->setCurValue($taskID, $result);
  284. } else {
  285. throw new Exception(self::PARAM_EX . "HTTPGET");
  286. }
  287. break;
  288. //run some script
  289. case 'script':
  290. if (!empty($this->taskData[$taskID]['param'])) {
  291. $command = $this->taskData[$taskID]['param'];
  292. $result = shell_exec($command);
  293. $result = trim($result);
  294. $this->setOldValue($taskID, $result);
  295. $this->setCurValue($taskID, $result);
  296. } else {
  297. throw new Exception(self::PARAM_EX . "SCRIPT");
  298. }
  299. break;
  300. //run one-punch script
  301. case 'onepunch':
  302. if (!empty($this->taskData[$taskID]['param'])) {
  303. $onePunchScriptData = $this->onePunch->getScriptContent($this->taskData[$taskID]['param']);
  304. eval($onePunchScriptData);
  305. $result = @$watchdogCallbackResult;
  306. $this->setOldValue($taskID, $result);
  307. $this->setCurValue($taskID, $result);
  308. } else {
  309. throw new Exception(self::PARAM_EX . "ONEPUNCH");
  310. }
  311. break;
  312. //do the TCP port check
  313. case 'tcpping':
  314. if (!empty($this->taskData[$taskID]['param'])) {
  315. if (ispos($this->taskData[$taskID]['param'], ':')) {
  316. $tcpPingData = explode(':', $this->taskData[$taskID]['param']);
  317. $tcpPingHost = $tcpPingData[0];
  318. $tcpPingPort = $tcpPingData[1];
  319. $tcpPingTimeout = 2;
  320. $transport = 'tcp://';
  321. @$connection = fsockopen($transport . $tcpPingHost, $tcpPingPort, $tcperrno, $tcperrstr, $tcpPingTimeout);
  322. if (!$connection) {
  323. $result = false;
  324. } else {
  325. $result = true;
  326. }
  327. $storeValue = ($result) ? 'true' : 'false';
  328. $this->setOldValue($taskID, $storeValue);
  329. $this->setCurValue($taskID, $storeValue);
  330. } else {
  331. throw new Exception(self::PARAMFMT_EX . "TCPPING");
  332. }
  333. } else {
  334. throw new Exception(self::PARAM_EX . "TCPPING");
  335. }
  336. break;
  337. //do the UDP port check
  338. case 'udpping':
  339. if (!empty($this->taskData[$taskID]['param'])) {
  340. if (ispos($this->taskData[$taskID]['param'], ':')) {
  341. $udpPingData = explode(':', $this->taskData[$taskID]['param']);
  342. $udpPingHost = $udpPingData[0];
  343. $udpPingPort = $udpPingData[1];
  344. //not using fsockopen/sockets here because they always returns non failed results on udp datagrams
  345. global $ubillingConfig;
  346. $altCfg = $ubillingConfig->getAlter();
  347. $billCfg = $ubillingConfig->getBilling();
  348. $cmd = $billCfg['SUDO'] . ' ' . $altCfg['NMAP_PATH'] . ' -p' . $udpPingPort . ' -sU ' . $udpPingHost . ' | ' . $billCfg['GREP'] . ' ' . $udpPingPort;
  349. $rawResult = shell_exec($cmd);
  350. if (ispos($rawResult, 'open')) {
  351. $result = true;
  352. } else {
  353. $result = false;
  354. }
  355. $storeValue = ($result) ? 'true' : 'false';
  356. $this->setOldValue($taskID, $storeValue);
  357. $this->setCurValue($taskID, $storeValue);
  358. } else {
  359. throw new Exception(self::PARAMFMT_EX . "UDPPING");
  360. }
  361. } else {
  362. throw new Exception(self::PARAM_EX . "UDPPING");
  363. }
  364. break;
  365. //perform some snmpwalk query
  366. case 'snmpwalk':
  367. if (!empty($this->taskData[$taskID]['param'])) {
  368. if (ispos($this->taskData[$taskID]['param'], ':')) {
  369. $snmpData = explode(':', $this->taskData[$taskID]['param']);
  370. $snmpHost = $snmpData[0];
  371. $snmpCommunity = $snmpData[1];
  372. $snmpOid = $snmpData[2];
  373. $snmpHandle = new SNMPHelper();
  374. $snmpHandle->setBackground(false);
  375. $result = $snmpHandle->walk($snmpHost, $snmpCommunity, $snmpOid);
  376. $result = trim($result);
  377. $result = zb_SanitizeSNMPValue($result);
  378. $storeValue = $result;
  379. $this->setOldValue($taskID, $storeValue);
  380. $this->setCurValue($taskID, $storeValue);
  381. } else {
  382. throw new Exception(self::PARAMFMT_EX . 'SNMPWALK');
  383. }
  384. } else {
  385. throw new Exception(self::PARAM_EX . 'SNMPWALK');
  386. }
  387. break;
  388. case 'freediskspace':
  389. if (!empty($this->taskData[$taskID]['param'])) {
  390. if (ispos($this->taskData[$taskID]['param'], '/')) {
  391. $rawSpace = disk_free_space($this->taskData[$taskID]['param']);
  392. $result = $rawSpace / 1073741824; //in Gb
  393. $result = round($result, 2);
  394. $this->setOldValue($taskID, $result);
  395. $this->setCurValue($taskID, $result);
  396. } else {
  397. throw new Exception(self::PARAMFMT_EX . 'FREEDISKSPACE');
  398. }
  399. } else {
  400. throw new Exception(self::PARAM_EX . 'FREEDISKSPACE');
  401. }
  402. break;
  403. // gets some user traffic by his login
  404. case 'getusertraff':
  405. if (!empty($this->taskData[$taskID]['param'])) {
  406. $userLoging = mysql_real_escape_string($this->taskData[$taskID]['param']);
  407. $traffQuery = "SELECT SUM(`D0`+`U0`) from `users` WHERE login='" . $userLogin . "'";
  408. $traffData = simple_query($traffQuery);
  409. $result = $traffData['SUM(`D0`+`U0`)'];
  410. $this->setOldValue($taskID, $result);
  411. $this->setCurValue($taskID, $result);
  412. } else {
  413. throw new Exception(self::PARAM_EX . "GETUSERTRAFF");
  414. }
  415. break;
  416. //check is some file exist?
  417. case 'fileexists':
  418. if (!empty($this->taskData[$taskID]['param'])) {
  419. $result = file_exists($this->taskData[$taskID]['param']);
  420. $storeValue = ($result) ? 'true' : 'false';
  421. $this->setOldValue($taskID, $storeValue);
  422. $this->setCurValue($taskID, $storeValue);
  423. } else {
  424. throw new Exception(self::PARAM_EX . "FILEEXISTS");
  425. }
  426. break;
  427. //open helpdesk tickets count check
  428. case 'opentickets':
  429. if (!empty($this->taskData[$taskID]['param'])) {
  430. $result = zb_TicketsGetAllNewCount();
  431. $this->setOldValue($taskID, $result);
  432. $this->setCurValue($taskID, $result);
  433. } else {
  434. throw new Exception(self::PARAM_EX . "OPENTICKETS");
  435. }
  436. break;
  437. }
  438. } else {
  439. $result = 0;
  440. }
  441. return ($result);
  442. }
  443. /**
  444. * gets oldresult from some task id
  445. *
  446. * @param int $taskID - existing task id
  447. *
  448. * @return string
  449. */
  450. protected function getOldValue($taskID = NULL) {
  451. $result = $this->oldResults[$taskID];
  452. return ($result);
  453. }
  454. /**
  455. * Checks condition for selected task
  456. *
  457. * @param int $taskID - existing task id
  458. *
  459. * @return string
  460. */
  461. protected function checkCondition($taskID = NULL) {
  462. if (!empty($taskID)) {
  463. switch ($this->taskData[$taskID]['operator']) {
  464. //boolean true
  465. case '=true':
  466. if ($this->doAction($taskID)) {
  467. return (true);
  468. } else {
  469. return (false);
  470. }
  471. break;
  472. //boolean false
  473. case '=false':
  474. if (!$this->doAction($taskID)) {
  475. return (true);
  476. } else {
  477. return (false);
  478. }
  479. break;
  480. //boolean equals
  481. case '==':
  482. $cond = trim($this->taskData[$taskID]['condition']);
  483. $actres = trim($this->doAction($taskID));
  484. if ($actres == $cond) {
  485. return (true);
  486. } else {
  487. return (false);
  488. }
  489. break;
  490. //boolean not equals
  491. case '!=':
  492. $cond = trim($this->taskData[$taskID]['condition']);
  493. $actres = trim($this->doAction($taskID));
  494. if ($actres != $cond) {
  495. return (true);
  496. } else {
  497. return (false);
  498. }
  499. break;
  500. //boolean gt
  501. case '>':
  502. $currentValue = $this->doAction($taskID);
  503. $currentValue = trim($currentValue);
  504. if ($currentValue > $this->taskData[$taskID]['condition']) {
  505. return (true);
  506. } else {
  507. return (false);
  508. }
  509. break;
  510. //boolean lt
  511. case '<':
  512. $currentValue = $this->doAction($taskID);
  513. $currentValue = trim($currentValue);
  514. if ($currentValue < $this->taskData[$taskID]['condition']) {
  515. return (true);
  516. } else {
  517. return (false);
  518. }
  519. break;
  520. //boolean gt or equal
  521. case '>=':
  522. $currentValue = $this->doAction($taskID);
  523. $currentValue = trim($currentValue);
  524. if ($currentValue >= $this->taskData[$taskID]['condition']) {
  525. return (true);
  526. } else {
  527. return (false);
  528. }
  529. break;
  530. //boolean lt or equal
  531. case '<=':
  532. $currentValue = $this->doAction($taskID);
  533. $currentValue = trim($currentValue);
  534. if ($currentValue <= $this->taskData[$taskID]['condition']) {
  535. return (true);
  536. } else {
  537. return (false);
  538. }
  539. break;
  540. //changes against previous results
  541. case 'changed':
  542. $oldValue = $this->oldResults[$taskID];
  543. $currentValue = $this->doAction($taskID);
  544. if (is_bool($currentValue)) {
  545. $currentValue = ($currentValue) ? 'true' : 'false';
  546. }
  547. if ($currentValue != $oldValue) {
  548. return (true);
  549. } else {
  550. return (false);
  551. }
  552. break;
  553. //any changes against previois results
  554. case 'notchanged':
  555. $oldValue = $this->oldResults[$taskID];
  556. $currentValue = $this->doAction($taskID);
  557. if (is_bool($currentValue)) {
  558. $currentValue = ($currentValue) ? 'true' : 'false';
  559. }
  560. if ($currentValue == $oldValue) {
  561. return (true);
  562. } else {
  563. return (false);
  564. }
  565. break;
  566. //empty check
  567. case 'empty':
  568. $emptyCheck = $this->doAction($taskID);
  569. $emptyCheck = trim($emptyCheck);
  570. if (empty($emptyCheck)) {
  571. return (true);
  572. } else {
  573. return (false);
  574. }
  575. break;
  576. //not empty check
  577. case 'notempty':
  578. $emptyCheck = $this->doAction($taskID);
  579. $emptyCheck = trim($emptyCheck);
  580. if (!empty($emptyCheck)) {
  581. return (true);
  582. } else {
  583. return (false);
  584. }
  585. break;
  586. //like substring check
  587. case 'like':
  588. $currentResult = $this->doAction($taskID);
  589. $needCondition = $this->taskData[$taskID]['condition'];
  590. if (ispos($currentResult, $needCondition)) {
  591. return (true);
  592. } else {
  593. return (false);
  594. }
  595. break;
  596. //not like substring check
  597. case 'notlike':
  598. $currentResult = $this->doAction($taskID);
  599. $needCondition = $this->taskData[$taskID]['condition'];
  600. if (!ispos($currentResult, $needCondition)) {
  601. return (true);
  602. } else {
  603. return (false);
  604. }
  605. break;
  606. //rised against previous results
  607. case 'rised':
  608. $oldValue = $this->oldResults[$taskID];
  609. $currentValue = $this->doAction($taskID);
  610. $changeLevel = (!empty($this->taskData[$taskID]['condition'])) ? $this->taskData[$taskID]['condition'] : 0;
  611. if ($currentValue > ($oldValue + $changeLevel)) {
  612. return (true);
  613. } else {
  614. return (false);
  615. }
  616. break;
  617. //decreased against previous results
  618. case 'decreased':
  619. $oldValue = $this->oldResults[$taskID];
  620. $currentValue = $this->doAction($taskID);
  621. $changeLevel = (!empty($this->taskData[$taskID]['condition'])) ? $this->taskData[$taskID]['condition'] : 0;
  622. if ($currentValue < ($oldValue - $changeLevel)) {
  623. return (true);
  624. } else {
  625. return (false);
  626. }
  627. break;
  628. }
  629. } else
  630. return (0);
  631. }
  632. /**
  633. * Main task processing subroutine
  634. *
  635. * @return void
  636. */
  637. public function processTask() {
  638. if (!$this->settings['WATCHDOG_MAINTENANCE']) {
  639. if ($this->processMgr->notRunning()) {
  640. $this->processMgr->start();
  641. foreach ($this->taskData as $taskID => $eachProcessTask) {
  642. if ($this->checkCondition($taskID)) {
  643. //task details collecting
  644. $alertTaskName = $this->taskData[$taskID]['name'];
  645. //if condition happens - do some task actions
  646. $taskActions = $this->taskData[$taskID]['action'];
  647. if (!empty($taskActions)) {
  648. /* different actions handling */
  649. // system log write
  650. if (ispos($taskActions, 'log')) {
  651. $notifyLogMessage = 'WATCHDOG NOTIFY THAT `' . $alertTaskName;
  652. //attach old result to log message if needed
  653. if (ispos($taskActions, 'oldresult')) {
  654. $notifyLogMessage .= ' ' . $this->taskData[$taskID]['oldresult'];
  655. }
  656. //attach current results to log message
  657. if (ispos($taskActions, 'andresult')) {
  658. $notifyLogMessage .= ' ' . $this->curResults[$taskID];
  659. }
  660. $notifyLogMessage .= '`';
  661. log_register($notifyLogMessage);
  662. }
  663. //send emails with alerts
  664. if (ispos($taskActions, 'email')) {
  665. if (!empty($this->settings['WATCHDOG_EMAILS'])) {
  666. $allNotifyEmails = explode(',', $this->settings['WATCHDOG_EMAILS']);
  667. if (!empty($allNotifyEmails)) {
  668. $notifyMessageMail = $this->settings['WATCHDOG_ALERT'] . ' ' . $alertTaskName;
  669. //attach old result to email if needed
  670. if (ispos($taskActions, 'oldresult')) {
  671. $notifyMessageMail .= ' ' . $this->taskData[$taskID]['oldresult'];
  672. }
  673. //attach current results
  674. if (ispos($taskActions, 'andresult')) {
  675. $notifyMessageMail .= ' ' . $this->curResults[$taskID];
  676. }
  677. foreach ($allNotifyEmails as $im => $eachmail) {
  678. $this->sendEmail($eachmail, $notifyMessageMail);
  679. }
  680. }
  681. }
  682. }
  683. //send telegram messages with alerts
  684. if (ispos($taskActions, 'telegram')) {
  685. if (!empty($this->settings['WATCHDOG_TELEGRAM'])) {
  686. $allNotifyTelegramChats = explode(',', $this->settings['WATCHDOG_TELEGRAM']);
  687. $additionalChats = array();
  688. if (preg_match('!\((.*?)\)!si', $taskActions, $tmpAddChats)) {
  689. $additionalChats = explode(',', $tmpAddChats[1]);
  690. if (!empty($additionalChats)) {
  691. if (!ispos($taskActions, 'no_tg_primary')) {
  692. foreach ($additionalChats as $ig => $eachAdditionalChat) {
  693. if (!empty($eachAdditionalChat)) {
  694. $allNotifyTelegramChats[] = $eachAdditionalChat;
  695. }
  696. }
  697. } else {
  698. $allNotifyTelegramChats = $additionalChats;
  699. }
  700. }
  701. }
  702. if (!empty($allNotifyTelegramChats)) {
  703. $notifyMessageTlg = $this->settings['WATCHDOG_ALERT'] . ' ' . $alertTaskName;
  704. //attach old result to email if needed
  705. if (ispos($taskActions, 'oldresult')) {
  706. $notifyMessageTlg .= ' ' . $this->taskData[$taskID]['oldresult'];
  707. }
  708. //attach current results
  709. if (ispos($taskActions, 'andresult')) {
  710. $notifyMessageTlg .= ' ' . $this->curResults[$taskID];
  711. }
  712. foreach ($allNotifyTelegramChats as $tlgm => $eachtlgchat) {
  713. $this->telegram->sendMessage($eachtlgchat, $notifyMessageTlg, false, 'WATCHDOG');
  714. }
  715. }
  716. }
  717. }
  718. //run some script with path like [path]
  719. if (ispos($taskActions, 'script')) {
  720. if (preg_match('!\[(.*?)\]!si', $taskActions, $tmpArr)) {
  721. $runScriptPath = $tmpArr[1];
  722. } else {
  723. $runScriptPath = '';
  724. }
  725. if (!empty($runScriptPath)) {
  726. shell_exec($runScriptPath);
  727. log_register("WATCHDOG RUN SCRIPT `" . $runScriptPath . "`");
  728. }
  729. }
  730. //send sms messages
  731. if (!$this->settings['WATCHDOG_SMSSILENCE']) {
  732. if (ispos($taskActions, 'sms')) {
  733. if (!empty($this->settings['WATCHDOG_PHONES'])) {
  734. $allNotifyPhones = explode(',', $this->settings['WATCHDOG_PHONES']);
  735. $additionalPhones = array();
  736. if (preg_match('!\{(.*?)\}!si', $taskActions, $tmpAddPhones)) {
  737. $additionalPhones = explode(',', $tmpAddPhones[1]);
  738. if (!empty($additionalPhones)) {
  739. if (!ispos($taskActions, 'noprimary')) {
  740. foreach ($additionalPhones as $ig => $eachAdditionalPhone) {
  741. if (!empty($eachAdditionalPhone)) {
  742. $allNotifyPhones[] = $eachAdditionalPhone;
  743. }
  744. }
  745. } else {
  746. $allNotifyPhones = $additionalPhones;
  747. }
  748. }
  749. }
  750. if (!empty($allNotifyPhones)) {
  751. $notifyMessage = $this->settings['WATCHDOG_ALERT'] . ' ' . $alertTaskName;
  752. //attach old result to sms if needed
  753. if (ispos($taskActions, 'oldresult')) {
  754. $notifyMessage .= ' ' . $this->taskData[$taskID]['oldresult'];
  755. }
  756. //attach current result to sms if needed
  757. if (ispos($taskActions, 'andresult')) {
  758. $notifyMessage .= ' ' . $this->curResults[$taskID];
  759. }
  760. foreach ($allNotifyPhones as $iu => $eachmobile) {
  761. $this->sendSMS($eachmobile, $notifyMessage);
  762. }
  763. }
  764. }
  765. }
  766. }
  767. } else {
  768. throw new Exception("NO_AVAILABLE_TASK_ACTIONS");
  769. }
  770. }
  771. }
  772. $this->processMgr->stop();
  773. } else {
  774. log_register('WATCHDOG ALREADY RUNNING TASKS SKIPPED');
  775. }
  776. } else {
  777. log_register('WATCHDOG MAINTENANCE TASKS SKIPPED');
  778. }
  779. }
  780. }
  781. ?>