Nagios Monitor Nginx
Open Firewall 161 port
252.10760.195.252.110
Iptables-i input-p udp-s 60.195.252.107–dport 161-j ACCEPT
Iptables-i input-p udp-s 60.195.252.110–dport 161-j ACCEPT
2012-03-21 23:07:38| Category: monitoring | reporting | font size subscription
1. Perform operation on the controlled side: put the Check_nginx plugin into the/usr/local/nagios/libexec, and authorize the host and the group to be Nagios (this is critical)
[email protected] libexec]# ll Check_nginx
-rwxr-xr-x 1 nagios nagios 7636 Oct 22:48 Check_nginx
2.vi/usr/local/nagios/etc/nrpe.cfg
Add this line: Command[check_nginx]=/usr/local/nagios/libexec/check_nginx-w 15000-c 20000
3. Restart Nrpe:
Killall-9 Nrpe
/usr/local/nagios/bin/nrpe-c/usr/local/nagios/etc/nrpe.cfg-d
4. The controlled side performs the operation test:
[Email protected] libexec]#/check_nginx-h 192.168.0.100-p 80-p/var/run/-n nginx.pid-s nginx_status-o/tmp/-W 15 000-c 20000
Ok-nginx is running. 1 requests per second, 1 connections per second (1.00 requests per connection) | ' Reqpsec ' =1 ' conpsec ' =1 ' conpreq ' = 1.00]
--------------------------------------------------------------------------------------------------------------- -----------------------------------
Following the Nagios server side operation:
1.vi/usr/local/nagios/etc/objects/service.cfg
Add to:
Define Service{
Use Generic-service
HOST_NAME nagios-server,nagios-client
Service_description Check_nginx
Check_command Check_nrpe!check_nginx
Max_check_attempts 3
Normal_check_interval 3
Retry_check_interval 3
Check_period 24x7
Notification_interval 5
Notification_period 24x7
Notification_options W,u,c,r
Contact_groups Admins
Process_perf_data 1
action_url/nagios/pnp/index.php?host= $HOSTNAME $&srv= $SERVICEDESC $
}
Once added, save to exit and restart Nagios.
/etc/init.d/nagios relaod
/etc/init.d/nagios restart
2. Test:/usr/local/nagios/libexec/check_nrpe-h 192.168.0.100-c Check_nginx
Ok-nginx is running. 1 requests per second, 1 connections per second (1.00 requests per connection) | ' Reqpsec ' =1 ' conpsec ' =1 ' conpreq ' = 1.00]
#---------------------192.168.0.100 is the IP address of my accused side------------------------------------------------------
Here are some areas of attention
1, about Nginx.pid
--in nginx.conf must be added, such as: Pid/opt/nginx.pid
2. If the following prompt appears.
--unknown-local copy/copies of Nginx_status is empty.
--it may be that there is no configuration status monitoring in your nginx.conf, such as
Server
{
Listen 80;
server_name IP | Domain Name
Location/nginx_status {
Stub_status on;
Access_log off;
}
}
3. Need to change check_nginx a place:
Change hostname= "localhost" to: the host IP you need to monitor.
such as: Hostname= "192.168.0.100"
Check_nginx Script
#!/bin/sh
Progname= ' basename
Version= "Version 1.0,"
Author= ", Mike adolphs (http://www.matejunkie.com/)"
St_ok=0
St_wr=1
st_cr=2
St_uk=3
Hostname= "192.168.0.100"
Port=80
Path_pid=/var/run
Name_pid= "Nginx.pid"
Status_page= "Nginx_status"
Output_dir=/tmp
Pid_check=1
Secure=0
Print_version () {
echo "$VERSION $AUTHOR"
}
Print_help () {
Print_version $PROGNAME $VERSION
echo ""
echo "$PROGNAME is a Nagios plugin to check whether Nginx is running."
echo "It also parses the Nginx ' s status page to get requests and"
echo "Connections per second as well as requests per connection. You "
echo "May has to alter your nginx config so, the plugin"
echo "can access the server's status page."
echo "The plugin is highly configurable for this reason. See below for "
echo "available options."
echo ""
echo "$PROGNAME-H localhost-p 80-p/var/run-n nginx.pid"
echo "-s NGINX_STATUT-O/tmp [-W int] [-c int] [-S] [-n]"
echo ""
echo "Options:"
echo "-h/--hostname)"
echo "defines the hostname. Default Is:localhost "
echo "-p/--port)"
echo "defines the port. Default is:80 "
echo "-p/--path-pid)"
echo "Path where Nginx ' s PID file is being stored. You might need "
echo "to alter this path according to your distribution. Default "
echo "is:/var/run"
echo "-n/--name_pid)"
echo "Name of the PID file. Default Is:nginx.pid "
echo "-n/--no-pid-check)"
echo "Turn this on, if you don ' t want to check for a PID file"
echo "Whether Nginx is running, e.g. if you ' re checking a"
echo "Remote server. Default Is:off "
echo "-s/--status-page)"
echo "Name of the server ' s status page defined in the"
echo "directive of your nginx configuration. Default is: "
echo "Nginx_status"
echo "-o/--output-directory)"
echo "specifies where to write the tmp-file that the check creates."
echo "Default is:/tmp"
echo "-s/--secure)"
echo "In case your server was only reachable via SSL, use this"
echo "This switch to use HTTPS instead of HTTP. Default Is:off "
echo "-w/--warning)"
echo "Sets a warning level for requests per second. Default Is:off "
echo "-c/--critical)"
echo "Sets a critical level for requests per second. Default is: "
echo "Off"
Exit $ST _uk
}
While Test-n "$"; Do
Case "$" in
-HELP|-H)
Print_help
Exit $ST _uk
;;
--VERSION|-V)
Print_version $PROGNAME $VERSION
Exit $ST _uk
;;
--HOSTNAME|-H)
Hostname=$2
Shift
;;
--PORT|-P)
Port=$2
Shift
;;
--PATH-PID|-P)
Path_pid=$2
Shift
;;
--NAME-PID|-N)
Name_pid=$2
Shift
;;
--NO-PID-CHECK|-N)
Pid_check=0
;;
--STATUS-PAGE|-S)
Status_page=$2
Shift
;;
--output-directory|-o)
Output_dir=$2
Shift
;;
--SECURE|-S)
Secure=1
;;
--WARNING|-W)
Warning=$2
Shift
;;
--CRITICAL|-C)
Critical=$2
Shift
;;
*)
echo "Unknown argument: $ $"
Print_help
Exit $ST _uk
;;
Esac
Shift
Done
Get_wcdiff () {
if [!-Z "$warning"-A!-Z "$critical"]
Then
Wclvls=1
If [${warning}-gt ${critical}]
Then
Wcdiff=1
Fi
elif [!-Z "$warning"-a-z "$critical"]
Then
wcdiff=2
elif [-Z "$warning"-A!-Z "$critical"]
Then
Wcdiff=3
Fi
}
Val_wcdiff () {
If ["$wcdiff" = 1]
Then
echo "Please adjust your warning/critical thresholds. The warning \
Must be lower than the critical level! "
Exit $ST _uk
elif ["$wcdiff" = 2]
Then
echo "also set a critical value when you want
Warning/critical thresholds! "
Exit $ST _uk
elif ["$wcdiff" = 3]
Then
echo "also set a warning value when you want
Warning/critical thresholds! "
Exit $ST _uk
Fi
}
Check_pid () {
If [-F ' $path _pid/$name _pid "]
Then
Retval=0
Else
Retval=1
Fi
}
Get_status () {
If ["$secure" = 1]
Then
wget--no-check-certificate-q-T 3-t 3 \
Http://${hostname}:${port}/${status_page}-o ${output_dir}/nginx-status.1
Sleep 1
wget--no-check-certificate-q-T 3-t 3 \
Http://${hostname}:${port}/${status_page}-o ${output_dir}/nginx-status.2
Else
Wget-q-T 3-t 3 http://${hostname}:${port}/${status_page} \
-O ${output_dir}/nginx-status.1
Sleep 1
Wget-q-T 3-t 3 http://${hostname}:${port}/${status_page} \
-O ${output_dir}/nginx-status.2
Fi
Stat_output1= ' stat-c%s ${output_dir}/nginx-status.1 '
Stat_output2= ' stat-c%s ${output_dir}/nginx-status.2 '
If ["$stat _output1" = 0-o "$stat _output2" = 0]
Then
echo "Unknown-local copy/copies of $status _page is empty."
Exit $ST _uk
Fi
}
Get_vals () {
Tmp1_reqpsec= ' grep ' ^ ' ${output_dir}/nginx-status.1|awk ' {print $} '
Tmp2_reqpsec= ' grep ' ^ ' ${output_dir}/nginx-status.2|awk ' {print $} '
reqpsec= ' expr $tmp 2_reqpsec-$tmp 1_reqpsec '
Tmp1_conpsec= ' grep ' ^ ' ${output_dir}/nginx-status.1|awk ' {print $} '
Tmp2_conpsec= ' grep ' ^ ' ${output_dir}/nginx-status.2|awk ' {print $} '
conpsec= ' expr $tmp 2_conpsec-$tmp 1_conpsec '
Reqpcon= ' echo ' scale=2; $reqpsec/$conpsec "| Bc-l '
If ["$reqpcon" = ". 99"]
Then
reqpcon= "1.00"
Fi
}
Do_output () {
Output= "Nginx is running. $reqpsec Requests per second, $conpsec \
Connections per second ($reqpcon requests per connection) "
}
Do_perfdata () {
Perfdata= "' reqpsec ' = $reqpsec ' conpsec ' = $conpsec ' conpreq ' = $reqpcon"
}
# Here we go!
Get_wcdiff
Val_wcdiff
if [${pid_check} = 1]
Then
Check_pid
If ["$retval" = 1]
Then
echo "There ' s no PID file for Nginx. is Nginx running? Please \
Also make sure whether your PID path and name are correct. "
Exit $ST _CR
Fi
Fi
Get_status
Get_vals
Do_output
Do_perfdata
If [-N "$warning"-a-n "$critical"]
Then
If ["$reqpsec"-ge "$warning"-A "$reqpsec"-lt "$critical"]
Then
echo "WARNING-${output} | ${perfdata} "
Exit $ST _WR
elif ["$reqpsec"-ge "$critical"]
Then
echo "CRITICAL-${output} | ${perfdata} "
Exit $ST _CR
Else
echo "OK-${output} | ${perfdata}] "
Exit $ST _OK
Fi
Else
echo "OK-${output} | ${perfdata} "
Exit $ST _OK
Fi
Nagios Monitor Nginx