1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Liip\MonitorBundle\Controller; |
4
|
|
|
|
5
|
|
|
use Liip\MonitorBundle\Helper\ArrayReporter; |
6
|
|
|
use Liip\MonitorBundle\Helper\RunnerManager; |
7
|
|
|
use Psr\Log\LoggerInterface; |
8
|
|
|
use Psr\Log\NullLogger; |
9
|
|
|
use Symfony\Component\HttpFoundation\JsonResponse; |
10
|
|
|
use Symfony\Component\HttpFoundation\Request; |
11
|
|
|
use Symfony\Component\HttpFoundation\Response; |
12
|
|
|
use Liip\MonitorBundle\Runner; |
13
|
|
|
use Liip\MonitorBundle\Helper\PathHelper; |
14
|
|
|
|
15
|
|
|
class HealthCheckController |
16
|
|
|
{ |
17
|
|
|
protected $runnerManager; |
18
|
|
|
protected $pathHelper; |
19
|
|
|
protected $template; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @param RunnerManager $runnerManager |
23
|
|
|
* @param PathHelper $pathHelper |
24
|
|
|
* @param $template |
25
|
|
|
*/ |
26
|
|
|
public function __construct(RunnerManager $runnerManager, PathHelper $pathHelper, $template, LoggerInterface $log = null) |
27
|
|
|
{ |
28
|
|
|
$this->runnerManager = $runnerManager; |
29
|
|
|
$this->pathHelper = $pathHelper; |
30
|
|
|
$this->template = $template; |
31
|
|
|
if ($log === null) { |
32
|
|
|
$log = new NullLogger(); |
33
|
|
|
} |
34
|
|
|
$this->log = $log; |
|
|
|
|
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @param \Symfony\Component\HttpFoundation\Request $request |
39
|
|
|
* |
40
|
|
|
* @return \Symfony\Component\HttpFoundation\Response |
41
|
|
|
*/ |
42
|
|
|
public function indexAction(Request $request) |
43
|
|
|
{ |
44
|
|
|
$group = $this->getGroup($request); |
45
|
|
|
|
46
|
|
|
$urls = $this->pathHelper->getRoutesJs(array( |
47
|
|
|
'liip_monitor_run_all_checks' => array('group' => $group), |
48
|
|
|
'liip_monitor_run_single_check' => array('checkId' => 'replaceme', 'group' => $group), |
49
|
|
|
)); |
50
|
|
|
|
51
|
|
|
$css = $this->pathHelper->getStyleTags(array( |
52
|
|
|
'bundles/liipmonitor/css/bootstrap/css/bootstrap.min.css', |
53
|
|
|
'bundles/liipmonitor/css/style.css', |
54
|
|
|
)); |
55
|
|
|
|
56
|
|
|
$javascript = $this->pathHelper->getScriptTags(array( |
57
|
|
|
'bundles/liipmonitor/javascript/jquery-1.7.1.min.js', |
58
|
|
|
'bundles/liipmonitor/javascript/ember-0.9.5.min.js', |
59
|
|
|
'bundles/liipmonitor/javascript/app.js', |
60
|
|
|
)); |
61
|
|
|
|
62
|
|
|
// this is a hack to make the bundle template agnostic. |
63
|
|
|
// URL generation for Assets and Routes is still handled by the framework. |
64
|
|
|
ob_start(); |
65
|
|
|
include $this->template; |
66
|
|
|
$content = ob_get_clean(); |
67
|
|
|
|
68
|
|
|
$this->log->debug("HealthCheckController | index"); |
69
|
|
|
return new Response($content, 200, array('Content-Type' => 'text/html')); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @return \Symfony\Component\HttpFoundation\Response |
74
|
|
|
*/ |
75
|
|
|
public function listAction(Request $request) |
76
|
|
|
{ |
77
|
|
|
$ret = array(); |
78
|
|
|
|
79
|
|
|
$runner = $this->getRunner($request); |
80
|
|
|
|
81
|
|
|
foreach ($runner->getChecks() as $alias => $check) { |
82
|
|
|
$ret[] = $alias; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
return new JsonResponse($ret); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @return JsonResponse |
90
|
|
|
*/ |
91
|
|
|
public function listAllAction() |
92
|
|
|
{ |
93
|
|
|
$allChecks = array(); |
94
|
|
|
|
95
|
|
|
foreach ($this->runnerManager->getRunners() as $group => $runner) { |
96
|
|
|
foreach ($runner->getChecks() as $alias => $check) { |
97
|
|
|
$allChecks[$group][] = $alias; |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
return new JsonResponse($allChecks); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* @return JsonResponse |
106
|
|
|
*/ |
107
|
|
|
public function listGroupsAction() |
108
|
|
|
{ |
109
|
|
|
$groups = $this->runnerManager->getGroups(); |
110
|
|
|
|
111
|
|
|
return new JsonResponse($groups); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* @param Request $request |
116
|
|
|
* |
117
|
|
|
* @return \Symfony\Component\HttpFoundation\Response |
118
|
|
|
*/ |
119
|
|
|
public function runAllChecksAction(Request $request) |
120
|
|
|
{ |
121
|
|
|
$report = $this->runTests($request); |
122
|
|
|
|
123
|
|
|
return new JsonResponse(array( |
124
|
|
|
'checks' => $report->getResults(), |
125
|
|
|
'globalStatus' => $report->getGlobalStatus(), |
126
|
|
|
)); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* @param Request $request |
131
|
|
|
* |
132
|
|
|
* @return \Symfony\Component\HttpFoundation\Response |
133
|
|
|
*/ |
134
|
|
|
public function runAllChecksHttpStatusAction(Request $request) |
135
|
|
|
{ |
136
|
|
|
$report = $this->runTests($request); |
137
|
|
|
|
138
|
|
|
return new Response( |
139
|
|
|
'', |
140
|
|
|
($report->getGlobalStatus() === ArrayReporter::STATUS_OK ? 200 : 502) |
141
|
|
|
); |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* @param string $checkId |
146
|
|
|
* @param Request $request |
147
|
|
|
* |
148
|
|
|
* @return \Symfony\Component\HttpFoundation\Response |
149
|
|
|
*/ |
150
|
|
|
public function runSingleCheckHttpStatusAction($checkId, Request $request) |
151
|
|
|
{ |
152
|
|
|
$report = $this->runTests($request, $checkId); |
153
|
|
|
|
154
|
|
|
return new Response( |
155
|
|
|
'', |
156
|
|
|
($report->getGlobalStatus() === ArrayReporter::STATUS_OK ? 200 : 502) |
157
|
|
|
); |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* @param string $checkId |
162
|
|
|
* @param Request $request |
163
|
|
|
* |
164
|
|
|
* @return \Symfony\Component\HttpFoundation\Response |
165
|
|
|
*/ |
166
|
|
|
public function runSingleCheckAction($checkId, Request $request) |
167
|
|
|
{ |
168
|
|
|
$results = $this->runTests($request, $checkId)->getResults(); |
169
|
|
|
|
170
|
|
|
return new JsonResponse($results[0]); |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
/** |
174
|
|
|
* @param Request $request |
175
|
|
|
* @param string|null $checkId |
176
|
|
|
* |
177
|
|
|
* @return ArrayReporter |
178
|
|
|
*/ |
179
|
|
|
protected function runTests(Request $request, $checkId = null) |
180
|
|
|
{ |
181
|
|
|
$reporters = $request->query->get('reporters', array()); |
182
|
|
|
|
183
|
|
|
if (!is_array($reporters)) { |
184
|
|
|
$reporters = array($reporters); |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
$reporter = new ArrayReporter(); |
188
|
|
|
|
189
|
|
|
$runner = $this->getRunner($request); |
190
|
|
|
|
191
|
|
|
$runner->addReporter($reporter); |
192
|
|
|
$runner->useAdditionalReporters($reporters); |
193
|
|
|
$runner->run($checkId); |
194
|
|
|
|
195
|
|
|
$this->logTestStatus($reporter->getGlobalStatus() === ArrayReporter::STATUS_OK, $reporter); |
196
|
|
|
return $reporter; |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
private function statusIsNotOK(array $resultItem) |
200
|
|
|
{ |
201
|
|
|
return $resultItem['status'] !== 0; |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
private function logTestStatus($isOK, ArrayReporter $reporter) |
205
|
|
|
{ |
206
|
|
|
if ($isOK) { |
207
|
|
|
$this->log->debug("HealthCheckController | runTests, OK"); |
208
|
|
|
return; |
209
|
|
|
} |
210
|
|
|
|
211
|
|
|
// Get the failing items for a report: |
212
|
|
|
$failures = array_filter($reporter->getResults(), array($this, 'statusIsNotOK')); |
213
|
|
|
$jsonStr = json_encode($failures); |
214
|
|
|
$this->log->alert("HealthCheckController | runTests, FAILURE | {$jsonStr}"); |
215
|
|
|
} |
216
|
|
|
|
217
|
|
|
/** |
218
|
|
|
* @param Request $request |
219
|
|
|
* |
220
|
|
|
* @return Runner |
221
|
|
|
* |
222
|
|
|
* @throws \Exception |
223
|
|
|
*/ |
224
|
|
|
private function getRunner(Request $request) |
225
|
|
|
{ |
226
|
|
|
$group = $this->getGroup($request); |
227
|
|
|
|
228
|
|
|
$runner = $this->runnerManager->getRunner($group); |
229
|
|
|
|
230
|
|
|
if ($runner) { |
231
|
|
|
return $runner; |
232
|
|
|
} |
233
|
|
|
|
234
|
|
|
throw new \RuntimeException(sprintf('Unknown check group "%s"', $group)); |
235
|
|
|
} |
236
|
|
|
|
237
|
|
|
/** |
238
|
|
|
* @param Request $request |
239
|
|
|
* |
240
|
|
|
* @return string |
241
|
|
|
*/ |
242
|
|
|
private function getGroup(Request $request) |
243
|
|
|
{ |
244
|
|
|
return $request->query->get('group') ?: $this->runnerManager->getDefaultGroup(); |
245
|
|
|
} |
246
|
|
|
} |
247
|
|
|
|
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: