1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Spatie\ServerMonitor\CheckDefinitions; |
4
|
|
|
|
5
|
|
|
use Exception; |
6
|
|
|
use Spatie\ServerMonitor\Models\Check; |
7
|
|
|
use Spatie\ServerMonitor\Models\Enums\CheckStatus; |
8
|
|
|
use Symfony\Component\Process\Process; |
9
|
|
|
|
10
|
|
|
abstract class CheckDefinition |
11
|
|
|
{ |
12
|
|
|
/** @var \Spatie\ServerMonitor\Models\Check */ |
13
|
|
|
protected $check; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @param \Spatie\ServerMonitor\Models\Check $check |
17
|
|
|
* |
18
|
|
|
* @return $this |
19
|
|
|
*/ |
20
|
|
|
public function setCheck(Check $check) |
21
|
|
|
{ |
22
|
|
|
$this->check = $check; |
23
|
|
|
|
24
|
|
|
return $this; |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
public function command(): string |
28
|
|
|
{ |
29
|
|
|
return $this->command; |
|
|
|
|
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
public function determineResult(Process $process) |
33
|
|
|
{ |
34
|
|
|
$this->check->storeProcessOutput($process); |
35
|
|
|
|
36
|
|
|
try { |
37
|
|
|
if (! empty($process->getErrorOutput())) { |
38
|
|
|
$this->resolveFailed($process); |
39
|
|
|
|
40
|
|
|
return; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
$this->resolve($process); |
44
|
|
|
} catch (Exception $exception) { |
45
|
|
|
$this->check->fail('Exception occurred: '.$exception->getMessage()); |
46
|
|
|
} |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
abstract public function resolve(Process $process); |
50
|
|
|
|
51
|
|
|
public function resolveFailed(Process $process) |
52
|
|
|
{ |
53
|
|
|
$this->check->fail("failed to run: {$process->getErrorOutput()}"); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* When a check is emitting a warning or is failing, a notification will only |
58
|
|
|
* be sent once in given amount of minutes. |
59
|
|
|
* |
60
|
|
|
* @return int |
61
|
|
|
*/ |
62
|
|
|
public function throttleFailingNotificationsForMinutes(): int |
63
|
|
|
{ |
64
|
|
|
return config('server-monitor.notifications.throttle_failing_notifications_for_minutes'); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
public function performNextRunInMinutes(): int |
68
|
|
|
{ |
69
|
|
|
if ($this->check->hasStatus(CheckStatus::SUCCESS)) { |
70
|
|
|
return (int) config('server-monitor.next_run_in_minutes'); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
return 0; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* The amount of seconds that check may run. |
78
|
|
|
* |
79
|
|
|
* @return int |
80
|
|
|
*/ |
81
|
|
|
public function timeoutInSeconds(): int |
82
|
|
|
{ |
83
|
|
|
return 10; |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: