summaryrefslogtreecommitdiffstats
path: root/frontends/php/include/scripts.inc.php
blob: f7c1082cfde33bc07ba3dc131cb3d9cd915d5724 (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
<?php

function get_script_by_scriptid($scriptid){
	$sql = 'SELECT * FROM scripts WHERE scriptid='.$scriptid;
	
	$rows = false;
	if($res = DBSelect($sql)){
		$rows = DBfetch($res);
	}
return $rows;
}

function add_script($name,$command,$usrgrpid,$groupid,$access){
	$scriptid = get_dbid('scripts','scriptid');
	$sql = 'INSERT INTO scripts (scriptid,name,command,usrgrpid,groupid,host_access) '.
				" VALUES ('$scriptid','$name',".zbx_dbstr($command).",$usrgrpid,$groupid,$access)";
	$result = DBexecute($sql);
	if($result){
		$result = $scriptid;
	}
return $result;
}

function delete_script($scriptid){
	$sql = 'DELETE FROM scripts WHERE scriptid='.$scriptid;
	$result = DBexecute($sql);
return $result;
}

function update_script($scriptid,$name,$command,$usrgrpid,$groupid,$access){

	$sql = 'UPDATE scripts SET '.
				' name='.zbx_dbstr($name).
				' ,command='.zbx_dbstr($command).
				' ,usrgrpid='.$usrgrpid.
				' ,groupid='.$groupid.
				' ,host_access='.$access.
			' WHERE scriptid='.$scriptid;
			
	$result = DBexecute($sql);
return $result;
}

function script_make_command($scriptid,$hostid)
{
	$host_db = DBfetch(DBselect("select dns,useip,ip from hosts where hostid=$hostid"));
	$script_db = DBfetch(DBselect("select command from scripts where scriptid=$scriptid"));
	if($host_db && $script_db)
	{
		$command = $script_db["command"];
		$command = str_replace("{HOST.DNS}", $host_db["dns"],$command);
		$command = str_replace("{IPADDRESS}", $host_db["ip"],$command);
		$command = ($host_db["useip"]==0)?
				str_replace("{HOST.CONN}", $host_db["dns"],$command):
				str_replace("{HOST.CONN}", $host_db["ip"],$command);
	}
	else
	{
		$command = FALSE;
	}
	return $command;
}

function execute_script($scriptid,$hostid){
	$res = 1;

	$command = script_make_command($scriptid,$hostid);
	$nodeid = id2nodeid($hostid);

	$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);

	if(!$socket){
		$res = 0;
	}
	
	if($res){
		global $ZBX_SERVER, $ZBX_SERVER_PORT;
		$res = socket_connect($socket, $ZBX_SERVER, $ZBX_SERVER_PORT);
	}
	
	if($res){
		$send = "Command\255$nodeid\255$command\n";
		socket_write($socket,$send);
	}
	
	if($res){
		$res = socket_read($socket,65535);
	}
	
	if($res){
		list($flag,$msg)=split("\255",$res);
		$message["flag"]=$flag;
		$message["message"]=$msg;
	}
	else{
		$message["flag"]=-1;
		$message["message"] = S_CONNECT_TO_SERVER_ERROR.' ['.$ZBX_SERVER.':'.$ZBX_SERVER_PORT.'] ['.socket_strerror(socket_last_error()).']';
	}
	
	if($socket){
		socket_close($socket);
	}
return $message;
}


function get_accessible_scripts_by_hosts($hosts){
	global $USER_DETAILS;
	
	if(!is_array($hosts)){
		$hosts = array('0' => hosts);
	}

// Selecting usrgroups by user	
	$sql = 'SELECT ug.usrgrpid '.
			' FROM users_groups ug '.
			' WHERE ug.userid='.$USER_DETAILS['userid'];
			
	$user_groups = DBfetch(DBselect($sql));
	$user_groups[] = 0;	// to ALL user groups
//


// Selecting groups by Hosts	
	$sql = 'SELECT hg.hostid,hg.groupid '.
			' FROM hosts_groups hg '.
			' WHERE '.DBcondition('hg.hostid',$hosts);
			
	$hg_res = DBselect($sql);
	while($hg_rows = DBfetch($hg_res)){
		$hosts_groups[$hg_rows['groupid']][$hg_rows['hostid']] = $hg_rows['hostid'];
		$hg_groups[$hg_rows['groupid']] = $hg_rows['groupid'];
	}
	$hg_groups[] = 0;	// to ALL host groups
//

	$hosts_read_only  = get_accessible_hosts_by_user($USER_DETAILS,PERM_READ_ONLY,PERM_RES_IDS_ARRAY);
	$hosts_read_write = get_accessible_hosts_by_user($USER_DETAILS,PERM_READ_WRITE,PERM_RES_IDS_ARRAY);

	$hosts_read_only = array_intersect($hosts,$hosts_read_only);
	$hosts_read_write = array_intersect($hosts,$hosts_read_write);

// initialize array 
	foreach($hosts as $id => $hostid){
		$scripts_by_host[$hostid] = array();
	}
//-----
	
	$sql = 'SELECT s.* FROM scripts s'.
			' WHERE '.DBin_node('s.scriptid').
				' AND '.DBcondition('s.groupid',$hg_groups).
				' AND '.DBcondition('s.usrgrpid',$user_groups).
			' ORDER BY scriptid ASC';

	$res=DBselect($sql);
	
	while($script = DBfetch($res)){
		$add_to_hosts = array();
		if(PERM_READ_WRITE == $script['host_access']){
			if($script['groupid'] > 0)
				$add_to_hosts = array_intersect($hosts_read_write, $hosts_groups[$script['groupid']]);
			else 
				$add_to_hosts = $hosts_read_write;
		}
		else if(PERM_READ_ONLY == $script['host_access']){
			if($script['groupid'] > 0)
				$add_to_hosts = array_intersect($hosts_read_only, $hosts_groups[$script['groupid']]);
			else 
				$add_to_hosts = $hosts_read_only;
		}
		
		foreach($add_to_hosts as $id => $hostid){
			$scripts_by_host[$hostid][] = $script;
		}
	}
/*
*/
//SDI($scripts_by_host);
return $scripts_by_host;
}
?>