Skip to content

Instantly share code, notes, and snippets.

@NeilHanlon
Created January 16, 2020 20:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save NeilHanlon/6035cfafc4d38db5521e7d359d12c872 to your computer and use it in GitHub Desktop.
Save NeilHanlon/6035cfafc4d38db5521e7d359d12c872 to your computer and use it in GitHub Desktop.
import com.santaba.agent.groovyapi.snmp.Snmp;
import javax.xml.bind.DatatypeConverter;
def convertHexIPtoString(hex) {
if (hex.contains(":")) {
tempaddr = hex.split(":")
hex = tempaddr.join("")
}
InetAddress address = InetAddress.getByAddress(DatatypeConverter.parseHexBinary(hex))
finalResult = ""
if (address instanceof Inet6Address) {
finalResult = address.toString().replaceFirst("(:0)+:", "::")
} else if (address instanceof Inet4Address) {
finalResult = address.toString()
}
return finalResult.replaceFirst("/", "")
}
// Define the hostname
def hostname = hostProps.get("system.hostname");
def timeout = 15000; // Timeout in Milliseconds
// This map will contain all the oid's and their names for us to later walk.
def snmp_oids = [:]
snmp_oids['.1.3.6.1.4.1.2636.5.1.1.2.1.1.1.11'] = 'peer_remote_address'
snmp_oids['.1.3.6.1.4.1.2636.5.1.1.2.1.1.1.13'] = 'peer_remote_asn'
snmp_oids['.1.3.6.1.4.1.2636.5.1.1.2.1.1.1.7'] = 'peer_local_address'
snmp_oids['.1.3.6.1.4.1.2636.5.1.1.2.1.1.1.15'] = 'peer_routing_instance'
snmp_oids['.1.3.6.1.4.1.2636.5.1.1.2.1.1.1.4'] = 'peer_configured_version'
snmp_oids['.1.3.6.1.4.1.2636.5.1.1.2.1.1.1.14'] = 'peer_index'
// The following maps are used to store k,v pairs from it's respective OID walk output.
def remoteAddress_map = [:]
def remoteASN_map = [:]
def localASN_map = [:]
def routingInstance_map = [:]
def localAddress_map = [:]
def configuredVersion_map = [:]
def index_map = [:]
// Iterate through each oid in our base map.
snmp_oids.each
{ base_oid, base_name ->
// Create SNMP walk.
def snmp_walk = Snmp.walk(hostname, base_oid, timeout);
// Try following code
try
{
// Execute the snmpwalk and iterate through each line.
snmp_walk.eachLine()
{ line ->
// split on the '=' sign and get the oid and value.
def (raw_oid, value) = line.split(/ = /, 2);
// pull out the remote peer address to use as our ID
tempID = raw_oid.replaceAll("${base_oid}.","")
//tempID_regex = /(\d+\.\d+)\.(\d+\.\d+\.\d+\.\d+)\.(\d+)\.(\d+\.\d+\.\d+\.\d+)/
//tempID_pattern = ~tempID_regex
//tempID_matcher = tempID_pattern.matcher(tempID)
//id = tempID_matcher[0][4]
id = tempID
//println("${id}##${base_name}##${value}")
//Place each output into the matching map for later use
if(base_name == 'peer_routing_instance')
{
routingInstance_map.put(id,value)
}
if(base_name == 'peer_remote_asn')
{
remoteASN_map.put(id,value)
}
if(base_name == 'peer_configured_version')
{
configuredVersion_map.put(id,value)
}
if(base_name == 'peer_remote_address')
{
ip_string = convertHexIPtoString(value)
remoteAddress_map.put(id,ip_string)
}
if(base_name == 'peer_local_address')
{
ip_string = convertHexIPtoString(value)
localAddress_map.put(id,ip_string)
}
//This pulls out the UID we will use for wildvalue
if(base_name == 'peer_index')
{
index_map.put(id,value)
}
}
}
// Catch any exception that may have occurred.
catch (Exception e)
{
// Print the exception.
println e
}
}
// Iterate through each unique tunnel instance.each
remoteAddress_map.each
{ id, remotepeer_ip ->
// assign variables
localpeer_ip = localAddress_map[id]
configured_version = configuredVersion_map[id]
remote_asn = remoteASN_map[id]
routing_instance = routingInstance_map[id]
alias = index_map[id]
// Our wildvalue will consist of our unique tunnel pair of the local ip address and the remote ip address
wildvalue = "${alias}"
// Print every unique tunnel instance and it's respective meta data as instance level properties.
if(configured_version == "4")
println "${wildvalue}##${remotepeer_ip}##Local IP: ${localpeer_ip}####" +
"auto.peer_Local_IP=${localpeer_ip}&" +
"auto.peer_Remote_IP=${remotepeer_ip}&" +
"auto.remote_asn=${remote_asn}&" +
"auto.version=${configured_version}&" +
"auto.routing_instance=${routing_instance}"
}
// Exit script by returning 0;
return 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment