Commit 520ec360 authored by root's avatar root
Browse files

Inital commit

parents
object NotificationCommand "mail-host-notification" {
import "plugin-notification-command"
command = [ SysconfDir + "/icinga2/scripts/mail-host-perfdata-notification.py" ]
env = {
NOTIFICATIONTYPE = "$notification.type$"
HOSTALIAS = "$host.display_name$"
HOSTADDRESS = "$address$"
HOSTSTATE = "$host.state$"
LONGDATETIME = "$icinga.long_date_time$"
HOSTOUTPUT = "$host.output$"
NOTIFICATIONAUTHORNAME = "$notification.author$"
NOTIFICATIONCOMMENT = "$notification.comment$"
HOSTDISPLAYNAME = "$host.display_name$"
USEREMAIL = "$user.email$"
// Custom ENV variables
HOSTPERFDATA = "$host.perfdata$"
HOSTURL = "$host.vars.host_url$"
PANELURL = "$host.vars.panel_url$"
}
}
object NotificationCommand "mail-service-notification" {
import "plugin-notification-command"
command = [ SysconfDir + "/icinga2/scripts/mail-service-perfdata-notification.py" ]
env = {
NOTIFICATIONTYPE = "$notification.type$"
SERVICEDESC = "$service.name$"
HOSTALIAS = "$host.display_name$"
HOSTADDRESS = "$address$"
SERVICESTATE = "$service.state$"
LONGDATETIME = "$icinga.long_date_time$"
SERVICEOUTPUT = "$service.output$"
NOTIFICATIONAUTHORNAME = "$notification.author$"
NOTIFICATIONCOMMENT = "$notification.comment$"
HOSTDISPLAYNAME = "$host.display_name$"
SERVICEDISPLAYNAME = "$service.display_name$"
USEREMAIL = "$user.email$"
// Custom ENV variables
SERVICEPERFDATA = "$service.perfdata$"
HOSTURL = "$host.vars.host_url$"
SERVICEURL = "$service.vars.service_url$"
PANELURL = "$service.vars.panel_url$"
}
}
#!/usr/bin/python
#------------
# Host notification script for Icinga2
# v.20160504 by mmarodin
#
# https://github.com/mmarodin/icinga2-plugins
#
import os
import smtplib
import socket
import urllib2
import json
from email.MIMEMultipart import MIMEMultipart
from email.MIMEText import MIMEText
from email.MIMEImage import MIMEImage
###############
# Static Vars #
###############
# User customization here
# Email
EMAILFROM = 'account@domain.name'
EMAILSERVER = 'localhost'
EMAILUSERNAME = ''
EMAILPASSWORD = ''
# Icinga
# INFO: no trailing / on the url, it can generate errors
ICINGA2BASE = 'http://icinga.domain.name/icingaweb2'
# Netbox
# INFO: leave netbox base empty if you don't use netbox and it won't be included
# INFO: no trailing / on the url, it can generate errors
NETBOXBASE = 'http://netbox.domain.name'
NETBOXTOKEN = 'abcdefghijklmnopqrstuvwxyz'
NETBOXPATHDEVICES = '/dcim/devices'
NETBOXPATHVMS = '/virtualization/virtual-machines'
NETBOXPATHIPS = '/ipam/ip-addresses'
# Grafana
# INFO: no trailing / on the url, it can generate errors
GRAFANABASE = 'http://grafana.domain.name'
GRAFANADASHBOARD = 'icinga2-with-influxdb'
# This is the key that grafana sets to search the host name value eg: $hostname translates to var-hostname in a url
GRAFANAVARHOST = 'var-hostname'
GRAFANATHEME = 'light'
GRAFANAAPIKEY = '0123456789987654321001234567899876543210'
# Misc
WIDTH = '640'
HEIGHT = '321'
COLUMN = '144'
DIFFERENCE = str(int(WIDTH) - int(COLUMN))
DEBUG = os.getenv('DEBUG')
#DEBUG = True
#################
# External Vars #
#################
NOTIFICATIONTYPE = os.getenv('NOTIFICATIONTYPE', 'none')
HOSTALIAS = os.getenv('HOSTALIAS', 'none')
HOSTADDRESS = os.getenv('HOSTADDRESS', HOSTALIAS)
HOSTSTATE = os.getenv('HOSTSTATE', 'none')
LONGDATETIME = os.getenv('LONGDATETIME', 'none')
HOSTOUTPUT = os.getenv('HOSTOUTPUT', 'none')
NOTIFICATIONAUTHORNAME = os.getenv('NOTIFICATIONAUTHORNAME', 'none')
NOTIFICATIONCOMMENT = os.getenv('NOTIFICATIONCOMMENT', 'none')
HOSTDISPLAYNAME = os.getenv('HOSTDISPLAYNAME', 'none')
USEREMAIL = os.getenv('USEREMAIL', 'none')
HOSTPERFDATA = os.getenv('HOSTPERFDATA') # needs to be null to avoid it
# TODO: Fix this in the icinga2 config so icinga doesn't pass anything in and we can use a sensible default
#HOSTURL = os.getenv('HOSTURL', HOSTALIAS)
HOSTURL = HOSTALIAS
#PANELURL = ('PANELURL', '2') # default initializer is 2 (ping), this is optional
PANELURL = '2' # default initializer is 2 (ping)
if DEBUG:
with open("/tmp/mail-enhanced.sh", "w+") as f:
f.write("#!/bin/bash")
f.write("\n")
f.write('\nexport NOTIFICATIONTYP="' + NOTIFICATIONTYPE + '"')
f.write('\nexport HOSTALIAS="' + HOSTALIAS + '"')
f.write('\nexport HOSTADDRESS="' + HOSTADDRESS + '"')
f.write('\nexport HOSTSTATE="' + HOSTSTATE + '"')
f.write('\nexport LONGDATETIME="' + LONGDATETIME + '"')
f.write('\nexport HOSTOUTPUT="' + HOSTOUTPUT + '"')
f.write('\nexport NOTIFICATIONAUTHORNAME="' + NOTIFICATIONAUTHORNAME + '"')
f.write('\nexport NOTIFICATIONCOMMENT="' + NOTIFICATIONCOMMENT + '"')
f.write('\nexport HOSTDISPLAYNAME="' + HOSTDISPLAYNAME + '"')
f.write('\nexport USEREMAIL="' + USEREMAIL + '"')
f.write('\nexport HOSTPERFDATA="' + HOSTPERFDATA + '"')
f.write('\nexport HOSTURL="' + HOSTURL + '"')
f.write("\n")
f.write("\n/etc/icinga2/scripts/mail-host-enhanced-notification.py")
f.close()
TO = USEREMAIL
#TO = 'root@icinga2.fqdn.here'
# Logo for Icinga2 <= 2.5.x
# logoImagePath = '/usr/share/icingaweb2/public/img/logo_icinga-inv.png'
# Logo for Icinga2 >= 2.6.x
logoImagePath = '/usr/share/icingaweb2/public/img/icinga-logo.png'
if not HOSTADDRESS:
HOSTADDRESS = HOSTALIAS
SUBJECTMESSAGE = NOTIFICATIONTYPE + ' - ' + HOSTDISPLAYNAME + ' is ' + HOSTSTATE
def getNetboxData(url):
request = urllib2.Request(url)
request.add_header('Accept','application/json')
if NETBOXTOKEN:
request.add_header('Authorization','Token ' + NETBOXTOKEN)
responce = urllib2.urlopen(request)
return json.load(responce)
def searchNetboxData(url):
result = getNetboxData(url)
if result['count'] == 1 and result['results']:
return result['results'][0]
else:
return {}
def getNetboxVal(obj, key1, key2 = None):
val = ''
if key1 in obj:
if key2 and key2 in obj[key1]:
val = obj[key1][key2]
else:
val = obj[key1]
return val
def addNetboxVal(title, obj, key1, key2 = None):
val = getNetboxVal(obj, key1, key2)
if val:
val = '\n<tr><th width="' + COLUMN + '">' + title + ':</th><td>' + val + '</td></tr>'
else:
val = ''
return val
def addNetboxLinkVal(title, obj, key1, key2):
val = getNetboxVal(obj, key1, key2)
if val:
val = '\n<tr><th width="' + COLUMN + '">' + title + ':</th><td><a href="' + getNetboxVal(obj, key1, "url") + '">' + val + '</a></td></tr>'
else:
val = ''
return val
if NETBOXBASE:
nb_device = searchNetboxData(NETBOXBASE + '/api' + NETBOXPATHDEVICES + '/?name=' + HOSTALIAS)
nb_vm = searchNetboxData(NETBOXBASE + '/api' + NETBOXPATHVMS + '/?name=' + HOSTALIAS)
nb_ip = searchNetboxData(NETBOXBASE + '/api' + NETBOXPATHIPS + '/?address=' + HOSTADDRESS)
if DEBUG:
print(json.dumps(nb_device, indent=4, sort_keys=True))
print(json.dumps(nb_vm, indent=4, sort_keys=True))
print(json.dumps(nb_ip, indent=4, sort_keys=True))
NETBOXHOST = {}
if nb_device and not nb_vm:
NETBOXHOST = nb_device
NETBOXHOSTURL = "{}/{}/".format(NETBOXBASE + NETBOXPATHDEVICES, nb_device['id'])
elif not nb_device and nb_vm:
NETBOXHOST = nb_vm
NETBOXHOSTURL = "{}/{}/".format(NETBOXBASE + NETBOXPATHVMS, nb_vm['id'])
elif nb_device and nb_vm:
print("Found multiple device's or vm's that match")
else:
print("Found no device's or vm's that match")
NETBOXIP = nb_ip
if nb_ip:
NETBOXIP = nb_ip
NETBOXIPURL = "{}/{}/".format(NETBOXBASE + NETBOXPATHIPS, nb_ip['id'])
if PANELURL:
GRAFANAPNG = GRAFANABASE + '/render/dashboard-solo/db/' + GRAFANADASHBOARD + '?panelId=' + PANELURL + '&' + GRAFANAVARHOST + '=' + HOSTURL + '&theme=' + GRAFANATHEME + '&width=' + WIDTH + '&height=' + HEIGHT
GRAFANALINK = GRAFANABASE + '/dashboard/db/' + GRAFANADASHBOARD + '?fullscreen&panelId=' + PANELURL + '&' + GRAFANAVARHOST + '=' + HOSTURL
# Prepare mail body
TEXT = '***** Icinga *****'
TEXT += '\n'
if DEBUG:
TEXT += '\nPanel: ' + PANELURL
TEXT += '\n'
TEXT += '\nNotification Type: ' + NOTIFICATIONTYPE
TEXT += '\n'
TEXT += '\nHost: ' + HOSTALIAS
TEXT += '\nAddress: ' + HOSTADDRESS
TEXT += '\nState: ' + HOSTSTATE
TEXT += '\n'
TEXT += '\nDate/Time: ' + LONGDATETIME
TEXT += '\n'
TEXT += '\nAdditional Info: ' + HOSTOUTPUT
TEXT += '\n'
TEXT += '\nComment: [' + NOTIFICATIONAUTHORNAME + '] ' + NOTIFICATIONCOMMENT
HTML = '<html><head><style type="text/css">'
HTML += '\nbody {text-align: left; font-family: calibri, sans-serif, verdana; font-size: 10pt; color: #7f7f7f;}'
HTML += '\ntable {margin-left: auto; margin-right: auto;}'
HTML += '\na:link {color: #0095bf; text-decoration: none;}'
HTML += '\na:visited {color: #0095bf; text-decoration: none;}'
HTML += '\na:hover {color: #0095bf; text-decoration: underline;}'
HTML += '\na:active {color: #0095bf; text-decoration: underline;}'
HTML += '\nth {font-family: calibri, sans-serif, verdana; font-size: 10pt; text-align:left; white-space: nowrap; color: #535353;}'
HTML += '\nth.icinga {background-color: #0095bf; color: #ffffff; margin-left: 7px; margin-top: 5px; margin-bottom: 5px;}'
HTML += '\nth.perfdata, th.perfdata a:link, th.perfdata a:visited {background-color: #0095bf; color: #ffffff; margin-left: 7px; margin-top: 5px; margin-bottom: 5px; text-align:center;}'
HTML += '\ntd {font-family: calibri, sans-serif, verdana; font-size: 10pt; text-align:left; color: #7f7f7f;}'
HTML += '\ntd.center {text-align:center; white-space: nowrap;}'
HTML += '\ntd.UP {background-color: #44bb77; color: #ffffff; margin-left: 2px;}'
#HTML += '\ntd.WARNING {background-color: #ffaa44; color: #ffffff; margin-left: 2px;}'
HTML += '\ntd.DOWN {background-color: #ff5566; color: #ffffff; margin-left: 2px;}'
HTML += '\ntd.UNREACHABLE {background-color: #aa44ff; color: #ffffff; margin-left: 2px;}'
HTML += '\n</style></head><body>'
HTML += '\n<table width=' + WIDTH + '>'
if os.path.exists(logoImagePath):
HTML += '\n<tr><th colspan=2 class=icinga width=' + WIDTH +'><img src="cid:icinga2_logo"></th></tr>'
if DEBUG:
HTML += '\n<tr><th width=' + COLUMN + '>Panel:</th><td>' + PANELURL + '</td></tr>'
HTML += '\n<tr><th width=' + COLUMN + '>Notification Type:</th><td class=' + HOSTSTATE + '>' + NOTIFICATIONTYPE + '</td></tr>'
HTML += '\n<tr><th>Hostalias:</th><td><a style="color: #0095bf; text-decoration: none;" href="' + ICINGA2BASE +'/monitoring/host/show?host=' + HOSTALIAS + '">' + HOSTALIAS + '</a></td></tr>'
HTML += '\n<tr><th>IP Address:</th><td>' + HOSTADDRESS + '</td></tr>'
HTML += '\n<tr><th>Host Status:</th><td>' + HOSTSTATE + '</td></tr>'
HTML += '\n<tr><th>Service Name:</th><td>ping4</td></tr>'
HTML += '\n<tr><th>Service Data:</th><td><a style="color: #0095bf; text-decoration: none;" href="' + ICINGA2BASE + '/monitoring/host/services?host=' + HOSTALIAS + '">' + HOSTOUTPUT + '</a></td></tr>'
HTML += '\n<tr><th>Event Time:</th><td>' + LONGDATETIME + '</td></tr>'
if (NOTIFICATIONAUTHORNAME and NOTIFICATIONCOMMENT):
HTML += '\n<tr><th>Comment:</th><td>' + NOTIFICATIONCOMMENT + ' (' + NOTIFICATIONAUTHORNAME + ')</td></tr>'
if NETBOXHOST:
HTML += '\n</table><br>'
HTML += '\n<table width=' + WIDTH + '>'
HTML += '\n<tr><th colspan=2 class=perfdata><a href="' + NETBOXHOSTURL + '">Netbox Info for ' + HOSTALIAS + '</a></th></tr>'
HTML += addNetboxVal('Display Name', NETBOXHOST, 'display_name')
HTML += addNetboxVal('Display Name', NETBOXHOST, 'name')
HTML += addNetboxLinkVal('Cluster', NETBOXHOST, 'cluster', 'name')
HTML += addNetboxLinkVal('Tennant', NETBOXHOST, 'tennant', 'name')
HTML += addNetboxLinkVal('Site', NETBOXHOST, 'site', 'name')
HTML += addNetboxLinkVal('Rack', NETBOXHOST, 'rack', 'name')
HTML += addNetboxVal('Position', NETBOXHOST, 'position')
HTML += addNetboxVal('Primary IP', NETBOXHOST, 'primary_ip')
HTML += addNetboxVal('Primary IPv4', NETBOXHOST, 'primary_ip4')
HTML += addNetboxVal('Primary IPv6', NETBOXHOST, 'primary_ip6')
HTML += addNetboxLinkVal('Device Type', NETBOXHOST, 'device_type', 'model')
HTML += addNetboxVal('Status', NETBOXHOST, 'status', 'label')
if NETBOXIP:
HTML += '\n</table><br>'
HTML += '\n<table width=' + WIDTH + '>'
HTML += '\n<tr><th colspan=2 class=perfdata><a href="' + NETBOXIPURL + '">Netbox Info for ' + HOSTADDRESS + '</a></th></tr>'
HTML += addNetboxVal('Display Name', NETBOXIP, 'address')
HTML += addNetboxVal('Status', NETBOXIP, 'status', 'label')
HTML += addNetboxVal('Host', NETBOXIP, 'virtual_machine', 'name')
HTML += addNetboxVal('Host', NETBOXIP, 'device', 'name')
if (HOSTPERFDATA or PANELURL):
HTML += '\n</table><br>'
HTML += '\n<table width=' + WIDTH + '>'
HTML += '\n<tr><th colspan=6 class=perfdata>Performance Data</th></tr>'
if (HOSTPERFDATA):
HTML += '\n<tr><th>Label</th><th>Last Value</th><th>Warning</th><th>Critical</th><th>Min</th><th>Max</th></tr>'
PERFDATALIST = HOSTPERFDATA.split(" ")
for PERFDATA in PERFDATALIST:
if '=' not in PERFDATA:
continue
(LABEL,DATA) = PERFDATA.split("=")
if (len(DATA.split(";")) is 5):
(VALUE,WARNING,CRITICAL,MIN,MAX) = DATA.split(";")
else:
(VALUE,WARNING,CRITICAL,MIN) = DATA.split(";")
MAX = ''
HTML += '\n<tr><td>' + LABEL + '</td><td>' + VALUE + '</td><td>' + WARNING + '</td><td>' + CRITICAL + '</td><td>' + MIN + '</td><td>' + MAX + '</td></tr>'
else:
HTML += '\n<tr><th width=' + COLUMN + ' colspan=1>Last Value:</th><td width=' + DIFFERENCE + ' colspan=5>none</td></tr>'
if PANELURL:
HTML += '\n<tr><td colspan=6><a href="' + GRAFANALINK + '"><img src="cid:grafana2_perfdata" width=' + WIDTH + ' height=' + HEIGHT + '></a></td></tr>'
HTML += '\n</table><br>'
HTML += '\n<table width=' + WIDTH + '>'
HTML += '\n<tr><td class=center>Generated by Icinga 2 with data from Icinga 2'
if PANELURL:
HTML += ', Grafana'
if NETBOXHOST:
HTML += ', Netbox'
HTML += '</td></tr>'
HTML += '\n</table><br>'
HTML += '\n</body></html>'
if DEBUG:
print HTML
# Prepare email
msgRoot = MIMEMultipart('related')
msgRoot['Subject'] = SUBJECTMESSAGE
msgRoot['From'] = EMAILFROM
msgRoot['To'] = TO
msgRoot.preamble = 'This is a multi-part message in MIME format.'
msgAlternative = MIMEMultipart('alternative')
msgRoot.attach(msgAlternative)
msgText = MIMEText(TEXT)
msgAlternative.attach(msgText)
msgText = MIMEText(HTML, 'html')
msgAlternative.attach(msgText)
# Attach images
if os.path.exists(logoImagePath):
fp = open(logoImagePath, 'rb')
msgImage = MIMEImage(fp.read())
fp.close()
msgImage.add_header('Content-ID', '<icinga2_logo>')
msgRoot.attach(msgImage)
if PANELURL:
if DEBUG:
print("\n PNG url: " + GRAFANAPNG)
GET = urllib2.Request(GRAFANAPNG)
GET.add_header('Authorization','Bearer ' + GRAFANAAPIKEY)
GRAPH = urllib2.urlopen(GET)
PNG = GRAPH.read()
msgImage = MIMEImage(PNG)
msgImage.add_header('Content-ID', '<grafana2_perfdata>')
msgRoot.attach(msgImage)
# Send mail using SMTP
smtp = smtplib.SMTP()
try:
smtp.connect(EMAILSERVER)
except socket.error as e:
print "Unable to connect to SMTP server '" + EMAILSERVER + "': " + e.strerror
os.sys.exit(e.errno)
if (EMAILUSERNAME and EMAILPASSWORD):
smtp.login(EMAILUSERNAME, EMAILPASSWORD)
try:
smtp.sendmail(EMAILFROM, TO, msgRoot.as_string())
smtp.quit()
except Exception as e:
print "Cannot send mail using SMTP: " + e.message
os.sys.exit(e.errno)
os.sys.exit(0)
#!/bin/python
#------------
# Service notification script for Icinga2
# v.20170310 by mmarodin
#
# https://github.com/mmarodin/icinga2-plugins
#
import os
import smtplib
import socket
import urllib2
from email.MIMEMultipart import MIMEMultipart
from email.MIMEText import MIMEText
from email.MIMEImage import MIMEImage
# User customization here
FROM = 'icinga@icinga2.fqdn.here'
SERVER = 'localhost'
USERNAME = ''
PASSWORD = ''
ICINGA2BASE = 'http://icinga2.fqdn.here/icingaweb2'
GRAFANABASE = 'http://grafana.fqdn.here:3000'
GRAFANADASHBOARD = 'yourdashboard'
GRAFANAVARHOST = 'var-HOST'
GRAFANAVARSERVICE = 'var-SERVICE'
GRAFANATHEME = 'light'
GRAFANAAPIKEY = 'yourAPIkey'
WIDTH = '640'
HEIGHT = '321'
COLUMN = '144'
DIFFERENCE = str(int(WIDTH) - int(COLUMN))
NOTIFICATIONTYPE = os.getenv('NOTIFICATIONTYPE', 'none')
SERVICEDESC = os.getenv('SERVICEDESC', 'none')
HOSTALIAS = os.getenv('HOSTALIAS', 'none')
HOSTADDRESS = os.getenv('HOSTADDRESS', HOSTALIAS)
SERVICESTATE = os.getenv('SERVICESTATE', 'none')
LONGDATETIME = os.getenv('LONGDATETIME', 'none')
SERVICEOUTPUT = os.getenv('SERVICEOUTPUT', 'none')
NOTIFICATIONAUTHORNAME = os.getenv('NOTIFICATIONAUTHORNAME', 'none')
NOTIFICATIONCOMMENT = os.getenv('NOTIFICATIONCOMMENT', 'none')
HOSTDISPLAYNAME = os.getenv('HOSTDISPLAYNAME', 'none')
SERVICEDISPLAYNAME = os.getenv('SERVICEDISPLAYNAME', 'none')
USEREMAIL = os.getenv('USEREMAIL', 'none')
SERVICEPERFDATA = os.getenv('SERVICEPERFDATA') # needs to be null to avoid it
HOSTURL = os.getenv('HOSTURL', 'none')
SERVICEURL = os.getenv('SERVICEURL', SERVICEDESC)
PANELURL = os.getenv('PANELURL') # no default initializer, this is optional
DEBUG = os.getenv('DEBUG')
TO = USEREMAIL
#TO = 'root@icinga2.fqdn.here'
# Logo for Icinga2 <= 2.5.x
# logoImagePath = '/usr/share/icingaweb2/public/img/logo_icinga-inv.png'
# Logo for Icinga2 >= 2.6.x
logoImagePath = '/usr/share/icingaweb2/public/img/icinga-logo.png'
if not HOSTADDRESS:
HOSTADDRESS = HOSTALIAS
if not SERVICEURL:
SERVICEURL = SERVICEDESC
SUBJECTMESSAGE = NOTIFICATIONTYPE + ' - ' + HOSTDISPLAYNAME + ' - ' + SERVICEDISPLAYNAME + ' is ' + SERVICESTATE
if PANELURL:
GRAFANAPNG = GRAFANABASE + '/render/dashboard-solo/db/' + GRAFANADASHBOARD + '?fullscreen&panelId=' + PANELURL + '&' + GRAFANAVARHOST + '=' + HOSTURL + '&' + GRAFANAVARSERVICE + '=' + SERVICEURL + '&theme=' + GRAFANATHEME + '&width=' + WIDTH + '&height=' + HEIGHT
GRAFANALINK = GRAFANABASE + '/dashboard/db/' + GRAFANADASHBOARD + '?fullscreen&panelId=' + PANELURL + '&' + GRAFANAVARHOST + '=' + HOSTURL + '&' + GRAFANAVARSERVICE + '=' + SERVICEURL
TEXT = '***** Icinga *****'
TEXT += '\n'
TEXT += '\nNotification Type: ' + NOTIFICATIONTYPE
TEXT += '\n'
TEXT += '\nService: ' + SERVICEDESC
TEXT += '\nHost: ' + HOSTALIAS
TEXT += '\nAddress: ' + HOSTADDRESS
TEXT += '\nState: ' + SERVICESTATE
TEXT += '\n'
TEXT += '\nDate/Time: ' + LONGDATETIME
TEXT += '\n'
TEXT += '\nAdditional Info: ' + SERVICEOUTPUT
TEXT += '\n'
TEXT += '\nComment: [' + NOTIFICATIONAUTHORNAME + '] ' + NOTIFICATIONCOMMENT
HTML = '<html><head><style type="text/css">'
HTML += '\nbody {text-align: left; font-family: calibri, sans-serif, verdana; font-size: 10pt; color: #7f7f7f;}'
HTML += '\ntable {margin-left: auto; margin-right: auto;}'
HTML += '\na:link {color: #0095bf; text-decoration: none;}'
HTML += '\na:visited {color: #0095bf; text-decoration: none;}'
HTML += '\na:hover {color: #0095bf; text-decoration: underline;}'
HTML += '\na:active {color: #0095bf; text-decoration: underline;}'
HTML += '\nth {font-family: calibri, sans-serif, verdana; font-size: 10pt; text-align:left; white-space: nowrap; color: #535353;}'
HTML += '\nth.icinga {background-color: #0095bf; color: #ffffff; margin-left: 7px; margin-top: 5px; margin-bottom: 5px;}'
HTML += '\nth.perfdata {background-color: #0095bf; color: #ffffff; margin-left: 7px; margin-top: 5px; margin-bottom: 5px; text-align:center;}'
HTML += '\ntd {font-family: calibri, sans-serif, verdana; font-size: 10pt; text-align:left; color: #7f7f7f;}'
HTML += '\ntd.center {text-align:center; white-space: nowrap;}'
HTML += '\ntd.OK {background-color: #44bb77; color: #ffffff; margin-left: 2px;}'
HTML += '\ntd.WARNING {background-color: #ffaa44; color: #ffffff; margin-left: 2px;}'
HTML += '\ntd.CRITICAL {background-color: #ff5566; color: #ffffff; margin-left: 2px;}'
HTML += '\ntd.UNKNOWN {background-color: #aa44ff; color: #ffffff; margin-left: 2px;}'
HTML += '\n</style></head><body>'
HTML += '\n<table width=' + WIDTH + '>'
if os.path.exists(logoImagePath):
HTML += '\n<tr><th colspan=2 class=icinga width=' + WIDTH +'><img src="cid:icinga2_logo"></th></tr>'
HTML += '\n<tr><th width=' + COLUMN + '>Notification Type:</th><td class=' + SERVICESTATE + '>' + NOTIFICATIONTYPE + '</td></tr>'
HTML += '\n<tr><th>Service Name:</th><td>' + SERVICEDISPLAYNAME+ '</td></tr>'
HTML += '\n<tr><th>Service Status:</th><td>' + SERVICESTATE + '</td></tr>'
HTML += '\n<tr><th>Service Data:</th><td><a style="color: #0095bf; text-decoration: none;" href="' + ICINGA2BASE + '/monitoring/service/show?host=' + HOSTALIAS + '&service=' + SERVICEDESC + '">' + SERVICEOUTPUT.replace("\n", "<br>") + '</a></td></tr>'
HTML += '\n<tr><th>Hostalias:</th><td><a style="color: #0095bf; text-decoration: none;" href="' + ICINGA2BASE +'/monitoring/host/show?host=' + HOSTALIAS + '">' + HOSTALIAS + '</a></td></tr>'
HTML += '\n<tr><th>IP Address:</th><td>' + HOSTADDRESS + '</td></tr>'
HTML += '\n<tr><th>Event Time:</th><td>' + LONGDATETIME + '</td></tr>'
if (NOTIFICATIONAUTHORNAME and NOTIFICATIONCOMMENT):
HTML += '\n<tr><th>Comment:</th><td>' + NOTIFICATIONCOMMENT + ' (' + NOTIFICATIONAUTHORNAME + ')</td></tr>'
if (SERVICEPERFDATA or PANELURL):
HTML += '\n</table><br>'
HTML += '\n<table width=' + WIDTH + '>'
HTML += '\n<tr><th colspan=6 class=perfdata>Performance Data</th></tr>'
if (SERVICEPERFDATA):
HTML += '\n<tr><th>Label</th><th>Last Value</th><th>Warning</th><th>Critical</th><th>Min</th><th>Max</th></tr>'
PERFDATALIST = SERVICEPERFDATA.split(" ")
for PERFDATA in PERFDATALIST:
if '=' not in PERFDATA:
continue
(LABEL,DATA) = PERFDATA.split("=")
if (len(DATA.split(";")) is 5):
(VALUE,WARNING,CRITICAL,MIN,MAX) = DATA.split(";")
# else:
# (VALUE,WARNING,CRITICAL,MIN) = DATA.split(";")
# MAX = ''
if (len(DATA.split(";")) is 4):
(VALUE,WARNING,CRITICAL,MIN) = DATA.split(";")
MAX = ''
if (len(DATA.split(";")) is 3):
(VALUE,WARNING,CRITICAL) = DATA.split(";")
MAX = ''
MIN = ''
if (len(DATA.split(";")) is 2):
(VALUE,WARNING) = DATA.split(";")
MAX = ''
MIN = ''
CRITICAL = ''
if (len(DATA.split(";")) is 1):
VALUE = DATA
MAX = ''
MIN = ''
CRITICAL = ''
WARNING = ''
HTML += '\n<tr><td>' + LABEL + '</td><td>' + VALUE + '</td><td>' + WARNING + '</td><td>' + CRITICAL + '</td><td>' + MIN + '</td><td>' + MAX + '</td></tr>'
else:
HTML += '\n<tr><th width=' + COLUMN + ' colspan=1>Last Value:</th><td width=' + DIFFERENCE + ' colspan=5>none</td></tr>'
if PANELURL:
HTML += '\n<tr><td colspan=6><a href="' + GRAFANALINK + '"><img src="cid:grafana2_perfdata" width=' + WIDTH + ' height=' + HEIGHT + '></a></td></tr>'
HTML += '\n</table><br>'
HTML += '\n<table width=' + WIDTH + '>'
HTML += '\n<tr><td class=center>Generated by Icinga 2 and Grafana</td></tr>'
HTML += '\n</table><br>'
HTML += '\n</body></html>'
if DEBUG:
print HTML
# Prepare email
msgRoot = MIMEMultipart('related')
msgRoot['Subject'] = SUBJECTMESSAGE
msgRoot['From'] = FROM
msgRoot['To'] = TO
msgRoot.preamble = 'This is a multi-part message in MIME format.'
msgAlternative = MIMEMultipart('alternative')
msgRoot.attach(msgAlternative)
msgText = MIMEText(TEXT)
msgAlternative.attach(msgText)
msgText = MIMEText(HTML, 'html')
msgAlternative.attach(msgText)