#!/usr/bin/env python
import requests
import os
from requests.auth import HTTPBasicAuth
import json
class OpenNMS:
def __init__(self, user_name="admin", pass_word="admin"):
self.nms_user = user_name
self.nms_pass = pass_word
self.base_url = "http://172.16.60.77:8980/opennms/rest/"
self.header = {"Accept": "application/json", "Content-Type": "application/json"}
# self.auth = HTTPBasicAuth(self.nms_user, self.nms_pass)
self.auth = ("admin", "admin")
def get_node_id(self, node_name):
url = self.base_url + "nodes?label=%s" % node_name
r = requests.get(url, data=None, headers=self.header, auth=self.auth)
response = json.loads(r.text)
print response
try:
if int(response['count']) == 1:
return response['node'][0]['id']
except:
import traceback
traceback.print_exc()
def get_os_identification(self, node_name):
url = self.base_url + "nodes?label=%s" % node_name
r = requests.get(url, data=None, headers=self.header, auth=self.auth)
response = json.loads(r.text)
if int(response['count']) == 1:
sys_obj_id = response['node'][0]['sysObjectId']
if sys_obj_id == ".1.3.6.1.4.1.8072.3.2.10":
os = "Linux"
elif sys_obj_id == ".1.3.6.1.4.1.311.1.1.3.1.2":
os = "Windows"
elif sys_obj_id == ".1.3.6.1.4.1.9.1.669":
os = "Cisco ASA"
else:
os = "Unkown"
else:
return "os"
def get_resource_name(self, node_name):
url = self.base_url + "nodes?label=%s" % node_name
r = requests.get(url, data=None, headers=self.header, auth=self.auth)
response = json.loads(r.text)
if int(response['count']) == 1:
foreign_source = response['node'][0]['foreignSource']
foreign_id = response['node'][0]['foreignId']
resource_name = "%s:%s" % (foreign_source, foreign_id)
return resource_name
def get_interface_map(self, node_name):
url = self.base_url + "nodes/%s/ipinterfaces" % self.get_node_id(node_name=node_name)
r = requests.get(url, data=None, headers=self.header, auth=self.auth)
response = json.loads(r.text)
if response['ipInterface']:
interface_list = []
for ip_interface in response['ipInterface']:
interface_list.append(
"%s-%s" % (ip_interface['snmpInterface']['ifName'], ip_interface['snmpInterface']['physAddr'])
)
return set(interface_list)
def get_ksc_reports(self):
url = self.base_url + "ksc/0"
r = requests.get(url, data=None, headers=self.header, auth=self.auth)
print r.text
def get_categories(self):
url = self.base_url + "categories"
r = requests.get(url, data=None, headers=self.header, auth=self.auth)
response = json.loads(r.text)
print response
def get_opennms_resource_id(self, node_name):
resource_id = "node[%s]" % self.get_resource_name(node_name=node_name)
return resource_id
def get_interface_snmp_resource_id(self, node_name):
interface_list = self.get_interface_map(node_name=node_name)
opennms_resource_id = self.get_opennms_resource_id(node_name=node_name)
if interface_list:
for interface in interface_list:
print '"%s.interfaceSnmp[%s]"' % (opennms_resource_id, interface)
def generate_basic_ksc_graph(self, node_name):
opennms_resource_id = self.get_opennms_resource_id(node_name=node_name)
print '<Report id="0" title="%s" show_timespan_button="true" show_graphtype_button="false" ' \
'graphs_per_line="4">' % node_name.lower()
if self.get_os_identification(node_name=node_name) == "Linux":
host_metrics = ['netsnmp.hrSystemProcesses', 'netsnmp.hrMemory', 'netsnmp.hrNumUsers',
'netsnmp.rawinterrupts', 'netsnmp.hrSystemUptime', 'netsnmp.loadavg',
'netsnmp.swapinout', 'netsnmp.memStats', 'netsnmp.cpuUsageFull', 'netsnmp.cpuStatsFull']
interface_metrics = ['mib2.HCtraffic-inout', 'mib2.HCbits']
storage_graphs = ['_root_fs']
for host_metric in host_metrics:
print '\t<Graph title="" resourceId="%s.nodeSnmp[]" timespan="1_hour" graphtype="%s"/>' % \
(opennms_resource_id, host_metric)
interface_lists = self.get_interface_map(node_name=node_name)
for interface_list in interface_lists:
for interface_metric in interface_metrics:
print '\t<Graph title="" resourceId="%s.interfaceSnmp[%s]" timespan="1_hour" graphtype="%s"/>' % \
(opennms_resource_id, interface_list, interface_metric)
for storage_graph in storage_graphs:
print '\t<Graph title="" resourceId="%s.hrStorageIndex[%s]" timespan="1_hour" graphtype="%s"/>' % \
(opennms_resource_id, storage_graph, 'mib2.storage.usage')
elif self.get_os_identification(node_name=node_name) == "Windows":
host_metrics = ['netsnmp.hrSystemUptime', 'microsoft.cpuPercentBusy',
'netsnmp.hrSystemProcesses', 'microsoft.memory', 'netsnmp.hrNumUsers']
interface_metrics = ['mib2.bits', 'mib2.traffic-inout']
storage_graphs = ['C', 'PhysicalMemory', 'VirtualMemory']
for host_metric in host_metrics:
print '\t<Graph title="" resourceId="%s.nodeSnmp[]" timespan="1_hour" graphtype="%s"/>' % \
(opennms_resource_id, host_metric)
interface_lists = self.get_interface_map(node_name=node_name)
for interface_list in interface_lists:
for interface_metric in interface_metrics:
print '\t<Graph title="" resourceId="%s.interfaceSnmp[%s]" timespan="1_hour" graphtype="%s"/>' % \
(opennms_resource_id, interface_list, interface_metric)
for storage_graph in storage_graphs:
print '\t<Graph title="" resourceId="%s.hrStorageIndex[%s]" timespan="1_hour" graphtype="%s"/>' % \
(opennms_resource_id, storage_graph, 'mib2.storage.usage')
else:
print ""
print '</Report>'
def requisitions(self):
url = os.path.join(self.base_url, "requisitions", "DeepValue", "nodes", "1518924842650", "assets")
print url
# data = {"name": "Production"}
data = {"name": "latitude", "value": "15.0826802"}
# r = requests.post(url, data=json.dumps(data), auth=self.auth, headers=self.header)
r = requests.get(url, data=None, auth=self.auth, headers=self.header)
print r
print r.text
def notice_ack(self):
url = os.path.join(self.base_url, "acks")
data = {"notifId": "227", "action": "ack"}
r = requests.post(url, data=data, auth=self.auth)
if r.status_code == 200:
print "Have successfully updated. url=%s and data=%s" % (r.url, data)
else:
print "Have receive status_code=%s and url=%s" % (r.status_code, r.url)
if __name__ == '__main__':
NMS = OpenNMS()
NMS.requisitions()