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