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