Skip to content
Snippets Groups Projects
Commit 4db24357 authored by David Kempe's avatar David Kempe
Browse files

updated for python3 and some changes

parent 6e746119
Branches master
No related tags found
No related merge requests found
#!/usr/bin/python
#!/usr/bin/python3
# ------------
# Host notification script for Icinga2
# v.20160504 by mmarodin
......@@ -10,11 +10,15 @@ import smtplib
import socket
import requests
import json
import ConfigParser
import configparser
import re
from email.MIMEMultipart import MIMEMultipart
from email.MIMEText import MIMEText
from email.MIMEImage import MIMEImage
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.image import MIMEImage
#from email.MIMEMultipart import MIMEMultipart
#from email.MIMEText import MIMEText
#from email.MIMEImage import MIMEImage
###############
# Static Vars #
......@@ -26,26 +30,25 @@ EMAILSERVER = 'localhost'
EMAILUSERNAME = ''
EMAILPASSWORD = ''
## Icinga ##
# Icinga
# INFO: no trailing / on the url, it can generate errors
ICINGA2BASE = 'http://icinga.domain.local/icingaweb2'
ICINGA2LOGOPATH = '/usr/share/icingaweb2/public/img/icinga-logo.png'
## Netbox ##
# 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.local'
NETBOXTOKEN = 'abcdefghijklmnopqrstuvwxyz1234567890'
NETBOXBASE = ''
NETBOXTOKEN = ''
NETBOXPATHDEVICES = '/dcim/devices'
NETBOXPATHVMS = '/virtualization/virtual-machines'
NETBOXPATHIPS = '/ipam/ip-addresses'
## Grafana ##
# Grafana
# INFO: no trailing / on the url, it can generate errors
GRAFANABASE = 'http://grafana.domain.local'
GRAFANADASHBOARD = 'icinga2-with-influxdb'
# The grafana module in icingaweb2 stores panel id for each service in a ini file
GRAFANAICINGAWEB2INI = '/etc/icingaweb2/modules/grafana/graphs.ini'
# This is the key that grafana users to search the host name value
GRAFANAVARHOST = 'var-hostname'
......@@ -208,18 +211,16 @@ class Netbox:
def __getVal(self, obj, key1, key2=None):
val = ''
try:
if key1 in obj:
if key2 and key2 in obj[key1]:
val = obj[key1][key2]
else:
val = obj[key1]
finally:
return val
if key1 in obj:
if key2 and key2 in obj[key1]:
val = obj[key1][key2]
else:
val = obj[key1]
return val
def addRow(self, title, obj, key1, key2=None):
"""Generate html row containing title and value from key(s) in object
Arguments:
:arg title: str : Title for the row
:arg obj : dict : netbox dict that we are parsing
......@@ -262,7 +263,7 @@ class Netbox:
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
......@@ -308,7 +309,7 @@ class Grafana:
if DEBUG:
print("\nGrafana ini file: {}".format(GRAFANAICINGAWEB2INI))
try:
self.__icingaweb2_ini = ConfigParser.ConfigParser()
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))
......@@ -446,9 +447,9 @@ if netbox.host:
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', 'address')
email_html += netbox.addRow('Primary IPv4', netbox.host, 'primary_ip4', 'address')
email_html += netbox.addRow('Primary IPv6', netbox.host, 'primary_ip6', 'address')
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')
......@@ -473,10 +474,17 @@ if (performance_data and '=' in performance_data) or grafana.png:
continue
(label, data) = perf.split("=")
if len(data.split(";")) is 5:
(value, warning, critical, min, max) = data.split(";")
splitdata = data.split(";")
if len(splitdata) is 5:
(value, warning, critical, min, max) = splitdata
elif len(splitdata) is 4:
(value, warning, critical, min) = splitdata
max = ''
else:
(value, warning, critical, min) = data.split(";")
value = data
warning = ''
critical = ''
min = ''
max = ''
email_html += '\n<tr><td>' + label + '</td><td>' + value + '</td><td>' + warning + '</td><td>' + critical + '</td><td>' + min + '</td><td>' + max + '</td></tr>'
else:
......@@ -555,3 +563,4 @@ except Exception as e:
os.sys.exit(e.errno)
os.sys.exit(0)
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment