-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathdeferred-data-import
executable file
·387 lines (351 loc) · 12.8 KB
/
deferred-data-import
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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
#!/usr/bin/php
<?php
/**
Program to upload data to SevOne using the Deferred Data API
Based on DK's script, located at https://gist.github.com/3004669
@author MC ([email protected])
@version 0.2
@since 27-June-2012
*/
function usage() { // {{{
echo 'Usage:', PHP_EOL;
echo ' deferred-data-import sevone-hostname-or-ip sevone-username sevone-password sevone-full-device-name', PHP_EOL;
echo PHP_EOL;
/* }}} */ }
//! The Appliance IP
$soapIp = '127.0.0.1';
//! The username and password
$username = '<username>';
$password = '<password>';
//! The Device to add the objects to
$deviceName = 'myrouter';
if($argc != 5) {
usage();
exit;
}
$soapIp = $argv[1];
$username = $argv[2];
$password = $argv[3];
$deviceName = $argv[4];
//! This is the WSDL url.
$soapUrl = 'http://' . $soapIp . '/soap3/api.wsdl';
//! Connect to the SOAP server.
$client = new SoapClient($soapUrl, array('cache_wsdl' => WSDL_CACHE_NONE));
if( !$client ) {
echo '!!! Could not connect to SOAP server at "', $soapUrl, '".', PHP_EOL;
exit( 1 );
}
$client->__setLocation('http://' . $soapIp . '/soap3/api.php');
echo '== Information ==', PHP_EOL;
echo 'IP: ', $soapIp, PHP_EOL;
echo 'URL: ', $soapUrl, PHP_EOL;
echo 'Username: ', $username, PHP_EOL;
echo 'Password: ', $password, PHP_EOL;
echo 'Device name: ', $deviceName, PHP_EOL;
echo '== Authentication ==', PHP_EOL;
try {
//! This authenticates a user for the duration of this script.
$result = $client->authenticate($username, $password);
if( !$result ) {
echo "!!! Could not authenticate with the server.\n";
exit( 1 );
} else {
echo 'Authentication successful: ', $result, PHP_EOL;
}
} catch( Exception $e ) {
echo "Exception:\n";
print_r( $e );
exit( 1 );
}
//! Get the device
try {
if(!$device = $client->core_getDeviceByName($deviceName)) {
echo '!!! Could not find host device.', PHP_EOL;
exit( 1 );
} else {
echo 'Device found: ', $device->id, PHP_EOL;
}
} catch(Exception $e) {
echo '!!! Could not find host device: Exception:', PHP_EOL;
print_r($e);
exit(1);
}
define('SO_PARSE_MODE_NORM', 0);
define('SO_PARSE_MODE_INQUOTE_SINGLE', 1);
define('SO_PARSE_MODE_INQUOTE_DOUBLE', 2);
// We are expecting:
// "timestamp" "object name" "object type" "object description" ["indicator name 1" "indicator format 1" "indicator units 1" "indicator value 1" ["indicator name 2" "indicator format 2" "indicator units 2" "indicator value 2" [... etc]]]
// Note that the units can be just a single unit for both measurement and display, or it can be of the format "measurement units//display units"
function split_data_line($line) { // {{{
//$parts = preg_split('/(\s+|\'|")/', $line, -1, PREG_SPLIT_DELIM_CAPTURE);
$parts = preg_split('/(\s+)/', $line, -1, PREG_SPLIT_DELIM_CAPTURE);
$items = array();
$mode = SO_PARSE_MODE_NORM;
$currentItem = '';
foreach($parts as $part) {
switch($mode) {
case SO_PARSE_MODE_NORM:
if(preg_match('/^\s+$/', $part)) {
if(substr($currentItem, -1) == "\\" && substr($currentItem, -2, 1) != "\\") {
$currentItem = substr($currentItem, 0, -1);
} else {
$items[] = $currentItem;
$currentItem = '';
continue;
}
} else {
$startChar = substr($part, 0, 1);
if($startChar == '"') {
$part = substr($part, 1);
$endChar1 = substr($part, -2, 1);
$endChar2 = substr($part, -1);
if($endChar2 == '"' && $endChar1 != "\\") {
$part = substr($part, 0, -1);
} else {
$mode = SO_PARSE_MODE_INQUOTE_DOUBLE;
}
} elseif($startChar == '\'') {
$part = substr($part, 1);
$endChar1 = substr($part, -2, 1);
$endChar2 = substr($part, -1);
if($endChar2 == '\'' && $endChar1 != "\\") {
$part = substr($part, 0, -1);
} else {
$mode = SO_PARSE_MODE_INQUOTE_SINGLE;
}
}
}
$currentItem .= $part;
break;
case SO_PARSE_MODE_INQUOTE_SINGLE:
$endChar1 = substr($part, -2, 1);
$endChar2 = substr($part, -1);
if($endChar2 == '\'' && $endChar1 != "\\") {
$part = substr($part, 0, -1);
$mode = SO_PARSE_MODE_NORM;
}
$currentItem .= $part;
break;
case SO_PARSE_MODE_INQUOTE_DOUBLE:
$endChar1 = substr($part, -2, 1);
$endChar2 = substr($part, -1);
if($endChar2 == '"' && $endChar1 != "\\") {
$part = substr($part, 0, -1);
$mode = SO_PARSE_MODE_NORM;
}
$currentItem .= $part;
break;
}
}
return $items;
/* }}} */ }
function parse_data_line($line) { // {{{
$items = split_data_line($line);
$original_time = $items[0];
$output = array(
'time' => is_numeric($original_time) ? $original_time : @strtotime($original_time),
'object_name' => $items[1],
'object_type' => $items[2],
'object_description' => $items[3],
'indicators' => array()
);
if(count($items) % 4 != 0) {
echo '!!! Data row for object "', $output['object_name'], '" at time "', $original_time, '" had an invalid number of arguments (', count($items), ')', PHP_EOL;
return false;
}
for($i = 4; $i + 2 < count($items); $i += 4) {
$output['indicators'][] = array(
'name' => $items[$i],
'type' => $items[$i + 1],
'units' => $items[$i + 2],
'value' => $items[$i + 3]
);
}
foreach($output['indicators'] as $i => &$indicator) {
if(!in_array($indicator['type'], array('GAUGE', 'COUNTER32', 'COUNTER64'))) {
echo '!!! Indicator number ', $i, ' for object "', $output['object_name'], '" at time "', $original_time, '" had invalid format "', $indicator['type'], '"', PHP_EOL;
return false;
}
$matches = null;
if(preg_match(':(.+)//(.+):', $indicator['units'], $matches)) {
$indicator['data_units'] = $matches[1];
$indicator['display_units'] = $matches[2];
unset($indicator['units']);
} else {
$indicator['data_units'] = $indicator['units'];
$indicator['display_units'] = $indicator['units'];
unset($indicator['units']);
}
if(preg_match(':(.+)/(.+):', $indicator['value'], $matches)) {
$indicator['value'] = $matches[1];
$indicator['maxValue'] = $matches[2];
}
}
return $output;
/* }}} */ }
/**
Report Statistics to the Server
This function will lookup and create the object type and indicator types necessary to submit data to the server
It will also handle flagging a device for discovery if a new object needs to be created.
This assumes the $client variable exists and it a valid SevOne API client
@param string[$device] The name of the device to associate this data with
@param int[$time] The timestamp to associate with the data
@param string[$objectType] The object type
@param string[$objectName] The name of the object
@param string[$objectDescription] The name of the object
@param array[$indicators] An array of indicators. having this structure
array(
"name" => "<indicator name>",
"value" => "<indicator value>",
// The following are optional
"type" => "<indicator type>",
"maxValue" => "<indicator Max Value>",
"data_units" => "<Units in which this indicator is measured>"
"display_units" => "<Units in which this indicator is displayed>"
)
@return None
*/
function report_stats($deviceId, $time, $objectType, $objectName, $objectDescription, $indicators) { /* {{{ */
global $client;
static $ot_by_name = array();
static $itypes_by_otId = array();
static $obj_by_devIdAndName = array();
static $inds_by_devIdAndObjId = array();
//! Find the object type (create if necessary)
if(array_key_exists($objectType, $ot_by_name)) {
$ot = $ot_by_name[$objectType];
} else {
// TODO: Verbose flag to print this
//echo '+++ ot_by_name cache miss ("', $objectType, '")', PHP_EOL;
try {
$ot = $client->plugin_deferred_getObjectTypeByOsIdAndName(0, $objectType);
} catch(SoapFault $e) {
$ots = $client->plugin_deferred_getObjectTypes();
$ot = null;
foreach($ots as $_ot) {
if($_ot->name == $objectType) {
$ot = $_ot;
break;
}
}
}
if(!$ot) {
$otid = $client->plugin_deferred_createObjectType(0, $objectType);
sleep(5);
$ot = $client->plugin_deferred_getObjectTypeById($otid);
}
if(!$ot) {
throw new Exception("Error creating new object type\n");
} elseif(isset($otid)) {
echo '--- Created new Object Type, called "', $objectType, '", with ID ', $otid, PHP_EOL;
}
$ot_by_name[$objectType] = $ot;
}
//! Find the Indicator Types (create if necessary)
unset($indicatorTypes);
if(array_key_exists($ot->id, $itypes_by_otId)) {
$indicatorTypes = &$itypes_by_otId[$ot->id];
} else {
// TODO: Verbose flag to print this
//echo '+++ itypes_by_otId cache miss ("', $ot->id, '")', PHP_EOL;
$indicatorTypes = array();
$itypes = $client->plugin_deferred_getIndicatorTypesByObjectTypeId($ot->id);
foreach($itypes as $indicator) {
$indicatorTypes[$indicator->name] = $indicator;
}
$itypes_by_otId[$ot->id] = &$indicatorTypes;
}
foreach($indicators as $indicator) {
if(!array_key_exists($indicator["name"], $indicatorTypes)){
$id = $client->plugin_deferred_createIndicatorType($ot->id, $indicator['name']);
echo '--- Created new Indicator Type, called "', $indicator['name'], '", with ID ', $id, ', on Object Type ', $ot->id, PHP_EOL;
if(array_key_exists("type", $indicator)) {
$client->plugin_deferred_setIndicatorTypeFormat($id, $indicator['type']);
}
if(array_key_exists('data_units', $indicator) && array_key_exists('display_units', $indicator)) {
$client->plugin_deferred_setIndicatorTypeUnits($id, $indicator['data_units'], $indicator['display_units']);
} elseif(array_key_exists('data_units', $indicator)) {
$client->plugin_deferred_setIndicatorTypeUnits($id, $indicator['data_units']);
}
if(array_key_exists("maxValue",$indicator)) {
$client->plugin_deferred_setIndicatorTypeHasMaxValue($id, 1);
}
$indicatorTypes[$indicator["name"]] = $client->plugin_deferred_getIndicatorTypeByObjectTypeIdAndName($ot->id, $indicator["name"]);
}
}
//! Get the object
$objectKey = $deviceId . ':' . $objectName;
if(array_key_exists($objectKey, $obj_by_devIdAndName)) {
$obj = $obj_by_devIdAndName[$objectKey];
} else {
// TODO: Verbose flag to print this
//echo '+++ $obj_by_devIdAndName cache miss ("', $objectKey, '")', PHP_EOL;
$obj = $client->plugin_deferred_getObject($deviceId,$objectName);
if(!(array)$obj) {
$objId = $client->plugin_deferred_createObject($deviceId,$ot->id,$objectName);
echo '--- Created new Object, called "', $objectName, '", with ID ', $objId, ' and Object Type ', $ot->id, PHP_EOL;
$client->plugin_deferred_setObjectOverrideDescription($deviceId, $objId, 1, $objectDescription);
$client->plugin_deferred_setObjectIsEnabled($deviceId, $objId, 1);
$client->plugin_deferred_setObjectIsHidden($deviceId, $objId, 0);
//! If this object doesn't exist we need discovery to make it
$client->core_rediscoverDevice($deviceId);
return;
}
$client->plugin_deferred_setObjectOverrideDescription($deviceId, $obj->id, 1, $objectDescription);
$obj_by_devIdAndName[$objectKey] = $obj;
}
//! Find the indicators (Create if necessary)
$indicatorKey = $deviceId . ':' . $obj->id;
if(array_key_exists($indicatorKey, $inds_by_devIdAndObjId)) {
$inds = $inds_by_devIdAndObjId[$indicatorKey];
} else {
// TODO: Verbose flag to print this
//echo '+++ $inds_by_devIdAndObjId cache miss ("', $indicatorKey, '")', PHP_EOL;
$inds = array();
$itypes = $client->plugin_deferred_getIndicatorsByObject($deviceId, $obj->name);
foreach($itypes as $indicator) {
$inds[$indicator->indicatorType] = $indicator;
}
$inds_by_devIdAndObjId[$indicatorKey] = $inds;
}
//! Submit the indicators
$data = array();
foreach($indicators as $indicator) {
if(array_key_exists($indicator["name"], $inds)){
$data[$inds[$indicator["name"]]->id] = $indicator["value"];
if(array_key_exists("maxValue", $indicator)) {
$client->plugin_deferred_setIndicatorOverrideMaxValue($deviceId,$inds[$indicator["name"]]->id,1,$indicator["maxValue"]);
}
}
}
if(count($data) > 0) {
foreach($data as &$value) {
if(!is_numeric($value)) {
$value = null;
}
}
if(is_numeric($time)) {
echo '--- Inserting data row for Object "', $objectName, '" (ID# ', $obj->id, ') at ', $time, ' (', @date('Y-m-d H:i:s', $time), ')', PHP_EOL;
$client->plugin_deferred_insertDataRows($deviceId, array_keys($data), array($time), array($time => array_values($data)));
} else {
echo '--- Inserting data row for Object "', $objectName, '" (ID# ', $obj->id, ') at ', date('U'), ' (', date('Y-m-d H:i:s'), ') [NOW]', PHP_EOL;
$client->plugin_deferred_insertDataRow($deviceId, array_keys($data), array_values($data));
}
}
} // }}}
//read in the lines from the script
$in = fopen('php://stdin', 'r');
while($line = fgets($in)) {
$parseyBits = parse_data_line($line);
if($parseyBits === false) {
continue;
}
//report the data
try {
report_stats($device->id, $parseyBits['time'], $parseyBits['object_type'], $parseyBits['object_name'], $parseyBits['object_description'], $parseyBits['indicators']);
} catch(Exception $e) {
echo 'Exception:', PHP_EOL;
print_r($e);
exit(1);
}
}