From 8ca6cc32b2c789a3149861159ad258f2cb9491e3 Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Sun, 28 Apr 2024 14:39:39 +0200 Subject: Adding upstream version 2.11.4. Signed-off-by: Daniel Baumann --- .../application/clicommands/ListCommand.php | 400 +++++++++++++++++++++ .../application/clicommands/NrpeCommand.php | 58 +++ 2 files changed, 458 insertions(+) create mode 100644 modules/monitoring/application/clicommands/ListCommand.php create mode 100644 modules/monitoring/application/clicommands/NrpeCommand.php (limited to 'modules/monitoring/application/clicommands') diff --git a/modules/monitoring/application/clicommands/ListCommand.php b/modules/monitoring/application/clicommands/ListCommand.php new file mode 100644 index 0000000..6dc4193 --- /dev/null +++ b/modules/monitoring/application/clicommands/ListCommand.php @@ -0,0 +1,400 @@ +backend = MonitoringBackend::instance($this->params->shift('backend')); + $this->dumpSql = $this->params->shift('showsql'); + } + + protected function getQuery($table, $columns) + { + $limit = $this->params->shift('limit'); + $format = $this->params->shift('format'); + if ($format !== null) { + if ($this->params->has('columns')) { + $columnParams = preg_split( + '/,/', + $this->params->shift('columns') + ); + $columns = array(); + foreach ($columnParams as $col) { + if (false !== ($pos = strpos($col, '='))) { + $columns[substr($col, 0, $pos)] = substr($col, $pos + 1); + } else { + $columns[] = $col; + } + } + } + } + + $query = $this->backend->select()->from($table, $columns); + if ($limit) { + $query->limit($limit, $this->params->shift('offset')); + } + foreach ($this->params->getParams() as $col => $filter) { + $query->where($col, $filter); + } + // $query->applyFilters($this->params->getParams()); + if ($this->dumpSql) { + echo wordwrap($query->dump(), 72); + exit; + } + + if ($format !== null) { + $this->showFormatted($query, $format, $columns); + } + + return $query; + } + + protected function showFormatted($query, $format, $columns) + { + $query = $query->getQuery(); + switch ($format) { + case 'json': + echo Json::sanitize($query->fetchAll()); + break; + case 'csv': + Csv::fromQuery($query)->dump(); + break; + default: + preg_match_all('~\$([a-z0-9_-]+)\$~', $format, $m); + $words = array(); + foreach ($columns as $key => $col) { + if (is_numeric($key)) { + if (in_array($col, $m[1])) { + $words[] = $col; + } + } else { + if (in_array($key, $m[1])) { + $words[] = $key; + } + } + } + foreach ($query->fetchAll() as $row) { + $output = $format; + foreach ($words as $word) { + $output = preg_replace( + '~\$' . $word . '\$~', + $row->{$word}, + $output + ); + } + echo $output . "\n"; + } + } + exit; + } + + /** + * List and filter hosts + * + * This command allows you to search and visualize your hosts in + * different ways. + * + * USAGE + * + * icingacli monitoring list hosts [options] + * + * OPTIONS + * + * --verbose Show detailled output + * --showsql Dump generated SQL query (DB backend only) + * + * --format=> + * Dump columns in the given format. format allows $column$ + * placeholders, e.g. --format='$host$: $service$'. This requires + * that the columns are specified within the --columns parameter. + * + * --[=filter] + * Filter given column by optional filter. Boolean (1/0) columns are + * true if no filter value is given. + * + * --problems + * Only show unhandled problems (HARD state and not acknowledged/in downtime). + * + * --columns='' + * Add a limited set of columns to the output. The following host + * attributes can be fetched: state, handled, output, acknowledged, in_downtime, perfdata last_state_change + * + * EXAMPLES + * + * icingacli monitoring list hosts --problems + * icingacli monitoring list hosts --problems --host_state_type 0 + * icingacli monitoring list hosts --host=local* + * icingacli monitoring list hosts --columns 'host,host_output' \ + * --format='$host$ ($host_output$)' + */ + public function hostsAction() + { + $columns = array( + 'host_name', + 'host_state', + 'host_output', + 'host_handled', + 'host_acknowledged', + 'host_in_downtime' + ); + $query = $this->getQuery('hoststatus', $columns) + ->order('host_name'); + echo $this->renderStatusQuery($query); + } + + /** + * List and filter services + * + * This command allows you to search and visualize your services in + * different ways. + * + * USAGE + * + * icingacli monitoring list services [options] + * + * OPTIONS + * + * --verbose Show detailled output + * --showsql Dump generated SQL query (DB backend only) + * + * --format=> + * Dump columns in the given format. format allows $column$ + * placeholders, e.g. --format='$host$: $service$'. This requires + * that the columns are specified within the --columns parameter. + * + * --[=filter] + * Filter given column by optional filter. Boolean (1/0) columns are + * true if no filter value is given. + * + * --problems + * Only show unhandled problems (HARD state and not acknowledged/in downtime). + * + * --columns='' + * Add a limited set of columns to the output. The following service + * attributes can be fetched: state, handled, output, acknowledged, in_downtime, perfdata last_state_change + * + * EXAMPLES + * + * icingacli monitoring list services --problems + * icingacli monitoring list services --problems --service_state_type 0 + * icingacli monitoring list services --host=local* --service=*disk* + * icingacli monitoring list services --columns 'host,service,service_output' \ + * --format='$host$: $service$ ($service_output$)' + */ + public function servicesAction() + { + $columns = array( + 'host_name', + 'host_state', + 'host_output', + 'host_handled', + 'host_acknowledged', + 'host_in_downtime', + 'service_description', + 'service_state', + 'service_acknowledged', + 'service_in_downtime', + 'service_handled', + 'service_output', + 'service_perfdata', + 'service_last_state_change' + ); + $query = $this->getQuery('servicestatus', $columns) + ->order('host_name'); + echo $this->renderStatusQuery($query); + } + + protected function renderStatusQuery($query) + { + $out = ''; + $last_host = null; + $screen = $this->screen; + $utils = new CliUtils($screen); + $maxCols = $screen->getColumns(); + $query = $query->getQuery(); + $rows = $query->fetchAll(); + $count = $query->count(); + $count = count($rows); + + for ($i = 0; $i < $count; $i++) { + $row = & $rows[$i]; + + $utils->setHostState($row->host_state); + if (! array_key_exists($i + 1, $rows) + || $row->host_name !== $rows[$i + 1]->host_name + ) { + $lastService = true; + } else { + $lastService = false; + } + + $hostUnhandled = ! ($row->host_state == 0 || $row->host_handled); + + if ($row->host_name !== $last_host) { + if (isset($row->service_description)) { + $out .= "\n"; + } + + $hostTxt = $utils->shortHostState(); + if ($hostUnhandled) { + $out .= $utils->hostStateBackground( + sprintf(' %s ', $utils->shortHostState()) + ); + } else { + $out .= sprintf( + '%s %s ', + $utils->hostStateBackground(' '), + $utils->shortHostState() + ); + } + $out .= sprintf( + " %s%s: %s\n", + $screen->underline($row->host_name), + $screen->colorize($utils->objectStateFlags('host', $row), 'lightblue'), + $row->host_output + ); + + if (isset($row->services_ok)) { + $out .= sprintf( + "%d services, %d problems (%d unhandled), %d OK\n", + $row->services_cnt, + $row->services_problem, + $row->services_problem_unhandled, + $row->services_ok + ); + } + } + + $last_host = $row->host_name; + if (! isset($row->service_description)) { + continue; + } + + $utils->setServiceState($row->service_state); + $serviceUnhandled = ! ( + $row->service_state == 0 || $row->service_handled + ); + + if ($lastService) { + $straight = ' '; + $leaf = '└'; + } else { + $straight = '│'; + $leaf = '├'; + } + $out .= $utils->hostStateBackground(' '); + + if ($serviceUnhandled) { + $out .= $utils->serviceStateBackground( + sprintf(' %s ', $utils->shortServiceState()) + ); + $emptyBg = ' '; + $emptySpace = ''; + } else { + $out .= sprintf( + '%s %s ', + $utils->serviceStateBackground(' '), + $utils->shortServiceState() + ); + $emptyBg = ' '; + $emptySpace = ' '; + } + + $emptyLine = "\n" + . $utils->hostStateBackground(' ') + . $utils->serviceStateBackground($emptyBg) + . $emptySpace + . ' ' . $straight . ' '; + + $perf = ''; + try { + $pset = PerfdataSet::fromString($row->service_perfdata); + $perfs = array(); + foreach ($pset as $p) { + if ($percent = $p->getPercentage()) { + if ($percent < 0 || $percent > 100) { + continue; + } + $perfs[] = ' ' + . $p->getLabel() + . ': ' + . $this->getPercentageSign($percent) + . ' ' + . number_format($percent, 2, ',', '.') + . '%'; + } + } + if (! empty($perfs)) { + $perf = ', ' . implode($perfs); + } + // TODO: fix wordwarp, then remove this line: + $perf = ''; + } catch (Exception $e) { + // Ignoring perfdata errors right now, we could show some hint + } + + $wrappedOutput = wordwrap( + preg_replace('~\@{3,}~', '@@@', $row->service_output), + $maxCols - 13 + ) . "\n"; + $out .= sprintf( + " %1s─ %s%s (%s)", + $leaf, + $screen->underline($row->service_description), + $screen->colorize($utils->objectStateFlags('service', $row) . $perf, 'lightblue'), + ucfirst(DateFormatter::timeSince($row->service_last_state_change)) + ); + if ($this->isVerbose) { + $out .= $emptyLine . preg_replace( + '/\n/', + $emptyLine, + $wrappedOutput + ) . "\n"; + } else { + $out .= "\n"; + } + } + + $out .= "\n"; + return $out; + } + + protected function getPercentageSign($percent) + { + $circles = array( + 0 => '○', + 15 => '◔', + 40 => '◑', + 65 => '◕', + 90 => '●', + ); + $last = $circles[0]; + foreach ($circles as $cur => $circle) { + if ($percent < $cur) { + return $last; + } + $last = $circle; + } + } +} diff --git a/modules/monitoring/application/clicommands/NrpeCommand.php b/modules/monitoring/application/clicommands/NrpeCommand.php new file mode 100644 index 0000000..fe82322 --- /dev/null +++ b/modules/monitoring/application/clicommands/NrpeCommand.php @@ -0,0 +1,58 @@ + [--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); + } + } +} -- cgit v1.2.3