public function getPreviewHtml(MonitoredObject $object)
 {
     $object->fetchCustomvars();
     if (array_key_exists("graphite_keys", $object->customvars)) {
         $graphiteKeys = $object->customvars["graphite_keys"];
     } else {
         $graphiteKeys = array();
         foreach (PerfdataSet::fromString($object->perfdata)->asArray() as $pd) {
             $graphiteKeys[] = $pd->getLabel();
         }
     }
     if ($object instanceof Host) {
         $host = $object;
         $service = null;
     } elseif ($object instanceof Service) {
         $service = $object;
         $host = null;
     } else {
         return '';
     }
     $html = "<table class=\"avp newsection\">\n" . "<tbody>\n";
     foreach ($graphiteKeys as $metric) {
         $html .= "<tr><th>\n" . "{$metric}\n" . '</th><td>' . $this->getPreviewImage($host, $service, $metric) . "</td>\n" . "<tr>\n";
     }
     $html .= "</tbody></table>\n";
     return $html;
 }
 public function getPerfdataKeys($object)
 {
     foreach (PerfdataSet::fromString($object->perfdata)->asArray() as $pd) {
         $this->graphiteKeys[] = $pd->getLabel();
         $this->graphiteLabels[] = $pd->getLabel();
     }
 }
Example #3
0
 /**
  * @depends testWhetherValidSimplePerfdataLabelsAreProperlyParsed
  */
 public function testWhetherAPerfdataSetIsIterable()
 {
     $pset = PerfdataSet::fromString('key=value');
     foreach ($pset as $p) {
         $this->assertEquals('key', $p->getLabel());
         return;
     }
     $this->fail('PerfdataSet objects cannot be iterated');
 }
Example #4
0
 /**
  * Display the given perfdata string to the user
  *
  * @param   string  $perfdataStr    The perfdata string
  * @param   bool    $compact        Whether to display the perfdata in compact mode
  * @param   int     $limit          Max labels to show; 0 for no limit
  * @param   string  $color          The color indicating the perfdata state
  *
  * @return string
  */
 public function perfdata($perfdataStr, $compact = false, $limit = 0, $color = Perfdata::PERFDATA_OK)
 {
     $pieChartData = PerfdataSet::fromString($perfdataStr)->asArray();
     uasort($pieChartData, function ($a, $b) {
         return $a->worseThan($b) ? -1 : ($b->worseThan($a) ? 1 : 0);
     });
     $results = array();
     $keys = array('', 'label', 'value', 'min', 'max', 'warn', 'crit');
     $columns = array();
     $labels = array_combine($keys, array('', $this->view->translate('Label'), $this->view->translate('Value'), $this->view->translate('Min'), $this->view->translate('Max'), $this->view->translate('Warning'), $this->view->translate('Critical')));
     foreach ($pieChartData as $perfdata) {
         if ($perfdata->isVisualizable()) {
             $columns[''] = '';
         }
         foreach ($perfdata->toArray() as $column => $value) {
             if (empty($value) || $column === 'min' && floatval($value) === 0.0 || $column === 'max' && $perfdata->isPercentage() && floatval($value) === 100) {
                 continue;
             }
             $columns[$column] = $labels[$column];
         }
     }
     // restore original column array sorting
     $headers = array();
     foreach ($keys as $column) {
         if (isset($columns[$column])) {
             $headers[$column] = $labels[$column];
         }
     }
     $table = array('<thead><tr><th>' . implode('</th><th>', $headers) . '</th></tr></thead><tbody>');
     foreach ($pieChartData as $perfdata) {
         if ($compact && $perfdata->isVisualizable()) {
             $results[] = $perfdata->asInlinePie($color)->render();
         } else {
             $data = array();
             if ($perfdata->isVisualizable()) {
                 $data[] = $perfdata->asInlinePie($color)->render();
             } elseif (isset($columns[''])) {
                 $data[] = '';
             }
             if (!$compact) {
                 foreach ($perfdata->toArray() as $column => $value) {
                     if (!isset($columns[$column])) {
                         continue;
                     }
                     $text = $this->view->escape(empty($value) ? '-' : $value);
                     $data[] = sprintf('<span title="%s">%s</span>', $text, String::ellipsisCenter($text, 24));
                 }
             }
             $table[] = '<tr><td class="sparkline-col">' . implode('</td><td>', $data) . '</td></tr>';
         }
     }
     $table[] = '</tbody>';
     if ($limit > 0) {
         $count = $compact ? count($results) : count($table);
         if ($count > $limit) {
             if ($compact) {
                 $results = array_slice($results, 0, $limit);
                 $title = sprintf($this->view->translate('%d more ...'), $count - $limit);
                 $results[] = '<span title="' . $title . '">...</span>';
             } else {
                 $table = array_slice($table, 0, $limit);
             }
         }
     }
     if ($compact) {
         return join('', $results);
     } else {
         if (empty($table)) {
             return '';
         }
         return sprintf('<table class="performance-data-table">%s</table>', implode("\n", $table));
     }
 }
 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;
 }