Exemple #1
0
});
$this->addHelperFunction('formatDuration', function ($seconds) {
    if (!$seconds) {
        return '';
    }
    return DateFormatter::formatDuration($seconds);
});
$this->addHelperFunction('formatTime', function ($time) {
    if (!$time) {
        return '';
    }
    return DateFormatter::formatTime($time);
});
$this->addHelperFunction('timeAgo', function ($time, $timeOnly = false) {
    if (!$time) {
        return '';
    }
    return sprintf('<span class="relative-time time-ago" title="%s">%s</span>', DateFormatter::formatDateTime($time), DateFormatter::timeAgo($time, $timeOnly));
});
$this->addHelperFunction('timeSince', function ($time, $timeOnly = false) {
    if (!$time) {
        return '';
    }
    return sprintf('<span class="relative-time time-since" title="%s">%s</span>', DateFormatter::formatDateTime($time), DateFormatter::timeSince($time, $timeOnly));
});
$this->addHelperFunction('timeUntil', function ($time, $timeOnly = false) {
    if (!$time) {
        return '';
    }
    return sprintf('<span class="relative-time time-until" title="%s">%s</span>', DateFormatter::formatDateTime($time), DateFormatter::timeUntil($time, $timeOnly));
});
 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;
 }