summaryrefslogtreecommitdiffstats
path: root/frontends/php/include/scripts.inc.php
blob: 037a15efd2b236a4b3b4e66678b0c672c517bd6b (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
<?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,$access){
	$scriptid = get_dbid('scripts','scriptid');
	$sql = 'INSERT INTO scripts (scriptid,name,command,host_access) '.
				" VALUES ('$scriptid','$name',".zbx_dbstr($command).",$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,$access){

	$sql = 'UPDATE scripts SET '.
				' name='.zbx_dbstr($name).
				' ,command='.zbx_dbstr($command).
				' ,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);
	}
	
	$hosts_read_only  = explode(',',get_accessible_hosts_by_user($USER_DETAILS,PERM_READ_ONLY));
	$hosts_read_write = explode(',',get_accessible_hosts_by_user($USER_DETAILS,PERM_READ_WRITE));

// initialize array 
	foreach($hosts as $id => $hostid){
		$scripts_by_host[$hostid] = array();
	}
//-----
	
	$sql = 'SELECT * FROM scripts '.
			' WHERE '.DBin_node('scriptid').
			' ORDER BY scriptid ASC';
	
	$res=DBselect($sql);
	
	while($script = DBfetch($res)){
		foreach($hosts as $id => $hostid){
			if($script['host_access'] == SCRIPT_HOST_ACCESS_WRITE){
				if(uint_in_array($hostid,$hosts_read_write)){
					$scripts_by_host[$hostid][] = $script;
				}
			}
			else{
				if(uint_in_array($hostid,$hosts_read_only)){
					$scripts_by_host[$hostid][] = $script;
				}
			}
		}
	}

return $scripts_by_host;
}
?>