summaryrefslogtreecommitdiffstats
path: root/modules/monitoring/application/clicommands/NrpeCommand.php
blob: fe823222e41617bb88127513fb1023e38b72d01c (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
<?php
/* Icinga Web 2 | (c) 2013 Icinga Development Team | GPLv2+ */

namespace Icinga\Module\Monitoring\Clicommands;

use Icinga\Protocol\Nrpe\Connection;
use Icinga\Cli\Command;
use Exception;

/**
 * NRPE
 */
class NrpeCommand extends Command
{
    protected $defaultActionName = 'check';

    /**
     * Execute an NRPE command
     *
     * This command will execute an NRPE check, fire it against the given host
     * and also pass through all your parameters. Output will be shown, exit
     * code respected.
     *
     * USAGE
     *
     * icingacli monitoring nrpe <host> <command> [--ssl] [nrpe options]
     *
     * EXAMPLE
     *
     * icingacli monitoring nrpe 127.0.0.1 CheckMEM --ssl --MaxWarn=80% \
     *   --MaxCrit=90% --type=physical
     */
    public function checkAction()
    {
        $host = $this->params->shift();
        if (! $host) {
            echo $this->showUsage();
            exit(3);
        }
        $command = $this->params->shift(null, '_NRPE_CHECK');
        $port = $this->params->shift('port', 5666);
        try {
            $nrpe = new Connection($host, $port);
            if ($this->params->shift('ssl')) {
                $nrpe->useSsl();
            }
            $args = array();
            foreach ($this->params->getParams() as $k => $v) {
                $args[] = $k . '=' . $v;
            }
            echo $nrpe->sendCommand($command, $args) . "\n";
            exit($nrpe->getLastReturnCode());
        } catch (Exception $e) {
            echo $e->getMessage() . "\n";
            exit(3);
        }
    }
}