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