|
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
|
|
|
* Loads the services based on your application configuration. |
|
27
|
|
|
*/ |
|
|
|
|
|
|
28
|
54 |
|
public function load(array $configs, ContainerBuilder $container) |
|
29
|
|
|
{ |
|
30
|
|
|
/* |
|
31
|
|
|
dump($configs); exit; |
|
32
|
|
|
//*/ |
|
33
|
|
|
|
|
34
|
54 |
|
$loader = new YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config')); |
|
35
|
|
|
|
|
36
|
54 |
|
$loader->load('service.yml'); |
|
37
|
54 |
|
$loader->load('command.yml'); |
|
38
|
|
|
|
|
39
|
54 |
|
$checksSearchPaths = []; |
|
40
|
54 |
|
if (isset($configs[1]['checks_search_paths'])) { |
|
41
|
9 |
|
$checksSearchPaths = $configs[1]['checks_search_paths'] ?? []; |
|
42
|
45 |
|
} elseif (isset($configs[0]['checks_search_paths'])) { |
|
43
|
1 |
|
$checksSearchPaths = $configs[0]['checks_search_paths'] ?? []; |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
54 |
|
$checkPluginFinderDefinition = $container->getDefinition('tvi_monitor.checks.plugin_finder'); |
|
47
|
54 |
|
$checkPluginFinderDefinition->setArguments([$checksSearchPaths]); |
|
48
|
|
|
|
|
49
|
54 |
|
$pluginFinder = $container->get('tvi_monitor.checks.plugin_finder'); |
|
50
|
|
|
|
|
51
|
54 |
|
$configuration = new Configuration($pluginFinder); |
|
52
|
|
|
|
|
53
|
54 |
|
$config = $this->processConfiguration($configuration, $configs); |
|
54
|
|
|
|
|
55
|
|
|
/* |
|
56
|
|
|
dump($config); exit; |
|
57
|
|
|
//*/ |
|
58
|
|
|
|
|
59
|
48 |
|
$this->configureUIViewTemplate($config, $container); |
|
60
|
48 |
|
$this->configureTags($config, $container); |
|
61
|
48 |
|
$this->configureGroups($config, $container); |
|
62
|
48 |
|
$this->configureReporters($config, $container, $loader); |
|
63
|
|
|
|
|
64
|
48 |
|
$this->configureChecks($config, $container, $loader, $configuration->getCheckPlugins()); |
|
65
|
|
|
|
|
66
|
48 |
|
$loader->load('controller.yml'); |
|
67
|
48 |
|
$loader->load('generator.yml'); |
|
68
|
48 |
|
|
|
69
|
|
|
return $configuration; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
public function getConfiguration(array $config, ContainerBuilder $container) |
|
|
|
|
|
|
73
|
47 |
|
{ |
|
74
|
|
|
return $this->load($config, $container); |
|
75
|
47 |
|
} |
|
76
|
|
|
|
|
77
|
48 |
|
/** |
|
|
|
|
|
|
78
|
|
|
* {@inheritdoc} |
|
79
|
48 |
|
*/ |
|
|
|
|
|
|
80
|
48 |
|
public function process(ContainerBuilder $container) |
|
81
|
|
|
{ |
|
82
|
48 |
|
} |
|
83
|
|
|
|
|
84
|
48 |
|
private function configureUIViewTemplate(array $config, ContainerBuilder $container) |
|
|
|
|
|
|
85
|
48 |
|
{ |
|
86
|
|
|
$container->setParameter(sprintf('%s.ui.view.template', $this->getAlias()), $config['ui_view_template']); |
|
87
|
48 |
|
} |
|
88
|
|
|
|
|
89
|
48 |
|
private function configureTags(array $config, ContainerBuilder $container) |
|
|
|
|
|
|
90
|
48 |
|
{ |
|
91
|
|
|
$container->setParameter(sprintf('%s.conf.tags', $this->getAlias()), $config['tags']); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
private function configureGroups(array $config, ContainerBuilder $container) |
|
|
|
|
|
|
95
|
|
|
{ |
|
96
|
|
|
$container->setParameter(sprintf('%s.conf.groups', $this->getAlias()), $config['groups']); |
|
97
|
48 |
|
} |
|
98
|
|
|
|
|
99
|
48 |
|
/** |
|
|
|
|
|
|
100
|
|
|
* @param string[] $checkPlugins |
|
|
|
|
|
|
101
|
48 |
|
* |
|
102
|
39 |
|
* @throws \Exception |
|
103
|
39 |
|
*/ |
|
|
|
|
|
|
104
|
39 |
|
private function configureChecks(array $config, ContainerBuilder $container, YamlFileLoader $loader, array $checkPlugins) |
|
|
|
|
|
|
105
|
|
|
{ |
|
106
|
39 |
|
$containerParams = []; |
|
107
|
39 |
|
|
|
108
|
|
|
if (isset($config['checks'])) { |
|
109
|
39 |
|
$config['checks'] = array_filter($config['checks'], static function ($i) { |
|
|
|
|
|
|
110
|
39 |
|
return $i; |
|
111
|
39 |
|
}); |
|
|
|
|
|
|
112
|
|
|
|
|
113
|
39 |
|
$containerParams = []; |
|
114
|
|
|
$checksLoaded = []; |
|
115
|
39 |
|
|
|
116
|
39 |
|
foreach ($config['checks'] as $checkName => &$checkSettings) { |
|
117
|
|
|
$checkPlugin = $checkPlugins[$checkName]; |
|
118
|
39 |
|
$service = $checkPlugin['service']; |
|
119
|
39 |
|
|
|
120
|
|
|
$checkServicePath = $checkPlugin['checkServicePath']; |
|
121
|
|
|
|
|
122
|
39 |
|
if (!\in_array($checkServicePath, $checksLoaded, true)) { |
|
123
|
37 |
|
$checksLoaded[] = $checkServicePath; |
|
124
|
|
|
|
|
125
|
37 |
|
$loader->load($checkServicePath); |
|
126
|
37 |
|
$checkPlugin['pligin']->checkRequirements($checkSettings); |
|
127
|
|
|
} |
|
128
|
37 |
|
|
|
129
|
1 |
|
if (isset($checkSettings['items'])) { |
|
130
|
1 |
|
$items = $checkSettings['items']; |
|
131
|
1 |
|
|
|
132
|
|
|
foreach ($items as $itemName => &$item) { |
|
133
|
|
|
$item['tags'] = array_unique(array_merge($item['tags'], $checkSettings['tags'])); |
|
134
|
37 |
|
|
|
135
|
|
|
if (null === $item['label'] && null !== $checkSettings['label']) { |
|
136
|
|
|
$val = $checkSettings['label']; |
|
137
|
|
|
$val = sprintf($val, $itemName); |
|
138
|
|
|
$item['label'] = $val; |
|
139
|
|
|
} |
|
140
|
37 |
|
|
|
141
|
2 |
|
if (null === $item['descr'] && null !== $checkSettings['descr']) { |
|
142
|
2 |
|
$val = $checkSettings['descr']; |
|
143
|
|
|
$val = sprintf($val, $itemName); |
|
144
|
|
|
$item['descr'] = $val; |
|
145
|
37 |
|
} |
|
146
|
1 |
|
|
|
147
|
1 |
|
if (empty($item['group']) && !empty($checkSettings['group'])) { |
|
148
|
|
|
$val = $checkSettings['group']; |
|
149
|
|
|
$item['group'] = $val; |
|
150
|
37 |
|
} |
|
151
|
35 |
|
|
|
152
|
|
|
if (empty($item['importance']) && !empty($checkSettings['importance'])) { |
|
153
|
|
|
$val = $checkSettings['importance']; |
|
154
|
37 |
|
$item['importance'] = $val; |
|
155
|
|
|
} |
|
156
|
|
|
|
|
157
|
37 |
|
if (empty($item['group']) && empty($checkSettings['group'])) { |
|
158
|
|
|
$item['group'] = $item['_group']; |
|
159
|
37 |
|
} |
|
160
|
36 |
|
|
|
161
|
|
|
unset($item['_group']); |
|
162
|
|
|
} |
|
163
|
37 |
|
|
|
164
|
39 |
|
$containerParams[$service]['_multi'] = $items; |
|
165
|
|
|
} else { |
|
166
|
|
|
if (empty($checkSettings['group'])) { |
|
167
|
|
|
$checkSettings['group'] = $checkSettings['_group']; |
|
168
|
|
|
} |
|
169
|
48 |
|
|
|
170
|
48 |
|
unset($checkSettings['_group']); |
|
171
|
|
|
$containerParams[$service]['_singl'] = $checkSettings; |
|
172
|
|
|
} |
|
173
|
|
|
} |
|
174
|
|
|
} |
|
175
|
48 |
|
|
|
176
|
|
|
$container->setParameter(sprintf('%s.conf.checks', $this->getAlias()), $containerParams); |
|
177
|
48 |
|
} |
|
178
|
|
|
|
|
179
|
48 |
|
/** |
|
|
|
|
|
|
180
|
2 |
|
* @throws \Exception |
|
181
|
|
|
*/ |
|
|
|
|
|
|
182
|
2 |
|
private function configureReporters(array $config, ContainerBuilder $container, YamlFileLoader $loader) |
|
|
|
|
|
|
183
|
2 |
|
{ |
|
184
|
|
|
$loader->load('reporter/reporters.yml'); |
|
185
|
|
|
|
|
186
|
48 |
|
if (isset($config['reporters']['mailer'])) { |
|
187
|
|
|
$loader->load('reporter/mailer.yml'); |
|
188
|
|
|
|
|
189
|
|
|
foreach ($config['reporters']['mailer'] as $key => $value) { |
|
190
|
|
|
$container->setParameter(sprintf('%s.mailer.%s', $this->getAlias(), $key), $value); |
|
191
|
|
|
} |
|
192
|
|
|
} |
|
193
|
|
|
} |
|
194
|
|
|
} |
|
195
|
|
|
|