-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcentreon_check_command.class.php
246 lines (225 loc) · 10.3 KB
/
centreon_check_command.class.php
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
<?php
/*
* Copyright 2005-2020 Centreon
* Centreon is developed by : Julien Mathis and Romain Le Merlus under
* GPL Licence 2.0.
*
* 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.
*
* 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, see <htcommand://www.gnu.org/licenses>.
*
* Linking this program statically or dynamically with other modules is making a
* combined work based on this program. Thus, the terms and conditions of the GNU
* General Public License cover the whole combination.
*
* As a special exception, the copyright holders of this program give Centreon
* permission to link this program with independent modules to produce an executable,
* regardless of the license terms of these independent modules, and to copy and
* distribute the resulting executable under terms of Centreon choice, provided that
* Centreon also meet, for each linked independent module, the terms and conditions
* of the license of that module. An independent module is a module which is not
* derived from this program. If you modify this program, you may extend this
* exception to your version of the program, but you are not obliged to do so. If you
* do not wish to do so, delete this exception statement from your version.
*
* For more information : [email protected]
*
*/
require_once _CENTREON_PATH_ . "/www/class/centreonDB.class.php";
require_once __DIR__ . "/centreon_configuration_objects.class.php";
class CentreonCheckCommand extends CentreonConfigurationObjects
{
/**
* CentreonConfigurationCommand constructor.
*/
public function __construct()
{
parent::__construct();
}
/**
* @return array
* @throws Exception
* @throws RestBadRequestException
*/
public function postgetCommand()
{
$queryValues = array();
$AllMacros = array();
$Macros_from_conf = array();
// Get the parameters from json
$json = file_get_contents('php://input');
$obj = json_decode($json);
foreach ($obj as $key => $json) {
foreach($json as $key => $value) {
if ($key == "macros") {
foreach($value as $key_macro=>$value_macro)
{
array_push($Macros_from_conf, array("macro_name" => '$_SERVICE'.$key_macro."$", "macro_value" => $value_macro));
array_push($AllMacros, $Macros_from_conf);
}
} else {
$queryValues[$key] = $value;
}
}
}
// get command line from sql
if (isset($queryValues["serviceCommandName"])) {
error_log("function getCommandFromCommandName with " .$queryValues["serviceCommandName"]);
$queryCommand = 'SELECT command_line ' .
'FROM command ' .
'WHERE command_activate = "1" AND command_name = :commandName ' .
'LIMIT 1';
$bindParam = ':commandName';
$bindValue = $queryValues["serviceCommandName"];
} else {
error_log("function getCommandFromServiceTemplateName with " .$queryValues["serviceTemplateName"]);
$queryCommand = 'SELECT command_line ' .
'FROM command ' .
'WHERE command_id ' .
'IN (SELECT command_command_id from service WHERE service_description = :serviceTemplateName)';
$bindParam = ':serviceTemplateName';
$bindValue = $queryValues["serviceTemplateName"];
}
$stmt = $this->pearDB->prepare($queryCommand);
$stmt->bindParam($bindParam,$bindValue, PDO::PARAM_STR);
$stmt->execute();
$commandWithPlaceholders = $stmt->fetch();
if(empty($commandWithPlaceholders)) {
$queryCommand7 = 'SELECT command_line ' .
'FROM command ' .
'WHERE command_id ' .
'IN (SELECT command_command_id FROM service WHERE service_id ' .
'IN (SELECT service_template_model_stm_id from service WHERE service_description = :serviceTemplateName))';
$stmt7 = $this->pearDB->prepare($queryCommand7);
$stmt7->bindParam(':serviceTemplateName', $queryValues["serviceTemplateName"], PDO::PARAM_STR);
$stmt7->execute();
$commandWithPlaceholders = $stmt7->fetch();
}
// get macros from host from template
//select host_macro_name,host_macro_value from on_demand_macro_host where host_host_id in (select host_tpl_id from host_template_relation where host_host_id in (select host_id from host where host_name = "65786_host") UNION ALL select host_tpl_id from host_template_relation where host_host_id in (select host_id from host where host_name = "65786_host" UNION ALL select host_tpl_id from host_template_relation where host_host_id in (select host_id from host where host_name = "65786_host")));
$queryCommand = 'SELECT host_macro_name,host_macro_value ' .
'FROM on_demand_macro_host ' .
'WHERE host_host_id ' .
'IN (SELECT host_tpl_id FROM host_template_relation WHERE host_host_id ' .
'IN (SELECT host_id FROM host WHERE host_name = :host_name) ' .
'UNION ALL SELECT host_tpl_id FROM host_template_relation WHERE host_host_id ' .
'IN (SELECT host_id FROM host WHERE host_name = :host_name ' .
'UNION ALL SELECT host_tpl_id FROM host_template_relation WHERE host_host_id ' .
'IN (SELECT host_id FROM host WHERE host_name = :host_name)))';
$stmt = $this->pearDB->prepare($queryCommand);
$stmt->bindParam(':host_name', $queryValues["host_name"], PDO::PARAM_STR);
$stmt->execute();
error_log(print_r($stmt, true));
$macrosFromHostTemplates = $stmt->fetchall();
error_log(print_r($macrosFromHostTemplates, true));
// get macros from host
$queryCommand = 'SELECT host_macro_name,host_macro_value ' .
'FROM on_demand_macro_host ' .
'WHERE host_host_id ' .
'IN (SELECT host_id from host where host_name = :host_name)';
$stmt = $this->pearDB->prepare($queryCommand);
$stmt->bindParam(':host_name', $queryValues["host_name"], PDO::PARAM_STR);
$stmt->execute();
$macrosFromHost = $stmt->fetchall();
$queryCommand = 'SELECT host_address, host_snmp_version, host_snmp_community ' .
'FROM host ' .
'WHERE host_id ' .
'IN (SELECT host_id FROM host WHERE host_name = :host_name)';
$stmt = $this->pearDB->prepare($queryCommand);
$stmt->bindParam(':host_name', $queryValues["host_name"], PDO::PARAM_STR);
$stmt->execute();
$infosFromHost = $stmt->fetch();
foreach($infosFromHost as $key => $value){
if ($key == "host_address") {
$new_key = "$".str_replace("_","",strtoupper($key))."$";
} else {
$new_key = "\$_".str_replace("_","",strtoupper($key))."$";
}
unset($infosFromHost[$key]);
array_push($infosFromHost, array("macro_name_host" => $new_key, "macro_value_host" => $value));
}
// get macros from nagios
$queryCommand = 'SELECT resource_name, resource_line FROM cfg_resource';
$stmt = $this->pearDB->prepare($queryCommand);
$stmt->execute();
$macrosFromNagios = $stmt->fetchall();
// replace all macros from commandWithPlaceholders
array_push($AllMacros,$macrosFromHost,$macrosFromHostTemplates,$macrosFromNagios,$infosFromHost);
foreach( $AllMacros as $macros ) {
foreach( $macros as $macro ) {
$macro_name = $macro[array_key_first($macro)];
$macro_value = $macro[array_key_last($macro)];
$commandWithPlaceholders = str_replace($macro_name,$macro_value,$commandWithPlaceholders);
}
}
// replace all empty macros
$patterns = array();
$patterns[0] = '/\$_[A-Z\][0-9]+\$/';
$patterns[1] = '/\$[A-Z\][0-9]+\$/';
$replacements = array();
$replacements[2] = '';
$replacements[1] = '';
$command = preg_replace($patterns,$replacements,$commandWithPlaceholders);
return $command;
}
public function postexecuteCommand()
{
$json = file_get_contents('php://input');
// Converts it into a PHP object
$json_data = json_decode($json, true);
$command = print_r($json_data[0]['command'], true);
$host_name = print_r($json_data[1]['host_name'], true);
//select nagios_server_id from ns_host_relation where host_host_id in (select host_id from host where host_name = "65786_host");
$queryCommand6 = 'SELECT nagios_server_id ' .
'FROM ns_host_relation ' .
'WHERE host_host_id ' .
'IN (SELECT host_id from host where host_name = :host_name)';
$stmt6 = $this->pearDB->prepare($queryCommand6);
$stmt6->bindParam(':host_name', $host_name, PDO::PARAM_STR);
$stmt6->execute();
$host_poller_id = $stmt6->fetch();
$url = "http://localhost:8085/api/nodes/".$host_poller_id['nagios_server_id']."/core/action/command";
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$headers = array(
"Accept: application/json",
"Content-Type: application/json",
);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
$command = json_encode($command);
$data = <<<DATA
[{"command": $command}]
DATA;
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
$json_curl_data = curl_exec($curl);
curl_close($curl);
$obj_resp = json_decode($json_curl_data);
$obj_resp->poller_id=$host_poller_id['nagios_server_id'];
$json_resp = json_encode($obj_resp);
return $json_resp;
}
public function getgetlogCommand(){
$token = $this->arguments['token_id'];
$poller_id = $this->arguments['poller_id'];
$url = "http://localhost:8085/api/nodes/".$poller_id."/log/".$token;
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$headers = array(
"Accept: application/json"
);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
$resp = curl_exec($curl);
curl_close($curl);
return $resp;
}
}