summaryrefslogtreecommitdiffstats
path: root/library/Director/Web/Widget/HealthCheckPluginOutput.php
blob: 83ac1029ffb2cd3d613c3311c4f7216f27be3ae1 (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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
<?php

namespace Icinga\Module\Director\Web\Widget;

use ipl\Html\Html;
use ipl\Html\HtmlDocument;
use ipl\Html\HtmlString;
use gipfl\Translation\TranslationHelper;
use Icinga\Module\Director\CheckPlugin\PluginState;
use Icinga\Module\Director\Health;

class HealthCheckPluginOutput extends HtmlDocument
{
    use TranslationHelper;

    /** @var Health */
    protected $health;

    /** @var PluginState */
    protected $state;

    public function __construct(Health $health)
    {
        $this->state = new PluginState('OK');
        $this->health = $health;
        $this->process();
    }

    protected function process()
    {
        $checks = $this->health->getAllChecks();

        foreach ($checks as $check) {
            $this->add([
                $title = Html::tag('h1', $check->getName()),
                $ul = Html::tag('ul', ['class' => 'health-check-result'])
            ]);

            $problems = $check->getProblemSummary();
            if (! empty($problems)) {
                $badges = Html::tag('span', ['class' => 'title-badges']);
                foreach ($problems as $state => $count) {
                    $badges->add(Html::tag('span', [
                        'class' => ['badge', 'state-' . strtolower($state)],
                        'title' => sprintf(
                            $this->translate('%s: %d'),
                            $this->translate($state),
                            $count
                        ),
                    ], $count));
                }
                $title->add($badges);
            }

            foreach ($check->getResults() as $result) {
                $state = $result->getState()->getName();
                $ul->add(Html::tag('li', [
                    'class' => 'state state-' . strtolower($state)
                ], $this->highlightNames($result->getOutput()))->setSeparator(' '));
            }
            $this->state->raise($check->getState());
        }
    }

    public function getState()
    {
        return $this->state;
    }

    protected function colorizeState($state)
    {
        return Html::tag('span', ['class' => 'badge state-' . strtolower($state)], $state);
    }

    protected function highlightNames($string)
    {
        $string = Html::escape($string);
        return new HtmlString(preg_replace_callback(
            "/'([^']+)'/",
            [$this, 'highlightName'],
            $string
        ));
    }

    protected function highlightName($match)
    {
        return '"' . Html::tag('strong', $match[1]) . '"';
    }

    protected function getColorized($match)
    {
        return $this->colorizeState($match[1]);
    }
}