summaryrefslogtreecommitdiffstats
path: root/frontends/php/include/discovery.inc.php
blob: a226f3578988deac271a864b0c6ecfb2c5c0ab92 (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
<?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.
**/
	 require_once "include/perm.inc.php";

?>
<?php
	function	check_right_on_discovery($permission)
	{
		global $USER_DETAILS;
		global $ZBX_CURNODEID;

		if( $USER_DETAILS['type'] >= USER_TYPE_ZABBIX_ADMIN )
		{
			$accessible_nodes = get_accessible_nodes_by_user($USER_DETAILS, $permission, null, PERM_RES_IDS_ARRAY);

			if( in_array($ZBX_CURNODEID, $accessible_nodes) )
				return true;
		}

		return false;
	}

	function	discovery_check_type2str($type_int)
	{
		$str_type[SVC_SSH]	= S_SSH;
		$str_type[SVC_LDAP]	= S_LDAP;
		$str_type[SVC_SMTP]	= S_SMTP;
		$str_type[SVC_FTP]	= S_FTP;
		$str_type[SVC_HTTP]	= S_HTTP;
		$str_type[SVC_POP]	= S_POP;
		$str_type[SVC_NNTP]	= S_NNTP;
		$str_type[SVC_IMAP]	= S_IMAP;
		$str_type[SVC_TCP]	= S_TCP;

		if(isset($str_type[$type_int]))
			return $str_type[$type_int];

		return S_UNKNOWN;
	}

	function	discovery_status2str($status_int)
	{
		switch($status_int)
		{
			case DRULE_STATUS_ACTIVE:	$status = S_ACTIVE;		break;
			case DRULE_STATUS_DISABLED:	$status = S_DISABLED;		break;
			default:
				$status = S_UNKNOWN;		break;
		}
		return $status;
	}

	function	discovery_status2style($status)
	{
		switch($status)
		{
			case DRULE_STATUS_ACTIVE:	$status = 'off';	break;
			case DRULE_STATUS_DISABLED:	$status = 'on';		break;
			default:
				$status = 'unknown';	break;
		}
		return $status;
	}

	function	get_discovery_rule_by_druleid($druleid)
	{
		return DBfetch(DBselect('select * from drules where druleid='.$druleid));
	}

	function	set_discovery_rule_status($druleid, $status)
	{
		return DBexecute('update drules set status='.$status.' where druleid='.$druleid);
	}

	function	add_discovery_check($druleid, $type, $ports)
	{
		$dcheckid = get_dbid('dchecks', 'dcheckid');
		$result = DBexecute('insert into dchecks (dcheckid,druleid,type,ports) '.
			' values ('.$dcheckid.','.$druleid.','.$type.','.zbx_dbstr($ports).')');

		if(!$result)
			return $result;

		return $dcheckid;
	}

	function	add_discovery_rule($name, $ipfirst, $iplast,
			$delay, $status, $upevent, $downevent,
			$svcupevent, $svcdownevent, $dchecks)
	{
		$upevent	*= 3600; /* convert hours to seconds */
		$downevent	*= 3600; /* convert hours to seconds */
		$svcupevent	*= 3600; /* convert hours to seconds */
		$svcdownevent	*= 3600; /* convert hours to seconds */

		$ip_1 = explode('.', $ipfirst);
		$ip_2 = explode('.', $iplast);
		for($i=0; $i<3; $i++)
		{
			if($ip_1[$i] != $ip_2[$i])
			{
				error('Incorrect IP range.');
				return false;
			}
		}

		$druleid = get_dbid('drules', 'druleid');
		$result = DBexecute('insert into drules (druleid,name,ipfirst,iplast,delay,status,upevent,downevent,svcupevent,svcdownevent) '.
			' values ('.$druleid.','.zbx_dbstr($name).','.zbx_dbstr($ipfirst).','.zbx_dbstr($iplast).','.$delay.','.$status.
			','.$upevent.','.$downevent.','.$svcupevent.','.$svcdownevent.')');

		if($result)
		{
			DBexecute('delete from dchecks where druleid='.$druleid);
			if(isset($dchecks)) foreach($dchecks as $val)
				add_discovery_check($druleid,$val["type"],$val["ports"]);

			$result = $druleid;
		}

		return $result;
	}

	function	update_discovery_rule($druleid, $name, $ipfirst, $iplast,
			$delay, $status, $upevent, $downevent,
			$svcupevent, $svcdownevent, $dchecks)
	{
		$upevent	*= 3600; /* convert hours to seconds */
		$downevent	*= 3600; /* convert hours to seconds */
		$svcupevent	*= 3600; /* convert hours to seconds */
		$svcdownevent	*= 3600; /* convert hours to seconds */

		$result = DBexecute('update drules set name='.zbx_dbstr($name).',ipfirst='.zbx_dbstr($ipfirst).','.
			'iplast='.zbx_dbstr($iplast).',delay='.$delay.',status='.$status.',upevent='.$upevent.','.
			'downevent='.$downevent.',svcupevent='.$svcupevent.',svcdownevent='.$svcdownevent.' '.
			' where druleid='.$druleid);

		if($result)
		{
			DBexecute('delete from dchecks where druleid='.$druleid);
			if(isset($dchecks)) foreach($dchecks as $val)
				add_discovery_check($druleid,$val["type"],$val["ports"]);
		}
		return $result;
	}

	function	delete_discovery_rule($druleid)
	{
		if($result = DBexecute('delete from drules where druleid='.$druleid))
		{
			$result = DBexecute('delete from dchecks where druleid='.$druleid);
		}
		return $result;
	}
?>