forked from librenms/librenms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsave-test-data.php
executable file
·159 lines (135 loc) · 4.54 KB
/
save-test-data.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
#!/usr/bin/env php
<?php
use LibreNMS\Exceptions\InvalidModuleException;
use LibreNMS\Util\Debug;
use LibreNMS\Util\ModuleTestHelper;
use LibreNMS\Util\Snmpsim;
global $device;
$install_dir = realpath(__DIR__ . '/..');
chdir($install_dir);
$options = getopt(
'o:v:m:nf:dh',
[
'os:',
'variant:',
'modules:',
'no-save',
'file:',
'debug',
'snmpsim',
'help',
]
);
$init_modules = ['discovery', 'polling'];
require $install_dir . '/includes/init.php';
Debug::setVerbose(
Debug::set(isset($options['d']) || isset($options['debug']))
);
if (isset($options['snmpsim'])) {
$snmpsim = new Snmpsim();
$snmpsim->run();
exit;
}
if (isset($options['h'])
|| isset($options['help'])
|| ! (isset($options['o']) || isset($options['os']) || isset($options['m']) || isset($options['modules']))
) {
echo "Script to update test data. Database data is saved in tests/data.
Usage:
- This script can process new test data (by specifying both OS and VARIANT).
- This script can refresh test data.
-> if an OS is specified, only this OS will be refreshed.
-> if MODULES are specified, only these modules will be refreshed.
Parameters:
-o, --os Name of the OS to save test data for.
-v, --variant The variant of the OS to use, usually the device model.
-m, --modules The discovery/poller module(s) to collect data for, comma delimited.
Use -m 'all' for all modules.
-n, --no-save Don't save database entries, print them out instead
-f, --file Save data to file instead of the standard location
-d, --debug Enable debug output
--snmpsim Run snmpsimd.py using the collected data for manual testing.
Examples:
./save-test-data.php -o ios -v 2960x
./save-test-data.php -o linux -v freeradius -m applications
";
exit;
}
$os_name = false;
if (isset($options['o'])) {
$os_name = $options['o'];
} elseif (isset($options['os'])) {
$os_name = $options['os'];
}
if ((isset($options['m']) && $options['m'] == 'all') || (isset($options['modules']) && $options['modules'] == 'all')) {
$modules_input = 'all';
$modules = [];
} elseif (isset($options['m'])) {
$modules_input = $options['m'];
$modules = explode(',', $modules_input);
} elseif (isset($options['modules'])) {
$modules_input = $options['modules'];
$modules = explode(',', $modules_input);
} else {
$modules_input = 'all';
$modules = [];
}
$full_os_name = $os_name;
$variant = null;
if (isset($options['v'])) {
$variant = $options['v'];
$full_os_name = $os_name . '_' . $variant;
} elseif (isset($options['variant'])) {
$variant = $options['variant'];
$full_os_name = $os_name . '_' . $variant;
}
$os_list = [];
if (isset($os_name) && isset($variant)) {
$os_list = [$full_os_name => [$os_name, $variant]];
} elseif (isset($os_name)) {
$os_list = ModuleTestHelper::findOsWithData($modules, $os_name);
} else {
$os_list = ModuleTestHelper::findOsWithData($modules);
}
if (isset($options['f'])) {
if (count($os_list) != 1) {
echo "Failed to create test data, -f/--file option can be used with one os/variant combination.\n";
echo 'Multiple combinations (' . count($os_list) . ") found.\n";
exit(1);
}
$output_file = $options['f'];
}
// Now use the saved data to update the saved database data
$snmpsim = new Snmpsim();
$snmpsim->fork();
$snmpsim_ip = $snmpsim->getIp();
$snmpsim_port = $snmpsim->getPort();
if (! $snmpsim->isRunning()) {
echo "Failed to start snmpsim, make sure it is installed, working, and there are no bad snmprec files.\n";
echo "Run ./scripts/save-test-data.php --snmpsim to see the log output\n";
exit(1);
}
try {
$no_save = isset($options['n']) || isset($options['no-save']);
foreach ($os_list as $full_os_name => $parts) {
[$target_os, $target_variant] = $parts;
echo "OS: $target_os\n";
echo "Module: $modules_input\n";
if ($target_variant) {
echo "Variant: $target_variant\n";
}
echo PHP_EOL;
\LibreNMS\Util\OS::updateCache(true); // Force update of OS Cache
$tester = new ModuleTestHelper($modules, $target_os, $target_variant);
if (! $no_save && ! empty($output_file)) {
$tester->setJsonSavePath($output_file);
}
$test_data = $tester->generateTestData($snmpsim, $no_save);
if ($no_save) {
print_r($test_data);
}
}
} catch (InvalidModuleException $e) {
echo $e->getMessage() . PHP_EOL;
}
$snmpsim->stop();