Linux Zabbix monitor memcached php-fpm Tomcat Nginx MySQL site Log

Source: Internet
Author: User
Tags error handling fpm memcached tomcat

Zabbix as a monitoring software is very flexible, supporting the data types are very rich, such as digital (no plus or minus), digital (floating-point), log, text and so on. All we need to do is use the script to get the data, then Zabbix collect and draw, and set the alarm line. Here we learn to use Zabbix to monitor memcached, PHP-FPM, Tomcat, Nginx, MySQL, and web logs.

memcached Monitoring

Custom Key values

Userparameter=memcached.stat[*],/data/sh/memcached-status.sh "$"
memcached-status.sh script content is:
#!/bin/bash

Item=$1
ip=127.0.0.1
port=11211
(echo "stats"; Sleep 0.5) | Telnet $ip $port 2>/dev/null | grep "STAT $item \b" | awk ' {print $} '

Import templates

memcached Zabbix Template Download

PHP-FPM Monitoring

Configure PHP-FPM Status Page

Open the php-fpm.conf configuration file and restart PHP after adding the following configuration:
Pm.status_path =/fpm_status

Custom Key values

Userparameter=php-fpm[*],/data/sh/php-fpm-status.sh "$"
php-fpm-status.sh Script content:
#!/bin/bash
##################################
# Zabbix Monitoring Script
#
# php-fpm:
#-Anything available via FPM status page
#
##################################
# Contact:
# vincent.viallet@gmail.com
##################################
# Changelog:
# 20100922 VV Initial creation
##################################

# Zabbix Requested parameter
Zbx_req_data= "$"

# FPM Defaults
Url= "Http://localhost/fpm_status"
Wget_bin= "/usr/bin/wget"

#
# Error Handling:
#-Need to is displayable in Zabbix (avoid not_supported)
#-Items need to be of type "float" (allow negative + float)
#
error_no_access_file= "-0.9900"
error_no_access= "-0.9901"
error_wrong_param= "-0.9902"
Error_data= " -0.9903" # either can not Connect/bad Host/bad port

# Save the FPM stats in a variable for future parsing
fpm_stats=$ ($WGET _bin-q $URL-O-2>/dev/null)

# Error during retrieve
If [$?-ne 0-o-Z "$FPM _stats"]; Then
Echo $ERROR _data
Exit 1
Fi

#
# Extract data from FPM stats
#
Result=$ (echo "$FPM _stats" | sed-n-R "s/^ $ZBX _req_data: + ([0-9]+)/\1/p")
If [$?-ne 0-o-Z "$RESULT"]; Then
Echo $ERROR _wrong_param
Exit 1
Fi

Echo $RESULT

Exit 0

Import templates

PHP-FPM Zabbix Template Download

Tomcat Monitor

When you first decided to monitor Tomcat, you used JMX, but the set was too complex and required a high firewall requirement to open several ports. It had to be monitored using Tomcat's own status page.

Custom Key values

userparameter=tomcat.status[*],/data/sh/tomcat-status.py $
Because you need to parse to XML, it's still a good decision to use Python for more convenience.
/data/sh/tomcat-status.py Script content:
#!/usr/bin/python
Import Urllib2
Import Xml.dom.minidom
Import Sys

url = ' Http://127.0.0.1:8080/manager/status?XML=true '
Username = ' username '
Password = ' password '

Passman = Urllib2. Httppasswordmgrwithdefaultrealm ()
Passman.add_password (None, URL, username, password)
Authhandler = Urllib2. Httpbasicauthhandler (Passman)
Opener = Urllib2.build_opener (Authhandler)
Urllib2.install_opener (opener)
Pagehandle = Urllib2.urlopen (URL)
XmlData = Pagehandle.read ()
doc = xml.dom.minidom.parseString (xmlData)

item = sys.argv[1]

If item = = "Memory.free":
Print Doc.getelementsbytagname ("Memory") [0].getattribute ("Free")
Elif item = = "Memory.total":
Print Doc.getelementsbytagname ("Memory") [0].getattribute ("Total")
Elif item = = "Memory.max":
Print Doc.getelementsbytagname ("Memory") [0].getattribute ("Max")
Elif item = = "Threadinfo.maxthreads":
Print Doc.getelementsbytagname ("Threadinfo") [0].getattribute ("MaxThreads")
Elif item = = "Threadinfo.currentthreadcount":
Print Doc.getelementsbytagname ("Threadinfo") [0].getattribute ("Currentthreadcount")
Elif item = = "Threadinfo.currentthreadsbusy":
Print Doc.getelementsbytagname ("Threadinfo") [0].getattribute ("Currentthreadsbusy")
Elif item = = "Requestinfo.maxtime":
Print Doc.getelementsbytagname ("Requestinfo") [0].getattribute ("MaxTime")
Elif item = = "Requestinfo.processingtime":
Print Doc.getelementsbytagname ("Requestinfo") [0].getattribute ("Processingtime")
Elif item = = "Requestinfo.requestcount":
Print Doc.getelementsbytagname ("Requestinfo") [0].getattribute ("RequestCount")
Elif item = = "Requestinfo.errorcount":
Print Doc.getelementsbytagname ("Requestinfo") [0].getattribute ("Errorcount")
Elif item = = "Requestinfo.bytesreceived":
Print Doc.getelementsbytagname ("Requestinfo") [0].getattribute ("bytesreceived")
Elif item = = "Requestinfo.bytessent":
Print Doc.getelementsbytagname ("Requestinfo") [0].getattribute ("BytesSent")
Else
Print "Unsupport item."
This script is monitor TOMCAT7, Tomcat6 did not try, should distinguish between the URL of the status page and the user password setting on the admin page. The above script can be run to add users in Tomcat-users.xml, at least for Manager-status.

Import templates

Tomcat Zabbix Template Download

Nginx Monitoring

Configure Nginx Status Page

In the Nginx configuration file server{}, add:
Location/nginx_status {
Stub_status on;
Access_log off;
}

Custom Key values

Userparameter=nginx[*],/data/sh/nginx-status.sh "$"
nginx-status.sh Script content:
#!/bin/bash
##################################
# Zabbix Monitoring Script
#
# Nginx:
#-Anything available via Nginx stub-status module
#
##################################
# Contact:
# vincent.viallet@gmail.com
##################################
# Changelog:
# 20100922 VV Initial creation
##################################

# Zabbix Requested parameter
Zbx_req_data= "$"
Zbx_req_data_url= "$"

# Nginx Defaults
Url= "Http://127.0.0.1/nginx_status"
Wget_bin= "/usr/bin/wget"

#
# Error Handling:
#-Need to is displayable in Zabbix (avoid not_supported)
#-Items need to be of type "float" (allow negative + float)
#
error_no_access_file= "-0.9900"
error_no_access= "-0.9901"
error_wrong_param= "-0.9902"
Error_data= " -0.9903" # either can not Connect/bad Host/bad port

# Save the Nginx stats in a variable for future parsing
nginx_stats=$ ($WGET _bin-q $URL-O-2>/dev/null)

# Error during retrieve
If [$?-ne 0-o-Z "$NGINX _stats"]; Then
Echo $ERROR _data
Exit 1
Fi

#
# Extract data from Nginx stats
#
Case $ZBX _req_data in
Active_connections) echo "$NGINX _stats" | head-1 | cut-f3-d ';;
Accepted_connections) echo "$NGINX _stats" | Grep-ev ' [a-za-z] ' | Cut-f2-d ';;
Handled_connections) echo "$NGINX _stats" | Grep-ev ' [a-za-z] ' | cut-f3-d ';;
handled_requests) echo "$NGINX _stats" | Grep-ev ' [a-za-z] ' | Cut-f4-d ';;
Reading) echo "$NGINX _stats" | Tail-1 | Cut-f2-d ';;
Writing) echo "$NGINX _stats" | Tail-1 | Cut-f4-d ';;
Waiting) echo "$NGINX _stats" | Tail-1 | Cut-f6-d ';;
*) echo $ERROR _wrong_param; Exit 1;;
Esac

Exit 0

Import templates

Nginx Zabbix Template Download

MySQL monitoring

MySQL monitoring, Zabbix is supported by default, there are ready-made templates, ready-made key values, we need to do is just a new. my.cnf file in/var/lib/zabbix, which reads as follows:
[Client]
host=127.0.0.1
port=1036
User=root
Password=root

Web site Log Monitoring

Configure log format

We assume that the Web server you are using is nginx, and we add a log format as follows:
Log_format withhost ' $remote _addr\t$remote_user\t$time_local\t$host\t$request\t '
' $status \t$body_bytes_sent\t$http_referer\t '
' $http _user_agent ';
We use tab as a separator to make it easier for awk to recognize the contents of the column in case of an error.
Then you set up the global log, and the other server does not need to set up the log:
access_log/data/home/logs/nginx/$host. Log withhost;

Get a minute log at timed intervals

To set a timed task:
* * * * */data/sh/get_nginx_access.sh * * *
The script reads:
#!/bin/bash

logdir=/data/home/logs/nginx/
lognames= ' ls ${logdir}/*.*.log |awk-f '/' {print $NF} '

For $logName in $logNames;
Todo
#设置变量
Split_log= "/tmp/split_$logname"
access_log= "${logdir}/$logName"
status_log= "/tmp/$logName"

#取出最近一分钟日志
TAC $access _log | awk '
begin{
Fs= "\ T"
Ofs= "\ T"
Cmd= "date-d\" 1 minute ago\ "+%h%m%s"
Cmd|getline Oneminuteago
}
{
$ = substr ($3,13,8)
Gsub (":", "", $)
if ($3>=oneminuteago) {
Print
} else {
Exit
}
} ' > $split _log


#统计状态码个数
Awk-f ' \ t ' {
Status[$4 "" $6]++
}
end{
For (I in Status)
{
Print I,status[i]
}
}
' $split _log > $status _log
Done
This timed task is performed every minute because the frequency we monitor is per minute. Add this task is to obtain the last minute of each domain name log, as well as the statistics of all the status code number of each domain, convenient Zabbix to obtain the required data.

Custom Key values

userparameter=nginx.detect,/data/sh/nginx-detect.sh
Userparameter=nginx.access[*],awk-v sum=0-v domain=$1-v code=$2 ' {if ($$1 = domain && $$2 = code) {Sum+=$$3}} End{print sum} '/tmp/$1.log
Userparameter=nginx.log[*],awk-f ' \ t '-v domain=$1-v code=$2-v number=$3-v sum=0-v line= ' "' {if ($$4 = = Domain &AMP;&A mp $$6 = = code) {sum++;line=line$$5 "\ n"}}end{if (sum > Number) Print line} '/tmp/split_$1.log | Sort | uniq-c | Sort-nr | head-10 | Sed-e ' s/^/<p>/'-e ' s/$/<\/p>/'
nginx-detect.sh script content is:
#!/bin/bash

function Json_head {
printf "{"
printf "\" data\ ": ["
}

function Json_end {
printf "]"
printf "}"
}

function Check_first_element {
if [[$FIRST _element-ne 1]]; Then
printf ","
Fi
First_element=0
}

First_element=1
Json_head

lognames= ' Ls/data/home/logs/nginx/*.*.log |awk-f '/' ' {print $NF} '
For LogName in $logNames;
Todo
While read domain code COUNT;DO
Check_first_element
printf "{"
printf "\" {#DOMAIN}\ ": \" $domain \ ", \" {#CODE}\ ": \" $code \ "" "
printf "}"
Done </tmp/$logName
Done
Json_end


Here we define three key values, Nginx.detect is to discover all domain names and all their status codes, nginx.access[*] is to count the number of status codes for the specified domain name, nginx.log[*] is to test the output of the top ten URLs when the status code for the specified domain name exceeds the specified value. We monitor the Nginx access log to use the Zabbix automatic discovery function, when we add domain name, do not need to modify the script, Zabbix will help us automatically discover the new domain name and for monitoring.


Related Article

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.