-
Notifications
You must be signed in to change notification settings - Fork 0
/
ConfigValidator.inc.php
99 lines (84 loc) · 2.9 KB
/
ConfigValidator.inc.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
<h1>The configuration of the following devices will be checked</h1>
<?php
error_reporting(E_ALL);
ini_set('display_errors', 'On');
$init_modules = array();
include_once(__DIR__ . '/../../../includes/init.php');
include_once("Differ.php");
include_once("LongestCommonSubsequenceCalculator.php");
include_once("TimeEfficientLongestCommonSubsequenceCalculator.php");
$configPath = "/opt/librenms/html/plugins/ConfigValidator/configs";
$templatePath = "/opt/librenms/html/plugins/ConfigValidator/templates";
foreach (dbFetchRows("SELECT distinct hardware, os from devices") as $device)
{
if ($device['hardware']) {
$templateFile = $templatePath . "/" . $device['os'] . '/' . $device['hardware'] . "/template.txt";
if (file_exists($templateFile)) {
echo "<h3>".$device['os']." - ".$device['hardware']."</h3>";
echo '<table class="table table-condensed table-hover bootgrid-table">';
foreach (dbFetchRows("SELECT * from devices WHERE os = '".$device['os']."' AND hardware = '".$device['hardware']."'") as $device2)
{
echo "<tr>";
echo "<td>".$device2['hostname']."</td>";
$result = checkConfig($device2['device_id'], $templateFile);
if ($result['result']) {
echo "<td>ok</td>";
} else {
echo "<td>";
if (isset($result['added']) && count($result['added']) > 0) {
echo "<h4>The following lines were added</h4>";
echo "<table>";
foreach ($result['added'] as $line) {
echo "<tr>";
echo "<td>".$line."</td>";
echo "</tr>";
}
echo "</table>";
}
if (isset($result['removed']) && count($result['removed']) > 0) {
echo "<h4>The following lines were removed</h4>";
echo "<table>";
foreach ($result['removed'] as $line) {
echo "<tr>";
echo "<td>".$line."</td>";
echo "</tr>";
}
echo "</table>";
}
echo "</td>";
}
echo "</tr>";
}
echo "</table>";
}
}
}
function checkConfig($deviceId, $templateFile)
{
global $configPath;
$result = array('added' => array(), 'removed' => array());
if (!file_exists($configPath."/".$deviceId.".txt")) {
return false;
}
$current = file_get_contents($configPath."/".$deviceId.".txt");
$template = file_get_contents($templateFile);
$differ = new SebastianBergmann\Diff\Differ();
$diff = $differ->diffToArray($template, $current);
foreach ($diff as $line) {
if ($line[1] == 1) {
$result['added'][] = $line[0];
}
if ($line[1] == 2) {
$result['removed'][] = $line[0];
}
}
if (count($result['added']) == 0 && count($result['removed']) == 0) {
$result['result'] = true;
dbUpdate(array('notes' => ''), 'devices', 'device_id = ?', array($deviceId));
} else {
$result['result'] = false;
dbUpdate(array('notes' => 'config_not_compliant'), 'devices', 'device_id = ?', array($deviceId));
}
return $result;
}
?>