lixiaolong 发表于 2018-11-2 11:53:57

监控CPU负载、Nginx、TCP、PHP、Memcached、Redis、Mysql、Tomcat

  
  监控CPU负载
  Agent端:
  # vim /etc/zabbix/zabbix_agentd.conf
  UserParameter=cpu_load1,/usr/bin/w|awk 'NR==1 {print $10}'|awk -F, '{print $1}'
  UserParameter=cpu_load5,/usr/bin/w|awk 'NR==1 {print $11}'|awk -F, '{print $1}'
  UserParameter=cpu_load15,/usr/bin/w|awk 'NR==1 {print $12}'
  Server端:
  # zabbix_get -s 10.0.0.101 -k cpu_load1
  0.00
  # zabbix_get -s 10.0.0.101 -k cpu_load5
  0.01
  # zabbix_get -s 10.0.0.101 -k cpu_load15
  0.05
  Step1(配置-->模板-->创建模板):

  Step2(创建克隆监控项):



  Step3(创建克隆触发器):



  Step4(创建图形):

  Step5(添加模板如下图):

  ab压测使CPU有波动
  Server端:
  # yum install -y httpd-tools
  # ab -n 10000 -c 100 http://10.0.0.101/index
  Agent端:
  # yum install -y httpd
  # systemctl start httpd
  # vim /var/www/html/index.html
  # curl --head http://10.0.0.101/index.html
  监控Nginx服务
  Agent端:
  #vim /etc/yum.repos.d/nginx.repo
  #cat /etc/yum.repos.d/nginx.repo
  
  name=nginx repo
  baseurl=http://nginx.org/packages/centos/7/x86_64/
  gpgcheck=0
  enabled=1
  #rpm -qa | grep centos-release
  centos-release-7-4.1708.el7.centos.x86_64
  # yum install -y nginx
  # systemctl start nginx##启动前查看是否有其他服务占用80端口(Apache)
  # netstat -lntup|grep nginx
  tcp      0      0 0.0.0.0:80      0.0.0.0:*         LISTEN      76182/nginx: master
  # vim /etc/nginx/conf.d/default.conf
  location /nginx_status {
  stub_status on;
  access_log off;
  allow 127.0.0.1;
  deny all;
  }
  # nginx -t
  nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
  nginx: configuration file /etc/nginx/nginx.conf test is successful

  # nginx -s>  浏览器查看http://10.0.0.101/nginx_status或者
  # curl http://10.0.0.101/nginx_status
  Active connections: 2
  server accepts handled requests
  123 123 129
  Reading: 0 Writing: 1 Waiting: 1
  # mkdir -p /etc/zabbix/scripts
  # cd /etc/zabbix/scripts
  # vim nginx_status.sh
  #!/bin/bash
  ############################################################
  # $Name:       zabbix_linux_plugins.sh
  # $Version:      v1.0
  # $Function:   zabbix plugins
  # $Author:       Andy
  # $organization: http://blog.51cto.com/13162375
  # $Create Date:2018-5-18
  # $Description:Monitor Linux Service Memcached Status
  ############################################################
  if [ $# -ne 1 ];then
  echo $"USAGE:$0 {active|reading|writing|waiting|accepts|handled|requests}"
  exit 1
  Fi
  NGINX_CMD=$1
  NGINX_PORT=80
  nginx_active(){
  /usr/bin/curl -s "http://127.0.0.1:"$NGINX_PORT"/nginx_status/" |awk '/Active/ {print $NF}'
  }
  nginx_reading(){
  /usr/bin/curl -s "http://127.0.0.1:"$NGINX_PORT"/nginx_status/" |awk '/Reading/ {print $2}'
  }
  nginx_writing(){
  /usr/bin/curl -s "http://127.0.0.1:"$NGINX_PORT"/nginx_status/" |awk '/Writing/ {print $4}'
  }
  nginx_waiting(){
  /usr/bin/curl -s "http://127.0.0.1:"$NGINX_PORT"/nginx_status/" |awk '/Waiting/ {print $6}'
  }
  nginx_accepts(){
  /usr/bin/curl -s "http://127.0.0.1:"$NGINX_PORT"/nginx_status/" |awk 'NR==3 {print $1}'
  }
  nginx_handled(){
  /usr/bin/curl -s "http://127.0.0.1:"$NGINX_PORT"/nginx_status/" |awk 'NR==3 {print $2}'
  }
  nginx_requests(){
  /usr/bin/curl -s "http://127.0.0.1:"$NGINX_PORT"/nginx_status/" |awk 'NR==3 {print $3}'
  }
  case $NGINX_CMD in
  active)
  nginx_active
  ;;
  reading)
  nginx_reading
  ;;
  writing)
  nginx_writing
  ;;
  waiting)
  nginx_waiting
  ;;
  accepts)
  nginx_accepts
  ;;
  handled)
  nginx_handled
  ;;
  requests)
  nginx_requests
  ;;
  *)
  echo $"USAGE:$0 {active|reading|writing|waiting|accepts|handled|requests}"
  esac
  # chmod +x nginx_status.sh
  # systemctl restart zabbix-agent
  # cd /etc/zabbix/zabbix_agentd.d
  # vim nginx_status.conf
  # cat nginx_status.conf
  UserParameter=nginx_status
[*],/bin/bash /etc/zabbix/scripts/nginx_status.sh "$1"
  Server端检测:
  # zabbix_get -s 10.0.0.101 -k nginx_status
  177
  Step1(配置-->模板-->创建模板):

  Step2(添加克隆监控项):

  Step3(添加触发器):

  Add部分

  Step4(添加图形):

  Step5(为主机添加模板):

  监控TCP信息状态
  Agent端:
  # cd /etc/zabbix/scripts/
  #vim tcp_status.sh
  # chmod +x tcp_status.sh
  # sh -x tcp_status.sh ESTAB##测试脚本
  + '[' 1 -ne 1 ']'
  + tcp_status_fun ESTAB
  + TCP_STAT=ESTAB
  + ss -ant
  + awk 'NR>1 {++s[$1]} END {for(k in s) print k,s}'
  ++ grep ESTAB /tmp/ss.txt
  ++ cut -d ' ' -f2
  + TCP_STAT_VALUE=1
  + '[' -z 1 ']'
  + echo 1
  1
  # vim tcp_status.conf
  # cat tcp_status.conf
  UserParameter=tcp_status
[*],/bin/bash /etc/zabbix/scripts/tcp_status.sh "$1"
  # systemctl restart zabbix-agent
  Server端
  # zabbix_get -s 10.0.0.101 -k tcp_status ##出现这种错误到客户端删掉/opt/ss.txt文件
  /etc/zabbix/scripts/tcp_status.sh: line 18: /tmp/ss.txt: Permission denied
  2
  # zabbix_get -s 10.0.0.101 -k tcp_status##检验键值
  2
  Step1(创建模板):

  Step2(创建克隆监控项):

  Step3(创建图形):

  Step4(关联主机):

  中文字符集乱码问题
  # cd /usr/share/zabbix/fonts/
  # rz simkai.ttf
  # vim /usr/share/zabbix/include/defines.inc.php##修改两处
  #define('ZBX_FONT_NAME', 'graphfont');
  define('ZBX_FONT_NAME', 'simkai');
  #define('ZBX_GRAPH_FONT_NAME',          'graphfont'); // font file name
  define('ZBX_GRAPH_FONT_NAME',         'simkai'); // font file name
  # systemctl restart zabbix-server
  监控php-fpm状态
  Agent端:
  # yum install -y php php-fpm
  # systemctl start php-fpm
  # netstat -lntup|grep 9000
  tcp      0    0 127.0.0.1:9000   0.0.0.0:*   LISTEN      64086/php-fpm: mast
  # vim /etc/nginx/conf.d/default.conf
  location ~ \.php$ {
  root         /usr/share/nginx/html;
  fastcgi_pass   127.0.0.1:9000;
  fastcgi_indexindex.php;
  fastcgi_paramSCRIPT_FILENAME/usr/share/nginx/html$fastcgi_script_name;
  include      fastcgi_params;
  }
  # vim /usr/share/nginx/html/index.php
  
  浏览器http://10.0.0.101/index.php能否成功
  # vim /etc/php-fpm.d/www.conf
  pm.status_path = /phpfpm_status
  # systemctl restart php-fpm
  # vim /etc/nginx/conf.d/default.conf
  location ~ ^/(phpfpm_status)$ {
  include fastcgi_params;
  fastcgi_pass    127.0.0.1:9000;
  fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
  }
  # nginx -t
  nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
  nginx: configuration file /etc/nginx/nginx.conf test is successful

  # nginx -s>  浏览器http://10.0.0.101/phpfpm_status或者curl http://10.0.0.101/phpfpm_status
  # curlhttp://10.0.0.101/phpfpm_status
  pool:               www
  process manager:      dynamic
  start time:          16/May/2018:19:33:39 +0800
  start since:         462
  accepted conn:       7
  listen queue:      0
  max listen queue:   0
  listen queue len:   128
  idle processes:       4
  active processes:   1
  total processes:      5
  max active processes: 1
  max children reached: 0
  slow requests:      0
  # curl -s http://10.0.0.101/phpfpm_status|awk '/accepted conn:/ {print $NF}'
  7
  # cd /etc/zabbix/scripts
  # vim phpfpm_status.sh
  # chmod +x phpfpm_status.sh
  # cd ..
  # cd zabbix_agentd.d/
  # vim phpfpm_status.conf
  UserParameter=phpfpm_status
[*],/bin/bash /etc/zabbix/scripts/phpfpm_status.sh "$1"
  # systemctl restart zabbix-agent
  Server端:
  # zabbix_get -s 10.0.0.101 -k phpfpm_status
  20
  Step1(创建模板):



  Step2(关联主机):

  监控mysql
  Agent端:
  # yum install -y mariadb-server
  # systemctl start mariadb
  # netstat -lntup|grep 3306
  tcp      0      0 0.0.0.0:3306    0.0.0.0:*         LISTEN      31688/mysqld
  # yum install -y https://www.percona.com/redir/downloads/percona-release/redhat/latest/percona-release-0.1-4.noarch.rpm
  # rpm -ql percona-release
  /etc/pki/rpm-gpg/RPM-GPG-KEY-Percona
  /etc/yum.repos.d/percona-release.repo
  /usr/share/doc/percona-release-0.1
  /usr/share/doc/percona-release-0.1/RPM-GPG-KEY-Percona
  # yum install -y percona-zabbix-templates
  # rpm -ql percona-zabbix-templates
  /var/lib/zabbix/percona
  /var/lib/zabbix/percona/scripts
  /var/lib/zabbix/percona/scripts/get_mysql_stats_wrapper.sh
  /var/lib/zabbix/percona/scripts/ss_get_mysql_stats.php
  /var/lib/zabbix/percona/templates
  /var/lib/zabbix/percona/templates/userparameter_percona_mysql.conf
  /var/lib/zabbix/percona/templates/zabbix_agent_template_percona_mysql_server_ht_2.0.9-sver1.1.8.xml
  # vim /var/lib/zabbix/percona/scripts/ss_get_mysql_stats.php
  $mysql_user = 'root';
  $mysql_pass = '';
  $mysql_port = 3306;
  # cp /var/lib/zabbix/percona/templates/userparameter_percona_mysql.conf /etc/zabbix/zabbix_agentd.d/
  # systemctl restart zabbix-agent
  由于agent端mariadb没有数据,监控也没有值,只能在server端测试
  # cd /var/lib/
  # scp -r zabbix/ root@10.0.0.102:/var/lib/
  Server端:
  # ls /var/lib/zabbix
  Percona
  # zabbix_get -s 10.0.0.102 -k MySQL.Threads-connected
  zabbix_get : Check access restrictions in Zabbix agent configuration
  # vim /etc/zabbix/zabbix_agentd.conf
  Server=10.0.0.102
  # systemctl restart zabbix-agent
  # zabbix_get -s 10.0.0.102 -k MySQL.Threads-connected
  25
  剩下就是导入模板和添加主机、关联主机。
  监控Redis
  Agent端:
  # wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
  # yum install -y redis
  # systemctl start redis
  # redis-cli
  127.0.0.1:6379> info
  127.0.0.1:6379> quit
  # cd /etc/zabbix/scripts/
  # vim redis_status.sh
  # chmod +x redis_status.sh
  # cd /etc/zabbix/zabbix_agentd.d/
  # vim redis_status.conf
  UserParameter=redis_status
[*],/bin/bash /etc/zabbix/scripts/redis_status.sh "$1"
  # systemctl restart zabbix-agent
  Server端:
  # zabbix_get -s 10.0.0.101 -k redis_status
  0.10
  剩下就是导入模板和添加主机、关联主机。
  监控Memcached
  Agent端:
  # yum install -y memcached
  # systemctl start memcached
  # netstat -lntup|grep 11211
  tcp      0      0 0.0.0.0:11211   0.0.0.0:*          LISTEN      58548/memcached
  tcp6       0      0 :::11211         :::*         LISTEN      58548/memcached
  udp       0      0 0.0.0.0:11211       0.0.0.0:*                  58548/memcached
  udp6      0      0 :::11211             :::*                      58548/memcached
  # mkdir -p /etc/zabbix/scripts
  # cd /etc/zabbix/scripts/
  # vim memcached_status.sh
  # chmod +x memcached.sh
  # cd /etc/zabbix/zabbix_agentd.d/
  # vim memcache_status.conf
  # cat memcache_status.conf
  UserParameter=memcached_status
[*],/bin/bash /etc/zabbix/scripts/memcached_status.sh "$1"
  Server端:
  # zabbix_get -s 10.0.0.103 -k memcached_status
  58548
  # zabbix_get -s 10.0.0.103 -k memcached_status
  10
  剩下就是导入模板和添加主机、关联主机。
  监控Tomcat
  tomcat端:
  # rz apache-tomcat-9.0.8.tar.gz
  # tar xf apache-tomcat-9.0.8.tar.gz -C /application/
  # cd ..
  # ln -s /application/apache-tomcat-9.0.8/ /application/apache-tomcat
  # ll
  total 0
  lrwxrwxrwx 1 root root33 May 18 23:51 apache-tomcat -> /application/apache-tomcat-9.0.8/
  drwxr-xr-x 9 root root 160 May 18 23:50 apache-tomcat-9.0.8
  drwxr-xr-x 2 root root40 May 18 23:47 package
  # yum install -y java
  # cd apache-tomcat/bin/
  # ./startup.sh
  Using CATALINA_BASE:   /application/apache-tomcat
  Using CATALINA_HOME:   /application/apache-tomcat
  Using CATALINA_TMPDIR: /application/apache-tomcat/temp
  Using JRE_HOME:      /usr

  Using>  Tomcat started.
  # netstat -lntup|grep java
  tcp6       0      0 :::8080         :::*            LISTEN      68252/java
  tcp6       0      0 127.0.0.1:8005      :::*             LISTEN      68252/java
  tcp6       0      0 :::8009         :::*             LISTEN      68252/java
  # vim catalina.sh
  CATALINA_OPTS="$CATALINA_OPTS
  -Dcom.sun.management.jmxremote
  -Dcom.sun.management.jmxremote.port=12345
  -Dcom.sun.management.jmxremote.authenticate=false
  -Dcom.sun.management.jmxremote.ssl=false -Djava.rmi.server.hostname=10.0.0.103"
  # ./shutdown.sh
  # ./startup.sh
  # netstat -lntup|grep java
  tcp6       0      0 :::39246      :::*            LISTEN      68696/java
  tcp6       0      0 :::8080         :::*            LISTEN      68696/java
  tcp6       0      0 :::12345      :::*            LISTEN      68696/java
  tcp6       0      0 :::33370      :::*            LISTEN      68696/java
  tcp6       0      0 127.0.0.1:8005    :::*            LISTEN      68696/java
  tcp6       0      0 :::8009      :::*             LISTEN      68696/java
  Zabbix-java-gateway端:
  # yum install -y zabbix-java-gateway java-1.8.0-openjdk
  # systemctl start zabbix-java-gateway
  # netstat -lntup|grep java
  tcp6       0      0 :::10052       :::*      LISTEN      6934/java
  Zabbix-Server端:
  # vim /etc/zabbix/zabbix_server.conf
  JavaGateway=10.0.0.101
  StartJavaPollers=5
  # systemctl restart zabbix-server
  Step1(配置-->主机-->创建主机):

  Step2(添加主机-->添加模板):


  Step3:


页: [1]
查看完整版本: 监控CPU负载、Nginx、TCP、PHP、Memcached、Redis、Mysql、Tomcat