Nagios: Difference between revisions
Line 244: | Line 244: | ||
import argparse |
import argparse |
||
# /usr/local/bin/nagios_slack_alert.py -nt "$NOTIFICATIONTYPE$" -ha "$HOSTALIAS$" - |
# /usr/local/bin/nagios_slack_alert.py -nt "$NOTIFICATIONTYPE$" -ha "$HOSTALIAS$" -sd "$SERVICEDESC$" -ss "$SERVICESTATE$" -had $HOSTADDRESS$ -so $SERVICEOUTPUT$ |
||
# -nt "Down" - |
# -nt "Down" -sd "SNMP SRV" -ss "Srv DOwn" -ha "My-Host-Alias" -so "sometest" -had "1.2.3.4" |
||
token = 'xoxp- |
token = 'xoxp-353634854-309156544575-359146663110-95d597hfghghtr57e31166a42822' |
||
sc = SlackClient(token) |
sc = SlackClient(token) |
||
Line 253: | Line 253: | ||
parser.add_argument("-nt", "--notificationtype") |
parser.add_argument("-nt", "--notificationtype") |
||
parser.add_argument("-ha", "--hostalias") |
parser.add_argument("-ha", "--hostalias") |
||
parser.add_argument("- |
parser.add_argument("-sd", "--servicedesc") |
||
⚫ | |||
parser.add_argument("-had", "--hostaddress") |
parser.add_argument("-had", "--hostaddress") |
||
parser.add_argument("- |
parser.add_argument("-so", "--serviceoutput") |
||
args = parser.parse_args() |
args = parser.parse_args() |
||
Line 261: | Line 262: | ||
notificationtype = str(args.notificationtype) |
notificationtype = str(args.notificationtype) |
||
hostalias = str(args.hostalias) |
hostalias = str(args.hostalias) |
||
servicedesc = str(args.servicedesc) |
|||
⚫ | |||
hostaddress = str(args.hostaddress) |
hostaddress = str(args.hostaddress) |
||
serviceoutput = str(args.serviceoutput) |
|||
colors = 0 |
colors = 0 |
||
if " |
if "CRITICAL" in servicestate: |
||
colors = 'danger' |
colors = 'danger' |
||
elif ' |
elif 'OK' in servicestate: |
||
colors = 'good' |
colors = 'good' |
||
else: |
else: |
||
Line 275: | Line 277: | ||
attachments = [] |
attachments = [] |
||
attachments.append({ |
attachments.append({ |
||
'title': ' |
'title': 'ServiceDesc: ' + servicedesc, |
||
'text': ' |
'text': 'Service State: ' + servicestate + '\n' + 'Host Alias: ' + hostalias + '\n' +'IP: ' + hostaddress + '\n', |
||
'color': colors, |
'color': colors, |
||
'footer': ' |
'footer': 'Service Output: ' + serviceoutput |
||
#'ts': longdatetime |
#'ts': longdatetime |
||
}) |
}) |
||
Line 289: | Line 291: | ||
response = sc.api_call('chat.postMessage', channel="@aman", |
response = sc.api_call('chat.postMessage', channel="@aman", |
||
text='Nagios Alert', **formatted_result, username='My Bot', |
text='Nagios Service Alert: ' + notificationtype, **formatted_result, username='My Bot', |
||
icon_emoji=':brief_case:') |
icon_emoji=':brief_case:') |
||
Line 305: | Line 307: | ||
import argparse |
import argparse |
||
# /usr/local/bin/nagios_slack_alert.py -nt "$NOTIFICATIONTYPE$" -ha "$HOSTALIAS$" - |
# /usr/local/bin/nagios_slack_alert.py -nt "$NOTIFICATIONTYPE$" -ha "$HOSTALIAS$" -hs $HOSTSTATE$ -had $HOSTADDRESS$ -ho $HOSTOUTPUT$ |
||
# -nt "Down" - |
# -nt "Down" -hs "DOWN" -ha "My-Host-Alias" -ho "sometest" -had "1.2.3.4" |
||
token = 'xoxp- |
token = 'xoxp-353634854-309156544575-359146663110-95d597hfghghtr57e31166a42822' |
||
sc = SlackClient(token) |
sc = SlackClient(token) |
||
Line 314: | Line 316: | ||
parser.add_argument("-nt", "--notificationtype") |
parser.add_argument("-nt", "--notificationtype") |
||
parser.add_argument("-ha", "--hostalias") |
parser.add_argument("-ha", "--hostalias") |
||
parser.add_argument("- |
parser.add_argument("-hs", "--hoststate") |
||
⚫ | |||
parser.add_argument("-had", "--hostaddress") |
parser.add_argument("-had", "--hostaddress") |
||
parser.add_argument("-ho", "--hostoutput") |
|||
args = parser.parse_args() |
args = parser.parse_args() |
||
Line 323: | Line 324: | ||
notificationtype = str(args.notificationtype) |
notificationtype = str(args.notificationtype) |
||
hostalias = str(args.hostalias) |
hostalias = str(args.hostalias) |
||
hoststate = str(args.hoststate) |
|||
⚫ | |||
hostaddress = str(args.hostaddress) |
hostaddress = str(args.hostaddress) |
||
hostoutput = str(args.hostoutput) |
|||
colors = 0 |
colors = 0 |
||
if " |
if "DOWN" in hoststate: |
||
colors = 'danger' |
colors = 'danger' |
||
elif ' |
elif 'UP' in hoststate: |
||
colors = 'good' |
colors = 'good' |
||
else: |
else: |
||
Line 338: | Line 338: | ||
attachments = [] |
attachments = [] |
||
attachments.append({ |
attachments.append({ |
||
'title': ' |
'title': 'Host Alias: ' + hostalias, |
||
'text': 'Host |
'text': 'Host State:' + hoststate + '\n' + 'IP:' + hostaddress, |
||
'color': colors, |
'color': colors, |
||
'footer': ' |
'footer': 'Host Output:' + hostoutput |
||
#'ts': longdatetime |
#'ts': longdatetime |
||
}) |
}) |
||
Line 352: | Line 352: | ||
response = sc.api_call('chat.postMessage', channel="@aman", |
response = sc.api_call('chat.postMessage', channel="@aman", |
||
text='Nagios Alert', **formatted_result, username='My Bot', |
text='Nagios Host Alert: ' + notificationtype, **formatted_result, username='My Bot', |
||
icon_emoji=':brief_case:') |
icon_emoji=':brief_case:') |
||
Revision as of 16:48, 24 November 2018
Versions
[Nagios]------------[NRPE] Ubuntu CentOS Server (to be monitored)
Ubuntu 16.04.5 LTS Nagios Core 3.5.1 CentOS 7.5.1804 (Core) NRPE 3.2.1
Paths
Nagios Server
Path for config files:
ls /etc/nagios3/ nagios.cfg commands.cfg
Path for Devices config files:
ls /etc/nagios3/conf.d/ client01.cfg hostgroups.cfg services.cfg
Path for Monitor Plugins
ls /usr/lib/nagios/plugins/ check_procs check_http check_load check_users
Monitored Client
NRPE Config file:
ls /etc/nagios/ nrpe.cfg
Nagios Plugins:
/usr/lib64/nagios/plugins/ check_procs check_disk check_load check_users check_nrpe
Installation
Update System:
sudo apt update -y sudo apt upgrade -y
Install Dependencies:
sudo apt install php php-cgi libapache2-mod-php php-common php-pear php-mbstring apache2
Install Nagios3:
sudo apt install nagios3 nagios-plugins-basic nagios-nrpe-plugin
Edit Config:
sudo nano /etc/nagios3/nagios.cfg check_external_commands=1
Adding a Host
sudo nano /etc/nagios3/conf.d/aman-client1.cfg
define host { use generic-host ; Inherit default values from a template host_name PROD_aman-client1 ; The name we're giving to this host alias XYZ_aman-client1 ; A longer name associated with the host address 10.10.30.81 ; IP address of the host #hostgroups allhosts ; Host groups this host is associated with }
sudo service nagios3 restart
Hostgroup
This section is under construction. |
Monitoring python process
Checking output using cli
/usr/lib/nagios/plugins/check_procs -c 1: -C python3 -a watchdog.py
Add a Custom Command for monitoring local Python Script:
sudo nano /etc/nagios3/commands.cfg
################# # Check_Python ################# define command{ command_name check_python command_line /usr/lib/nagios/plugins/check_procs -c 1: -C '$ARG1$' -a '$ARG2$' }
Define a Service for monitoring local Python Script:
sudo nano /etc/nagios3/conf.d/aman-client1.cfg
## Check Watchdog Script define service { use generic-service ; Inherit default values from a template host_name localhost service_description Watchdog Script check_command check_python!python3!watchdog.py }
Monitor HTTP Site
Checking output using cli
/usr/lib/nagios/plugins/check_http -H localhost -u /aviwiki/index.php/Main_Page
Adding a Service for the above monitor
sudo nano /etc/nagios3/conf.d/aman-client1.cfg
## Check Mediawiki Site define service { use generic-service ; Inherit default values from a template host_name localhost service_description MediaWiki Server check_command check_http!-H localhost!-u /aviwiki/index.php/Main_Page }
NRPE
Installation on CentOS Client
Installation on Client:
yum install nrpe nagios-plugins yum install nagios-plugins-users nagios-plugins-load nagios-plugins-swap nagios-plugins-disk nagios-plugins-procs
Check Config File:
egrep -v '^#|^$' /etc/nagios/nrpe.cfg
Edit Config File:
sudo nano nrpe.cfg
dont_blame_nrpe=1 # Allow arguments allowed_hosts=127.0.0.1, <ip_of_nagios_server> # Permit nagios server to connect to this client
sudo systemctl restart nrpe
Verify Connection to NRPE from Nagios Server
/usr/lib/nagios/plugins/check_nrpe -H 192.168.1.11 NRPE v2.15
Installation on Ubuntu Client
This section is under construction. |
Configuration
On client to be monitored
Test the monitor command:
/usr/lib64/nagios/plugins/check_procs -c 1: -C 'python3.6' -a 'watchdog.py' -p
Add the custom command:
sudo nano /etc/nagios/nrpe.cfg command[check_watchdog]=/usr/lib64/nagios/plugins/check_procs -c 1: -C 'python3.6' -a 'watchdog.py' sudo systemctl restart nrpe
On Nagios Server
First test the above custom command:
/usr/lib/nagios/plugins/check_nrpe -H 10.10.30.81 -c check_watchdog
Add a Custom Monitor Command:
sudo nano /etc/nagios3/commands.cfg
############################################################################### # NRPE CHECK COMMAND # # Command to use NRPE to check remote host systems ############################################################################### define command{ command_name check_nrpe_args command_line $USER1$/check_nrpe -H $HOSTADDRESS$ -c $ARG1$ }
Create a Service for the host:
sudo nano /etc/nagios3/conf.d/host01.cfg
## Check Watchdog Script define service { use generic-service ; Inherit default values from a template host_name PROD_aman-client1 service_description Watchdog Script check_command check_nrpe_args!check_watchdog
Slack Notifications
Refer: https://github.com/obaarne/Nagios2Slack/archive/master.zip
- Add Nagios Commands
sudo nano /etc/nagios3/commands.cfg
# 'notify-host-by-slack' command definition define command { command_name notify-host-by-slack command_line /usr/local/bin/nagios_slack_host_alert.py -nt "$NOTIFICATIONTYPE$" -ha "$HOSTALIAS$" -hs "$HOSTSTATE$" -had "$HOSTADDRESS$" -ho "$HOSTOUTPUT$" } # 'notify-service-by-slack' command definition define command { command_name notify-service-by-slack command_line /usr/local/bin/nagios_slack_service_alert.py -nt "$NOTIFICATIONTYPE$" -ha "$HOSTALIAS$" -sd "$SERVICEDESC$" -ss "$SERVICESTATE$" -had "$HOSTADDRESS$" }
- Update the Contact to add above commands
sudo nano /etc/nagios3/conf.d/contacts_nagios2.cfg
define contact{ contact_name root alias Root service_notification_period 24x7 host_notification_period 24x7 service_notification_options w,u,c,r,f,s host_notification_options d,u,r,f,s service_notification_commands notify-service-by-slack host_notification_commands notify-host-by-slack email root@localhost }
- Create Service Alert Script
sudo nano /usr/local/bin/nagios_slack_service_alert.py
#!/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-353634854-309156544575-359146663110-95d597hfghghtr57e31166a42822'
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': 'ServiceDesc: ' + servicedesc,
'text': 'Service State: ' + servicestate + '\n' + 'Host Alias: ' + hostalias + '\n' +'IP: ' + hostaddress + '\n',
'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 Service Alert: ' + notificationtype, **formatted_result, username='My Bot',
icon_emoji=':brief_case:')
if not response['ok']:
print('Slack Error: {}'.format(response['error']))
- Create Host Alert Script
sudo nano /usr/local/bin/nagios_slack_host_alert.py
#!/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-353634854-309156544575-359146663110-95d597hfghghtr57e31166a42822'
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': 'Host Alias: ' + hostalias,
'text': 'Host State:' + hoststate + '\n' + 'IP:' + hostaddress,
'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 Host Alert: ' + notificationtype, **formatted_result, username='My Bot',
icon_emoji=':brief_case:')
if not response['ok']:
print('Slack Error: {}'.format(response['error']))
Change Permissions & Ownership
sudo chown nagios:nagios /usr/local/bin/nagios_slack_service_alert.py sudo chown nagios:nagios /usr/local/bin/nagios_slack_host_alert.py sudo chmod +x /usr/local/bin/nagios_slack_service_alert.py sudo chmod +x /usr/local/bin/nagios_slack_host_alert.py
- References
{{#widget:DISQUS
|id=networkm
|uniqid=Nagios
|url=https://aman.awiki.org/wiki/Nagios
}}