PageRenderTime 27ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

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

#
PHP | 349 lines | 274 code | 54 blank | 21 comment | 58 complexity | 3e1139221094da39f79a5211aea9af9c 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. function update_node_profile($nodeids) {
  21. DBstart();
  22. DBexecute('DELETE FROM profiles WHERE userid='.CWebUser::$data['userid'].' AND idx='.zbx_dbstr('web.nodes.selected'));
  23. foreach ($nodeids as $nodeid) {
  24. DBexecute('INSERT INTO profiles (profileid,userid,idx,value_id,type)'.
  25. ' VALUES ('.get_dbid('profiles', 'profileid').','.CWebUser::$data['userid'].','
  26. .zbx_dbstr('web.nodes.selected').','.$nodeid.',4)');
  27. }
  28. DBend();
  29. }
  30. function get_node_profile($default = null) {
  31. $result = array();
  32. $db_profiles = DBselect(
  33. 'SELECT p.value_id'.
  34. ' FROM profiles p'.
  35. ' WHERE p.userid='.CWebUser::$data['userid'].
  36. ' AND p.idx='.zbx_dbstr('web.nodes.selected')
  37. );
  38. while ($profile = DBfetch($db_profiles)) {
  39. $result[] = $profile['value_id'];
  40. }
  41. return (empty($result) ? $default : $result);
  42. }
  43. function init_nodes() {
  44. // init current node id
  45. if (defined('ZBX_NODES_INITIALIZED')) {
  46. return null;
  47. }
  48. global $ZBX_LOCALNODEID, $ZBX_LOCMASTERID, $ZBX_CURRENT_NODEID, $ZBX_CURMASTERID, $ZBX_NODES, $ZBX_NODES_IDS,
  49. $ZBX_AVAILABLE_NODES, $ZBX_VIEWED_NODES, $ZBX_WITH_ALL_NODES;
  50. $ZBX_AVAILABLE_NODES = array();
  51. $ZBX_NODES_IDS = array();
  52. $ZBX_NODES = array();
  53. $ZBX_CURRENT_NODEID = $ZBX_LOCALNODEID;
  54. $ZBX_WITH_ALL_NODES = !defined('ZBX_NOT_ALLOW_ALL_NODES');
  55. if (!defined('ZBX_PAGE_NO_AUTHORIZATION') && ZBX_DISTRIBUTED) {
  56. if (CWebUser::$data['type'] == USER_TYPE_SUPER_ADMIN) {
  57. $sql = 'SELECT DISTINCT n.nodeid,n.name,n.masterid FROM nodes n';
  58. }
  59. else {
  60. $sql = 'SELECT DISTINCT n.nodeid,n.name,n.masterid'.
  61. ' FROM nodes n,groups hg,rights r,users_groups g'.
  62. ' WHERE r.id=hg.groupid'.
  63. ' AND r.groupid=g.usrgrpid'.
  64. ' AND g.userid='.CWebUser::$data['userid'].
  65. ' AND n.nodeid='.DBid2nodeid('hg.groupid');
  66. }
  67. $db_nodes = DBselect($sql);
  68. while ($node = DBfetch($db_nodes)) {
  69. $ZBX_NODES[$node['nodeid']] = $node;
  70. $ZBX_NODES_IDS[$node['nodeid']] = $node['nodeid'];
  71. }
  72. $ZBX_AVAILABLE_NODES = get_accessible_nodes_by_user(CWebUser::$data, PERM_READ_LIST, PERM_RES_IDS_ARRAY, $ZBX_NODES_IDS);
  73. $ZBX_VIEWED_NODES = get_viewed_nodes();
  74. $ZBX_CURRENT_NODEID = $ZBX_VIEWED_NODES['selected'];
  75. if ($node_data = DBfetch(DBselect('SELECT n.masterid FROM nodes n WHERE n.nodeid='.$ZBX_CURRENT_NODEID))) {
  76. $ZBX_CURMASTERID = $node_data['masterid'];
  77. }
  78. if (!isset($ZBX_NODES[$ZBX_CURRENT_NODEID])) {
  79. $ZBX_CURRENT_NODEID = $ZBX_LOCALNODEID;
  80. $ZBX_CURMASTERID = $ZBX_LOCMASTERID;
  81. }
  82. if (isset($_REQUEST['select_nodes'])) {
  83. update_node_profile($ZBX_VIEWED_NODES['nodeids']);
  84. }
  85. if (isset($_REQUEST['switch_node'])) {
  86. CProfile::update('web.nodes.switch_node', $ZBX_VIEWED_NODES['selected'], PROFILE_TYPE_ID);
  87. }
  88. }
  89. else {
  90. $ZBX_CURRENT_NODEID = $ZBX_LOCALNODEID;
  91. $ZBX_CURMASTERID = $ZBX_LOCMASTERID;
  92. }
  93. define('ZBX_NODES_INITIALIZED', 1);
  94. // reset profiles if node is different than local
  95. if ($ZBX_CURRENT_NODEID != $ZBX_LOCALNODEID) {
  96. CProfile::init();
  97. }
  98. }
  99. function get_current_nodeid($force_all_nodes = null, $perm = null) {
  100. global $ZBX_CURRENT_NODEID, $ZBX_AVAILABLE_NODES, $ZBX_VIEWED_NODES;
  101. if (!isset($ZBX_CURRENT_NODEID)) {
  102. init_nodes();
  103. }
  104. if (!is_null($perm)) {
  105. return get_accessible_nodes_by_user(CWebUser::$data, $perm, PERM_RES_IDS_ARRAY, $ZBX_AVAILABLE_NODES);
  106. }
  107. elseif (is_null($force_all_nodes)) {
  108. if ($ZBX_VIEWED_NODES['selected'] == 0) {
  109. $result = $ZBX_VIEWED_NODES['nodeids'];
  110. }
  111. else {
  112. $result = $ZBX_VIEWED_NODES['selected'];
  113. }
  114. if (empty($result)) {
  115. $result = CWebUser::$data['node']['nodeid'];
  116. }
  117. if (empty($result)) {
  118. $result = $ZBX_CURRENT_NODEID;
  119. }
  120. }
  121. elseif ($force_all_nodes) {
  122. $result = $ZBX_AVAILABLE_NODES;
  123. }
  124. else {
  125. $result = $ZBX_CURRENT_NODEID;
  126. }
  127. return $result;
  128. }
  129. function get_viewed_nodes() {
  130. global $ZBX_LOCALNODEID;
  131. $result = array('selected' => 0, 'nodes' => array(), 'nodeids' => array());
  132. if (!defined('ZBX_NOT_ALLOW_ALL_NODES')) {
  133. $result['nodes'][0] = array('nodeid' => 0, 'name' => _('All'));
  134. }
  135. $available_nodes = get_accessible_nodes_by_user(CWebUser::$data, PERM_READ_LIST, PERM_RES_DATA_ARRAY);
  136. $available_nodes = get_tree_by_parentid($ZBX_LOCALNODEID, $available_nodes, 'masterid'); // remove parent nodes
  137. $selected_nodeids = get_request('selected_nodes', get_node_profile(array(CWebUser::$data['node']['nodeid'])));
  138. // +++ Fill $result['NODEIDS'], $result['NODES'] +++
  139. $nodeids = array();
  140. foreach ($selected_nodeids as $num => $nodeid) {
  141. if (isset($available_nodes[$nodeid])) {
  142. $result['nodes'][$nodeid] = array(
  143. 'nodeid' => $available_nodes[$nodeid]['nodeid'],
  144. 'name' => $available_nodes[$nodeid]['name'],
  145. 'masterid' => $available_nodes[$nodeid]['masterid']
  146. );
  147. $nodeids[$nodeid] = $nodeid;
  148. }
  149. }
  150. $switch_node = get_request('switch_node', CProfile::get('web.nodes.switch_node', -1));
  151. if (!isset($available_nodes[$switch_node]) || !uint_in_array($switch_node, $selected_nodeids)) { // check switch_node
  152. $switch_node = 0;
  153. }
  154. $result['nodeids'] = $nodeids;
  155. if (!defined('ZBX_NOT_ALLOW_ALL_NODES')) {
  156. $result['selected'] = $switch_node;
  157. }
  158. elseif (!empty($nodeids)) {
  159. $result['selected'] = ($switch_node > 0) ? $switch_node : array_shift($nodeids);
  160. }
  161. return $result;
  162. }
  163. function get_node_name_by_elid($id_val, $force_with_all_nodes = null, $delimiter = '') {
  164. global $ZBX_NODES, $ZBX_VIEWED_NODES;
  165. if ($force_with_all_nodes === false || (is_null($force_with_all_nodes) && $ZBX_VIEWED_NODES['selected'] != 0)) {
  166. return null;
  167. }
  168. $nodeid = id2nodeid($id_val);
  169. if (!isset($ZBX_NODES[$nodeid])) {
  170. return null;
  171. }
  172. return $ZBX_NODES[$nodeid]['name'].$delimiter;
  173. }
  174. function getNodeIdByNodeName($nodeName) {
  175. global $ZBX_NODES;
  176. foreach ($ZBX_NODES as $nodeid => $node) {
  177. if ($node['name'] == $nodeName) {
  178. return $nodeid;
  179. }
  180. }
  181. return 0;
  182. }
  183. function is_show_all_nodes() {
  184. global $ZBX_VIEWED_NODES;
  185. return ZBX_DISTRIBUTED && $ZBX_VIEWED_NODES['selected'] == 0;
  186. }
  187. function detect_node_type($nodeid, $masterid) {
  188. global $ZBX_CURMASTERID, $ZBX_LOCALNODEID;
  189. if (bccomp($nodeid, $ZBX_LOCALNODEID) == 0) {
  190. $nodetype = ZBX_NODE_LOCAL;
  191. }
  192. elseif (bccomp($nodeid, get_current_nodeid(false)) == 0) {
  193. $nodetype = ZBX_NODE_LOCAL;
  194. }
  195. elseif (bccomp($nodeid, $ZBX_CURMASTERID) == 0) {
  196. $nodetype = ZBX_NODE_MASTER;
  197. }
  198. elseif (bccomp($masterid, get_current_nodeid(false)) == 0) {
  199. $nodetype = ZBX_NODE_CHILD;
  200. }
  201. else {
  202. $nodetype = -1;
  203. }
  204. return $nodetype;
  205. }
  206. function node_type2str($nodetype) {
  207. switch ($nodetype) {
  208. case ZBX_NODE_CHILD:
  209. $result = _('Child');
  210. break;
  211. case ZBX_NODE_MASTER:
  212. $result = _('Master');
  213. break;
  214. case ZBX_NODE_LOCAL:
  215. $result = _('Local');
  216. break;
  217. default:
  218. $result = _('Unknown');
  219. break;
  220. }
  221. return $result;
  222. }
  223. function add_node($nodeid, $name, $ip, $port, $nodetype, $masterid) {
  224. global $ZBX_LOCMASTERID, $ZBX_LOCALNODEID;
  225. if (!preg_match('/^'.ZBX_PREG_NODE_FORMAT.'$/i', $name)) {
  226. error(_('Incorrect characters used for Node name.'));
  227. return false;
  228. }
  229. switch ($nodetype) {
  230. case ZBX_NODE_CHILD:
  231. break;
  232. case ZBX_NODE_MASTER:
  233. if (!empty($masterid)) {
  234. error(_('Master node "ID" must be empty.'));
  235. return false;
  236. }
  237. if ($ZBX_LOCMASTERID) {
  238. error(_('Master node already exists.'));
  239. return false;
  240. }
  241. $masterid = 'NULL';
  242. break;
  243. default:
  244. error(_('Incorrect node type.'));
  245. return false;
  246. }
  247. if (DBfetch(DBselect('SELECT n.nodeid FROM nodes n WHERE n.nodeid='.$nodeid))) {
  248. error(_('Node with same ID already exists.'));
  249. return false;
  250. }
  251. $result = DBexecute('INSERT INTO nodes (nodeid,name,ip,port,nodetype,masterid)'.
  252. ' VALUES ('.$nodeid.','.zbx_dbstr($name).','.zbx_dbstr($ip).','.$port.','.$nodetype.','.$masterid.')');
  253. if ($result && $nodetype == ZBX_NODE_MASTER) {
  254. DBexecute('UPDATE nodes SET masterid='.$nodeid.' WHERE nodeid='.$ZBX_LOCALNODEID);
  255. $ZBX_CURMASTERID = $nodeid; // apply master node for this script
  256. }
  257. return $result ? $nodeid : $result;
  258. }
  259. function update_node($nodeid, $name, $ip, $port) {
  260. if (!preg_match('/^'.ZBX_PREG_NODE_FORMAT.'$/i', $name)) {
  261. error(_('Incorrect characters used for Node name.'));
  262. return false;
  263. }
  264. return DBexecute('UPDATE nodes SET name='.zbx_dbstr($name).',ip='.zbx_dbstr($ip).',port='.$port.' WHERE nodeid='.$nodeid);
  265. }
  266. function delete_node($nodeid) {
  267. $result = false;
  268. $node = DBfetch(DBselect('SELECT n.nodeid,n.masterid FROM nodes n WHERE n.nodeid='.$nodeid));
  269. $nodetype = detect_node_type($node['nodeid'], $node['masterid']);
  270. if ($nodetype == ZBX_NODE_LOCAL) {
  271. error(_('Unable to remove local node.'));
  272. }
  273. else {
  274. $result = (
  275. DBexecute('UPDATE nodes SET masterid=NULL WHERE masterid='.$nodeid) &&
  276. DBexecute('DELETE FROM nodes WHERE nodeid='.$nodeid)
  277. );
  278. error(_('Please be aware that database still contains data related to the deleted node.'));
  279. }
  280. return $result;
  281. }
  282. function get_node_by_nodeid($nodeid) {
  283. return DBfetch(DBselect('SELECT n.* FROM nodes n WHERE n.nodeid='.$nodeid));
  284. }
  285. function get_node_path($nodeid, $result = '') {
  286. global $ZBX_NODES;
  287. $node_data = isset($ZBX_NODES[$nodeid]) ? $ZBX_NODES[$nodeid] : false;
  288. if ($node_data) {
  289. if ($node_data['masterid']) {
  290. $result = get_node_path($node_data['masterid'], $result);
  291. }
  292. $result .= $node_data['name'].' &rArr; ';
  293. }
  294. return $result;
  295. }