1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Leankoala\HealthFoundation\Check\System; |
4
|
|
|
|
5
|
|
|
use Leankoala\HealthFoundation\Check\Check; |
6
|
|
|
use Leankoala\HealthFoundation\Check\Result; |
7
|
|
|
|
8
|
|
|
class UptimeCheck implements Check |
9
|
|
|
{ |
10
|
|
|
const IDENTIFIER = 'base:system:uptime'; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* @var \DateInterval |
14
|
|
|
*/ |
15
|
|
|
private $dateInterval; |
16
|
|
|
|
17
|
|
|
public function init($maxUptime) |
18
|
|
|
{ |
19
|
|
|
$this->dateInterval = \DateInterval::createFromDateString($maxUptime); |
20
|
|
|
} |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Checks if the server uptime is ok |
24
|
|
|
* |
25
|
|
|
* @return Result |
26
|
|
|
*/ |
27
|
|
|
public function run() |
28
|
|
|
{ |
29
|
|
|
try { |
30
|
|
|
$uptime = $this->getUptime(); |
31
|
|
|
if ($this->dateIntervalToSeconds($uptime) > $this->dateIntervalToSeconds($this->dateInterval)) { |
32
|
|
|
return new Result(Result::STATUS_FAIL, 'Servers uptime is too high (' . $this->dateIntervalToString($uptime) . ')'); |
33
|
|
|
} else { |
34
|
|
|
return new Result(Result::STATUS_PASS, 'Servers uptime is ok (' . $this->dateIntervalToString($uptime) . ')'); |
35
|
|
|
} |
36
|
|
|
} catch (\Exception $e) { |
37
|
|
|
return new Result(Result::STATUS_FAIL, 'Error: ' . $e->getMessage()); |
38
|
|
|
} |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @return \DateInterval |
43
|
|
|
* @throws \Exception |
44
|
|
|
*/ |
45
|
|
|
private function getUptime() |
46
|
|
|
{ |
47
|
|
|
$uptimeTimestamp = \uptime(); |
48
|
|
|
|
49
|
|
|
$systemStartDate = new \DateTime(date('Y-m-d H:i:s', (int)$uptimeTimestamp)); |
50
|
|
|
$now = new \DateTime(); |
51
|
|
|
|
52
|
|
|
$uptime = $systemStartDate->diff($now); |
53
|
|
|
|
54
|
|
|
if ($uptime === false) { |
55
|
|
|
throw new \RuntimeException('Uptime cannot be calculated.'); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
return $uptime; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @param \DateInterval $dateInterval |
63
|
|
|
* @return int |
64
|
|
|
* @throws \Exception |
65
|
|
|
*/ |
66
|
|
|
private function dateIntervalToSeconds(\DateInterval $dateInterval) |
67
|
|
|
{ |
68
|
|
|
$reference = new \DateTimeImmutable; |
69
|
|
|
$endTime = $reference->add($dateInterval); |
70
|
|
|
|
71
|
|
|
return $endTime->getTimestamp() - $reference->getTimestamp(); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
private function dateIntervalToString(\DateInterval $dateInterval) |
75
|
|
|
{ |
76
|
|
|
$string = ''; |
77
|
|
|
|
78
|
|
|
if ($dateInterval->y > 0) { |
79
|
|
|
$string .= $dateInterval->y . ' years '; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
if ($dateInterval->d > 0) { |
83
|
|
|
$string .= $dateInterval->d . ' days '; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
if ($dateInterval->h > 0) { |
87
|
|
|
$string .= $dateInterval->h . ' hours '; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
if ($dateInterval->i > 0) { |
91
|
|
|
$string .= $dateInterval->i . ' minutes '; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
return trim($string); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
public function getIdentifier() |
98
|
|
|
{ |
99
|
|
|
return self::IDENTIFIER; |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
|