Skip to content

Commit 4a987c1

Browse files
authored
Merge pull request #1720 from it-novum/ITC-3373
ITC-3373 Implement a resolver to allow passing of host and service na…
2 parents d2f67b0 + 9454e02 commit 4a987c1

File tree

3 files changed

+293
-33
lines changed

3 files changed

+293
-33
lines changed

plugins/NagiosModule/src/Controller/CmdController.php

Lines changed: 55 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,26 @@
11
<?php
2-
// Copyright (C) <2015> <it-novum GmbH>
2+
// Copyright (C) <2015-present> <it-novum GmbH>
33
//
44
// This file is dual licensed
55
//
66
// 1.
7-
// This program is free software: you can redistribute it and/or modify
8-
// it under the terms of the GNU General Public License as published by
9-
// the Free Software Foundation, version 3 of the License.
7+
// This program is free software: you can redistribute it and/or modify
8+
// it under the terms of the GNU General Public License as published by
9+
// the Free Software Foundation, version 3 of the License.
1010
//
11-
// This program is distributed in the hope that it will be useful,
12-
// but WITHOUT ANY WARRANTY; without even the implied warranty of
13-
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14-
// GNU General Public License for more details.
11+
// This program is distributed in the hope that it will be useful,
12+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14+
// GNU General Public License for more details.
1515
//
16-
// You should have received a copy of the GNU General Public License
17-
// along with this program. If not, see <http://www.gnu.org/licenses/>.
16+
// You should have received a copy of the GNU General Public License
17+
// along with this program. If not, see <http://www.gnu.org/licenses/>.
1818
//
19+
// 2.
20+
// If you purchased an openITCOCKPIT Enterprise Edition you can use this file
21+
// under the terms of the openITCOCKPIT Enterprise Edition license agreement.
22+
// License agreement and license key will be shipped with the order
23+
// confirmation.
1924

2025
// 2.
2126
// If you purchased an openITCOCKPIT Enterprise Edition you can use this file
@@ -27,12 +32,14 @@
2732

2833
namespace NagiosModule\Controller;
2934

35+
use App\itnovum\openITCOCKPIT\Monitoring\Naemon\ExternalCommands;
3036
use Cake\Http\Exception\BadRequestException;
3137
use Cake\Http\Exception\MethodNotAllowedException;
3238
use Cake\Http\Exception\NotFoundException;
39+
use Cake\Log\Log;
3340
use Cake\Utility\Hash;
3441
use itnovum\openITCOCKPIT\Core\System\Gearman;
35-
use App\itnovum\openITCOCKPIT\Monitoring\Naemon\ExternalCommands;
42+
use itnovum\openITCOCKPIT\Resolver\NameToUuidResolver;
3643

3744
class CmdController extends AppController {
3845

@@ -84,6 +91,31 @@ public function submit() {
8491
$command = $args['command'];
8592
unset($args['command']);
8693

94+
// ITC-3373 Check if hostUuid and serviceUuid are valid UUIDs.
95+
// Otherwise, the user passed a human name such as "localhost" or "PING" and we try to resolve it.
96+
if (isset($args['hostUuid']) || isset($args['serviceUuid'])) {
97+
$Resolver = NameToUuidResolver::createResolver();
98+
if (isset($args['serviceUuid'])) {
99+
// Resolve both host and service name into a UUID (if needed)
100+
$result = $Resolver->resolveHostAndServicename($args['hostUuid'], $args['serviceUuid']);
101+
if ($result['hostUuid'] === false || $result['serviceUuid'] === false) {
102+
Log::error(sprintf('CmdController: Could not resolve service name for "%s/%s" into a UUID', $args['hostUuid'], $args['serviceUuid']));
103+
throw new BadRequestException(sprintf('CmdController: Could not resolve service name for "%s/%s" into a UUID', $args['hostUuid'], $args['serviceUuid']));
104+
}
105+
106+
$args['hostUuid'] = $result['hostUuid'];
107+
$args['serviceUuid'] = $result['serviceUuid'];
108+
} else {
109+
// Resolve host name into a UUID (if needed)
110+
$hostUuid = $Resolver->resolveHostname($args['hostUuid']);
111+
if ($hostUuid === false) {
112+
Log::error(sprintf('CmdController: Could not resolve hostname "%s" into a UUID', $args['hostUuid']));
113+
throw new BadRequestException(sprintf('CmdController: Could not resolve hostname "%s" into a UUID', $args['hostUuid']));
114+
}
115+
$args['hostUuid'] = $hostUuid;
116+
}
117+
}
118+
87119
// Command is now ready to submit to sudo_server
88120
// Auto-Lookup if Master System or SAT system (satelliteId=null)
89121
$GearmanClient = new Gearman();
@@ -178,7 +210,7 @@ public function submit_bulk() {
178210
]);
179211
}
180212

181-
public function submit_bulk_naemon(){
213+
public function submit_bulk_naemon() {
182214
if (!$this->request->is('post') && !$this->request->is('get')) {
183215
throw new MethodNotAllowedException();
184216
}
@@ -195,34 +227,34 @@ public function submit_bulk_naemon(){
195227
throw new BadRequestException();
196228
}
197229
$externalNaemonCommands = new ExternalCommands();
198-
foreach($data as $value){
230+
foreach ($data as $value) {
199231
switch ($value['command']) {
200232
case 'rescheduleHost':
201-
$result = $externalNaemonCommands->rescheduleHost(['uuid' => $value['hostUuid'], 'type' =>$value['type'], 'satellite_id' => $value['satelliteId']]);
233+
$result = $externalNaemonCommands->rescheduleHost(['uuid' => $value['hostUuid'], 'type' => $value['type'], 'satellite_id' => $value['satelliteId']]);
202234
break;
203235
case 'rescheduleService':
204-
$result = $externalNaemonCommands->rescheduleService(['hostUuid' => $value['hostUuid'], 'serviceUuid' => $value['serviceUuid'], 'satellite_id' => $value['satelliteId']]);
236+
$result = $externalNaemonCommands->rescheduleService(['hostUuid' => $value['hostUuid'], 'serviceUuid' => $value['serviceUuid'], 'satellite_id' => $value['satelliteId']]);
205237
break;
206238
case 'submitHostDowntime':
207-
$result = $externalNaemonCommands->setHostDowntime(['hostUuid' => $value['hostUuid'], 'start' => $value['start'], 'end' => $value['end'], 'comment' => $value['comment'], 'author' => $value['author'], 'downtimetype' => $value['downtimetype']]);
239+
$result = $externalNaemonCommands->setHostDowntime(['hostUuid' => $value['hostUuid'], 'start' => $value['start'], 'end' => $value['end'], 'comment' => $value['comment'], 'author' => $value['author'], 'downtimetype' => $value['downtimetype']]);
208240
break;
209241
case 'submitServiceDowntime':
210-
$result = $externalNaemonCommands->setServiceDowntime(['hostUuid' => $value['hostUuid'], 'serviceUuid' => $value['serviceUuid'], 'start' => $value['start'], 'end' => $value['end'], 'comment' => $value['comment'], 'author' => $value['author']]);
242+
$result = $externalNaemonCommands->setServiceDowntime(['hostUuid' => $value['hostUuid'], 'serviceUuid' => $value['serviceUuid'], 'start' => $value['start'], 'end' => $value['end'], 'comment' => $value['comment'], 'author' => $value['author']]);
211243
break;
212244
case 'submitHoststateAck':
213-
$result = $externalNaemonCommands->setHostAck(['hostUuid' => $value['hostUuid'], 'comment' => $value['comment'], 'author' => $value['author'], 'sticky' => $value['sticky'], 'type' => $value['hostAckType'], 'notify' => $value['notify']]);
245+
$result = $externalNaemonCommands->setHostAck(['hostUuid' => $value['hostUuid'], 'comment' => $value['comment'], 'author' => $value['author'], 'sticky' => $value['sticky'], 'type' => $value['hostAckType'], 'notify' => $value['notify']]);
214246
break;
215247
case 'submitServicestateAck':
216-
$result = $externalNaemonCommands->setServiceAck(['hostUuid' => $value['hostUuid'], 'serviceUuid' => $value['serviceUuid'], 'comment' => $value['comment'], 'author' => $value['author'], 'sticky' => $value['sticky'], 'notify' => $value['notify']]);
248+
$result = $externalNaemonCommands->setServiceAck(['hostUuid' => $value['hostUuid'], 'serviceUuid' => $value['serviceUuid'], 'comment' => $value['comment'], 'author' => $value['author'], 'sticky' => $value['sticky'], 'notify' => $value['notify']]);
217249
break;
218250
case 'commitPassiveServiceResult':
219-
$result = $externalNaemonCommands->passiveTransferServiceCheckresult(['hostUuid' => $value['hostUuid'], 'serviceUuid' => $value['serviceUuid'], 'comment' => $value['plugin_output'], 'state' => $value['status_code'], 'forceHardstate' => $value['forceHardstate'], 'repetitions' => $value['maxCheckAttempts']]);
251+
$result = $externalNaemonCommands->passiveTransferServiceCheckresult(['hostUuid' => $value['hostUuid'], 'serviceUuid' => $value['serviceUuid'], 'comment' => $value['plugin_output'], 'state' => $value['status_code'], 'forceHardstate' => $value['forceHardstate'], 'repetitions' => $value['maxCheckAttempts']]);
220252
break;
221253
case 'commitPassiveResult':
222-
$result = $externalNaemonCommands->passiveTransferHostCheckresult(['uuid' => $value['hostUuid'], 'comment' => $value['plugin_output'], 'state' => $value['status_code'], 'forceHardstate' => $value['forceHardstate'], 'repetitions' => $value['maxCheckAttempts']]);
254+
$result = $externalNaemonCommands->passiveTransferHostCheckresult(['uuid' => $value['hostUuid'], 'comment' => $value['plugin_output'], 'state' => $value['status_code'], 'forceHardstate' => $value['forceHardstate'], 'repetitions' => $value['maxCheckAttempts']]);
223255
break;
224256
case 'sendCustomServiceNotification':
225-
$result = $externalNaemonCommands->sendCustomServiceNotification(['hostUuid' => $value['hostUuid'], 'serviceUuid' => $value['serviceUuid'], 'type' => $value['options'], 'author' => $value['author'], 'comment' => $value['comment']]);
257+
$result = $externalNaemonCommands->sendCustomServiceNotification(['hostUuid' => $value['hostUuid'], 'serviceUuid' => $value['serviceUuid'], 'type' => $value['options'], 'author' => $value['author'], 'comment' => $value['comment']]);
226258
break;
227259
case 'sendCustomHostNotification':
228260
$result = $externalNaemonCommands->sendCustomHostNotification(['hostUuid' => $value['hostUuid'], 'type' => $value['options'], 'author' => $value['author'], 'comment' => $value['comment']]);
@@ -249,7 +281,7 @@ public function submit_bulk_naemon(){
249281
$result = false;
250282
break;
251283
}
252-
if(!$result) {
284+
if (!$result) {
253285
throw new BadRequestException('Could not send command');
254286
}
255287
}

plugins/NagiosModule/templates/Cmd/index.php

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,26 @@
11
<?php
2-
// Copyright (C) <2015> <it-novum GmbH>
2+
// Copyright (C) <2015-present> <it-novum GmbH>
33
//
44
// This file is dual licensed
55
//
66
// 1.
7-
// This program is free software: you can redistribute it and/or modify
8-
// it under the terms of the GNU General Public License as published by
9-
// the Free Software Foundation, version 3 of the License.
7+
// This program is free software: you can redistribute it and/or modify
8+
// it under the terms of the GNU General Public License as published by
9+
// the Free Software Foundation, version 3 of the License.
1010
//
11-
// This program is distributed in the hope that it will be useful,
12-
// but WITHOUT ANY WARRANTY; without even the implied warranty of
13-
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14-
// GNU General Public License for more details.
11+
// This program is distributed in the hope that it will be useful,
12+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14+
// GNU General Public License for more details.
1515
//
16-
// You should have received a copy of the GNU General Public License
17-
// along with this program. If not, see <http://www.gnu.org/licenses/>.
16+
// You should have received a copy of the GNU General Public License
17+
// along with this program. If not, see <http://www.gnu.org/licenses/>.
1818
//
19+
// 2.
20+
// If you purchased an openITCOCKPIT Enterprise Edition you can use this file
21+
// under the terms of the openITCOCKPIT Enterprise Edition license agreement.
22+
// License agreement and license key will be shipped with the order
23+
// confirmation.
1924

2025
// 2.
2126
// If you purchased an openITCOCKPIT Enterprise Edition you can use this file
@@ -65,6 +70,9 @@
6570
<?php echo __('This API can be used by third party application to send commands to the monitoring backend.'); ?>
6671
<br/>
6772
<?php echo __('This could be useful to transfer passive check results or to acknowledge host or service states.'); ?>
73+
<br/>
74+
<code>hostUuid</code> <?php echo __('and'); ?> <code>serviceUuid</code>
75+
<?php echo __('can also be passed by their name, e.g. "localhost" or "Ping". It is recommended to use the UUIDs when possible.'); ?>
6876
</div>
6977
</div>
7078
</div>

0 commit comments

Comments
 (0)