forked from librenms/librenms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Validator.php
327 lines (292 loc) · 10.5 KB
/
Validator.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
<?php
/**
* Validator.php
*
* Class to run validations. Also allows sharing data between ValidationGroups.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
* @link https://www.librenms.org
*
* @copyright 2017 Tony Murray
* @author Tony Murray <[email protected]>
*/
namespace LibreNMS;
use Illuminate\Support\Str;
use LibreNMS\Interfaces\ValidationGroup;
use LibreNMS\Util\Laravel;
use ReflectionClass;
use ReflectionException;
class Validator
{
/** @var array */
private $validation_groups = [];
/** @var array */
private $results = [];
/** @var string|null */
private $username;
/**
* Validator constructor.
*/
public function __construct()
{
// load all validations
$pattern = $this->getBaseDir() . '/LibreNMS/Validations/*.php';
foreach (glob($pattern) as $file) {
$class_name = basename($file, '.php');
$class = '\LibreNMS\Validations\\' . $class_name;
try {
$rc = new ReflectionClass($class);
if (! $rc->isAbstract()) {
$validation_name = strtolower($class_name);
$this->validation_groups[$validation_name] = new $class();
$this->results[$validation_name] = [];
}
} catch (ReflectionException $e) {
}
}
}
/**
* Run validations. An empty array will run all default validations.
*
* @param array $validation_groups selected validation groups to run
* @param bool $print_group_status print out group status
*/
public function validate(array $validation_groups = [], bool $print_group_status = false): void
{
foreach ($this->validation_groups as $group_name => $group) {
// only run each group once
if ($group->isCompleted()) {
continue;
}
if ((empty($validation_groups) && $group->isDefault()) || in_array($group_name, $validation_groups)) {
if ($print_group_status && Laravel::isCli()) {
echo "Checking $group_name:";
}
/** @var ValidationGroup $group */
$group->validate($this);
if (Laravel::isCli()) {
if ($print_group_status) {
$status = ValidationResult::getStatusText($this->getGroupStatus($group_name));
c_echo(" $status\n");
}
$this->printResults($group_name);
}
// validation is complete for this group
$group->markCompleted();
}
}
}
/**
* Get the overall status of a validation group.
*
* @param string $validation_group
* @return int
*/
public function getGroupStatus(string $validation_group): int
{
$results = $this->getResults($validation_group);
return array_reduce($results, function ($compound, ValidationResult $result) {
return min($compound, $result->getStatus());
}, ValidationResult::SUCCESS);
}
/**
* Get the ValidationResults for a specific validation group.
*
* @param string|null $validation_group
* @return ValidationResult[]
*/
public function getResults(string $validation_group = null): array
{
if (isset($validation_group)) {
return $this->results[$validation_group] ?? [];
}
return array_reduce($this->results, 'array_merge', []);
}
/**
* Get all of the ValidationResults that have been submitted.
* ValidationResults will be grouped by the validation group.
*
* @return ValidationResult[][]
*/
public function getAllResults(): array
{
return $this->results;
}
/**
* Print all ValidationResults or a group of them.
*
* @param string|null $validation_group
*/
public function printResults(string $validation_group = null): void
{
$results = $this->getResults($validation_group);
foreach ($results as $result) {
$result->consolePrint();
if ($result->hasFixer()) {
$input = readline('Attempt to fix this issue (y or n)?:');
if ($input === 'y') {
$result = app()->make($result->getFixer())->fix();
if ($result) {
echo "Attempted to apply fix.\n";
} else {
echo "Failed to apply fix.\n";
}
}
}
}
}
/**
* Submit a validation result.
* This allows customizing ValidationResults before submitting.
*
* @param ValidationResult $result
* @param string|null $group manually specify the group, otherwise this will be inferred from the callers class name
*/
public function result(ValidationResult $result, string $group = null): void
{
// get the name of the validation that submitted this result
if (empty($group)) {
$group = 'unknown';
$bt = debug_backtrace();
foreach ($bt as $entry) {
if (Str::startsWith($entry['class'], 'LibreNMS\Validations')) {
$group = str_replace('LibreNMS\Validations\\', '', $entry['class']);
break;
}
}
}
$this->results[strtolower($group)][] = $result;
}
/**
* Submit an ok validation result.
*
* @param string $message
* @param string|null $fix
* @param string|null $group manually specify the group, otherwise this will be inferred from the callers class name
*/
public function ok(string $message, string $fix = null, string $group = null): void
{
$this->result(new ValidationResult($message, ValidationResult::SUCCESS, $fix), $group);
}
/**
* Submit a warning validation result.
*
* @param string $message
* @param string|null $fix
* @param string|null $group manually specify the group, otherwise this will be inferred from the callers class name
*/
public function warn(string $message, string $fix = null, string $group = null): void
{
$this->result(new ValidationResult($message, ValidationResult::WARNING, $fix), $group);
}
/**
* Submit a failed validation result.
*
* @param string $message
* @param string|null $fix
* @param string|null $group manually specify the group, otherwise this will be inferred from the callers class name
*/
public function fail(string $message, string $fix = null, string $group = null): void
{
$this->result(new ValidationResult($message, ValidationResult::FAILURE, $fix), $group);
}
/**
* Submit an informational validation result.
*
* @param string $message
* @param string|null $group manually specify the group, otherwise this will be inferred from the callers class name
*/
public function info(string $message, string $group = null): void
{
$this->result(new ValidationResult($message, ValidationResult::INFO), $group);
}
/**
* Execute a command, but don't run it as root. If we are root, run as the LibreNMS user.
* Arguments match exec()
*
* @param string $command the command to run
* @param array|null $output will hold the output of the command
* @param int|null $code will hold the return code from the command
*/
public function execAsUser(string $command, array &$output = null, int &$code = null): void
{
if (self::getUsername() === 'root') {
$command = 'su ' . \config('librenms.user') . ' -s /bin/sh -c "' . $command . '"';
}
exec($command, $output, $code);
}
/**
* Get the username of the user running this and cache it for future requests.
*
* @return string
*/
public function getUsername(): string
{
if (! isset($this->username)) {
if (function_exists('posix_getpwuid')) {
$userinfo = posix_getpwuid(posix_geteuid());
$this->username = $userinfo['name'];
} else {
$this->username = getenv('USERNAME') ?: getenv('USER');
}
}
return $this->username;
}
/**
* Get the base url for this LibreNMS install, this will only work from web pages.
* (unless base_url is set)
*
* @return string the base url without a trailing /
*/
public function getBaseURL(): string
{
$url = function_exists('get_url') ? get_url() : Config::get('base_url');
return rtrim(str_replace('validate', '', $url), '/'); // get base_url from current url
}
public function getBaseDir(): string
{
return realpath(__DIR__ . '/..');
}
public function getStatusText(int $status): string
{
switch ($status) {
case ValidationResult::SUCCESS:
return 'Ok';
case ValidationResult::FAILURE:
return 'Failure';
case ValidationResult::WARNING:
return 'Warning';
case ValidationResult::INFO:
return 'Info';
default:
return '';
}
}
public function toArray(): array
{
return array_map(function (array $results, string $group) {
$groupStatus = $this->getGroupStatus($group);
return [
'group' => $group,
'name' => ucfirst($group),
'status' => $groupStatus,
'statusText' => $this->getStatusText($groupStatus),
'results' => array_map(function (ValidationResult $result) {
return $result->toArray();
}, $results),
];
}, $this->getAllResults(), array_keys($this->getAllResults()));
}
}