api.watchdoginterface.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565
  1. <?php
  2. /**
  3. * Watchdog tasks management and other interfaces
  4. */
  5. class WatchDogInterface {
  6. /**
  7. * Contains all watchdog tasks
  8. *
  9. * @var array
  10. */
  11. protected $allTasks = array();
  12. /**
  13. * Contains watchdog settings as key=>value
  14. *
  15. * @var array
  16. */
  17. protected $settings = array();
  18. /**
  19. * Contains previous watchdog alerts parsed from log
  20. *
  21. * @var array
  22. */
  23. protected $previousAlerts = array();
  24. /**
  25. * Contains available checktypes
  26. *
  27. * @var array
  28. */
  29. protected $checktypes = array();
  30. /**
  31. * Contains available operators
  32. *
  33. * @var array
  34. */
  35. protected $operators = array();
  36. const TASKID_EX = 'NO_REQUIRED_TASK_ID';
  37. const TASKADD_EX = 'MISSING_REQUIRED_OPTION';
  38. /**
  39. * load all watchdog tasks intoo private prop allTasks
  40. *
  41. * @return void
  42. */
  43. public function loadAllTasks() {
  44. $taskQuery = "SELECT * from `watchdog` ORDER BY `id` DESC;";
  45. $alltasks = simple_queryall($taskQuery);
  46. if (!empty($alltasks)) {
  47. foreach ($alltasks as $iz => $eachTask) {
  48. $this->allTasks[$eachTask['id']]['id'] = $eachTask['id'];
  49. $this->allTasks[$eachTask['id']]['active'] = $eachTask['active'];
  50. $this->allTasks[$eachTask['id']]['name'] = $eachTask['name'];
  51. $this->allTasks[$eachTask['id']]['checktype'] = $eachTask['checktype'];
  52. $this->allTasks[$eachTask['id']]['param'] = $eachTask['param'];
  53. $this->allTasks[$eachTask['id']]['operator'] = $eachTask['operator'];
  54. $this->allTasks[$eachTask['id']]['condition'] = $eachTask['condition'];
  55. $this->allTasks[$eachTask['id']]['action'] = $eachTask['action'];
  56. $this->allTasks[$eachTask['id']]['oldresult'] = $eachTask['oldresult'];
  57. }
  58. }
  59. }
  60. /**
  61. * load all watchdog previous alerts into private data prop
  62. *
  63. * @return void
  64. */
  65. public function loadAllPreviousAlerts() {
  66. //select year to load
  67. if (wf_CheckPost(array('alertsyearsel'))) {
  68. $curYear = vf($_POST['alertsyearsel'], 3);
  69. } else {
  70. $curYear = curyear();
  71. }
  72. $query = "SELECT `id`,`date`,`event` from `weblogs` WHERE `event` LIKE 'WATCHDOG NOTIFY THAT%' AND `date` LIKE '" . $curYear . "-%';";
  73. $all = simple_queryall($query);
  74. if (!empty($all)) {
  75. foreach ($all as $io => $each) {
  76. $this->previousAlerts[$each['id']]['id'] = $each['id'];
  77. $this->previousAlerts[$each['id']]['date'] = $each['date'];
  78. $event = str_replace('WATCHDOG NOTIFY THAT', '', $each['event']);
  79. $event = str_replace('`', '', $event);
  80. $this->previousAlerts[$each['id']]['event'] = $event;
  81. }
  82. }
  83. }
  84. /**
  85. * protected property allTasks getter
  86. *
  87. * @return array
  88. */
  89. public function getAllTasks() {
  90. $result = $this->allTasks;
  91. return ($result);
  92. }
  93. /**
  94. * Gets watchdog settings from database and load it into settings property
  95. * Also it sets default values into the database
  96. *
  97. * @return void
  98. */
  99. public function loadSettings() {
  100. $alert = zb_StorageGet('WATCHDOG_ALERT');
  101. if (empty($alert)) {
  102. $alert = __('Watchdog notifies that');
  103. zb_StorageSet('WATCHDOG_ALERT', $alert);
  104. }
  105. $phones = zb_StorageGet('WATCHDOG_PHONES');
  106. if (empty($phones)) {
  107. zb_StorageSet('WATCHDOG_PHONES', '');
  108. }
  109. $emails = zb_StorageGet('WATCHDOG_EMAILS');
  110. if (empty($emails)) {
  111. zb_StorageSet('WATCHDOG_EMAILS', '');
  112. }
  113. $telegramchats = zb_StorageGet('WATCHDOG_TELEGRAM');
  114. $maintenanceMode = zb_StorageGet('WATCHDOG_MAINTENANCE');
  115. $smsSilenceMode = zb_StorageGet('WATCHDOG_SMSSILENCE');
  116. $this->settings['WATCHDOG_ALERT'] = $alert;
  117. $this->settings['WATCHDOG_PHONES'] = $phones;
  118. $this->settings['WATCHDOG_EMAILS'] = $emails;
  119. $this->settings['WATCHDOG_TELEGRAM'] = $telegramchats;
  120. $this->settings['WATCHDOG_MAINTENANCE'] = $maintenanceMode;
  121. $this->settings['WATCHDOG_SMSSILENCE'] = $smsSilenceMode;
  122. $this->checktypes = array(
  123. 'icmpping' => 'icmpping',
  124. 'tcpping' => 'tcpping',
  125. 'udpping' => 'udpping',
  126. 'hopeping' => 'hopeping',
  127. 'script' => 'script',
  128. 'httpget' => 'httpget',
  129. 'getusertraff' => 'getusertraff',
  130. 'fileexists' => 'fileexists',
  131. 'opentickets' => 'opentickets',
  132. 'onepunch' => 'onepunch',
  133. 'snmpwalk' => 'snmpwalk',
  134. 'freediskspace' => 'freediskspace'
  135. );
  136. $this->operators = array(
  137. '=true' => '=true',
  138. '=false' => '=false',
  139. '==' => '==',
  140. '!=' => '!=',
  141. '>' => '>',
  142. '<' => '<',
  143. '>=' => '>=',
  144. '<=' => '<=',
  145. 'empty' => 'empty',
  146. 'notempty' => 'notempty',
  147. 'changed' => 'changed',
  148. 'notchanged' => 'notchanged',
  149. 'like' => 'like',
  150. 'notlike' => 'notlike',
  151. 'rised' => 'rised',
  152. 'decreased' => 'decreased'
  153. );
  154. }
  155. /**
  156. * Returns current watchdog settings
  157. *
  158. * @return array
  159. */
  160. public function getSettings() {
  161. $result = $this->settings;
  162. return ($result);
  163. }
  164. /**
  165. * shows all available tasks list
  166. *
  167. * @return string
  168. */
  169. public function listAllTasks() {
  170. $cells = wf_TableCell(__('ID'));
  171. $cells .= wf_TableCell(__('Active'));
  172. $cells .= wf_TableCell(__('Name'));
  173. $cells .= wf_TableCell(__('Check type'));
  174. $cells .= wf_TableCell(__('Parameter'));
  175. $cells .= wf_TableCell(__('Operator'));
  176. $cells .= wf_TableCell(__('Condition'));
  177. $cells .= wf_TableCell(__('Actions'));
  178. $cells .= wf_TableCell(__('Manage'));
  179. $rows = wf_TableRow($cells, 'row1');
  180. if (!empty($this->allTasks)) {
  181. foreach ($this->allTasks as $io => $eachtask) {
  182. $details = wf_tag('pre') . print_r($eachtask, true) . wf_tag('pre', true);
  183. $detailLink = wf_modal($eachtask['id'], $eachtask['name'], $details, '', '600', '400');
  184. $cells = wf_TableCell($detailLink, '', '', 'sorttable_customkey="' . $eachtask['id'] . '"');
  185. $cells .= wf_TableCell(web_bool_led($eachtask['active']), '', '', 'sorttable_customkey="' . $eachtask['active'] . '"');
  186. $cells .= wf_TableCell($eachtask['name']);
  187. $cells .= wf_TableCell($eachtask['checktype']);
  188. $cells .= wf_TableCell($eachtask['param']);
  189. $cells .= wf_TableCell($eachtask['operator']);
  190. $cells .= wf_TableCell($eachtask['condition']);
  191. $cells .= wf_TableCell($eachtask['action']);
  192. $controls = wf_JSAlert('?module=watchdog&delete=' . $eachtask['id'], web_delete_icon(), __('Removing this may lead to irreparable results'));
  193. $controls .= wf_JSAlert('?module=watchdog&edit=' . $eachtask['id'], web_edit_icon(), __('Are you serious'));
  194. $cells .= wf_TableCell($controls);
  195. $rows .= wf_tag('tr', false, 'row5');
  196. $rows .= $cells;
  197. $rows .= wf_tag('tr', true);
  198. }
  199. }
  200. $result = wf_TableBody($rows, '100%', '0', 'sortable');
  201. return ($result);
  202. }
  203. /**
  204. * shows new task creation form
  205. *
  206. * @return string
  207. */
  208. public function newTaskForm() {
  209. $inputs = wf_TextInput('newname', __('Name'), '', true);
  210. $inputs .= wf_Selector('newchecktype', $this->checktypes, __('Check type'), '', true);
  211. $inputs .= wf_TextInput('newparam', __('Parameter'), '', true);
  212. $inputs .= wf_Selector('newoperator', $this->operators, __('Operator'), '', true);
  213. $inputs .= wf_TextInput('newcondition', __('Condition'), '', true);
  214. $inputs .= wf_TextInput('newaction', __('Actions'), '', true);
  215. $inputs .= wf_CheckInput('newactive', __('Active'), true, true);
  216. $inputs .= wf_Submit(__('Create'));
  217. $form = wf_Form("", 'POST', $inputs, 'glamour');
  218. return ($form);
  219. }
  220. /**
  221. * shows modify form for some existing task
  222. *
  223. * @param int $taskID existing task id
  224. *
  225. * @return string
  226. */
  227. public function editTaskForm($taskID) {
  228. $taskID = vf($taskID, 3);
  229. if (empty($taskID)) {
  230. throw new Exception(self::TASKID_EX);
  231. }
  232. $inputs = wf_TextInput('editname', __('Name'), $this->allTasks[$taskID]['name'], true);
  233. $inputs .= wf_Selector('editchecktype', $this->checktypes, __('Check type'), $this->allTasks[$taskID]['checktype'], true);
  234. $inputs .= wf_TextInput('editparam', __('Parameter'), $this->allTasks[$taskID]['param'], true);
  235. $inputs .= wf_Selector('editoperator', $this->operators, __('Operator'), $this->allTasks[$taskID]['operator'], true);
  236. $inputs .= wf_TextInput('editcondition', __('Condition'), $this->allTasks[$taskID]['condition'], true);
  237. $inputs .= wf_TextInput('editaction', __('Actions'), $this->allTasks[$taskID]['action'], true);
  238. $inputs .= wf_CheckInput('editactive', __('Active'), true, $this->allTasks[$taskID]['active']);
  239. $inputs .= wf_Submit(__('Save'));
  240. $form = wf_Form("", 'POST', $inputs, 'glamour');
  241. $form .= wf_BackLink("?module=watchdog");
  242. return ($form);
  243. }
  244. /**
  245. * saves changes in the watchdog task as selected in editTaskForm
  246. *
  247. * @return void
  248. */
  249. public function changeTask() {
  250. $taskID = vf($_GET['edit'], 3);
  251. if (wf_CheckPost(array('editname', 'editaction', 'editparam'))) {
  252. if (!empty($taskID)) {
  253. if (isset($_POST['editactive'])) {
  254. $actFlag = 1;
  255. } else {
  256. $actFlag = 0;
  257. }
  258. simple_update_field('watchdog', 'name', $_POST['editname'], "WHERE `id`='" . $taskID . "'");
  259. simple_update_field('watchdog', 'checktype', $_POST['editchecktype'], "WHERE `id`='" . $taskID . "'");
  260. simple_update_field('watchdog', 'param', $_POST['editparam'], "WHERE `id`='" . $taskID . "'");
  261. simple_update_field('watchdog', 'operator', $_POST['editoperator'], "WHERE `id`='" . $taskID . "'");
  262. simple_update_field('watchdog', 'condition', $_POST['editcondition'], "WHERE `id`='" . $taskID . "'");
  263. simple_update_field('watchdog', 'action', $_POST['editaction'], "WHERE `id`='" . $taskID . "'");
  264. simple_update_field('watchdog', 'active', $actFlag, "WHERE `id`='" . $taskID . "'");
  265. log_register("WATCHDOG CHANGE TASK [" . $taskID . "] `" . $_POST['editname'] . "`");
  266. } else {
  267. throw new Exception(self::TASKID_EX);
  268. }
  269. } else {
  270. throw new Exception(self::TASKADD_EX);
  271. }
  272. }
  273. /**
  274. * delete some existing watchdog task
  275. *
  276. * @param int $taskID - existing task id
  277. *
  278. * @return void
  279. */
  280. public function deleteTask($taskID) {
  281. $taskID = vf($taskID, 3);
  282. if (empty($taskID)) {
  283. throw new Exception(self::TASKID_EX);
  284. }
  285. $query = "DELETE from `watchdog` WHERE `id`='" . $taskID . "'";
  286. nr_query($query);
  287. log_register("WATCHDOG DELETE TASK [" . $taskID . "]");
  288. }
  289. /**
  290. * creates new watchdog task
  291. *
  292. * @param string $name - task name
  293. * @param string $checktype - task check type
  294. * @param string $param - parameter
  295. * @param string $operator - operator
  296. * @param string $condition - condition for action
  297. * @param string $action - actions list
  298. * @param int $active - activity tinyint flag
  299. *
  300. * @return void
  301. */
  302. public function createTask($name, $checktype, $param, $operator, $condition, $action, $active = 0) {
  303. $active = mysql_real_escape_string($active);
  304. $name = mysql_real_escape_string($name);
  305. $checktype = mysql_real_escape_string($checktype);
  306. $param = mysql_real_escape_string($param);
  307. $operator = mysql_real_escape_string($operator);
  308. $condition = mysql_real_escape_string($condition);
  309. $action = mysql_real_escape_string($action);
  310. if ((empty($name)) OR ( empty($param)) OR ( empty($action))) {
  311. throw new Exception(self::TASKADD_EX);
  312. }
  313. $query = "INSERT INTO `watchdog` (`id` , `active` , `name` , `checktype` , `param` ,`operator` , `condition` ,`action` ,`oldresult`)
  314. VALUES (NULL , '" . $active . "', '" . $name . "', '" . $checktype . "', '" . $param . "', '" . $operator . "', '" . $condition . "', '" . $action . "', NULL);";
  315. nr_query($query);
  316. log_register("WATCHDOG CREATE TASK `" . $name . "`");
  317. }
  318. /**
  319. * Shows watchdog control panel
  320. *
  321. * @return string
  322. */
  323. public function panel() {
  324. $createWindow = $this->newTaskForm();
  325. $settingsWindow = $this->settingsForm();
  326. $result = '';
  327. $result .= wf_modalAuto(wf_img('skins/add_icon.png') . ' ' . __('Create new task'), __('Create new task'), $createWindow, 'ubButton');
  328. $result .= wf_Link("?module=watchdog", wf_img('skins/icon_search_small.gif') . ' ' . __('Show all tasks'), false, 'ubButton');
  329. $result .= wf_Link("?module=watchdog&manual=true", wf_img('skins/refresh.gif') . ' ' . __('Manual run'), false, 'ubButton');
  330. $result .= wf_Link("?module=watchdog&previousalerts=true", wf_img('skins/time_machine.png') . ' ' . __('Previous alerts'), false, 'ubButton');
  331. $result .= wf_modalAuto(wf_img('skins/settings.png') . ' ' . __('Settings'), __('Settings'), $settingsWindow, 'ubButton');
  332. return ($result);
  333. }
  334. /**
  335. * Sets maintenance mode state
  336. *
  337. * @param string $action
  338. *
  339. * @return void
  340. */
  341. public function setMaintenance($action) {
  342. if ($action == 'enable') {
  343. zb_StorageSet('WATCHDOG_MAINTENANCE', 'enabled');
  344. log_register('WATCHDOG MAINTENANCE ENABLED');
  345. }
  346. if ($action == 'disable') {
  347. zb_StorageDelete('WATCHDOG_MAINTENANCE');
  348. log_register('WATCHDOG MAINTENANCE DISABLED');
  349. }
  350. //update notification area
  351. $darkVoid = new DarkVoid();
  352. $darkVoid->flushCache();
  353. }
  354. /**
  355. * Sets SMS silince mode state
  356. *
  357. * @param string $action
  358. *
  359. * @return void
  360. */
  361. public function setSmsSilence($action) {
  362. if ($action == 'enable') {
  363. zb_StorageSet('WATCHDOG_SMSSILENCE', 'enabled');
  364. log_register('WATCHDOG SMSSILENCE ENABLED');
  365. }
  366. if ($action == 'disable') {
  367. zb_StorageDelete('WATCHDOG_SMSSILENCE');
  368. log_register('WATCHDOG SMSSILENCE DISABLED');
  369. }
  370. //update notification area
  371. $darkVoid = new DarkVoid();
  372. $darkVoid->flushCache();
  373. }
  374. /**
  375. * returns watchdog settings edit form
  376. *
  377. * @return string
  378. */
  379. public function settingsForm() {
  380. $result = '';
  381. $inputs = wf_TextInput('changealert', __('Watchdog alert text'), $this->settings['WATCHDOG_ALERT'], true, '30');
  382. $inputs .= wf_TextInput('changephones', __('Phone numbers to send alerts'), $this->settings['WATCHDOG_PHONES'], true, '30');
  383. $inputs .= wf_TextInput('changeemails', __('Emails to send alerts'), $this->settings['WATCHDOG_EMAILS'], true, '30');
  384. $inputs .= wf_TextInput('changetelegram', __('Telegram chat ids to send alerts'), $this->settings['WATCHDOG_TELEGRAM'], true, '30');
  385. $inputs .= wf_Submit(__('Save'));
  386. $result .= wf_Form("", 'POST', $inputs, 'glamour');
  387. if (cfr('ROOT')) {
  388. $result .= wf_tag('br');
  389. if (!$this->settings['WATCHDOG_MAINTENANCE']) {
  390. $result .= wf_Link('?module=watchdog&maintenance=enable', wf_img('skins/icon_ok.gif') . ' ' . __('Watchdog') . ': ' . __('Enabled'), false, 'ubButton');
  391. } else {
  392. $result .= wf_Link('?module=watchdog&maintenance=disable', wf_img('skins/icon_minus.png') . ' ' . __('Watchdog') . ': ' . __('Disabled'), false, 'ubButton');
  393. }
  394. $result .= ' ';
  395. if (!$this->settings['WATCHDOG_SMSSILENCE']) {
  396. $result .= wf_Link('?module=watchdog&smssilence=enable', wf_img('skins/icon_smsenabled.png') . ' ' . __('SMS silence') . ': ' . __('Disabled'), false, 'ubButton');
  397. } else {
  398. $result .= wf_Link('?module=watchdog&smssilence=disable', wf_img('skins/icon_smsdisabled.png') . ' ' . __('SMS silence') . ': ' . __('Enabled'), false, 'ubButton');
  399. }
  400. }
  401. return ($result);
  402. }
  403. /**
  404. * save the current settings of watchdog as it posted in settingsForm
  405. *
  406. * @return void
  407. */
  408. public function saveSettings() {
  409. if (wf_CheckPost(array('changealert'))) {
  410. zb_StorageSet('WATCHDOG_ALERT', $_POST['changealert']);
  411. zb_StorageSet('WATCHDOG_PHONES', $_POST['changephones']);
  412. zb_StorageSet('WATCHDOG_EMAILS', $_POST['changeemails']);
  413. zb_StorageSet('WATCHDOG_TELEGRAM', $_POST['changetelegram']);
  414. log_register("WATCHDOG SETTINGS CHANGED");
  415. }
  416. }
  417. /**
  418. * returns year selector to load alerts
  419. *
  420. * @return string
  421. */
  422. public function yearSelectorAlerts() {
  423. $inputs = wf_YearSelector('alertsyearsel', __('Year') . ' ', false);
  424. $inputs .= wf_Submit(__('Show'));
  425. $result = wf_Form("", 'POST', $inputs, 'glamour');
  426. $result .= wf_tag('br');
  427. return ($result);
  428. }
  429. /**
  430. * preprocess and return full calendar data for alerts report
  431. *
  432. * @retun string
  433. */
  434. public function renderAlertsCalendar() {
  435. $result = '';
  436. $controls = wf_TableCell($this->yearSelectorAlerts());
  437. $controls .= wf_TableCell($this->alertsSearchForm());
  438. $controls = wf_TableRow($controls);
  439. $result = wf_TableBody($controls, '60%', 0, '');
  440. if (!empty($this->previousAlerts)) {
  441. $calendarData = '';
  442. foreach ($this->previousAlerts as $io => $each) {
  443. $timestamp = strtotime($each['date']);
  444. $date = date("Y, n-1, j", $timestamp);
  445. $rawTime = date("H:i:s", $timestamp);
  446. $calendarData .= "
  447. {
  448. title: '" . $rawTime . ' ' . $each['event'] . "',
  449. start: new Date(" . $date . "),
  450. end: new Date(" . $date . "),
  451. className : 'undone'
  452. },
  453. ";
  454. }
  455. $result .= wf_FullCalendar($calendarData);
  456. } else {
  457. $result .= __('Nothing found');
  458. }
  459. return ($result);
  460. }
  461. /**
  462. * Returns previous alerts search form
  463. *
  464. * @return string
  465. */
  466. public function alertsSearchForm() {
  467. $result = '';
  468. $availTaskNames = array();
  469. if (!empty($this->allTasks)) {
  470. foreach ($this->allTasks as $io => $each) {
  471. $availTaskNames[$each['name']] = $each['name'];
  472. }
  473. }
  474. $inputs = wf_Selector('previousalertsearch', $availTaskNames, __('Name'), '', false);
  475. $inputs .= wf_Submit(__('Search'));
  476. $result = wf_Form("", 'POST', $inputs, 'glamour');
  477. return ($result);
  478. }
  479. /**
  480. * Returns previousa alerts search results
  481. *
  482. * @param string $request
  483. * @return string
  484. */
  485. public function alertSearchResults($request) {
  486. $result = $this->alertsSearchForm();
  487. $cells = wf_TableCell(__('Date'));
  488. $cells .= wf_TableCell(__('Event'));
  489. $rows = wf_TableRow($cells, 'row1');
  490. $counter = 0;
  491. if (!empty($this->previousAlerts)) {
  492. foreach ($this->previousAlerts as $io => $each) {
  493. if (ispos($each['event'], $request)) {
  494. $cells = wf_TableCell($each['date']);
  495. $cells .= wf_TableCell($each['event']);
  496. $rows .= wf_TableRow($cells, 'row3');
  497. $counter++;
  498. }
  499. }
  500. }
  501. $result .= wf_TableBody($rows, '100%', 0, 'sortable');
  502. $result .= __('Total') . ': ' . $counter;
  503. return ($result);
  504. }
  505. }