Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
I
icinga2-enhanced-mail-notification
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Container registry
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Sol1 Icinga
icinga2-enhanced-mail-notification
Commits
4db24357
Commit
4db24357
authored
2 years ago
by
David Kempe
Browse files
Options
Downloads
Patches
Plain Diff
updated for python3 and some changes
parent
6e746119
Branches
master
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
enhanced-mail-notification.py
+37
-28
37 additions, 28 deletions
enhanced-mail-notification.py
with
37 additions
and
28 deletions
enhanced-mail-notification.py
+
37
−
28
View file @
4db24357
#!/usr/bin/python
#!/usr/bin/python
3
# ------------
# Host notification script for Icinga2
# v.20160504 by mmarodin
...
...
@@ -10,11 +10,15 @@ import smtplib
import
socket
import
requests
import
json
import
C
onfig
P
arser
import
c
onfig
p
arser
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
(
"
\n
Grafana ini file: {}
"
.
format
(
GRAFANAICINGAWEB2INI
))
try
:
self
.
__icingaweb2_ini
=
C
onfig
P
arser
.
C
onfig
P
arser
()
self
.
__icingaweb2_ini
=
c
onfig
p
arser
.
c
onfig
p
arser
()
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
)
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment