forked from pgmillon/observium
-
Notifications
You must be signed in to change notification settings - Fork 0
/
add_device.php
executable file
·270 lines (235 loc) · 8.23 KB
/
add_device.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
#!/usr/bin/env php
<?php
/**
* Observium
*
* This file is part of Observium.
*
* @package observium
* @subpackage cli
* @author Adam Armstrong <[email protected]>
* @copyright (C) 2006-2013 Adam Armstrong, (C) 2013-2016 Observium Limited
*
*/
chdir(dirname($argv[0]));
$scriptname = basename($argv[0]);
$options = getopt("dhpt");
if (isset($options['d'])) { array_shift($argv); }
include("includes/sql-config.inc.php");
include("includes/discovery/functions.inc.php");
print_message("%g" . OBSERVIUM_PRODUCT . " " . OBSERVIUM_VERSION . "\n%WAdd Device(s)%n\n", 'color');
if (OBS_DEBUG) { print_versions(); }
if (isset($options['h'])) { print_help($scriptname); exit; }
$snmp_options = array();
// Just test, do not add device
if (isset($options['t']))
{
$snmp_options['test'] = TRUE;
array_shift($argv);
}
// Add skip pingable checks if argument -p passed
if (isset($options['p']))
{
$snmp_options['ping_skip'] = 1;
array_shift($argv);
}
$added = 0;
if (!empty($argv[1]))
{
$add_array = array();
if (is_file($argv[1]))
{
// Parse file into array with devices to add
foreach (new SplFileObject($argv[1]) as $line)
{
$d = preg_split('/\s/', $line, -1, PREG_SPLIT_NO_EMPTY);
if (empty($d) || substr(reset($d), 0, 1) == '#') continue;
$add_array[] = $d;
}
} else {
$add_array[0] = $argv;
array_shift($add_array[0]);
}
// Save base SNMP v3 credentials and v2c/v1 community
$snmp_config_v3 = $config['snmp']['v3'];
$snmp_config_community = $config['snmp']['community'];
foreach ($add_array as $add)
{
$hostname = strtolower($add[0]);
$snmp_community = $add[1];
$snmp_version = strtolower($add[2]);
$snmp_port = 161;
$snmp_transport = 'udp';
if ($snmp_version == "v3")
{
$config['snmp']['v3'] = $snmp_config_v3; // Restore base SNMP v3 credentials
$snmp_v3_seclevel = $snmp_community;
// These values are the same as in defaults.inc.php
$snmp_v3_auth = array(
'authlevel' => "noAuthNoPriv",
'authname' => "observium",
'authpass' => "",
'authalgo' => "MD5",
'cryptopass' => "",
'cryptoalgo' => "AES"
);
if ($snmp_v3_seclevel == "nanp" || $snmp_v3_seclevel == "any" || $snmp_v3_seclevel == "noAuthNoPriv")
{
$snmp_v3_auth['authlevel'] = "noAuthNoPriv";
$snmp_v3_args = array_slice($add, 3);
while ($arg = array_shift($snmp_v3_args))
{
// parse all remaining args
if (is_numeric($arg))
{
$snmp_port = $arg;
}
else if (preg_match('/^(' . implode("|", $config['snmp']['transports']) . ')$/', $arg))
{
$snmp_transport = $arg;
} else {
// FIXME: should add a sanity check of chars allowed in user
$user = $arg;
}
}
if ($snmp_v3_seclevel != "any")
{
array_push($config['snmp']['v3'], $snmp_v3_auth);
}
}
else if ($snmp_v3_seclevel == "anp" || $snmp_v3_seclevel == "authNoPriv")
{
$snmp_v3_auth['authlevel'] = "authNoPriv";
$snmp_v3_args = array_slice($argv, 4);
$snmp_v3_auth['authname'] = array_shift($snmp_v3_args);
$snmp_v3_auth['authpass'] = array_shift($snmp_v3_args);
while ($arg = array_shift($snmp_v3_args))
{
// parse all remaining args
if (is_numeric($arg))
{
$snmp_port = $arg;
}
else if (preg_match('/^(' . implode("|", $config['snmp']['transports']) . ')$/i', $arg))
{
$snmp_transport = $arg;
}
else if (preg_match('/^(sha|md5)$/i', $arg))
{
$snmp_v3_auth['authalgo'] = $arg;
}
}
array_push($config['snmp']['v3'], $snmp_v3_auth);
}
else if ($snmp_v3_seclevel == "ap" || $snmp_v3_seclevel == "authPriv")
{
$snmp_v3_auth['authlevel'] = "authPriv";
$snmp_v3_args = array_slice($argv, 4);
$snmp_v3_auth['authname'] = array_shift($snmp_v3_args);
$snmp_v3_auth['authpass'] = array_shift($snmp_v3_args);
$snmp_v3_auth['cryptopass'] = array_shift($snmp_v3_args);
while ($arg = array_shift($snmp_v3_args))
{
// parse all remaining args
if (is_numeric($arg))
{
$snmp_port = $arg;
}
elseif (preg_match('/^(' . implode("|", $config['snmp']['transports']) . ')$/i', $arg))
{
$snmp_transport = $arg;
}
elseif (preg_match('/^(sha|md5)$/i', $arg))
{
$snmp_v3_auth['authalgo'] = $arg;
}
elseif (preg_match('/^(aes|des)$/i', $arg))
{
$snmp_v3_auth['cryptoalgo'] = $arg;
}
}
array_push($config['snmp']['v3'], $snmp_v3_auth);
}
} else {
// v1 or v2c
$snmp_v2_args = array_slice($argv, 2);
while ($arg = array_shift($snmp_v2_args))
{
// parse all remaining args
if (is_numeric($arg))
{
$snmp_port = $arg;
}
elseif (preg_match('/(' . implode("|", $config['snmp']['transports']) . ')/i', $arg))
{
$snmp_transport = $arg;
}
elseif (preg_match('/^(v1|v2c)$/i', $arg))
{
$snmp_version = $arg;
}
}
$config['snmp']['community'] = ($snmp_community ? array($snmp_community) : $snmp_config_community);
}
print_message("Try to add $hostname:");
if (in_array($snmp_version, array('v1', 'v2c', 'v3')))
{
// If snmp version passed in arguments, then use the exact version
$device_id = add_device($hostname, $snmp_version, $snmp_port, $snmp_transport, $snmp_options);
} else {
// If snmp version unknown ckeck all possible snmp versions and auth options
$device_id = add_device($hostname, NULL, $snmp_port, $snmp_transport, $snmp_options);
}
if ($device_id)
{
if (!isset($options['t']))
{
$device = device_by_id_cache($device_id);
print_success("Added device " . $device['hostname'] . " (" . $device_id . ").");
} // Else this is device testing, success message already written by add_device()
$added++;
}
}
}
$count = count($add_array);
$failed = $count - $added;
if ($added)
{
print_message("\nDevices success: $added.");
if ($failed)
{
print_message("Devices failed: $failed.");
}
} else {
if ($count)
{
print_message("Devices failed: $failed.");
}
print_help($scriptname);
}
function print_help($scriptname)
{
global $config;
print_message("%n
USAGE:
$scriptname <hostname> [community] [v1|v2c] [port] [" . implode("|", $config['snmp']['transports']) . "]
$scriptname <hostname> [any|nanp|anp|ap] [v3] [user] [password] [enckey] [md5|sha] [aes|des] [port] [" . implode("|", $config['snmp']['transports']) . "]
$scriptname <filename>
EXAMPLE:
%WSNMPv1/2c%n: $scriptname <%Whostname%n> [community] [v1|v2c] [port] [" . implode("|", $config['snmp']['transports']) . "]
%WSNMPv3%n : Defaults : $scriptname <%Whostname%n> any v3 [user] [port] [" . implode("|", $config['snmp']['transports']) . "]
No Auth, No Priv : $scriptname <%Whostname%n> nanp v3 [user] [port] [" . implode("|", $config['snmp']['transports']) . "]
Auth, No Priv : $scriptname <%Whostname%n> anp v3 <user> <password> [md5|sha] [port] [" . implode("|", $config['snmp']['transports']) . "]
Auth, Priv : $scriptname <%Whostname%n> ap v3 <user> <password> <enckey> [md5|sha] [aes|des] [port] [" . implode("|", $config['snmp']['transports']) . "]
%WFILE%n : $scriptname <%Wfilename%n>
ADD FROM FILE:
To add multiple devices, create a file in which each line contains one device with or without options.
Format for device options, the same as specified in USAGE.
OPTIONS:
-p Skip icmp echo checks, device added only by SNMP checks
DEBUGGING OPTIONS:
-d Enable debugging output.
-dd More verbose debugging output.
-t Do not add device(s), only test auth options.", 'color', FALSE);
}
// EOF