234cfds1 发表于 2018-12-5 12:55:23

Zabbix监控Memcached PHP-FPM Tomcat Nginx MySQL 网站日志

Zabbix监控Memcached PHP-FPM Tomcat Nginx MySQL 网站日志
  作者:朱 茂海 /分类:监控 /Tag:memcached, mysql, nginx, php-fpm, tomcat, zabbix
  文章目录
  [隐藏]

[*]  Memcached监控

[*]  自定义键值
[*]  导入模板

[*]  PHP-FPM监控

[*]  配置php-fpm状态页
[*]  自定义键值
[*]  导入模板

[*]  Tomcat监控

[*]  自定义键值
[*]  导入模板

[*]  Nginx监控

[*]  配置Nginx状态页
[*]  自定义键值
[*]  导入模板

[*]  MySQL监控
[*]  网站日志监控

[*]  配置日志格式
[*]  定时获取一分钟日志
[*]  自定义键值
[*]  配置探索规则
[*]  配置监控项原型
[*]  配置触发器

  Zabbix作为监控软件非常的灵活,支持的数据类型非常丰富,比如数字(无正负),数字(浮点),日志,文字等。我们需要做的就是使用脚本来收集好数据,然后zabbix收集并画图,设置告警线。这里我们来学习使用Zabbix监控Memcached、PHP-FPM、Tomcat、Nginx、MySQL及网站日志。
Memcached监控
自定义键值

[*]  UserParameter=memcached.stat
[*],/data/sh/memcached-status.sh "$1"
  memcached-status.sh脚本内容为:

[*]  #!/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 $3}'
导入模板
  memcached zabbix模板下载
PHP-FPM监控
配置php-fpm状态页
  打开php-fpm.conf配置文件,添加如下配置后重启php:

[*]  pm.status_path = /fpm_status
自定义键值

[*]  UserParameter=php-fpm
[*],/data/sh/php-fpm-status.sh "$1"
  php-fpm-status.sh脚本内容:

[*]  #!/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="$1"
[*]
[*]  # FPM defaults
[*]  URL="http://localhost/fpm_status"
[*]  WGET_BIN="/usr/bin/wget"
[*]
[*]  #
[*]  # Error handling:
[*]  #- need to be 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: +(+)/\1/p")
[*]  if [ $? -ne 0 -o -z "$RESULT" ]; then
[*]  echo $ERROR_WRONG_PARAM
[*]  exit 1
[*]  fi
[*]
[*]  echo $RESULT
[*]
[*]  exit 0
导入模板
  php-fpm zabbix模板下载
Tomcat监控
  刚开始决定监控Tomcat时,使用的是JMX,不过这货设置太复杂了,而且对防火墙要求还挺高,需要开放几个端口。只好使用Tomcat自带的状态页来监控了。
自定义键值

[*]  UserParameter=tomcat.status
[*],/data/sh/tomcat-status.py $1
  因为需要解析到xml,所以还是决定用python实现比较方便。
  /data/sh/tomcat-status.py脚本内容:

[*]  #!/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
[*]
[*]  if item == "memory.free":
[*]  printdoc.getElementsByTagName("memory").getAttribute("free")
[*]  elif item == "memory.total":
[*]  printdoc.getElementsByTagName("memory").getAttribute("total")
[*]  elif item == "memory.max":
[*]  printdoc.getElementsByTagName("memory").getAttribute("max")
[*]  elif item == "threadInfo.maxThreads":
[*]  printdoc.getElementsByTagName("threadInfo").getAttribute("maxThreads")
[*]  elif item == "threadInfo.currentThreadCount":
[*]  printdoc.getElementsByTagName("threadInfo").getAttribute("currentThreadCount")
[*]  elif item == "threadInfo.currentThreadsBusy":
[*]  printdoc.getElementsByTagName("threadInfo").getAttribute("currentThreadsBusy")
[*]  elif item == "requestInfo.maxTime":
[*]  printdoc.getElementsByTagName("requestInfo").getAttribute("maxTime")
[*]  elif item == "requestInfo.processingTime":
[*]  printdoc.getElementsByTagName("requestInfo").getAttribute("processingTime")
[*]  elif item == "requestInfo.requestCount":
[*]  printdoc.getElementsByTagName("requestInfo").getAttribute("requestCount")
[*]  elif item == "requestInfo.errorCount":
[*]  printdoc.getElementsByTagName("requestInfo").getAttribute("errorCount")
[*]  elif item == "requestInfo.bytesReceived":
[*]  printdoc.getElementsByTagName("requestInfo").getAttribute("bytesReceived")
[*]  elif item == "requestInfo.bytesSent":
[*]  printdoc.getElementsByTagName("requestInfo").getAttribute("bytesSent")
[*]  else:
[*]  print "unsupport item."
  这个脚本是监控Tomcat7的,Tomcat6没有试过,应该区别在状态页的url以及管理页面的用户密码设置上。以上脚本可运行需要在tomcat-users.xml里添加用户,至少权限为manager-status。
导入模板
  tomcat zabbix模板下载
Nginx监控
配置Nginx状态页
  在nginx配置文件server{}中加入:

[*]  location /nginx_status {
[*]  stub_status on;
[*]  access_log off;
[*]  }
自定义键值

[*]  UserParameter=nginx
[*],/data/sh/nginx-status.sh "$1"
  nginx-status.sh脚本内容:

[*]  #!/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="$1"
[*]  ZBX_REQ_DATA_URL="$2"
[*]
[*]  # Nginx defaults
[*]  URL="http://127.0.0.1/nginx_status"
[*]  WGET_BIN="/usr/bin/wget"
[*]
[*]  #
[*]  # Error handling:
[*]  #- need to be 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 '' | cut -f2 -d' ';;
[*]  handled_connections)echo "$NGINX_STATS" | grep -Ev '' | cut -f3 -d' ';;
[*]  handled_requests)   echo "$NGINX_STATS" | grep -Ev '' | 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
导入模板
  nginx zabbix模板下载
MySQL监控
  MySQL的监控,zabbix是默认支持的,已经有现成的模板,现成的键值,我们需要做的只是在/var/lib/zabbix里新建一个.my.cnf文件,内容如下:

[*]  
[*]  host=127.0.0.1
[*]  port=1036
[*]  user=root
[*]  password=root
网站日志监控
配置日志格式
  我们假设你用的web服务器是Nginx,我们添加一个日志格式,如下:

[*]  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';
  我们使用tab作分隔符,为了方便awk识别列的内容,以防出错。
  然后再设置全局的日志,其它server就不需要设置日志了:

[*]  access_log/data/home/logs/nginx/$host.log withHost;
定时获取一分钟日志
  设置一个定时任务:

[*]  * * * * * /data/sh/get_nginx_access.sh
  脚本内容为:

[*]  #!/bin/bash
[*]
[*]  logDir=/data/home/logs/nginx/
[*]  logNames=`ls ${logDir}/*.*.log|awk -F"/" '{print $NF}'`
[*]
[*]  for $logName in $logNames;
[*]  do
[*]  #设置变量
[*]  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
[*]  }
[*]  {
[*]  $3 = substr($3,13,8)
[*]  gsub(":","",$3)
[*]
[*]  if ($3>=oneMinuteAgo){
[*]  print
[*]  } else {
[*]  exit;
[*]  }
[*]  }' > $split_log
[*]
[*]
[*]  #统计状态码个数
[*]  awk -F'\t' '{
[*]  status[$4" "$6]++
[*]  }
[*]  END{
[*]  for (i in status)
[*]  {
[*]  print i,status
[*]  }
[*]  }
[*]  ' $split_log> $status_log
[*]  done
  这个定时任务是每分钟执行,因为我们监控的频率是每分钟。添加这个任务是为了取得最近一分钟各域名的日志,以及统计各域名的所有状态码个数,方便zabbix来获取所需的数据。
自定义键值

[*]  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 && $$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/^//' -e 's/$//'
  nginx-detect.sh脚本内容为:

[*]  #!/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;
[*]  do
[*]  while read domain code count;do
[*]  check_first_element
[*]  printf "{"
[*]  printf "\"{#DOMAIN}\":\"$domain\",\"{#CODE}\":\"$code\""
[*]  printf "}"
[*]  done < /tmp/$logName
[*]  done
[*]  json_end
  这里我们定义了三个键值,nginx.detect是为了发现所有域名及其所有状态码,nginx.access
[*]是为了统计指定域名的状态码的数量,nginx.log
[*]是为了测试指定域名的状态码超过指定值时输出排在前十的url。我们监控nginx访问日志用到了zabbix的自动发现功能,当我们增加域名时,不需要修改脚本,zabbix会帮助我们自动发现新增的域名并作监控。
配置探索规则
  添加一个探索规则,用来发现域名及状态码,如图:

配置监控项原型
  监控所有的域名及状态码:

  域名状态码404超过200次监控:

  域名状态码500超过50次监控:

配置触发器
  404状态码超过200告警:


  500状态码超过50告警:


页: [1]
查看完整版本: Zabbix监控Memcached PHP-FPM Tomcat Nginx MySQL 网站日志