Commit d7fd713f authored by root's avatar root
Browse files

Stable host script

parent c4228b3a
......@@ -69,7 +69,7 @@ notification_comment = os.getenv('NOTIFICATIONCOMMENT', 'none')
host_display_name = os.getenv('HOSTDISPLAYNAME', 'none')
user_email = os.getenv('USEREMAIL', 'none')
host_performance_data = os.getenv('HOSTPERFDATA') # needs to be null to avoid it
netbox_host_name = os.getenv('netbox.hostNAME', host_alias)
netbox_host_name = os.getenv('NETBOXHOSTNAME', host_alias)
if netbox_host_name:
# If the env is set but empty
netbox_host_name = host_alias
......@@ -88,7 +88,7 @@ EMAILMESSAGE = notification_type + ' - ' + host_display_name + ' is ' + host_sta
# With debug on each run produces a template that can be rerun for testing
if DEBUG:
with open("/tmp/mail-enhanced.sh", "w+") as f:
with open("/tmp/enhanced-mail.sh", "w+") as f:
f.write("#!/bin/bash")
f.write("\n")
f.write('\nexport NOTIFICATIONTYPE="' + notification_type + '"')
......@@ -102,7 +102,7 @@ if DEBUG:
f.write('\nexport HOSTDISPLAYNAME="' + host_display_name + '"')
f.write('\nexport USEREMAIL="' + user_email + '"')
f.write('\nexport HOSTPERFDATA="' + host_performance_data + '"')
f.write('\nexport netbox.hostNAME="' + netbox_host_name + '"')
f.write('\nexport NETBOXHOSTNAME="' + netbox_host_name + '"')
f.write('\nexport NETBOXPANELID="' + grafana_panel_id + '"')
f.write("\n")
f.write("\n/etc/icinga2/scripts/mail-host-enhanced-notification.py")
......@@ -113,10 +113,10 @@ if DEBUG:
class Netbox:
def __init__(self):
self.host: dict = {}
self.host_url: str = ''
self.ip: dict = {}
self.ip_url: str = ''
self.host = {}
self.host_url = ''
self.ip = {}
self.ip_url = ''
self.parseNetbox()
......@@ -151,11 +151,15 @@ class Netbox:
self.ip_url = "{}/{}/".format(NETBOXBASE + NETBOXPATHIPS, nb_ip['id'])
def getNetboxData(self, url):
headers = { 'Accept': 'application/json' }
headers = { 'Accept': 'application/json' }
if NETBOXTOKEN:
headers.update({ 'Authorization': 'Token ' + NETBOXTOKEN })
response = requests.get(url, headers=headers)
return response.json
if DEBUG:
print("Netbox response: ")
print(response)
print(response.json())
return response.json()
def searchNetboxData(self, url):
result = self.getNetboxData(url)
......@@ -204,10 +208,14 @@ class Grafana:
self.png = self.__getPNG()
def __getPNG(self):
headers = {'Authorization': 'Bearer ' + GRAFANAAPIKEY}
if DEBUG:
print("\n PNG url: " + self.png_url)
headers = {'Authorization', ':Bearer ' + GRAFANAAPIKEY}
return requests.get(self.png_url, headers=headers)
print("\n PNG headers: {}".format(headers))
response = requests.get(self.png_url, headers=headers)
if DEBUG:
print(response.status_code)
return response
netbox = Netbox()
......@@ -368,7 +376,7 @@ if os.path.exists(ICINGA2LOGOPATH):
msgRoot.attach(msgImage)
if grafana.png:
msgImage = MIMEImage(grafana.png)
msgImage = MIMEImage(grafana.png.content)
msgImage.add_header('Content-ID', '<grafana2_perfdata>')
msgRoot.attach(msgImage)
......
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