Python--nagios Monitoring Nginx Log Request_time

Source: Internet
Author: User

problem encountered : Nginx logs contain very important information. Like Upstream_response_time and Request_time. The current need to monitor the value of changes.

Fix the problem : Write a Python script to complete monitoring with the Nagios Check_nrpe plugin.


Prerequisite Understanding : Nginx Log format:

Log_format main ' $remote _addr | $time _local| $request | $status | $body _byte

s_sent | $http _referer | $http _user_agent | $upstream _response_time | $request _t

IME | $upstream _addr ';


Log Example:

10.113.205.117 |13/sep/2016:21:24:07 +0800| Post/test/test http/1.0 | 200 | 223 | - | mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; SV1;. NET CLR 1.1.4322;. NET CLR 2.0.50727) | 0.034 | 0.042 | 10.156.168.193:8001


By observing the Nginx log example, there is the idea of a monitoring script. The last output line of the monitoring log, the penultimate column and the penultimate column are carried out to compare the value of the size. A value greater than 10s indicates that the site responds slowly and should be alerted in a timely manner. Nagios has a feedback on the status code returned by the script execution. Return 0,nagios display OK, return 2,nagios display critial, and trigger alarm.


Nagios Client operations:

Nagios Client Add script, Python script is as follows:


#!/usr/bin/python

#-*-Coding:utf-8-*-


Import subprocess

Import OS

Import Sys


API = ("Tail-n 1/mnt/log/nginx/test.test.cn.log")


Def testtime ():

Child= subprocess. Popen (api,shell=true,stdout=subprocess. PIPE)

line = Child.stdout.readline ()

Upstream_response_time = float (line.split ("|") [ -3].strip ())

Request_time = float (line.split ("|") [ -2].strip ())

line = Line.replace (' | ', ')


If Upstream_response_time < ten or Request_time < 10:

print "ok-api.test.cn Request time OK"

Sys.exit (0)

Else

Print "critial-api.test.cn%s%s"% (Upstream_response_time, request_time), "Link is:", line,

Sys.exit (2)


if __name__ = = ' __main__ ':

Testtime ()


Add the script to the plug-in directory and give execute permissions.

[Email protected] mcsitesconf]# ll/usr/local/nagios/libexec/check_requestime_test.py

-rwxr-xr-x 1 root root 716 Sep 14:03 /usr/local/nagios/libexec/check_requestime_test.py

[Email protected] mcsitesconf]#


Nrpe configuration file Add monitoring command:

command[check_requestime_test]=/usr/local/nagios/libexec/check_requestime_test.py


Add complete, restart the client Nrpe

PS aux | grep Nrpe

Kill Process ID

Start as follows:

/usr/local/nagios/bin/nrpe-c/usr/local/nagios/etc/nrpe.cfg-d


At this point, the Nagios client adds complete. Can verify success on server


Nagios Service-side operation:

[Email protected] ~]#/usr/lib64/nagios/plugins/check_nrpe-h 10.134.6.129-c check_requestime_test

ok-api.test.cn Request time OK


Verify success, start adding monitoring service files

/etc/nagios/conf.d/services/nginx101.cfg Add the following content

Define Service{

Hostgroup_name Nginx.linux

Service_description check_requestime_test Status

Use System-level-service

Check_command Check_nrpe!check_requestime_test

}


Check that the Nagios configuration file is correct and restart Nagios

Service Nagios Checkconfig

Service Nagios Reload


650) this.width=650; "Src=" Http://s1.51cto.com/wyfs02/M00/87/38/wKioL1fYAvPz1eJxAAC8JrhgeXo138.png-wh_500x0-wm_3 -wmp_4-s_1819898761.png "title=" 1.png "alt=" Wkiol1fyavpz1ejxaac8jrhgexo138.png-wh_50 "/>


Note: The above involves some self-considered private links and IP slightly modified, code and monitoring methods are correct, for reference.

This article is from the "Meng Sir accumulation" blog, please be sure to keep this source http://mengsir.blog.51cto.com/2992083/1852450

Python--nagios Monitoring Nginx Log Request_time

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.