Commit a2ea5e6a authored by Matthew Smith's avatar Matthew Smith
Browse files

improve error handling for netbox

parent 957c68b1
...@@ -100,6 +100,8 @@ if host_state: ...@@ -100,6 +100,8 @@ if host_state:
email_subject = 'Host ' + notification_type + ' - ' + host_display_name + ' is ' + host_state email_subject = 'Host ' + notification_type + ' - ' + host_display_name + ' is ' + host_state
elif service_state: elif service_state:
email_subject = 'Service ' + notification_type + ' - ' + host_display_name + ' service ' + service_display_name + ' is ' + service_state email_subject = 'Service ' + notification_type + ' - ' + host_display_name + ' service ' + service_display_name + ' is ' + service_state
else:
email_subject = 'Unknown' + notification_type + ' - ' + host_display_name + ' service ' + service_display_name + ' (no host or service state)'
# With debug on each run produces a template that can be rerun for testing # With debug on each run produces a template that can be rerun for testing
if DEBUG: if DEBUG:
...@@ -224,7 +226,7 @@ class Grafana: ...@@ -224,7 +226,7 @@ class Grafana:
self.icingaweb2_ini = None self.icingaweb2_ini = None
self.panelID = None self.panelID = None
if GRAFANAICINGAWEB2INI: if os.path.exists(GRAFANAICINGAWEB2INI):
self.__parseIcingaweb2INI() self.__parseIcingaweb2INI()
if service_state: if service_state:
...@@ -241,17 +243,25 @@ class Grafana: ...@@ -241,17 +243,25 @@ class Grafana:
def __parseIcingaweb2INI(self): def __parseIcingaweb2INI(self):
if DEBUG: if DEBUG:
print("\nGrafana ini file: {}".format(GRAFANAICINGAWEB2INI)) print("\nGrafana ini file: {}".format(GRAFANAICINGAWEB2INI))
self.icingaweb2_ini = ConfigParser.ConfigParser() try:
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
def __getPNG(self): def __getPNG(self):
headers = {'Authorization': 'Bearer ' + GRAFANAAPIKEY} headers = {'Authorization': 'Bearer ' + GRAFANAAPIKEY}
if DEBUG: if DEBUG:
print("\n PNG url: " + self.png_url) print("PNG url: " + self.png_url)
print("\n PNG headers: {}".format(headers)) print("PNG headers: {}".format(headers))
response = requests.get(self.png_url, headers=headers) try:
if DEBUG: response = requests.get(self.png_url, headers=headers)
print(response.status_code) if DEBUG:
print("PNG get status code: {}".format(response.status_code))
except Exception as e:
print("Error getting png from {} with error {}".format(self.page_url, e))
response = None
return response return response
def __searchSections(self, display_name, name, command): def __searchSections(self, display_name, name, command):
...@@ -263,16 +273,20 @@ class Grafana: ...@@ -263,16 +273,20 @@ class Grafana:
if command and pattern is None: if command and pattern is None:
pattern = re.compile(command) pattern = re.compile(command)
if DEBUG:
print("\nGrafana ini sections: {}".format(self.icingaweb2_ini.sections()))
print("\nGrafana ini pattern: {}".format(pattern))
section = None section = None
if pattern: try:
section = filter(pattern.match, self.icingaweb2_ini.sections())
if DEBUG: if DEBUG:
print("\nGrafana section: {}".format(section)) print("\nGrafana ini sections: {}".format(self.icingaweb2_ini.sections()))
if len(section) == 1: print("\nGrafana ini pattern: {}".format(pattern))
section = section[0] if pattern:
section = filter(pattern.match, self.icingaweb2_ini.sections())
if DEBUG:
print("\nGrafana section: {}".format(section))
if len(section) == 1:
section = section[0]
except Exception as e:
print("Error reading grafana ini file ({}) with error {}".format(GRAFANAICINGAWEB2INI, e))
section = None
if DEBUG: if DEBUG:
print("\nGrafana section: {}".format(section)) print("\nGrafana section: {}".format(section))
...@@ -379,7 +393,7 @@ if netbox.ip: ...@@ -379,7 +393,7 @@ if netbox.ip:
email_html += netbox.addNetboxVal('Host', netbox.ip, 'virtual_machine', 'name') email_html += netbox.addNetboxVal('Host', netbox.ip, 'virtual_machine', 'name')
email_html += netbox.addNetboxVal('Host', netbox.ip, 'device', 'name') email_html += netbox.addNetboxVal('Host', netbox.ip, 'device', 'name')
if performance_data or grafana.panelID: if performance_data or grafana.png:
email_html += '\n</table><br>' email_html += '\n</table><br>'
email_html += '\n<table width=' + WIDTH + '>' email_html += '\n<table width=' + WIDTH + '>'
email_html += '\n<tr><th colspan=6 class=perfdata>Performance Data</th></tr>' email_html += '\n<tr><th colspan=6 class=perfdata>Performance Data</th></tr>'
...@@ -400,8 +414,8 @@ if performance_data or grafana.panelID: ...@@ -400,8 +414,8 @@ if performance_data or grafana.panelID:
else: else:
email_html += '\n<tr><th width=' + COLUMN + ' colspan=1>Last Value:</th><td width=' + DIFFERENCE + ' colspan=5>none</td></tr>' email_html += '\n<tr><th width=' + COLUMN + ' colspan=1>Last Value:</th><td width=' + DIFFERENCE + ' colspan=5>none</td></tr>'
if grafana.page_url: if grafana.png:
email_html += '\n<tr><td colspan=6><a href="' + grafana.page_url + '"><img src="cid:grafana2_perfdata" width=' + WIDTH + ' height=' + HEIGHT + '></a></td></tr>' email_html += '\n<tr><td colspan=6><a href="' + grafana.page_url + '"><img src="cid:grafana2_perfdata" width=' + WIDTH + ' height=' + HEIGHT + '></a></td></tr>'
email_html += '\n</table><br>' email_html += '\n</table><br>'
email_html += '\n<table width=' + WIDTH + '>' email_html += '\n<table width=' + WIDTH + '>'
...@@ -444,9 +458,14 @@ if os.path.exists(ICINGA2LOGOPATH): ...@@ -444,9 +458,14 @@ if os.path.exists(ICINGA2LOGOPATH):
msgRoot.attach(msgImage) msgRoot.attach(msgImage)
if grafana.png: if grafana.png:
msgImage = MIMEImage(grafana.png.content) try:
msgImage.add_header('Content-ID', '<grafana2_perfdata>') msgImage = MIMEImage(grafana.png.content)
msgRoot.attach(msgImage) msgImage.add_header('Content-ID', '<grafana2_perfdata>')
msgRoot.attach(msgImage)
except Exception as e:
print("Grafana PNG response exists but was unable to attach the content, failed with error {}".format(e))
print(grafana.png)
# Send mail using SMTP # Send mail using SMTP
smtp = smtplib.SMTP() smtp = smtplib.SMTP()
......
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