summaryrefslogtreecommitdiffstats
path: root/frontends/php/include/hosts.inc.php
blob: 70662082c456e3cd70e836ff3ee85991217e8a9a (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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
<?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
	# Add Host definition

	function	add_host($host,$port,$status,$useip,$ip,$host_templateid,$newgroup,$groups)
	{
		if(!check_right("Host","A",0))
		{
			error("Insufficient permissions");
			return 0;
		}

 		if (!eregi('^([0-9a-zA-Z\_\.-]+)$', $host, &$arr)) 
		{
			error("Hostname should contain 0-9a-zA-Z_.- characters only");
			return 0;
		}

		$sql="select * from hosts where host='$host'";
		$result=DBexecute($sql);
		if(DBnum_rows($result)>0)
		{
			error("Host '$host' already exists");
			return 0;
		}

		if($useip=="on" || $useip=="yes" || $useip==1)
		{
			$useip=1;
		}
		else
		{
			$useip=0;
		}

		$sql="insert into hosts (host,port,status,useip,ip,disable_until,available) values ('$host',$port,$status,$useip,'$ip',0,".HOST_AVAILABLE_UNKNOWN.")";
		$result=DBexecute($sql);
		if(!$result)
		{
			return	$result;
		}
		
		$hostid=DBinsert_id($result,"hosts","hostid");

		if($host_templateid != 0)
		{
			add_templates_to_host($hostid,$host_templateid);
			sync_host_with_templates($hostid);
		}
		if($groups != "")
		{
			update_host_groups($hostid,$groups);
		}
		if($newgroup != "")
		{
			add_group_to_host($hostid,$newgroup);
		}

		update_profile("HOST_PORT",$port);
		
		return	$result;
	}

	function	update_host($hostid,$host,$port,$status,$useip,$ip,$newgroup,$groups)
	{
		if(!check_right("Host","U",$hostid))
		{
			error("Insufficient permissions");
			return 0;
		}

 		if (!eregi('^([0-9a-zA-Z\_\.-]+)$', $host, &$arr)) 
		{
			error("Hostname should contain 0-9a-zA-Z_.- characters only");
			return 0;
		}

		$sql="select * from hosts where host='$host' and hostid<>$hostid";
		$result=DBexecute($sql);
		if(DBnum_rows($result)>0)
		{
			error("Host '$host' already exists");
			return 0;
		}


		if($useip=="on" || $useip=="yes" || $useip==1)
		{
			$useip=1;
		}
		else
		{
			$useip=0;
		}

		$sql="update hosts set host='$host',port=$port,useip=$useip,ip='$ip' where hostid=$hostid";
		$result=DBexecute($sql);


		update_host_status($hostid, $status);
		update_host_groups($hostid,$groups);
		if($newgroup != "")
		{
			add_group_to_host($hostid,$newgroup);
		}
		return	$result;
	}

	# Add templates linked to template host to the host

	function	add_templates_to_host($hostid,$host_templateid)
	{
		$sql="select * from hosts_templates where hostid=$host_templateid";
		$result=DBselect($sql);
		while($row=DBfetch($result))
		{
			add_template_linkage($hostid,$row["templateid"],$row["items"],$row["triggers"],
						$row["graphs"]);
		}
	}

	function	delete_groups_by_hostid($hostid)
	{
		$sql="select groupid from hosts_groups where hostid=$hostid";
		$result=DBselect($sql);
		while($row=DBfetch($result))
		{
			$sql="delete from hosts_groups where hostid=$hostid and groupid=".$row["groupid"];
			DBexecute($sql);
			$sql="select count(*) as count from hosts_groups where groupid=".$row["groupid"];
			$result2=DBselect($sql);
			$row2=DBfetch($result2);
			if($row2["count"]==0)
			{
				$sql="delete from groups where groupid=".$row["groupid"];
				DBexecute($sql);
			}
		}
	}

	# Delete Host

	function	delete_host($hostid)
	{
		global $DB_TYPE;

		$ret = FALSE;

		for($i=0;$i<100;$i++)
		{
			if($DB_TYPE=="MYSQL")
			{
				$sql="update hosts set status=".HOST_STATUS_DELETED.",host=concat(host,\" [DEL$i]\") where hostid=$hostid";
			}
			else
			{
				$sql="update hosts set status=".HOST_STATUS_DELETED.",host=host||' [DEL$i]' where hostid=$hostid";
			}
			if($ret = DBexecute($sql,1))	break;
		}
		if($ret){
			delete_host_profile($hostid);
		}
		return	$ret;
	}

	function	delete_host_group($groupid)
	{
		$sql="delete from hosts_groups where groupid=$groupid";
		DBexecute($sql);
		$sql="delete from groups where groupid=$groupid";
		return DBexecute($sql);
	}

	function	get_group_by_groupid($groupid)
	{
		$result=DBselect("select * from groups where groupid=".$groupid);
		if(DBnum_rows($result) == 1)
		{
			return DBfetch($result);
		}
		error("No groups with groupid=[$groupid]");
		return  FALSE;
	}

	function	get_host_by_itemid($itemid)
	{
		$sql="select h.* from hosts h, items i where i.hostid=h.hostid and i.itemid=$itemid";
		$result=DBselect($sql);
		if(DBnum_rows($result) == 1)
		{
			return DBfetch($result);
		}
		error("No host with itemid=[$itemid]");
		return	FALSE;
	}

	function	get_host_by_hostid($hostid)
	{
		$sql="select * from hosts where hostid=$hostid";
		$result=DBselect($sql);
		if(DBnum_rows($result) == 1)
		{
			return DBfetch($result);
		}
		error("No host with hostid=[$hostid]");
		return	FALSE;
	}

	# Update Host status

	function	update_host_status($hostid,$status)
	{
                if(!check_right("Host","U",0))
                {
                        error("Insufficient permissions");
                        return 0;
                }

		$sql="select status,host from hosts where hostid=$hostid";
		$result=DBselect($sql);
		$row=DBfetch($result);
		$old_status=$row["status"];
		if($status != $old_status)
		{
			update_trigger_value_to_unknown_by_hostid($hostid);
			$sql="update hosts set status=$status where hostid=$hostid and status!=".HOST_STATUS_DELETED;
			info("Updated status of host ".$row["host"]);
			return	DBexecute($sql);
		}
		else
		{
			return 1;
		}
	}
?>