File: ProcessesProblemsBadge.php

package info (click to toggle)
icingaweb2-module-businessprocess 2.5.2-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 3,720 kB
  • sloc: php: 10,971; javascript: 2,019; sh: 115; xml: 49; makefile: 18
file content (69 lines) | stat: -rw-r--r-- 2,344 bytes parent folder | download
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
<?php

namespace Icinga\Module\Businessprocess\Web\Navigation\Renderer;

use Icinga\Application\Logger;
use Icinga\Application\Modules\Module;
use Icinga\Module\Businessprocess\Node;
use Icinga\Module\Businessprocess\ProvidedHook\Icingadb\IcingadbSupport;
use Icinga\Module\Businessprocess\State\IcingaDbState;
use Icinga\Module\Businessprocess\State\MonitoringState;
use Icinga\Module\Businessprocess\Storage\LegacyStorage;
use Icinga\Web\Navigation\Renderer\BadgeNavigationItemRenderer;
use Throwable;

class ProcessesProblemsBadge extends BadgeNavigationItemRenderer
{
    /**
     * Cached count
     *
     * @var int
     */
    protected $count;

    public function getCount()
    {
        if ($this->count === null) {
            $count = 0;
            $state = Node::ICINGA_OK;

            try {
                $storage = LegacyStorage::getInstance();

                foreach ($storage->listProcessNames() as $processName) {
                    $bp = $storage->loadProcess($processName);
                    if (Module::exists('icingadb') &&
                        (! $bp->hasBackendName() && IcingadbSupport::useIcingaDbAsBackend())
                    ) {
                        IcingaDbState::apply($bp);
                    } else {
                        MonitoringState::apply($bp);
                    }

                    foreach ($bp->getRootNodes() as $rootNode) {
                        $nodeState = $rootNode->getState();
                        if (! $rootNode->isEmpty() &&
                            ! in_array($nodeState, [Node::ICINGA_OK, Node::ICINGA_PENDING], true)
                        ) {
                            if ($nodeState === $state) {
                                $count++;
                            } elseif ($nodeState > $state) {
                                $count = 1;
                                $state = $nodeState;
                            }

                            break;
                        }
                    }
                }
            } catch (Throwable $e) {
                Logger::error('Failed to load business processes: %s', $e);
            }

            $this->count = $count;
            $this->setState(ProcessProblemsBadge::NODE_STATE_TO_BADGE_STATE[$state] ?? self::STATE_UNKNOWN);
        }

        return $this->count;
    }
}