PageRenderTime 51ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 1ms

/zabbix-2.0.1/frontends/php/include/config.inc.php

#
PHP | 775 lines | 646 code | 93 blank | 36 comment | 116 complexity | 9d61db32263276b6de0275748c25c4d1 MD5 | raw file
Possible License(s): GPL-2.0, AGPL-1.0
  1. <?php
  2. /*
  3. ** Zabbix
  4. ** Copyright (C) 2000-2011 Zabbix SIA
  5. **
  6. ** This program is free software; you can redistribute it and/or modify
  7. ** it under the terms of the GNU General Public License as published by
  8. ** the Free Software Foundation; either version 2 of the License, or
  9. ** (at your option) any later version.
  10. **
  11. ** This program is distributed in the hope that it will be useful,
  12. ** but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. ** GNU General Public License for more details.
  15. **
  16. ** You should have received a copy of the GNU General Public License
  17. ** along with this program; if not, write to the Free Software
  18. ** Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  19. **/
  20. require_once dirname(__FILE__).'/classes/core/Z.php';
  21. Z::getInstance()->run();
  22. require_once dirname(__FILE__).'/debug.inc.php';
  23. require_once dirname(__FILE__).'/gettextwrapper.inc.php';
  24. require_once dirname(__FILE__).'/defines.inc.php';
  25. require_once dirname(__FILE__).'/func.inc.php';
  26. require_once dirname(__FILE__).'/html.inc.php';
  27. CProfiler::getInstance()->start();
  28. require_once dirname(__FILE__).'/profiles.inc.php';
  29. require_once dirname(__FILE__).'/../conf/maintenance.inc.php';
  30. // abc sorting
  31. require_once dirname(__FILE__).'/acknow.inc.php';
  32. require_once dirname(__FILE__).'/actions.inc.php';
  33. require_once dirname(__FILE__).'/discovery.inc.php';
  34. require_once dirname(__FILE__).'/events.inc.php';
  35. require_once dirname(__FILE__).'/graphs.inc.php';
  36. require_once dirname(__FILE__).'/hosts.inc.php';
  37. require_once dirname(__FILE__).'/httptest.inc.php';
  38. require_once dirname(__FILE__).'/ident.inc.php';
  39. require_once dirname(__FILE__).'/images.inc.php';
  40. require_once dirname(__FILE__).'/items.inc.php';
  41. require_once dirname(__FILE__).'/maintenances.inc.php';
  42. require_once dirname(__FILE__).'/maps.inc.php';
  43. require_once dirname(__FILE__).'/media.inc.php';
  44. require_once dirname(__FILE__).'/nodes.inc.php';
  45. require_once dirname(__FILE__).'/services.inc.php';
  46. require_once dirname(__FILE__).'/sounds.inc.php';
  47. require_once dirname(__FILE__).'/triggers.inc.php';
  48. require_once dirname(__FILE__).'/users.inc.php';
  49. require_once dirname(__FILE__).'/valuemap.inc.php';
  50. global $USER_DETAILS, $USER_RIGHTS, $ZBX_PAGE_POST_JS, $page;
  51. global $ZBX_LOCALNODEID, $ZBX_LOCMASTERID, $ZBX_CONFIGURATION_FILE, $DB;
  52. global $ZBX_SERVER, $ZBX_SERVER_PORT;
  53. global $ZBX_LOCALES;
  54. $page = array();
  55. $USER_DETAILS = array();
  56. $USER_RIGHTS = array();
  57. $ZBX_LOCALNODEID = 0;
  58. $ZBX_LOCMASTERID = 0;
  59. $ZBX_CONFIGURATION_FILE = '/etc/zabbix/web/zabbix.conf.php';
  60. // include tactical overview modules
  61. require_once dirname(__FILE__).'/locales.inc.php';
  62. require_once dirname(__FILE__).'/perm.inc.php';
  63. require_once dirname(__FILE__).'/audit.inc.php';
  64. require_once dirname(__FILE__).'/js.inc.php';
  65. // include validation
  66. require_once dirname(__FILE__).'/validate.inc.php';
  67. function zbx_err_handler($errno, $errstr, $errfile, $errline) {
  68. $pathLength = strlen(__FILE__);
  69. $pathLength -= 22;
  70. $errfile = substr($errfile, $pathLength);
  71. error($errstr.' ['.$errfile.':'.$errline.']');
  72. }
  73. /*
  74. * start initialization
  75. */
  76. set_error_handler('zbx_err_handler');
  77. unset($show_setup);
  78. if (defined('ZBX_DENY_GUI_ACCESS')) {
  79. if (isset($ZBX_GUI_ACCESS_IP_RANGE) && is_array($ZBX_GUI_ACCESS_IP_RANGE)) {
  80. $user_ip = (isset($_SERVER['HTTP_X_FORWARDED_FOR']) && !empty($_SERVER['HTTP_X_FORWARDED_FOR'])) ? $_SERVER['HTTP_X_FORWARDED_FOR'] : $_SERVER['REMOTE_ADDR'];
  81. if (!str_in_array($user_ip, $ZBX_GUI_ACCESS_IP_RANGE)) {
  82. $DENY_GUI = true;
  83. }
  84. }
  85. else {
  86. $DENY_GUI = true;
  87. }
  88. }
  89. if (file_exists($ZBX_CONFIGURATION_FILE) && !isset($_COOKIE['ZBX_CONFIG']) && !isset($DENY_GUI)) {
  90. $config = new CConfigFile($ZBX_CONFIGURATION_FILE);
  91. if ($config->load()) {
  92. $config->makeGlobal();
  93. }
  94. else {
  95. $show_warning = true;
  96. define('ZBX_DISTRIBUTED', false);
  97. if (!defined('ZBX_PAGE_NO_AUTHORIZATION')) {
  98. define('ZBX_PAGE_NO_AUTHORIZATION', true);
  99. }
  100. error($config->error);
  101. }
  102. require_once dirname(__FILE__).'/db.inc.php';
  103. if (!isset($show_warning)) {
  104. $error = '';
  105. if (!DBconnect($error)) {
  106. $_REQUEST['message'] = $error;
  107. define('ZBX_DISTRIBUTED', false);
  108. if (!defined('ZBX_PAGE_NO_AUTHORIZATION')) {
  109. define('ZBX_PAGE_NO_AUTHORIZATION', true);
  110. }
  111. $show_warning = true;
  112. }
  113. else {
  114. global $ZBX_LOCALNODEID, $ZBX_LOCMASTERID;
  115. // init LOCAL NODE ID
  116. if ($local_node_data = DBfetch(DBselect('SELECT n.* FROM nodes n WHERE n.nodetype=1 ORDER BY n.nodeid'))) {
  117. $ZBX_LOCALNODEID = $local_node_data['nodeid'];
  118. $ZBX_LOCMASTERID = $local_node_data['masterid'];
  119. $ZBX_NODES[$local_node_data['nodeid']] = $local_node_data;
  120. define('ZBX_DISTRIBUTED', true);
  121. }
  122. else {
  123. define('ZBX_DISTRIBUTED', false);
  124. }
  125. unset($local_node_data);
  126. }
  127. unset($error);
  128. }
  129. }
  130. else {
  131. if (file_exists($ZBX_CONFIGURATION_FILE)) {
  132. ob_start();
  133. include $ZBX_CONFIGURATION_FILE;
  134. ob_end_clean();
  135. }
  136. require_once dirname(__FILE__).'/db.inc.php';
  137. if (!defined('ZBX_PAGE_NO_AUTHORIZATION')) {
  138. define('ZBX_PAGE_NO_AUTHORIZATION', true);
  139. }
  140. define('ZBX_DISTRIBUTED', false);
  141. $show_setup = true;
  142. }
  143. if (!defined('ZBX_PAGE_NO_AUTHORIZATION') && !defined('ZBX_RPC_REQUEST')) {
  144. if (!CWebUser::checkAuthentication(get_cookie('zbx_sessionid'))) {
  145. include('index.php');
  146. exit();
  147. }
  148. if (function_exists('bindtextdomain')) {
  149. // initializing gettext translations depending on language selected by user
  150. $locales = zbx_locale_variants(CWebUser::$data['lang']);
  151. $locale_found = false;
  152. foreach ($locales as $locale) {
  153. putenv('LC_ALL='.$locale);
  154. putenv('LANG='.$locale);
  155. putenv('LANGUAGE='.$locale);
  156. if (setlocale(LC_ALL, $locale)) {
  157. $locale_found = true;
  158. CWebUser::$data['locale'] = $locale;
  159. break;
  160. }
  161. }
  162. if (!$locale_found && CWebUser::$data['lang'] != 'en_GB' && CWebUser::$data['lang'] != 'en_gb') {
  163. error('Locale for language "'.CWebUser::$data['lang'].'" is not found on the web server. Tried to set: '.implode(', ', $locales).'. Unable to translate Zabbix interface.');
  164. }
  165. bindtextdomain('frontend', 'locale');
  166. bind_textdomain_codeset('frontend', 'UTF-8');
  167. textdomain('frontend');
  168. }
  169. else {
  170. error('Your PHP has no gettext support. Zabbix translations are not available.');
  171. }
  172. // numeric Locale to default
  173. setlocale(LC_NUMERIC, array('C', 'POSIX', 'en', 'en_US', 'en_US.UTF-8', 'English_United States.1252', 'en_GB', 'en_GB.UTF-8'));
  174. }
  175. else {
  176. CWebUser::setDefault();
  177. }
  178. set_zbx_locales();
  179. // init mb strings if it's available
  180. init_mbstrings();
  181. // ajax - do not need warnings or errors
  182. if ((isset($DENY_GUI) || isset($show_setup) || isset($show_warning)) && PAGE_TYPE_HTML <> detect_page_type()) {
  183. header('Ajax-response: false');
  184. exit();
  185. }
  186. if (isset($DENY_GUI)) {
  187. unset($show_warning);
  188. require_once dirname(__FILE__).'/../warning.php';
  189. }
  190. if (isset($show_setup)) {
  191. unset($show_setup);
  192. require_once dirname(__FILE__).'/../setup.php';
  193. }
  194. elseif (isset($show_warning)) {
  195. unset($show_warning);
  196. require_once dirname(__FILE__).'/../warning.php';
  197. }
  198. function access_deny() {
  199. require_once dirname(__FILE__).'/page_header.php';
  200. if (CWebUser::$data['alias'] != ZBX_GUEST_USER) {
  201. show_error_message(_('No permissions to referred object or it does not exist!'));
  202. }
  203. else {
  204. $url = new CUrl($_SERVER['REQUEST_URI']);
  205. $url->setArgument('sid', null);
  206. $url = urlencode($url->toString());
  207. $warning = new CWarning(_('You are not logged in.'), array(
  208. _('You cannot view this URL as a'),
  209. SPACE,
  210. bold(ZBX_GUEST_USER),
  211. '. ',
  212. _('You must login to view this page.'),
  213. BR(),
  214. _('If you think this message is wrong, please consult your administrators about getting the necessary permissions.')
  215. ));
  216. $warning->setButtons(array(
  217. new CButton('login', _('Login'), 'javascript: document.location = "index.php?request='.$url.'";', 'formlist'),
  218. new CButton('back', _('Cancel'), 'javascript: window.history.back();', 'formlist')
  219. ));
  220. $warning->show();
  221. }
  222. require_once dirname(__FILE__).'/page_footer.php';
  223. }
  224. function detect_page_type($default = PAGE_TYPE_HTML) {
  225. if (isset($_REQUEST['output'])) {
  226. switch (strtolower($_REQUEST['output'])) {
  227. case 'text':
  228. return PAGE_TYPE_TEXT;
  229. break;
  230. case 'ajax':
  231. return PAGE_TYPE_JS;
  232. break;
  233. case 'json':
  234. return PAGE_TYPE_JSON;
  235. break;
  236. case 'json-rpc':
  237. return PAGE_TYPE_JSON_RPC;
  238. break;
  239. case 'html':
  240. return PAGE_TYPE_HTML_BLOCK;
  241. break;
  242. case 'img':
  243. return PAGE_TYPE_IMAGE;
  244. break;
  245. case 'css':
  246. return PAGE_TYPE_CSS;
  247. break;
  248. }
  249. }
  250. return $default;
  251. }
  252. function show_messages($bool = true, $okmsg = null, $errmsg = null) {
  253. global $page, $ZBX_MESSAGES;
  254. if (!defined('PAGE_HEADER_LOADED')) {
  255. return null;
  256. }
  257. if (defined('ZBX_API_REQUEST')) {
  258. return null;
  259. }
  260. if (!isset($page['type'])) {
  261. $page['type'] = PAGE_TYPE_HTML;
  262. }
  263. $message = array();
  264. $width = 0;
  265. $height= 0;
  266. if (!$bool && !is_null($errmsg)) {
  267. $msg = _('ERROR').': '.$errmsg;
  268. }
  269. elseif ($bool && !is_null($okmsg)) {
  270. $msg = $okmsg;
  271. }
  272. if (isset($msg)) {
  273. switch ($page['type']) {
  274. case PAGE_TYPE_IMAGE:
  275. array_push($message, array(
  276. 'text' => $msg,
  277. 'color' => (!$bool) ? array('R' => 255, 'G' => 0, 'B' => 0) : array('R' => 34, 'G' => 51, 'B' => 68),
  278. 'font' => 2
  279. ));
  280. $width = max($width, imagefontwidth(2) * zbx_strlen($msg) + 1);
  281. $height += imagefontheight(2) + 1;
  282. break;
  283. case PAGE_TYPE_XML:
  284. echo htmlspecialchars($msg)."\n";
  285. break;
  286. case PAGE_TYPE_HTML:
  287. default:
  288. $msg_tab = new CTable($msg, ($bool ? 'msgok' : 'msgerr'));
  289. $msg_tab->setCellPadding(0);
  290. $msg_tab->setCellSpacing(0);
  291. $row = array();
  292. $msg_col = new CCol(bold($msg), 'msg_main msg');
  293. $msg_col->setAttribute('id', 'page_msg');
  294. $row[] = $msg_col;
  295. if (isset($ZBX_MESSAGES) && !empty($ZBX_MESSAGES)) {
  296. $msg_details = new CDiv(_('Details'), 'blacklink');
  297. $msg_details->setAttribute('onclick', 'javascript: showHide("msg_messages", IE ? "block" : "table");');
  298. $msg_details->setAttribute('title', _('Maximize').'/'._('Minimize'));
  299. array_unshift($row, new CCol($msg_details, 'clr'));
  300. }
  301. $msg_tab->addRow($row);
  302. $msg_tab->show();
  303. break;
  304. }
  305. }
  306. if (isset($ZBX_MESSAGES) && !empty($ZBX_MESSAGES)) {
  307. if ($page['type'] == PAGE_TYPE_IMAGE) {
  308. $msg_font = 2;
  309. foreach ($ZBX_MESSAGES as $msg) {
  310. if ($msg['type'] == 'error') {
  311. array_push($message, array(
  312. 'text' => $msg['message'],
  313. 'color' => array('R' => 255, 'G' => 55, 'B' => 55),
  314. 'font' => $msg_font
  315. ));
  316. }
  317. else {
  318. array_push($message, array(
  319. 'text' => $msg['message'],
  320. 'color' => array('R' => 155, 'G' => 155, 'B' => 55),
  321. 'font' => $msg_font
  322. ));
  323. }
  324. $width = max($width, imagefontwidth($msg_font) * zbx_strlen($msg['message']) + 1);
  325. $height += imagefontheight($msg_font) + 1;
  326. }
  327. }
  328. elseif ($page['type'] == PAGE_TYPE_XML) {
  329. foreach ($ZBX_MESSAGES as $msg) {
  330. echo '['.$msg['type'].'] '.$msg['message']."\n";
  331. }
  332. }
  333. else {
  334. $lst_error = new CList(null,'messages');
  335. foreach ($ZBX_MESSAGES as $msg) {
  336. $lst_error->addItem($msg['message'], $msg['type']);
  337. $bool = ($bool && 'error' != zbx_strtolower($msg['type']));
  338. }
  339. $msg_show = 6;
  340. $msg_count = count($ZBX_MESSAGES);
  341. if ($msg_count > $msg_show) {
  342. $msg_count = $msg_show * 16;
  343. $lst_error->setAttribute('style', 'height: '.$msg_count.'px;');
  344. }
  345. $tab = new CTable(null, ($bool ? 'msgok' : 'msgerr'));
  346. $tab->setCellPadding(0);
  347. $tab->setCellSpacing(0);
  348. $tab->setAttribute('id', 'msg_messages');
  349. $tab->setAttribute('style', 'width: 100%;');
  350. if (isset($msg_tab) && $bool) {
  351. $tab->setAttribute('style', 'display: none;');
  352. }
  353. $tab->addRow(new CCol($lst_error, 'msg'));
  354. $tab->show();
  355. }
  356. $ZBX_MESSAGES = null;
  357. }
  358. if ($page['type'] == PAGE_TYPE_IMAGE && count($message) > 0) {
  359. $width += 2;
  360. $height += 2;
  361. $canvas = imagecreate($width, $height);
  362. imagefilledrectangle($canvas, 0, 0, $width, $height, imagecolorallocate($canvas, 255, 255, 255));
  363. foreach ($message as $id => $msg) {
  364. $message[$id]['y'] = 1 + (isset($previd) ? $message[$previd]['y'] + $message[$previd]['h'] : 0);
  365. $message[$id]['h'] = imagefontheight($msg['font']);
  366. imagestring(
  367. $canvas,
  368. $msg['font'],
  369. 1,
  370. $message[$id]['y'],
  371. $msg['text'],
  372. imagecolorallocate($canvas, $msg['color']['R'], $msg['color']['G'], $msg['color']['B'])
  373. );
  374. $previd = $id;
  375. }
  376. imageOut($canvas);
  377. imagedestroy($canvas);
  378. }
  379. }
  380. function show_message($msg) {
  381. show_messages(true, $msg, '');
  382. }
  383. function show_error_message($msg) {
  384. show_messages(false, '', $msg);
  385. }
  386. function info($msgs) {
  387. global $ZBX_MESSAGES;
  388. zbx_value2array($msgs);
  389. if (is_null($ZBX_MESSAGES)) {
  390. $ZBX_MESSAGES = array();
  391. }
  392. foreach ($msgs as $msg) {
  393. array_push($ZBX_MESSAGES, array('type' => 'info', 'message' => $msg));
  394. }
  395. }
  396. function error($msgs) {
  397. global $ZBX_MESSAGES;
  398. if (is_null($ZBX_MESSAGES)) {
  399. $ZBX_MESSAGES = array();
  400. }
  401. $msgs = zbx_toArray($msgs);
  402. foreach ($msgs as $msg) {
  403. if (isset(CWebUser::$data['debug_mode']) && !is_object($msg) && !CWebUser::$data['debug_mode']) {
  404. $msg = preg_replace('/^\[.+?::.+?\]/', '', $msg);
  405. }
  406. array_push($ZBX_MESSAGES, array('type' => 'error', 'message' => $msg));
  407. }
  408. }
  409. function clear_messages($count = null) {
  410. global $ZBX_MESSAGES;
  411. $result = array();
  412. if (!is_null($count)) {
  413. while ($count-- > 0) {
  414. array_unshift($result, array_pop($ZBX_MESSAGES));
  415. }
  416. }
  417. else {
  418. $result = $ZBX_MESSAGES;
  419. $ZBX_MESSAGES = null;
  420. }
  421. return $result;
  422. }
  423. function fatal_error($msg) {
  424. require_once dirname(__FILE__).'/page_header.php';
  425. show_error_message($msg);
  426. require_once dirname(__FILE__).'/page_footer.php';
  427. }
  428. function get_tree_by_parentid($parentid, &$tree, $parent_field, $level = 0) {
  429. if (empty($tree)) {
  430. return $tree;
  431. }
  432. $level++;
  433. if ($level > 32) {
  434. return array();
  435. }
  436. $result = array();
  437. if (isset($tree[$parentid])) {
  438. $result[$parentid] = $tree[$parentid];
  439. }
  440. $tree_ids = array_keys($tree);
  441. foreach ($tree_ids as $key => $id) {
  442. $child = $tree[$id];
  443. if (bccomp($child[$parent_field], $parentid) == 0) {
  444. $result[$id] = $child;
  445. $childs = get_tree_by_parentid($id, $tree, $parent_field, $level); // attention recursion !!!
  446. $result += $childs;
  447. }
  448. }
  449. return $result;
  450. }
  451. function parse_period($str) {
  452. $out = null;
  453. $str = trim($str, ';');
  454. $periods = explode(';', $str);
  455. foreach ($periods as $period) {
  456. if (!preg_match('/^([1-7])-([1-7]),([0-9]{1,2}):([0-9]{1,2})-([0-9]{1,2}):([0-9]{1,2})$/', $period, $arr)) {
  457. return null;
  458. }
  459. for ($i = $arr[1]; $i <= $arr[2]; $i++) {
  460. if (!isset($out[$i])) {
  461. $out[$i] = array();
  462. }
  463. array_push($out[$i], array(
  464. 'start_h' => $arr[3],
  465. 'start_m' => $arr[4],
  466. 'end_h' => $arr[5],
  467. 'end_m' => $arr[6]
  468. ));
  469. }
  470. }
  471. return $out;
  472. }
  473. function get_status() {
  474. $status = array(
  475. 'triggers_count' => 0,
  476. 'triggers_count_enabled' => 0,
  477. 'triggers_count_disabled' => 0,
  478. 'triggers_count_off' => 0,
  479. 'triggers_count_on' => 0,
  480. 'triggers_count_unknown' => 0,
  481. 'items_count' => 0,
  482. 'items_count_monitored' => 0,
  483. 'items_count_disabled' => 0,
  484. 'items_count_not_supported' => 0,
  485. 'hosts_count' => 0,
  486. 'hosts_count_monitored' => 0,
  487. 'hosts_count_not_monitored' => 0,
  488. 'hosts_count_template' => 0,
  489. 'users_online' => 0,
  490. 'qps_total' => 0
  491. );
  492. // server
  493. $status['zabbix_server'] = zabbixIsRunning() ? _('Yes') : _('No');
  494. // triggers
  495. $dbTriggers = DBselect('SELECT COUNT(DISTINCT t.triggerid) AS cnt,t.status,t.value'.
  496. ' FROM triggers t'.
  497. ' INNER JOIN functions f ON t.triggerid=f.triggerid'.
  498. ' INNER JOIN items i ON f.itemid=i.itemid'.
  499. ' INNER JOIN hosts h ON i.hostid=h.hostid'.
  500. ' WHERE i.status='.ITEM_STATUS_ACTIVE.
  501. ' AND h.status='.HOST_STATUS_MONITORED.
  502. ' GROUP BY t.status,t.value');
  503. while ($dbTrigger = DBfetch($dbTriggers)) {
  504. switch ($dbTrigger['status']) {
  505. case TRIGGER_STATUS_ENABLED:
  506. switch ($dbTrigger['value']) {
  507. case TRIGGER_VALUE_FALSE:
  508. $status['triggers_count_off'] = $dbTrigger['cnt'];
  509. break;
  510. case TRIGGER_VALUE_TRUE:
  511. $status['triggers_count_on'] = $dbTrigger['cnt'];
  512. break;
  513. case TRIGGER_VALUE_UNKNOWN:
  514. $status['triggers_count_unknown'] = $dbTrigger['cnt'];
  515. break;
  516. }
  517. break;
  518. case TRIGGER_STATUS_DISABLED:
  519. $status['triggers_count_disabled'] += $dbTrigger['cnt'];
  520. break;
  521. }
  522. }
  523. $status['triggers_count_enabled'] = $status['triggers_count_off'] + $status['triggers_count_on']
  524. + $status['triggers_count_unknown'];
  525. $status['triggers_count'] = $status['triggers_count_enabled'] + $status['triggers_count_disabled'];
  526. // items
  527. $dbItems = DBselect('SELECT COUNT(*) AS cnt,i.status'.
  528. ' FROM items i'.
  529. ' INNER JOIN hosts h ON i.hostid=h.hostid'.
  530. ' WHERE h.status='.HOST_STATUS_MONITORED.
  531. ' AND '.DBcondition('i.status', array(ITEM_STATUS_ACTIVE, ITEM_STATUS_DISABLED, ITEM_STATUS_NOTSUPPORTED)).
  532. ' GROUP BY i.status');
  533. while ($dbItem = DBfetch($dbItems)) {
  534. switch ($dbItem['status']) {
  535. case ITEM_STATUS_ACTIVE:
  536. $status['items_count_monitored'] = $dbItem['cnt'];
  537. break;
  538. case ITEM_STATUS_DISABLED:
  539. $status['items_count_disabled'] = $dbItem['cnt'];
  540. break;
  541. case ITEM_STATUS_NOTSUPPORTED:
  542. $status['items_count_not_supported'] = $dbItem['cnt'];
  543. break;
  544. }
  545. }
  546. $status['items_count'] = $status['items_count_monitored'] + $status['items_count_disabled']
  547. + $status['items_count_not_supported'];
  548. // hosts
  549. $dbHosts = DBselect('SELECT COUNT(*) AS cnt,h.status'.
  550. ' FROM hosts h'.
  551. ' WHERE h.status IN ('.HOST_STATUS_MONITORED.','.HOST_STATUS_NOT_MONITORED.','.HOST_STATUS_TEMPLATE.' )'.
  552. ' GROUP BY h.status');
  553. while ($dbHost = DBfetch($dbHosts)) {
  554. switch ($dbHost['status']) {
  555. case HOST_STATUS_MONITORED:
  556. $status['hosts_count_monitored'] = $dbHost['cnt'];
  557. break;
  558. case HOST_STATUS_NOT_MONITORED:
  559. $status['hosts_count_not_monitored'] = $dbHost['cnt'];
  560. break;
  561. case HOST_STATUS_TEMPLATE:
  562. $status['hosts_count_template'] = $dbHost['cnt'];
  563. break;
  564. }
  565. }
  566. $status['hosts_count'] = $status['hosts_count_monitored'] + $status['hosts_count_not_monitored']
  567. + $status['hosts_count_template'];
  568. // users
  569. $row = DBfetch(DBselect('SELECT COUNT(*) AS usr_cnt FROM users u WHERE '.DBin_node('u.userid')));
  570. $status['users_count'] = $row['usr_cnt'];
  571. $status['users_online'] = 0;
  572. $db_sessions = DBselect(
  573. 'SELECT s.userid,s.status,MAX(s.lastaccess) AS lastaccess'.
  574. ' FROM sessions s'.
  575. ' WHERE '.DBin_node('s.userid').
  576. ' AND s.status='.ZBX_SESSION_ACTIVE.
  577. ' GROUP BY s.userid,s.status'
  578. );
  579. while ($session = DBfetch($db_sessions)) {
  580. if (($session['lastaccess'] + ZBX_USER_ONLINE_TIME) >= time()) {
  581. $status['users_online']++;
  582. }
  583. }
  584. // comments: !!! Don't forget sync code with C !!!
  585. $row = DBfetch(DBselect(
  586. 'SELECT SUM(1.0/i.delay) AS qps'.
  587. ' FROM items i,hosts h'.
  588. ' WHERE i.status='.ITEM_STATUS_ACTIVE.
  589. ' AND i.hostid=h.hostid'.
  590. ' AND h.status='.HOST_STATUS_MONITORED.
  591. ' AND i.delay<>0'
  592. ));
  593. $status['qps_total'] = round($row['qps'], 2);
  594. return $status;
  595. }
  596. function zabbixIsRunning() {
  597. global $ZBX_SERVER, $ZBX_SERVER_PORT;
  598. if (empty($ZBX_SERVER) || empty ($ZBX_SERVER_PORT)) {
  599. return false;
  600. }
  601. $result = (bool) fsockopen($ZBX_SERVER, $ZBX_SERVER_PORT, $errnum, $errstr, ZBX_SOCKET_TIMEOUT);
  602. if (!$result) {
  603. clear_messages();
  604. }
  605. return $result;
  606. }
  607. function set_image_header($format = null) {
  608. global $IMAGE_FORMAT_DEFAULT;
  609. if (is_null($format)) {
  610. $format = $IMAGE_FORMAT_DEFAULT;
  611. }
  612. if (IMAGE_FORMAT_JPEG == $format) {
  613. header('Content-type: image/jpeg');
  614. }
  615. if (IMAGE_FORMAT_TEXT == $format) {
  616. header('Content-type: text/html');
  617. }
  618. else {
  619. header('Content-type: image/png');
  620. }
  621. header('Expires: Mon, 17 Aug 1998 12:51:50 GMT');
  622. }
  623. function imageOut(&$image, $format = null) {
  624. global $page, $IMAGE_FORMAT_DEFAULT;
  625. if (is_null($format)) {
  626. $format = $IMAGE_FORMAT_DEFAULT;
  627. }
  628. ob_start();
  629. if (IMAGE_FORMAT_JPEG == $format) {
  630. imagejpeg($image);
  631. }
  632. else {
  633. imagepng($image);
  634. }
  635. $imageSource = ob_get_contents();
  636. ob_end_clean();
  637. if ($page['type'] != PAGE_TYPE_IMAGE) {
  638. session_start();
  639. $imageId = md5(strlen($imageSource));
  640. $_SESSION['image_id'] = array();
  641. $_SESSION['image_id'][$imageId] = $imageSource;
  642. session_write_close();
  643. }
  644. switch ($page['type']) {
  645. case PAGE_TYPE_IMAGE:
  646. echo $imageSource;
  647. break;
  648. case PAGE_TYPE_JSON:
  649. $json = new CJSON();
  650. echo $json->encode(array('result' => $imageId));
  651. break;
  652. case PAGE_TYPE_TEXT:
  653. default:
  654. echo $imageId;
  655. }
  656. }
  657. function encode_log($data) {
  658. return (defined('ZBX_LOG_ENCODING_DEFAULT') && function_exists('mb_convert_encoding'))
  659. ? mb_convert_encoding($data, _('UTF-8'), ZBX_LOG_ENCODING_DEFAULT)
  660. : $data;
  661. }
  662. // function used in defines, so can't move it to func.inc.php
  663. function zbx_stripslashes($value) {
  664. if (is_array($value)) {
  665. foreach ($value as $id => $data) {
  666. $value[$id] = zbx_stripslashes($data);
  667. }
  668. }
  669. elseif (is_string($value)) {
  670. $value = stripslashes($value);
  671. }
  672. return $value;
  673. }
  674. function no_errors() {
  675. global $ZBX_MESSAGES;
  676. foreach ($ZBX_MESSAGES as $message) {
  677. if ($message['type'] == 'error') {
  678. return false;
  679. }
  680. }
  681. return true;
  682. }