Commit 62a401e6 authored by Matthew Smith's avatar Matthew Smith
Browse files

docstrings and class method name clean up

parent 8c12b3f9
......@@ -80,7 +80,7 @@ long_date_time = os.getenv('LONGDATETIME', '')
notification_author = os.getenv('NOTIFICATIONAUTHORNAME', '')
notification_comment = os.getenv('NOTIFICATIONCOMMENT', '')
email_to = os.getenv('USEREMAIL', '')
performance_data = os.getenv('PERFDATA') # needs to be null to avoid it
performance_data = os.getenv('PERFDATA', '')
netbox_host_name = os.getenv('NETBOXHOSTNAME', host_alias)
if netbox_host_name == '':
# If the env is set but empty
......@@ -124,8 +124,15 @@ if DEBUG:
# Classes for getting data from each external system
class Netbox:
"""Netbox object that parses data from the Netbox api
all ivars are intialized as empty and filled if valid data is found for each type
:ivar host: dict : api data to the host for the NETBOXBASE and host_alias
:ivar host_url: str : url to the host for the NETBOXBASE and host_alias
:ivar ip: dict : api data to the host ip address for the NETBOXBASE and host_alias
:ivar ip_url: str : url to the host ip address for the NETBOXBASE and host_alias
"""
def __init__(self):
self.host = {}
......@@ -134,12 +141,12 @@ class Netbox:
self.ip_url = ''
if NETBOXBASE:
self.parseNetbox()
self.__parse()
def parseNetbox(self):
nb_device = self.searchNetboxData(NETBOXBASE + '/api' + NETBOXPATHDEVICES + '/?name=' + host_alias)
nb_vm = self.searchNetboxData(NETBOXBASE + '/api' + NETBOXPATHVMS + '/?name=' + host_alias)
nb_ip = self.searchNetboxData(NETBOXBASE + '/api' + NETBOXPATHIPS + '/?address=' + host_address)
def __parse(self):
nb_device = self.__searchData(NETBOXBASE + '/api' + NETBOXPATHDEVICES + '/?name=' + host_alias)
nb_vm = self.__searchData(NETBOXBASE + '/api' + NETBOXPATHVMS + '/?name=' + host_alias)
nb_ip = self.__searchData(NETBOXBASE + '/api' + NETBOXPATHIPS + '/?address=' + host_address)
if DEBUG:
print(json.dumps(nb_device, indent=4, sort_keys=True))
......@@ -161,7 +168,7 @@ class Netbox:
self.ip = nb_ip
self.ip_url = "{}/{}/".format(NETBOXBASE + NETBOXPATHIPS, nb_ip['id'])
def getNetboxData(self, url):
def __getServerData(self, url):
headers = {'Accept': 'application/json'}
if NETBOXTOKEN:
headers.update({'Authorization': 'Token ' + NETBOXTOKEN})
......@@ -179,14 +186,14 @@ class Netbox:
print(result)
return result
def searchNetboxData(self, url):
result = self.getNetboxData(url)
def __searchData(self, url):
result = self.__getServerData(url)
if result['count'] == 1 and result['results']:
return result['results'][0]
else:
return {}
def getNetboxVal(self, obj, key1, key2=None):
def __getVal(self, obj, key1, key2=None):
val = ''
if key1 in obj:
if key2 and key2 in obj[key1]:
......@@ -195,30 +202,67 @@ class Netbox:
val = obj[key1]
return val
def addNetboxVal(self, title, obj, key1, key2=None):
val = self.getNetboxVal(obj, key1, key2)
def addRow(self, title, obj, key1, key2=None):
"""Generate html row containing title and value from key(s) in object
Arguments:
title {str} -- Title for the row
obj {dict} -- netbox dict that we are parsing
key1 {str} -- key to get value for
Keyword Arguments:
key2 {str} -- sub key to get value for (default: {None})
Returns:
str -- html row if keys exist else empty string
"""
val = self.__getVal(obj, key1, key2)
if val:
val = '\n<tr><th width="' + COLUMN + '">' + title + ':</th><td>' + val + '</td></tr>'
else:
val = ''
return val
def addNetboxLinkVal(self, title, obj, key1, key2=None):
val = self.getNetboxVal(obj, key1, key2)
def addLinkRow(self, title, obj, key1, key2=None):
"""Generate html row containing title, value and link from key(s) in object
assumes the last key contains a key value == url
Arguments:
title {str} -- Title for the row
obj {dict} -- netbox dict that we are parsing
key1 {str} -- key to get value for
Keyword Arguments:
key2 {str} -- sub key to get value for (default: {None})
Returns:
str -- html row if keys exist else empty
"""
val = self.__getVal(obj, key1, key2)
if val:
val = '\n<tr><th width="' + COLUMN + '">' + title + ':</th><td><a href="' + re.sub(r"\/api\/", "/", self.getNetboxVal(obj, key1, "url")) + '">' + val + '</a></td></tr>'
val = '\n<tr><th width="' + COLUMN + '">' + title + ':</th><td><a href="' + re.sub(r"\/api\/", "/", self.__getVal(obj, key1, "url")) + '">' + val + '</a></td></tr>'
else:
val = ''
return val
# If you don't have a panelID then we can't get any data
class Grafana:
"""
Grafana object that parses icingaweb2 grafana module (optional) and data from the grafana api
all ivars are intialized as empty and filled if valid data is found for each type
:ivar png_url: str: url to the png for the GRAFANABASE, netbox_host_name and panelid
:ivar page_url: str: url to the page for the GRAFANABASE, netbox_host_name and panelid
:ivar png: request.get object : contains the png for the GRAFANABASE, netbox_host_name and panelid
:ivar self.panelID: int : number representing the panelid for the service
:ivar __icingaweb2_ini: ConfigParser.read object : contains the icingaweb2 grafana module ini settings for the GRAFANAICINGAWEB2INI
"""
def __init__(self):
self.png_url = ''
self.page_url = ''
self.png = None
self.icingaweb2_ini = None
self.__icingaweb2_ini = None
self.panelID = None
if os.path.exists(GRAFANAICINGAWEB2INI):
......@@ -239,11 +283,11 @@ class Grafana:
if DEBUG:
print("\nGrafana ini file: {}".format(GRAFANAICINGAWEB2INI))
try:
self.icingaweb2_ini = ConfigParser.ConfigParser()
self.icingaweb2_ini.read(GRAFANAICINGAWEB2INI)
self.__icingaweb2_ini = ConfigParser.ConfigParser()
self.__icingaweb2_ini.read(GRAFANAICINGAWEB2INI)
except Exception as e:
print("Unable to parse grafana ini file ({}) with error {}".format(GRAFANAICINGAWEB2INI, e))
self.icingaweb2_ini = None
self.__icingaweb2_ini = None
def __getPNG(self):
headers = {'Authorization': 'Bearer ' + GRAFANAAPIKEY}
......@@ -271,10 +315,10 @@ class Grafana:
section = None
try:
if DEBUG:
print("\nGrafana ini sections: {}".format(self.icingaweb2_ini.sections()))
print("\nGrafana ini sections: {}".format(self.__icingaweb2_ini.sections()))
print("\nGrafana ini pattern: {}".format(pattern))
if pattern:
section = filter(pattern.match, self.icingaweb2_ini.sections())
section = filter(pattern.match, self.__icingaweb2_ini.sections())
if DEBUG:
print("\nGrafana section: {}".format(section))
if len(section) == 1:
......@@ -291,7 +335,7 @@ class Grafana:
section = self.__searchSections(display_name, name, command)
panel_id = None
if section:
panel_id = self.icingaweb2_ini.get(section, 'panelId').replace('"', '')
panel_id = self.__icingaweb2_ini.get(section, 'panelId').replace('"', '')
return panel_id
# initalise objects for 3rd party info
......@@ -370,29 +414,29 @@ if netbox.host:
email_html += '\n</table><br>'
email_html += '\n<table width=' + WIDTH + '>'
email_html += '\n<tr><th colspan=2 class=perfdata><a href="' + netbox.host_url + '">Netbox Info for ' + host_alias + '</a></th></tr>'
email_html += netbox.addNetboxVal('Display Name', netbox.host, 'display_name')
email_html += netbox.addNetboxVal('Display Name', netbox.host, 'name')
email_html += netbox.addNetboxLinkVal('Cluster', netbox.host, 'cluster', 'name')
email_html += netbox.addNetboxLinkVal('Tennant', netbox.host, 'tennant', 'name')
email_html += netbox.addNetboxLinkVal('Site', netbox.host, 'site', 'name') # Sites use the slug
email_html += netbox.addNetboxLinkVal('Rack', netbox.host, 'rack', 'name')
email_html += netbox.addNetboxVal('Position', netbox.host, 'position')
email_html += netbox.addNetboxVal('Primary IP', netbox.host, 'primary_ip')
email_html += netbox.addNetboxVal('Primary IPv4', netbox.host, 'primary_ip4')
email_html += netbox.addNetboxVal('Primary IPv6', netbox.host, 'primary_ip6')
email_html += netbox.addNetboxLinkVal('Device Type', netbox.host, 'device_type', 'model')
email_html += netbox.addNetboxVal('Status', netbox.host, 'status', 'label')
email_html += netbox.addRow('Display Name', netbox.host, 'display_name')
email_html += netbox.addRow('Display Name', netbox.host, 'name')
email_html += netbox.addLinkRow('Cluster', netbox.host, 'cluster', 'name')
email_html += netbox.addLinkRow('Tennant', netbox.host, 'tennant', 'name')
email_html += netbox.addLinkRow('Site', netbox.host, 'site', 'name') # Sites use the slug
email_html += netbox.addLinkRow('Rack', netbox.host, 'rack', 'name')
email_html += netbox.addRow('Position', netbox.host, 'position')
email_html += netbox.addRow('Primary IP', netbox.host, 'primary_ip')
email_html += netbox.addRow('Primary IPv4', netbox.host, 'primary_ip4')
email_html += netbox.addRow('Primary IPv6', netbox.host, 'primary_ip6')
email_html += netbox.addLinkRow('Device Type', netbox.host, 'device_type', 'model')
email_html += netbox.addRow('Status', netbox.host, 'status', 'label')
if netbox.ip:
email_html += '\n</table><br>'
email_html += '\n<table width=' + WIDTH + '>'
email_html += '\n<tr><th colspan=2 class=perfdata><a href="' + netbox.ip_url + '">Netbox Info for ' + host_address + '</a></th></tr>'
email_html += netbox.addNetboxVal('Display Name', netbox.ip, 'address')
email_html += netbox.addNetboxVal('Status', netbox.ip, 'status', 'label')
email_html += netbox.addNetboxVal('Host', netbox.ip, 'virtual_machine', 'name')
email_html += netbox.addNetboxVal('Host', netbox.ip, 'device', 'name')
email_html += netbox.addRow('Display Name', netbox.ip, 'address')
email_html += netbox.addRow('Status', netbox.ip, 'status', 'label')
email_html += netbox.addRow('Host', netbox.ip, 'virtual_machine', 'name')
email_html += netbox.addRow('Host', netbox.ip, 'device', 'name')
if performance_data or grafana.png:
if (performance_data and '=' in performance_data) or grafana.png:
email_html += '\n</table><br>'
email_html += '\n<table width=' + WIDTH + '>'
email_html += '\n<tr><th colspan=6 class=perfdata>Performance Data</th></tr>'
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment