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