Nagios: Difference between revisions

Content added Content deleted
Line 237: Line 237:
sudo nano /usr/local/bin/nagios_slack_service_alert.py
sudo nano /usr/local/bin/nagios_slack_service_alert.py


<syntaxhighlight lang="python">
#!/usr/bin/env python3
from slackclient import SlackClient
import argparse

# /usr/local/bin/nagios_slack_alert.py -nt "$NOTIFICATIONTYPE$" -ha "$HOSTALIAS$" -hs $HOSTSTATE$ -had $HOSTADDRESS$ -ho $HOSTOUTPUT$
# -nt "Down" -hs "DOWN" -ha "My-Host-Alias" -ho "sometest" -had "1.2.3.4"

token = 'xoxp-3533956854-309159035575-359198033110-95d5978ecdaa34529057e31166a42822'
sc = SlackClient(token)

parser = argparse.ArgumentParser()
parser.add_argument("-nt", "--notificationtype")
parser.add_argument("-ha", "--hostalias")
parser.add_argument("-hs", "--hoststate")
parser.add_argument("-had", "--hostaddress")
parser.add_argument("-ho", "--hostoutput")

args = parser.parse_args()

notificationtype = str(args.notificationtype)
hostalias = str(args.hostalias)
hoststate = str(args.hoststate)
hostaddress = str(args.hostaddress)
hostoutput = str(args.hostoutput)

colors = 0
if "DOWN" in hoststate:
colors = 'danger'
elif 'UP' in hoststate:
colors = 'good'
else:
colors = '#ffee00'

attachments = []
attachments.append({
'title': 'Notification Type: ' + notificationtype,
'text': 'Host Alias: ' + hostalias + '\n' + 'IP:' + hostaddress + '\n' + 'Host State:' + hoststate,
'color': colors,
'footer': 'Host Output:' + hostoutput
#'ts': longdatetime
})

formatted_result = ({
'title': 'Execution Results',
'attachments': attachments,
'as_user': 'false'
})

response = sc.api_call('chat.postMessage', channel="@aman",
text='Nagios Alert', **formatted_result, username='My Bot',
icon_emoji=':brief_case:')

if not response['ok']:
print('Slack Error: {}'.format(response['error']))
</syntaxhighlight>


sudo nano /usr/local/bin/nagios_slack_host_alert.py
sudo nano /usr/local/bin/nagios_slack_host_alert.py


<syntaxhighlight lang="python">
#!/usr/bin/env python3
from slackclient import SlackClient
import argparse

# /usr/local/bin/nagios_slack_alert.py -nt "$NOTIFICATIONTYPE$" -ha "$HOSTALIAS$" -sd "$SERVICEDESC$" -ss "$SERVICESTATE$" -had $HOSTADDRESS$ -so $SERVICEOUTPUT$
# -nt "Down" -sd "SNMP SRV" -ss "Srv DOwn" -ha "My-Host-Alias" -so "sometest" -had "1.2.3.4"

token = 'xoxp-3533956854-309159035575-359198033110-95d5978ecdaa34529057e31166a42822'
sc = SlackClient(token)

parser = argparse.ArgumentParser()
parser.add_argument("-nt", "--notificationtype")
parser.add_argument("-ha", "--hostalias")
parser.add_argument("-sd", "--servicedesc")
parser.add_argument("-ss", "--servicestate")
parser.add_argument("-had", "--hostaddress")
#parser.add_argument("-so", "--serviceoutput")

args = parser.parse_args()

notificationtype = str(args.notificationtype)
hostalias = str(args.hostalias)
servicedesc = str(args.servicedesc)
servicestate = str(args.servicestate)
hostaddress = str(args.hostaddress)
#serviceoutput = str(args.serviceoutput)

colors = 0
if "CRITICAL" in servicestate:
colors = 'danger'
elif 'OK' in servicestate:
colors = 'good'
else:
colors = '#ffee00'

attachments = []
attachments.append({
'title': 'Notification Type: ' + notificationtype,
'text': 'Host Alias: ' + hostalias + '\n' +'IP:' + hostaddress + '\n' + 'Service State:' + servicestate+ '\n' + 'ServiceDesc: ' + servicedesc,
'color': colors,
'footer': 'Service Output:' + "serviceoutput"
#'ts': longdatetime
})

formatted_result = ({
'title': 'Execution Results',
'attachments': attachments,
'as_user': 'false'
})

response = sc.api_call('chat.postMessage', channel="@aman",
text='Nagios Alert', **formatted_result, username='My Bot',
icon_emoji=':brief_case:')

if not response['ok']:
print('Slack Error: {}'.format(response['error']))
</syntaxhighlight>


sudo chown nagios:nagios /usr/local/bin/nagios_slack_service_alert.py
sudo chown nagios:nagios /usr/local/bin/nagios_slack_service_alert.py