Lines
100.00%
55 / 55
Functions and Methods
100.00%
2 / 2
Classes and Traits
100.00%
1 / 1
| Name | Lines | Functions and Methods | CRAP | Classes and Traits | ||||||
|---|---|---|---|---|---|---|---|---|---|---|
| Status | 100.00% | 55 / 55 | 100.00% | 2 / 2 | 25 | 100.00% | 1 / 1 | |||
| testStatus | 100.00% | 45 / 45 | 100.00% | 1 / 1 | 20 | |||||
| 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 | * | |
| 16 | * @SuppressWarnings(Complexity) | |
| 17 | */ | |
| 18 | public static function testStatus(ResponseInterface $response, mixed $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 !== false && $status !== null && $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 | && is_string($status['processes']['lastCalculate']) | |
| 56 | ) { | |
| 57 | $lastCalculate = strtotime($status['processes']['lastCalculate']); | |
| 58 | if ($lastCalculate !== false && time() - $lastCalculate > 600) { | |
| 59 | $slotOutdate = time() - $lastCalculate; | |
| 60 | $result[] = "WARN - slot calculation is $slotOutdate seconds old"; | |
| 61 | } | |
| 62 | } | |
| 63 | $result = preg_grep('/./', $result); | |
| 64 | if (!count($result)) { | |
| 65 | $result = "OK - " | |
| 66 | . "DB=" . $status['database']['nodeConnections'] . "%" | |
| 67 | . " Threads=" . $status['database']['threads'] | |
| 68 | . " Locks=" . $status['database']['locks'] | |
| 69 | ; | |
| 70 | } else { | |
| 71 | $result = implode('; ', $result); | |
| 72 | } | |
| 73 | } | |
| 74 | ||
| 75 | $response->getBody()->write($result); | |
| 76 | $response = $response->withHeader('Content-Type', 'text/plain'); | |
| 77 | ||
| 78 | if (strpos($result, 'CRIT') !== false || strpos($result, 'FATAL') !== false) { | |
| 79 | $response = $response->withStatus( | |
| 80 | StatusCodeInterface::STATUS_INTERNAL_SERVER_ERROR, | |
| 81 | 'The Server is in a bad condition.' | |
| 82 | ); | |
| 83 | } | |
| 84 | ||
| 85 | return $response; | |
| 86 | } | |
| 87 | ||
| 88 | public static function getDldbUpdateStats(mixed $status): string | |
| 89 | { | |
| 90 | $result = ''; | |
| 91 | if (isset($status['sources'])) { | |
| 92 | $now = new \DateTimeImmutable(); | |
| 93 | $lastDldbUpdate = new \DateTimeImmutable($status['sources']['dldb']['last']); | |
| 94 | if ( | |
| 95 | ($lastDldbUpdate->getTimestamp() + 7200) < $now->getTimestamp() && | |
| 96 | ($lastDldbUpdate->getTimestamp() + 14400) > $now->getTimestamp() | |
| 97 | ) { | |
| 98 | $result = "WARN - Last DLDB Import is more then 2 hours ago"; | |
| 99 | } elseif (($lastDldbUpdate->getTimestamp() + 14400) < $now->getTimestamp()) { | |
| 100 | $result = "CRIT - Last DLDB Import is more then 4 hours ago"; | |
| 101 | } | |
| 102 | } | |
| 103 | return $result; | |
| 104 | } | |
| 105 | } |