summaryrefslogtreecommitdiffstats
path: root/frontends/php/include/nodes.inc.php
blob: 10a1fcaecb90afb5532462b37a4635f2716dd7a9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
<?php
/*
** ZABBIX
** Copyright (C) 2000-2005 SIA Zabbix
**
** This program is free software; you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation; either version 2 of the License, or
** (at your option) any later version.
**
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with this program; if not, write to the Free Software
** Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
**/
?>
<?php
	function	detect_node_type($node_data)
	{
		global $ZBX_CURMASTERID;

		if(bccomp($node_data['nodeid'],get_current_nodeid(false)) == 0)		$node_type = ZBX_NODE_LOCAL;
		else if(bccomp($node_data['nodeid'] ,$ZBX_CURMASTERID)==0)		$node_type = ZBX_NODE_MASTER;
		else if(bccomp($node_data['masterid'], get_current_nodeid(false))==0)	$node_type = ZBX_NODE_REMOTE;
		else $node_type = -1;

	return $node_type;
	}

	function	node_type2str($node_type)
	{
		$result = '';
		switch($node_type)
		{
			case ZBX_NODE_REMOTE:	$result = S_REMOTE;	break;
			case ZBX_NODE_MASTER:	$result = S_MASTER;	break;
			case ZBX_NODE_LOCAL:	$result = S_LOCAL;	break;
			default:		$result = S_UNKNOWN;	break;
		}

		return $result;
	}

	function	add_node($new_nodeid,$name,$timezone,$ip,$port,$slave_history,$slave_trends,$node_type)
	{
		global $ZBX_CURMASTERID;

		if( !eregi('^'.ZBX_EREG_NODE_FORMAT.'$', $name) )
		{
			error("Incorrect characters used for Node name");
			return false;
		}

		switch($node_type)
		{
			case ZBX_NODE_REMOTE:
				$masterid = get_current_nodeid(false);
				$nodetype = 0;
				break;
			case ZBX_NODE_MASTER:
				$masterid = 0;
				$nodetype = 0;
				if($ZBX_CURMASTERID)
				{
					error('Master node already exist');
					return false;
				}
				break;
			case ZBX_NODE_LOCAL:
				$masterid = $ZBX_CURMASTERID;
				$nodetype = 1;
				break;
			default:
				error('Incorrect node type');
				return false;
				break;
		}

		if(DBfetch(DBselect('select nodeid from nodes where nodeid='.$new_nodeid)))
		{
			error('Node with same ID already exist.');
			return false;
		}

		$result = DBexecute('insert into nodes (nodeid,name,timezone,ip,port,slave_history,slave_trends,'.
				'nodetype,masterid) values ('.
				$new_nodeid.','.zbx_dbstr($name).','.$timezone.','.zbx_dbstr($ip).','.$port.','.$slave_history.','.$slave_trends.','.
				$nodetype.','.$masterid.')');

		if($result && $node_type == ZBX_NODE_MASTER)
		{
			DBexecute('update nodes set masterid='.$new_nodeid.' where nodeid='.get_current_nodeid(false));
			$ZBX_CURMASTERID = $new_nodeid; /* applay Master node for this script */
		}

		return ($result ? $new_nodeid : $result);
	}

	function	update_node($nodeid,$new_nodeid,$name,$timezone,$ip,$port,$slave_history,$slave_trends)
	{
		if( !eregi('^'.ZBX_EREG_NODE_FORMAT.'$', $name) )
		{
			error("Incorrect characters used for Node name");
			return false;
		}

		$result = DBexecute('update nodes set nodeid='.$new_nodeid.',name='.zbx_dbstr($name).','.
				'timezone='.$timezone.',ip='.zbx_dbstr($ip).',port='.$port.','.
				'slave_history='.$slave_history.',slave_trends='.$slave_trends.
				' where nodeid='.$nodeid);
		return $result;
	}

	function	delete_node($nodeid)
	{
		$result = false;
		$node_data = DBfetch(DBselect('select * from nodes where nodeid='.$nodeid));

		$node_type = detect_node_type($node_data);

		if($node_type == ZBX_NODE_LOCAL)
		{
			error('Unable to remove local node');
		}
		else
		{
			$housekeeperid = get_dbid('housekeeper','housekeeperid');
			$result = (
				DBexecute("insert into housekeeper (housekeeperid,tablename,field,value)".
					" values ($housekeeperid,'nodes','nodeid',$nodeid)") &&
				DBexecute('delete from nodes where nodeid='.$nodeid) &&
				DBexecute('update nodes set masterid=0 where masterid='.$nodeid)
				);
			error('Please be aware that database still contains data related to the deleted Node');
		}
		return $result;
	}

	function	get_node_by_nodeid($nodeid)
	{
		return DBfetch(DBselect('select * from nodes where nodeid='.$nodeid));
	}

	function	get_node_path($nodeid, $result='/')
	{
		if($node_data = get_node_by_nodeid($nodeid))
		{
			if($node_data['masterid'])
			{
				$result = get_node_path($node_data['masterid'],$result);
			}
			$result .= $node_data['name'].'/';
		}
		return $result;
	}
?>