4
** Copyright (C) 2000-2005 SIA Zabbix
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.
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.
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., 675 Mass Ave, Cambridge, MA 02139, USA.
20
require_once "include/db.inc.php";
23
function detect_node_type($node_data)
25
global $ZBX_CURNODEID, $ZBX_CURMASTERID;
27
if($node_data['nodeid'] == $ZBX_CURNODEID) $node_type = ZBX_NODE_LOCAL;
28
else if($node_data['nodeid'] == $ZBX_CURMASTERID) $node_type = ZBX_NODE_MASTER;
29
else if($node_data['masterid'] == $ZBX_CURNODEID) $node_type = ZBX_NODE_REMOTE;
35
function node_type2str($node_type)
40
case ZBX_NODE_REMOTE: $result = S_REMOTE; break;
41
case ZBX_NODE_MASTER: $result = S_MASTER; break;
42
case ZBX_NODE_LOCAL: $result = S_LOCAL; break;
43
default: $result = S_UNKNOWN; break;
49
function add_node($new_nodeid,$name,$timezone,$ip,$port,$slave_history,$slave_trends,$node_type)
51
global $ZBX_CURNODEID, $ZBX_CURMASTERID;
53
if( !eregi('^'.ZBX_EREG_NODE_FORMAT.'$', $name) )
55
error("Incorrect characters used for Node name");
62
$masterid = $ZBX_CURNODEID;
70
error('Master node already exist');
75
$masterid = $ZBX_CURMASTERID;
79
error('Incorrect node type');
84
if(DBfetch(DBselect('select nodeid from nodes where nodeid='.$new_nodeid)))
86
error('Node with same ID already exist.');
90
$result = DBexecute('insert into nodes (nodeid,name,timezone,ip,port,slave_history,slave_trends,'.
91
'event_lastid,history_lastid,nodetype,masterid) values ('.
92
$new_nodeid.','.zbx_dbstr($name).','.$timezone.','.zbx_dbstr($ip).','.$port.','.$slave_history.','.$slave_trends.','.
93
'0,0,'.$nodetype.','.$masterid.')');
95
if($result && $node_type == ZBX_NODE_MASTER)
97
DBexecute('update nodes set masterid='.$new_nodeid.' where nodeid='.$ZBX_CURNODEID);
98
$ZBX_CURMASTERID = $new_nodeid; /* applay Master node for this script */
101
return ($result ? $new_nodeid : $result);
104
function update_node($nodeid,$new_nodeid,$name,$timezone,$ip,$port,$slave_history,$slave_trends)
106
if( !eregi('^'.ZBX_EREG_NODE_FORMAT.'$', $name) )
108
error("Incorrect characters used for Node name");
112
$result = DBexecute('update nodes set nodeid='.$new_nodeid.',name='.zbx_dbstr($name).','.
113
'timezone='.$timezone.',ip='.zbx_dbstr($ip).',port='.$port.','.
114
'slave_history='.$slave_history.',slave_trends='.$slave_trends.
115
' where nodeid='.$nodeid);
119
function delete_node($nodeid)
122
$node_data = DBfetch(DBselect('select * from nodes where nodeid='.$nodeid));
124
$node_type = detect_node_type($node_data);
126
if($node_type == ZBX_NODE_LOCAL)
128
error('Unable to remove local node');
132
$housekeeperid = get_dbid('housekeeper','housekeeperid');
134
DBexecute("insert into housekeeper (housekeeperid,tablename,field,value)".
135
" values ($housekeeperid,'nodes','nodeid',$nodeid)") &&
136
DBexecute('delete from nodes where nodeid='.$nodeid) &&
137
DBexecute('update nodes set masterid=0 where masterid='.$nodeid)
139
error('Please be aware that database still contains data related to the deleted Node');
144
function get_node_by_nodeid($nodeid)
146
return DBfetch(DBselect('select * from nodes where nodeid='.$nodeid));
149
function get_node_path($nodeid, $result='/')
151
if($node_data = get_node_by_nodeid($nodeid))
153
if($node_data['masterid'])
155
$result = get_node_path($node_data['masterid'],$result);
157
$result .= $node_data['name'].'/';