Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
Sol1 Icinga
icinga2-enhanced-mail-notification
Commits
a2ea5e6a
Commit
a2ea5e6a
authored
Dec 13, 2018
by
Matthew Smith
Browse files
improve error handling for netbox
parent
957c68b1
Changes
1
Hide whitespace changes
Inline
Side-by-side
enhanced-mail-notification.py
View file @
a2ea5e6a
...
...
@@ -100,6 +100,8 @@ 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
:
...
...
@@ -224,7 +226,7 @@ class Grafana:
self
.
icingaweb2_ini
=
None
self
.
panelID
=
None
if
GRAFANAICINGAWEB2INI
:
if
os
.
path
.
exists
(
GRAFANAICINGAWEB2INI
)
:
self
.
__parseIcingaweb2INI
()
if
service_state
:
...
...
@@ -241,17 +243,25 @@ class Grafana:
def
__parseIcingaweb2INI
(
self
):
if
DEBUG
:
print
(
"
\n
Grafana ini file: {}"
.
format
(
GRAFANAICINGAWEB2INI
))
self
.
icingaweb2_ini
=
ConfigParser
.
ConfigParser
()
self
.
icingaweb2_ini
.
read
(
GRAFANAICINGAWEB2INI
)
try
:
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
))
self
.
icingaweb2_ini
=
None
def
__getPNG
(
self
):
headers
=
{
'Authorization'
:
'Bearer '
+
GRAFANAAPIKEY
}
if
DEBUG
:
print
(
"
\n
PNG url: "
+
self
.
png_url
)
print
(
"
\n
PNG headers: {}"
.
format
(
headers
))
response
=
requests
.
get
(
self
.
png_url
,
headers
=
headers
)
if
DEBUG
:
print
(
response
.
status_code
)
print
(
"PNG url: "
+
self
.
png_url
)
print
(
"PNG headers: {}"
.
format
(
headers
))
try
:
response
=
requests
.
get
(
self
.
png_url
,
headers
=
headers
)
if
DEBUG
:
print
(
"PNG get status code: {}"
.
format
(
response
.
status_code
))
except
Exception
as
e
:
print
(
"Error getting png from {} with error {}"
.
format
(
self
.
page_url
,
e
))
response
=
None
return
response
def
__searchSections
(
self
,
display_name
,
name
,
command
):
...
...
@@ -263,16 +273,20 @@ class Grafana:
if
command
and
pattern
is
None
:
pattern
=
re
.
compile
(
command
)
if
DEBUG
:
print
(
"
\n
Grafana ini sections: {}"
.
format
(
self
.
icingaweb2_ini
.
sections
()))
print
(
"
\n
Grafana ini pattern: {}"
.
format
(
pattern
))
section
=
None
if
pattern
:
section
=
filter
(
pattern
.
match
,
self
.
icingaweb2_ini
.
sections
())
try
:
if
DEBUG
:
print
(
"
\n
Grafana section: {}"
.
format
(
section
))
if
len
(
section
)
==
1
:
section
=
section
[
0
]
print
(
"
\n
Grafana ini sections: {}"
.
format
(
self
.
icingaweb2_ini
.
sections
()))
print
(
"
\n
Grafana ini pattern: {}"
.
format
(
pattern
))
if
pattern
:
section
=
filter
(
pattern
.
match
,
self
.
icingaweb2_ini
.
sections
())
if
DEBUG
:
print
(
"
\n
Grafana section: {}"
.
format
(
section
))
if
len
(
section
)
==
1
:
section
=
section
[
0
]
except
Exception
as
e
:
print
(
"Error reading grafana ini file ({}) with error {}"
.
format
(
GRAFANAICINGAWEB2INI
,
e
))
section
=
None
if
DEBUG
:
print
(
"
\n
Grafana section: {}"
.
format
(
section
))
...
...
@@ -379,7 +393,7 @@ if netbox.ip:
email_html
+=
netbox
.
addNetboxVal
(
'Host'
,
netbox
.
ip
,
'virtual_machine'
,
'name'
)
email_html
+=
netbox
.
addNetboxVal
(
'Host'
,
netbox
.
ip
,
'device'
,
'name'
)
if
performance_data
or
grafana
.
p
anelID
:
if
performance_data
or
grafana
.
p
ng
:
email_html
+=
'
\n
</table><br>'
email_html
+=
'
\n
<table width='
+
WIDTH
+
'>'
email_html
+=
'
\n
<tr><th colspan=6 class=perfdata>Performance Data</th></tr>'
...
...
@@ -400,8 +414,8 @@ if performance_data or grafana.panelID:
else
:
email_html
+=
'
\n
<tr><th width='
+
COLUMN
+
' colspan=1>Last Value:</th><td width='
+
DIFFERENCE
+
' colspan=5>none</td></tr>'
if
grafana
.
p
age_url
:
email_html
+=
'
\n
<tr><td colspan=6><a href="'
+
grafana
.
page_url
+
'"><img src="cid:grafana2_perfdata" width='
+
WIDTH
+
' height='
+
HEIGHT
+
'></a></td></tr>'
if
grafana
.
p
ng
:
email_html
+=
'
\n
<tr><td colspan=6><a href="'
+
grafana
.
page_url
+
'"><img src="cid:grafana2_perfdata" width='
+
WIDTH
+
' height='
+
HEIGHT
+
'></a></td></tr>'
email_html
+=
'
\n
</table><br>'
email_html
+=
'
\n
<table width='
+
WIDTH
+
'>'
...
...
@@ -444,9 +458,14 @@ if os.path.exists(ICINGA2LOGOPATH):
msgRoot
.
attach
(
msgImage
)
if
grafana
.
png
:
msgImage
=
MIMEImage
(
grafana
.
png
.
content
)
msgImage
.
add_header
(
'Content-ID'
,
'<grafana2_perfdata>'
)
msgRoot
.
attach
(
msgImage
)
try
:
msgImage
=
MIMEImage
(
grafana
.
png
.
content
)
msgImage
.
add_header
(
'Content-ID'
,
'<grafana2_perfdata>'
)
msgRoot
.
attach
(
msgImage
)
except
Exception
as
e
:
print
(
"Grafana PNG response exists but was unable to attach the content, failed with error {}"
.
format
(
e
))
print
(
grafana
.
png
)
# Send mail using SMTP
smtp
=
smtplib
.
SMTP
()
...
...
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment