Created
June 27, 2012 15:06
-
-
Save dkhenry/3004669 to your computer and use it in GitHub Desktop.
SevOne deferred data example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
Program to upload data to SevOne using the Deferred Data API | |
@author DK ([email protected]) | |
@version 0.1 | |
@since 27-June-2012 | |
*/ | |
//! 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"; | |
//! This is the WSDL url. | |
$soapUrl = "http://{$soapIp}/soap3/api.wsdl"; | |
//! Connect to the SOAP server. | |
$client = new SoapClient($soapUrl); | |
if( !$client ) { | |
echo "!!! Could not connect to SOAP server at '{$soapUrl}'.\n"; | |
exit( 1 ); | |
} | |
echo "== Information ==\n"; | |
echo "IP: {$soapIp}\n"; | |
echo "URL: {$soapUrl}\n"; | |
echo "Username: {$username}\n"; | |
echo "Password: {$password}\n"; | |
echo "== Authentication ==\n"; | |
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 ); | |
} | |
} catch( Exception $e ) { | |
echo "Exception:\n"; | |
print_r( $e ); | |
exit( 1 ); | |
} | |
/** | |
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 string[$objectType] The object type | |
@param string[$object] 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>", | |
"units" => "<Units for this indicator>" | |
) | |
@return None | |
*/ | |
function report_stats($device, $objectType, $object, $indicators) { | |
global $client; | |
//! Get the device | |
$host = $client->core_getDeviceByName($device); | |
if( !$host ) { | |
echo "!!! Could not find host device.\n"; | |
exit( 1 ); | |
} | |
//! Find the object type (create if necessary) | |
$ot = $client->plugin_deferred_getObjectTypeByOsIdAndName( 0 , $objectType ); | |
if(!$ot) { | |
$otid = $client->plugin_deferred_createObjectType( 0 , $objectType ); | |
$ot = $client->plugin_deferred_getObjectTypeById($otid); | |
} | |
if(!$ot) { | |
throw new Exception("Error creating new object type\n."); | |
} | |
//! Find the Indicator Types (create if necessary) | |
$indicatorTypes = array(); | |
$itypes = $client->plugin_deferred_getIndicatorTypesByObjectTypeId($ot->id); | |
foreach( $itypes as $indicator ){ | |
$indicatorTypes[$indicator->name] = $indicator; | |
} | |
foreach($indicators as $indicator) { | |
if(!array_key_exists($indicator["name"], $indicatorTypes)){ | |
$id = $client->plugin_deferred_createIndicatorType($ot->id,$indicator["name"]); | |
if(array_key_exists("type", $indicator)) { | |
$client->plugin_deferred_setIndicatorTypeFormat($id,$indicator["type"]); | |
} | |
if(array_key_exists("units", $indicator)) { | |
$client->plugin_deferred_setIndicatorTypeUnits($id,$indicator["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 | |
$obj = $client->plugin_deferred_getObject($host->id,$object); | |
if(!$obj) { | |
$client->plugin_deferred_createObject($host->id,$ot->id,$object); | |
//! If this object doesn't exist we need discovery to make it | |
$client->core_rediscoverDevice($host->id); | |
return; | |
} | |
//! Find the indicators (Create if necessary) | |
$inds = array(); | |
$itypes = $client->plugin_deferred_getIndicatorsByObject( $host->id, $obj->name); | |
foreach($itypes as $indicator) { | |
$inds[$indicator->indicatorType] = $indicator; | |
} | |
//! 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($host->id,$inds[$indicator["name"]]->id,1,$indicator["maxValue"]); | |
} | |
} | |
} | |
if( count($data) > 0 ) { | |
$client->plugin_deferred_insertDataRow($host->id, array_keys($data), array_values(($data))); | |
} | |
} | |
try { | |
//read in the lines from the script | |
$in = fopen("php://stdin","r"); | |
$input = ""; | |
while($line = fgets($in)) { | |
$input = $input . $line; | |
} | |
// We are expecting | |
// 10 | |
// 200 | |
// uptime | |
// target (we are interpreting this as object name) | |
$data = split("\n",$input); | |
if(count($data) < 4 ) { | |
echo "Input data is the wrong length (". count($data). ").\n"; | |
exit(1); | |
} | |
//! Here we set up the indicators | |
$indicators = array( | |
array( | |
"name" => "Incoming bytes count", | |
"value" => $data[0], | |
"type" => "COUNTER64", | |
"units" => "Bytes" | |
), | |
array( | |
"name" => "Outgoing bytes count", | |
"value" => $data[1], | |
"type" => "COUNTER64", | |
"units" => "Bytes" | |
) | |
); | |
//report the data | |
report_stats($deviceName,"Interface",$data[3],$indicators); | |
} catch( Exception $e ) { | |
echo "Exception:\n"; | |
print_r( $e ); | |
exit( 1 ); | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment