Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
54 / 54
100.00% covered (success)
100.00%
2 / 2
CRAP
100.00% covered (success)
100.00%
1 / 1
Status
100.00% covered (success)
100.00%
54 / 54
100.00% covered (success)
100.00%
2 / 2
22
100.00% covered (success)
100.00%
1 / 1
 testStatus
100.00% covered (success)
100.00%
44 / 44
100.00% covered (success)
100.00%
1 / 1
17
 getDldbUpdateStats
100.00% covered (success)
100.00%
10 / 10
100.00% covered (success)
100.00%
1 / 1
5
1<?php
2
3namespace BO\Zmsclient;
4
5use Fig\Http\Message\StatusCodeInterface;
6use Psr\Http\Message\ResponseInterface;
7
8/**
9 * Healthcheck concerning the API
10 */
11class Status
12{
13    /**
14     * throws exception on critical status variables
15     *
16     * @SuppressWarnings(Complexity)
17     */
18    public static function testStatus(ResponseInterface $response, $status): ResponseInterface
19    {
20        $result = '';
21        if ($status instanceof \Closure) {
22            try {
23                $status = $status();
24            } catch (\Exception $exception) {
25                $status = false;
26                $result = "FATAL - " . $exception->getMessage();
27            }
28        }
29        if ($status && !$result) {
30            $result = [];
31            $result[] = self::getDldbUpdateStats($status);
32
33            if ($status['mail']['oldestSeconds'] > 300) {
34                $result[] = "WARN - Oldest mail with age in seconds: "
35                    . $status['mail']['oldestSeconds'] . 's';
36            }
37            if ($status['database']['logbin'] != 'ON') {
38                $result[] = "WARN - DB connection without replication log detected";
39            }
40            if ($status['database']['clusterStatus'] == 'OFF') {
41                $result[] = "WARN - DB connection is not part of a galera cluster";
42            }
43            if ($status['database']['locks'] > 10) {
44                $result[] = "WARN - High amount of DB-Locks: " . $status['database']['locks'];
45            }
46            if ($status['database']['threads'] > 30) {
47                $result[] = "WARN - High amount of DB-Threads: " . $status['database']['threads'];
48            }
49            if ($status['database']['nodeConnections'] > 50) {
50                $result[] = "WARN - DB connected thread over 50% of available connections";
51            }
52            if (
53                isset($status['processes'])
54                && isset($status['processes']['lastCalculate'])
55                && time() - strtotime($status['processes']['lastCalculate']) > 600
56            ) {
57                $slotOutdate =
58                    time() - strtotime($status['processes']['lastCalculate']);
59                $result[] = "WARN - slot calculation is $slotOutdate seconds old";
60            }
61            $result = preg_grep('/./', $result);
62            if (!count($result)) {
63                $result = "OK - "
64                    . "DB=" . $status['database']['nodeConnections'] . "%"
65                    . " Threads=" . $status['database']['threads']
66                    . " Locks=" . $status['database']['locks']
67                    ;
68            } else {
69                $result = implode('; ', $result);
70            }
71        }
72
73        $response->getBody()->write($result);
74        $response = $response->withHeader('Content-Type', 'text/plain');
75
76        if (strpos($result, 'CRIT') !== false || strpos($result, 'FATAL') !== false) {
77            $response = $response->withStatus(
78                StatusCodeInterface::STATUS_INTERNAL_SERVER_ERROR,
79                'The Server is in a bad condition.'
80            );
81        }
82
83        return $response;
84    }
85
86    public static function getDldbUpdateStats($status): string
87    {
88        $result = '';
89        if (isset($status['sources'])) {
90            $now = new \DateTimeImmutable();
91            $lastDldbUpdate = new \DateTimeImmutable($status['sources']['dldb']['last']);
92            if (
93                ($lastDldbUpdate->getTimestamp() + 7200) < $now->getTimestamp() &&
94                ($lastDldbUpdate->getTimestamp() + 14400) > $now->getTimestamp()
95            ) {
96                $result = "WARN - Last DLDB Import is more then 2 hours ago";
97            } elseif (($lastDldbUpdate->getTimestamp() + 14400) < $now->getTimestamp()) {
98                $result = "CRIT - Last DLDB Import is more then 4 hours ago";
99            }
100        }
101        return $result;
102    }
103}