1
|
|
|
<?php |
2
|
|
|
namespace Kunstmaan\Skylab\Skeleton; |
3
|
|
|
use UptimeRobot\API; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* MonitoringSkeleton |
7
|
|
|
*/ |
8
|
|
|
class MonitoringSkeleton extends AbstractSkeleton |
9
|
|
|
{ |
10
|
|
|
|
11
|
|
|
const NAME = "monitoring"; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* @return string |
15
|
|
|
*/ |
16
|
|
|
public function getName() |
17
|
|
|
{ |
18
|
|
|
return self::NAME; |
19
|
|
|
} |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @param \ArrayObject $project |
23
|
|
|
* |
24
|
|
|
* @return mixed |
25
|
|
|
*/ |
26
|
|
|
public function create(\ArrayObject $project) |
27
|
|
|
{ |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @return mixed |
32
|
|
|
*/ |
33
|
|
|
public function preMaintenance() |
34
|
|
|
{ |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @return mixed |
39
|
|
|
*/ |
40
|
|
|
public function postMaintenance() |
41
|
|
|
{ |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @param \ArrayObject $project |
46
|
|
|
* |
47
|
|
|
* @return mixed |
48
|
|
|
*/ |
49
|
|
View Code Duplication |
public function maintenance(\ArrayObject $project) |
|
|
|
|
50
|
|
|
{ |
51
|
|
|
if (isset($this->app["config"]["monitoring"]["enabled"]) && $this->app["config"]["monitoring"]["enabled"]){ |
52
|
|
|
$id = $this->getTestId($project); |
53
|
|
|
if ($id) { |
54
|
|
|
$this->updateTest($id, $project); |
55
|
|
|
} else { |
56
|
|
|
$this->createTest($project); |
57
|
|
|
} |
58
|
|
|
} else { |
59
|
|
|
$this->dialogProvider->logConfig("Monitoring is disabled"); |
60
|
|
|
} |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
private function getCheckName(\ArrayObject $project){ |
64
|
|
|
return $project["name"] . " @ " . str_replace(".kunstmaan.com", "", $this->app["config"]["webserver"]["hostmachine"]); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
private function getTestId(\ArrayObject $project) |
|
|
|
|
68
|
|
|
{ |
69
|
|
|
$tests = $this->performAPICall("/getMonitors", array("search" => $this->getCheckName($project))); |
70
|
|
|
if ($tests["stat"] == "fail"){ |
71
|
|
|
return false; |
72
|
|
|
} else { |
73
|
|
|
return $tests["monitors"]["monitor"][0]["id"]; |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
private function createTest(\ArrayObject $project) |
78
|
|
|
{ |
79
|
|
|
$data = $this->getTestArray($project); |
80
|
|
|
$this->performAPICall("/newMonitor", $data); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
private function updateTest($id, \ArrayObject $project) |
84
|
|
|
{ |
85
|
|
|
$data = $this->getTestArray($project, $id); |
86
|
|
|
$this->performAPICall("/editMonitor", $data); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
private function deleteTest($id) |
90
|
|
|
{ |
91
|
|
|
$data = $this->getTestArray(null, $id); |
92
|
|
|
$this->performAPICall("/deleteMonitor", $data); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* @param \ArrayObject $project |
|
|
|
|
97
|
|
|
* @return array |
98
|
|
|
*/ |
99
|
|
|
private function getTestArray(\ArrayObject $project = null, $id = false) |
100
|
|
|
{ |
101
|
|
|
$data = array(); |
102
|
|
|
if (!is_null($project)) { |
103
|
|
|
|
104
|
|
|
$monitorPath = ''; |
105
|
|
|
if (isset($project['monuri'])) { //for backwards compatibility only |
106
|
|
|
$monitorPath = $project['monuri']; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
if (isset($project['monitoring_path'])) { |
110
|
|
|
$monitorPath = $project['monitoring_path']; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
$monitorUrl = ($this->skeletonProvider->hasSkeleton($project, $this->skeletonProvider->findSkeleton("ssl"))?"https://":"http://") . $project["url"] . $monitorPath; |
114
|
|
|
if (filter_var($monitorUrl, FILTER_VALIDATE_URL, array('flags' => array(FILTER_FLAG_SCHEME_REQUIRED, FILTER_FLAG_HOST_REQUIRED))) === false) { |
115
|
|
|
//fallback to basic url |
116
|
|
|
$this->dialogProvider->logWarning("The ".$project["name"]." monitoring url is not valid: " . $monitorUrl); |
117
|
|
|
$monitorUrl = ($this->skeletonProvider->hasSkeleton($project, $this->skeletonProvider->findSkeleton("ssl"))?"https://":"http://") . $project["url"]; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
$data = array( |
121
|
|
|
"monitorFriendlyName" => $this->getCheckName($project), |
122
|
|
|
"monitorURL" => $monitorUrl, |
123
|
|
|
"monitorType" => 1, |
124
|
|
|
"monitorAlertContacts" => "2351398_5_60-0254691_5_60-2356728_5_60-2356781_5_60", |
125
|
|
|
"monitorInterval" => 1 |
126
|
|
|
); |
127
|
|
|
} |
128
|
|
|
if ($id) { |
129
|
|
|
$data["monitorID"] = $id; |
130
|
|
|
} |
131
|
|
|
return $data; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
private function performAPICall($url, $data = array()) |
135
|
|
|
{ |
136
|
|
|
|
137
|
|
|
//Set configuration settings |
138
|
|
|
$config = [ |
139
|
|
|
'apiKey' => $this->app["config"]["monitoring"]["apikey"], |
140
|
|
|
'url' => 'http://api.uptimerobot.com' |
141
|
|
|
]; |
142
|
|
|
|
143
|
|
|
try { |
144
|
|
|
$api = new API($config); |
145
|
|
|
$results = $api->request($url, $data); |
146
|
|
|
$this->dialogProvider->logConfig("Sent a call to " . $url . " and got a response: " . print_r($results, true)); |
147
|
|
|
return $results; |
148
|
|
|
} catch (\Exception $e) { |
149
|
|
|
$this->dialogProvider->logConfig("Sent a call to " . $url . " and got an error: " . $e->getMessage()); |
150
|
|
|
} |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* @param \ArrayObject $project |
155
|
|
|
* |
156
|
|
|
* @return mixed |
157
|
|
|
*/ |
158
|
|
|
public function preBackup(\ArrayObject $project) |
159
|
|
|
{ |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* @param \ArrayObject $project |
164
|
|
|
* |
165
|
|
|
* @return mixed |
166
|
|
|
*/ |
167
|
|
|
public function postBackup(\ArrayObject $project) |
168
|
|
|
{ |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
/** |
172
|
|
|
* @param \ArrayObject $project |
173
|
|
|
* |
174
|
|
|
* @return mixed |
175
|
|
|
*/ |
176
|
|
|
public function preRemove(\ArrayObject $project) |
177
|
|
|
{ |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
/** |
181
|
|
|
* @param \ArrayObject $project |
182
|
|
|
* |
183
|
|
|
* @return mixed |
184
|
|
|
*/ |
185
|
|
View Code Duplication |
public function postRemove(\ArrayObject $project) |
|
|
|
|
186
|
|
|
{ |
187
|
|
|
if (isset($this->app["config"]["monitoring"]["enabled"]) && $this->app["config"]["monitoring"]["enabled"]){ |
188
|
|
|
$id = $this->getTestId($project); |
189
|
|
|
$this->deleteTest($id); |
190
|
|
|
} else { |
191
|
|
|
$this->dialogProvider->logConfig("Monitoring is disabled"); |
192
|
|
|
} |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
/** |
196
|
|
|
* @return string[] |
197
|
|
|
*/ |
198
|
|
|
public function dependsOn() |
199
|
|
|
{ |
200
|
|
|
return array("base", "apache"); |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
} |
204
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.