1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file is part of the `tvi/monitor-bundle` project. |
5
|
|
|
* |
6
|
|
|
* (c) https://github.com/turnaev/monitor-bundle/graphs/contributors |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE.md |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
|
|
|
|
11
|
|
|
|
12
|
|
|
namespace Tvi\MonitorBundle\DependencyInjection; |
13
|
|
|
|
14
|
|
|
use Symfony\Component\Config\FileLocator; |
15
|
|
|
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; |
16
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder; |
17
|
|
|
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader; |
18
|
|
|
use Symfony\Component\HttpKernel\DependencyInjection\Extension; |
19
|
|
|
|
20
|
|
|
/** |
|
|
|
|
21
|
|
|
* @author Vladimir Turnaev <[email protected]> |
22
|
|
|
*/ |
|
|
|
|
23
|
|
|
class TviMonitorExtension extends Extension implements CompilerPassInterface |
24
|
|
|
{ |
25
|
|
|
/** |
|
|
|
|
26
|
|
|
* @var Configuration |
27
|
|
|
*/ |
28
|
|
|
private $configuration; |
|
|
|
|
29
|
|
|
|
30
|
|
|
/** |
|
|
|
|
31
|
|
|
* Loads the services based on your application configuration. |
32
|
|
|
*/ |
|
|
|
|
33
|
57 |
|
public function load(array $configs, ContainerBuilder $container) |
34
|
|
|
{ |
35
|
|
|
/* |
36
|
|
|
dump($configs); exit; |
37
|
|
|
//*/ |
38
|
|
|
|
39
|
57 |
|
$loader = new YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config')); |
40
|
|
|
|
41
|
57 |
|
$loader->load('service.yml'); |
42
|
57 |
|
$loader->load('command.yml'); |
43
|
|
|
|
44
|
57 |
|
|
45
|
57 |
|
$this->configurePluginFinder($configs, $container); |
46
|
9 |
|
|
47
|
48 |
|
$pluginFinder = $container->get('tvi_monitor.checks.plugin_finder'); |
48
|
1 |
|
$configuration = new Configuration($pluginFinder); |
49
|
|
|
|
50
|
|
|
$config = $this->processConfiguration($configuration, $configs); |
51
|
57 |
|
|
52
|
57 |
|
/* |
53
|
|
|
dump($config); |
54
|
57 |
|
exit; |
55
|
|
|
//*/ |
56
|
57 |
|
|
57
|
|
|
$this->configureUIViewTemplate($config, $container); |
58
|
57 |
|
$this->configureTags($config, $container); |
59
|
|
|
$this->configureGroups($config, $container); |
60
|
|
|
$this->configureReporters($config, $container, $loader); |
61
|
|
|
|
62
|
|
|
$this->configureChecks($config, $container, $loader, $configuration->getCheckPlugins()); |
63
|
|
|
|
64
|
|
|
$loader->load('controller.yml'); |
65
|
51 |
|
$loader->load('generator.yml'); |
66
|
51 |
|
|
67
|
51 |
|
$this->configuration = $configuration; |
68
|
51 |
|
} |
69
|
|
|
|
70
|
51 |
|
public function getConfiguration(array $config, ContainerBuilder $container) |
|
|
|
|
71
|
|
|
{ |
72
|
51 |
|
return $this->configuration; |
73
|
51 |
|
} |
74
|
|
|
|
75
|
51 |
|
/** |
|
|
|
|
76
|
51 |
|
* {@inheritdoc} |
77
|
|
|
*/ |
|
|
|
|
78
|
1 |
|
public function process(ContainerBuilder $container) |
79
|
|
|
{ |
80
|
1 |
|
} |
81
|
|
|
|
82
|
|
|
private function configurePluginFinder(array $configs, ContainerBuilder $container) |
|
|
|
|
83
|
|
|
{ |
84
|
|
|
$checksSearchPaths = []; |
85
|
|
|
if (isset($configs[1]['checks_search_paths'])) { |
86
|
50 |
|
$checksSearchPaths = $configs[1]['checks_search_paths'] ?? []; |
87
|
|
|
} elseif (isset($configs[0]['checks_search_paths'])) { |
88
|
50 |
|
$checksSearchPaths = $configs[0]['checks_search_paths'] ?? []; |
89
|
|
|
} |
90
|
51 |
|
|
91
|
|
|
$checkPluginFinderDefinition = $container->getDefinition('tvi_monitor.checks.plugin_finder'); |
92
|
51 |
|
$checkPluginFinderDefinition->setArguments([$checksSearchPaths]); |
93
|
51 |
|
|
94
|
|
|
foreach ($container->findTaggedServiceIds(DiTags::CHECK_PLUGIN_SEARCH_PATH) as $id => $null) { |
95
|
51 |
|
$service = new \Symfony\Component\DependencyInjection\Reference($id); |
96
|
|
|
$checkPluginFinderDefinition->addMethodCall('addCheckPluginFinderPath', [$service]); |
97
|
51 |
|
} |
98
|
51 |
|
} |
99
|
|
|
|
100
|
51 |
|
private function configureUIViewTemplate(array $config, ContainerBuilder $container) |
|
|
|
|
101
|
|
|
{ |
102
|
51 |
|
$container->setParameter(sprintf('%s.ui.view.template', $this->getAlias()), $config['ui_view_template']); |
103
|
51 |
|
} |
104
|
|
|
|
105
|
|
|
private function configureTags(array $config, ContainerBuilder $container) |
|
|
|
|
106
|
|
|
{ |
107
|
|
|
$container->setParameter(sprintf('%s.conf.tags', $this->getAlias()), $config['tags']); |
108
|
|
|
} |
109
|
|
|
|
110
|
51 |
|
private function configureGroups(array $config, ContainerBuilder $container) |
|
|
|
|
111
|
|
|
{ |
112
|
51 |
|
$container->setParameter(sprintf('%s.conf.groups', $this->getAlias()), $config['groups']); |
113
|
|
|
} |
114
|
51 |
|
|
115
|
42 |
|
/** |
|
|
|
|
116
|
42 |
|
* @param string[] $checkPlugins |
|
|
|
|
117
|
42 |
|
* |
118
|
|
|
* @throws \Exception |
119
|
42 |
|
*/ |
|
|
|
|
120
|
42 |
|
private function configureChecks(array $config, ContainerBuilder $container, YamlFileLoader $loader, array $checkPlugins) |
|
|
|
|
121
|
|
|
{ |
122
|
42 |
|
$containerParams = []; |
123
|
42 |
|
|
124
|
42 |
|
if (isset($config['checks'])) { |
125
|
|
|
$config['checks'] = array_filter($config['checks'], static function ($i) { |
|
|
|
|
126
|
42 |
|
return $i; |
127
|
|
|
}); |
|
|
|
|
128
|
42 |
|
|
129
|
42 |
|
$containerParams = []; |
130
|
|
|
$checksLoaded = []; |
131
|
42 |
|
|
132
|
42 |
|
foreach ($config['checks'] as $checkName => &$checkSettings) { |
133
|
|
|
$checkPlugin = $checkPlugins[$checkName]; |
134
|
|
|
$service = $checkPlugin['service']; |
135
|
42 |
|
|
136
|
40 |
|
$checkServicePath = $checkPlugin['checkServicePath']; |
137
|
|
|
|
138
|
40 |
|
if (!\in_array($checkServicePath, $checksLoaded, true)) { |
139
|
40 |
|
$checksLoaded[] = $checkServicePath; |
140
|
|
|
|
141
|
40 |
|
$loader->load($checkServicePath); |
142
|
1 |
|
$checkPlugin['pligin']->checkRequirements($checkSettings); |
143
|
1 |
|
} |
144
|
1 |
|
|
145
|
|
|
if (isset($checkSettings['items'])) { |
146
|
|
|
$items = $checkSettings['items']; |
147
|
40 |
|
|
148
|
|
|
foreach ($items as $itemName => &$item) { |
149
|
|
|
$item['tags'] = array_unique(array_merge($item['tags'], $checkSettings['tags'])); |
150
|
|
|
|
151
|
|
|
if (null === $item['label'] && null !== $checkSettings['label']) { |
152
|
|
|
$val = $checkSettings['label']; |
153
|
40 |
|
$val = sprintf($val, $itemName); |
154
|
2 |
|
$item['label'] = $val; |
155
|
2 |
|
} |
156
|
|
|
|
157
|
|
|
if (null === $item['descr'] && null !== $checkSettings['descr']) { |
158
|
40 |
|
$val = $checkSettings['descr']; |
159
|
1 |
|
$val = sprintf($val, $itemName); |
160
|
1 |
|
$item['descr'] = $val; |
161
|
|
|
} |
162
|
|
|
|
163
|
40 |
|
if (empty($item['group']) && !empty($checkSettings['group'])) { |
164
|
38 |
|
$val = $checkSettings['group']; |
165
|
|
|
$item['group'] = $val; |
166
|
|
|
} |
167
|
40 |
|
|
168
|
|
|
if (empty($item['importance']) && !empty($checkSettings['importance'])) { |
169
|
|
|
$val = $checkSettings['importance']; |
170
|
40 |
|
$item['importance'] = $val; |
171
|
|
|
} |
172
|
38 |
|
|
173
|
37 |
|
if (empty($item['group']) && empty($checkSettings['group'])) { |
174
|
|
|
$item['group'] = $item['_group']; |
175
|
|
|
} |
176
|
38 |
|
|
177
|
42 |
|
unset($item['_group']); |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
$containerParams[$service]['_multi'] = $items; |
181
|
|
|
} else { |
182
|
51 |
|
if (empty($checkSettings['group'])) { |
183
|
51 |
|
$checkSettings['group'] = $checkSettings['_group']; |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
unset($checkSettings['_group']); |
187
|
|
|
$containerParams[$service]['_singl'] = $checkSettings; |
188
|
51 |
|
} |
189
|
|
|
} |
190
|
51 |
|
} |
191
|
|
|
|
192
|
51 |
|
$container->setParameter(sprintf('%s.conf.checks', $this->getAlias()), $containerParams); |
193
|
2 |
|
} |
194
|
|
|
|
195
|
2 |
|
/** |
|
|
|
|
196
|
2 |
|
* @throws \Exception |
197
|
|
|
*/ |
|
|
|
|
198
|
|
|
private function configureReporters(array $config, ContainerBuilder $container, YamlFileLoader $loader) |
|
|
|
|
199
|
51 |
|
{ |
200
|
|
|
$loader->load('reporter/reporters.yml'); |
201
|
|
|
|
202
|
|
|
if (isset($config['reporters']['mailer'])) { |
203
|
|
|
$loader->load('reporter/mailer.yml'); |
204
|
|
|
|
205
|
|
|
foreach ($config['reporters']['mailer'] as $key => $value) { |
206
|
|
|
$container->setParameter(sprintf('%s.mailer.%s', $this->getAlias(), $key), $value); |
207
|
|
|
} |
208
|
|
|
} |
209
|
|
|
} |
210
|
|
|
} |
211
|
|
|
|