Commit 101f4364 authored by Matthew Smith's avatar Matthew Smith
Browse files

convert concatinated strings to formated strings

add sanity checks if url is missing
parent db3d37c3
......@@ -95,14 +95,6 @@ if grafana_host_panel_id == '':
if not host_address:
host_address = host_alias
# Email subject
if host_state:
email_subject = 'Host ' + notification_type + ' - ' + host_display_name + ' is ' + host_state
elif 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
if DEBUG:
with open("/tmp/enhanced-mail.sh", "w+") as f:
......@@ -141,16 +133,13 @@ class Netbox:
self.ip = {}
self.ip_url = ''
self.parseNetbox()
if NETBOXBASE:
self.parseNetbox()
def parseNetbox(self):
nb_device = {}
nb_vm = {}
nb_ip = {}
if NETBOXBASE:
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)
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)
if DEBUG:
print(json.dumps(nb_device, indent=4, sort_keys=True))
......@@ -241,7 +230,7 @@ class Grafana:
if host_state:
self.panelID = grafana_host_panel_id
if self.panelID:
if self.panelID and GRAFANABASE:
self.png_url = GRAFANABASE + '/render/dashboard-solo/db/' + GRAFANADASHBOARD + '?panelId=' + self.panelID + '&' + GRAFANAVARHOST + '=' + netbox_host_name + '&theme=' + GRAFANATHEME + '&width=' + WIDTH + '&height=' + HEIGHT
self.page_url = GRAFANABASE + '/dashboard/db/' + GRAFANADASHBOARD + '?fullscreen&panelId=' + self.panelID + '&' + GRAFANAVARHOST + '=' + netbox_host_name
self.png = self.__getPNG()
......@@ -305,34 +294,42 @@ class Grafana:
panel_id = self.icingaweb2_ini.get(section, 'panelId').replace('"', '')
return panel_id
# initalise objects for 3rd party info
netbox = Netbox()
grafana = Grafana()
# Email subject
if host_state:
email_subject = 'Host {0} - {1} is {2}'.format(notification_type, host_display_name, host_state)
elif service_state:
email_subject = 'Service {0} - {1} service {2} is {3}'.format(notification_type, host_display_name, service_display_name, service_state)
else:
email_subject = 'Unknown {0} - {1} service {2} (no host or service state)'.format(notification_type, host_display_name, service_display_name)
# Prepare mail body
email_plain_text = '***** Icinga *****'
email_plain_text += '\n'
email_plain_text += '\nNotification Type: ' + notification_type
email_plain_text += '\nNotification Type: {0}'.format(notification_type)
email_plain_text += '\n'
email_plain_text += '\nHost: ' + host_alias
email_plain_text += '\nAddress: ' + host_address
email_plain_text += '\nService: ' + service_display_name
email_plain_text += '\nState: {}{}'.format(host_state, service_state)
email_plain_text += '\nHost: {0}'.format(host_alias)
email_plain_text += '\nAddress: {0}'.format(host_address)
email_plain_text += '\nService: {0}'.format(service_display_name)
email_plain_text += '\nState: {0}{1}'.format(host_state, service_state)
email_plain_text += '\n'
email_plain_text += '\nDate/Time: ' + long_date_time
email_plain_text += '\nDate/Time: {0}'.format(long_date_time)
email_plain_text += '\n'
email_plain_text += '\nAdditional Info: {}{}'.format(host_output, service_output)
email_plain_text += '\nAdditional Info: {0}{1}'.format(host_output, service_output)
email_plain_text += '\n'
email_plain_text += '\nComment: [' + notification_author + '] ' + notification_comment
email_plain_text += '\nComment: [{0}] {1}'.format(notification_author, notification_comment)
if grafana.page_url:
email_plain_text += '\n'
email_plain_text += '\nGrafana: ' + grafana.page_url
email_plain_text += '\nGrafana: {0}'.format(grafana.page_url)
if netbox.host_url:
email_plain_text += '\n'
email_plain_text += '\nNetbox Host: ' + netbox.host_url
email_plain_text += '\nNetbox Host: {0}'.format(netbox.host_url)
if netbox.ip_url:
email_plain_text += '\n'
email_plain_text += '\nNetbox IP: ' + netbox.ip_url
email_plain_text += '\nNetbox IP: {0}'.format(netbox.ip_url)
email_plain_text += '\n'
email_html = '<html><head><style type="text/css">'
......@@ -356,10 +353,6 @@ email_html += '\n<table width=' + WIDTH + '>'
if os.path.exists(ICINGA2LOGOPATH):
email_html += '\n<tr><th colspan=2 class=icinga width=' + WIDTH + '><img src="cid:icinga2_logo"></th></tr>'
if DEBUG:
email_html += '\n<tr><th width={}>Panel:</th><td>{}</td></tr>'.format(COLUMN, grafana.panelID)
email_html += '\n<tr><th width={}>Notification Type:</th><td class={} {}>{}</td></tr>'.format(COLUMN, host_state, service_state, notification_type)
email_html += '\n<tr><th>Hostalias:</th><td><a style="color: #0095bf; text-decoration: none;" href="' + ICINGA2BASE + '/monitoring/host/show?host=' + host_alias + '">' + host_alias + '</a></td></tr>'
email_html += '\n<tr><th>IP Address:</th><td>' + host_address + '</td></tr>'
email_html += '\n<tr><th>Status:</th><td>' + host_state + service_state + '</td></tr>'
......@@ -371,7 +364,7 @@ if service_state:
email_html += '\n<tr><th>Event Time:</th><td>' + long_date_time + '</td></tr>'
if notification_author and notification_comment:
email_html += '\n<tr><th>Comment:</th><td>' + notification_comment + ' (' + notification_author + ')</td></tr>'
email_html += '\n<tr><th>Comment:</th><td>{0} ({1})</td></tr>'.format(notification_comment, notification_author)
if netbox.host:
email_html += '\n</table><br>'
......
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