码迷,mamicode.com
首页 > 数据库 > 详细

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

时间:2018-05-20 18:49:20      阅读:240      评论:0      收藏:0      [点我收藏+]

标签:zabbix cpu负载 nginx

监控CPU负载

Agent:

[root@zabbix-agent1 ~]# 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:

[root@zabbix-server ~]# zabbix_get -s 10.0.0.101 -k cpu_load1

0.00

[root@zabbix-server ~]# zabbix_get -s 10.0.0.101 -k cpu_load5

0.01

[root@zabbix-server ~]# zabbix_get -s 10.0.0.101 -k cpu_load15

0.05

Step1(配置-->模板-->创建模板):

技术分享图片

Step2(创建克隆监控项):

技术分享图片

技术分享图片

技术分享图片

Step3(创建克隆触发器):

技术分享图片

技术分享图片

技术分享图片

Step4(创建图形):

技术分享图片

Step5(添加模板如下图):

技术分享图片

ab压测使CPU有波动 

Server端:

[root@zabbix-server ~]# yum install -y httpd-tools

[root@zabbix-server ~]# ab -n 10000 -c 100 http://10.0.0.101/index

Agent:

[root@zabbix-agent1 ~]# yum install -y httpd

[root@zabbix-agent1 ~]# systemctl start httpd

[root@zabbix-agent1 ~]# vim /var/www/html/index.html

[root@zabbix-agent1 ~]# curl --head http://10.0.0.101/index.html

监控Nginx服务

Agent:

[root@zabbix-agent1 ~]#  vim /etc/yum.repos.d/nginx.repo

[root@zabbix-agent1 ~]#  cat /etc/yum.repos.d/nginx.repo

[nginx]

name=nginx repo

baseurl=http://nginx.org/packages/centos/7/x86_64/

gpgcheck=0

enabled=1

[root@zabbix-agent1 ~]#  rpm -qa | grep centos-release

centos-release-7-4.1708.el7.centos.x86_64

[root@zabbix-agent1 ~]# yum install -y nginx

[root@zabbix-agent1 ~]# systemctl start nginx  ##启动前查看是否有其他服务占用80端口(Apache)

[root@zabbix-agent1 ~]# netstat -lntup|grep nginx

tcp        0      0 0.0.0.0:80      0.0.0.0:*         LISTEN      76182/nginx: master

[root@zabbix-agent1 ~]# vim /etc/nginx/conf.d/default.conf

    location /nginx_status {

        stub_status on;

        access_log off;

        allow 127.0.0.1;

        deny all;

    }

[root@zabbix-agent1 ~]# nginx -t

nginx: the configuration file /etc/nginx/nginx.conf syntax is ok

nginx: configuration file /etc/nginx/nginx.conf test is successful

[root@zabbix-agent1 ~]# nginx -s reload

浏览器查看http://10.0.0.101/nginx_status或者

[root@zabbix-agent1 ~]# curl http://10.0.0.101/nginx_status

Active connections: 2

server accepts handled requests

 123 123 129

Reading: 0 Writing: 1 Waiting: 1

[root@zabbix-agent1 ~]# mkdir -p /etc/zabbix/scripts

[root@zabbix-agent1 ~]# cd /etc/zabbix/scripts

[root@zabbix-agent1 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

[root@zabbix-agent1 scripts]# chmod +x nginx_status.sh

[root@zabbix-agent1 scripts]# systemctl restart zabbix-agent

[root@zabbix-agent1 scripts]# cd /etc/zabbix/zabbix_agentd.d

[root@zabbix-agent1 zabbix_agentd.d]# vim nginx_status.conf

[root@zabbix-agent1 zabbix_agentd.d]# cat nginx_status.conf

UserParameter=nginx_status[*],/bin/bash /etc/zabbix/scripts/nginx_status.sh "$1"

Server端检测:

[root@zabbix-server ~]# zabbix_get -s 10.0.0.101 -k nginx_status[accepts]

177

Step1(配置-->模板-->创建模板):

技术分享图片

Step2(添加克隆监控项):

技术分享图片

Step3(添加触发器):

技术分享图片

Add部分

技术分享图片

Step4(添加图形):

技术分享图片

Step5(为主机添加模板):

技术分享图片

监控TCP信息状态

Agent:

[root@zabbix-agent1 ~]# cd /etc/zabbix/scripts/

[root@zabbix-agent1 scripts]#vim tcp_status.sh

[root@zabbix-agent1 scripts]# chmod +x tcp_status.sh

[root@zabbix-agent1 scripts]# 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[k]}'

++ grep ESTAB /tmp/ss.txt

++ cut -d ' ' -f2

+ TCP_STAT_VALUE=1

+ '[' -z 1 ']'

+ echo 1

1

[root@zabbix-agent1 zabbix_agentd.d]# vim tcp_status.conf

[root@zabbix-agent1 zabbix_agentd.d]# cat tcp_status.conf

UserParameter=tcp_status[*],/bin/bash /etc/zabbix/scripts/tcp_status.sh "$1"

[root@zabbix-agent1 zabbix_agentd.d]# systemctl restart zabbix-agent

Server

[root@zabbix-server ~]# zabbix_get -s 10.0.0.101 -k tcp_status[ESTAB] ##出现这种错误到客户端删掉/opt/ss.txt文件

/etc/zabbix/scripts/tcp_status.sh: line 18: /tmp/ss.txt: Permission denied

2

[root@zabbix-server ~]# zabbix_get -s 10.0.0.101 -k tcp_status[ESTAB]  ##检验键值

2

Step1(创建模板):

技术分享图片

Step2(创建克隆监控项):

技术分享图片

Step3(创建图形):

技术分享图片

Step4(关联主机):

技术分享图片

中文字符集乱码问题

[root@zabbix-server ~]# cd /usr/share/zabbix/fonts/

[root@zabbix-server fonts]# rz simkai.ttf

[root@zabbix-server fonts]# 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

[root@zabbix-server fonts]# systemctl restart zabbix-server

监控php-fpm状态

Agent:

[root@zabbix-agent1 ~]# yum install -y php php-fpm

[root@zabbix-agent1 ~]# systemctl start php-fpm

[root@zabbix-agent1 ~]# netstat -lntup|grep 9000

tcp      0    0 127.0.0.1:9000   0.0.0.0:*     LISTEN      64086/php-fpm: mast

[root@zabbix-agent1 ~]# vim /etc/nginx/conf.d/default.conf

    location ~ \.php$ {

         root           /usr/share/nginx/html;

         fastcgi_pass   127.0.0.1:9000;

         fastcgi_index  index.php;

         fastcgi_param  SCRIPT_FILENAME  /usr/share/nginx/html$fastcgi_script_name;

         include        fastcgi_params;

    }

[root@zabbix-agent1 ~]# vim /usr/share/nginx/html/index.php

<?php

        phpinfo();

?>

浏览器http://10.0.0.101/index.php能否成功

[root@zabbix-agent1 ~]# vim /etc/php-fpm.d/www.conf

pm.status_path = /phpfpm_status

[root@zabbix-agent1 ~]# systemctl restart php-fpm

[root@zabbix-agent1 ~]# 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;

   }

[root@zabbix-agent1 ~]# nginx -t

nginx: the configuration file /etc/nginx/nginx.conf syntax is ok

nginx: configuration file /etc/nginx/nginx.conf test is successful

[root@zabbix-agent1 ~]# nginx -s reload

浏览器http://10.0.0.101/phpfpm_status或者curl http://10.0.0.101/phpfpm_status

[root@zabbix-agent1 ~]# curl  http://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

[root@zabbix-agent1 ~]# curl -s http://10.0.0.101/phpfpm_status|awk '/accepted conn:/ {print $NF}'

7

[root@zabbix-agent1 ~]# cd /etc/zabbix/scripts

[root@zabbix-agent1 scripts]# vim phpfpm_status.sh

[root@zabbix-agent1 scripts]# chmod +x phpfpm_status.sh

[root@zabbix-agent1 scripts]# cd ..

[root@zabbix-agent1 zabbix]# cd zabbix_agentd.d/

[root@zabbix-agent1 zabbix_agentd.d]# vim phpfpm_status.conf

UserParameter=phpfpm_status[*],/bin/bash /etc/zabbix/scripts/phpfpm_status.sh "$1"

[root@zabbix-agent1 ~]# systemctl restart zabbix-agent

Server端:

[root@zabbix-server zabbix]# zabbix_get -s 10.0.0.101 -k phpfpm_status[accepted_conn]

20

Step1(创建模板):

技术分享图片

技术分享图片

技术分享图片

Step2(关联主机):

技术分享图片

监控mysql

Agent:

[root@zabbix-agent1 ~]# yum install -y mariadb-server

[root@zabbix-agent1 ~]# systemctl start mariadb

[root@zabbix-agent1 ~]# netstat -lntup|grep 3306

tcp        0      0 0.0.0.0:3306    0.0.0.0:*           LISTEN      31688/mysqld

[root@zabbix-agent1 ~]# yum install -y https://www.percona.com/redir/downloads/percona-release/redhat/latest/percona-release-0.1-4.noarch.rpm

[root@zabbix-agent1 ~]# 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

[root@zabbix-agent1 ~]# yum install -y percona-zabbix-templates

[root@zabbix-agent1 ~]# 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

[root@zabbix-agent1 ~]# vim /var/lib/zabbix/percona/scripts/ss_get_mysql_stats.php

$mysql_user = 'root';

$mysql_pass = '';

$mysql_port = 3306;

[root@zabbix-agent1 ~]# cp /var/lib/zabbix/percona/templates/userparameter_percona_mysql.conf /etc/zabbix/zabbix_agentd.d/

[root@zabbix-agent1 ~]# systemctl restart zabbix-agent

由于agentmariadb没有数据,监控也没有值,只能在server端测试

[root@zabbix-agent1 ~]# cd /var/lib/

[root@zabbix-agent1 lib]# scp -r zabbix/ root@10.0.0.102:/var/lib/

Server端:

[root@zabbix-server ~]# ls /var/lib/zabbix

Percona

[root@zabbix-server scripts]# zabbix_get -s 10.0.0.102 -k MySQL.Threads-connected

zabbix_get [82941]: Check access restrictions in Zabbix agent configuration

[root@zabbix-server ~]# vim /etc/zabbix/zabbix_agentd.conf

Server=10.0.0.102

[root@zabbix-server scripts]# systemctl restart zabbix-agent

[root@zabbix-server scripts]# zabbix_get -s 10.0.0.102 -k MySQL.Threads-connected

25

剩下就是导入模板和添加主机、关联主机。

监控Redis

Agent:

[root@zabbix-agent1 ~]# wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo

[root@zabbix-agent1 ~]# yum install -y redis

[root@zabbix-agent1 ~]# systemctl start redis

[root@zabbix-agent1 ~]# redis-cli

127.0.0.1:6379> info

127.0.0.1:6379> quit

[root@zabbix-agent1 ~]# cd /etc/zabbix/scripts/

[root@zabbix-agent1 scripts]# vim redis_status.sh

[root@zabbix-agent1 scripts]# chmod +x redis_status.sh

[root@zabbix-agent1 scripts]# cd /etc/zabbix/zabbix_agentd.d/

[root@zabbix-agent1 zabbix_agentd.d]# vim redis_status.conf

UserParameter=redis_status[*],/bin/bash /etc/zabbix/scripts/redis_status.sh "$1"

[root@zabbix-agent1 zabbix_agentd.d]# systemctl restart zabbix-agent

Server端:

[root@zabbix-server scripts]# zabbix_get -s 10.0.0.101 -k redis_status[used_cpu_sys]

0.10

剩下就是导入模板和添加主机、关联主机。

监控Memcached

Agent:

[root@zabbix-agent2 ~]# yum install -y memcached

[root@zabbix-agent2 ~]# systemctl start memcached

[root@zabbix-agent2 ~]# 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     

[root@zabbix-agent2 ~]# mkdir -p /etc/zabbix/scripts

[root@zabbix-agent2 ~]# cd /etc/zabbix/scripts/

[root@zabbix-agent2 scripts]# vim memcached_status.sh

[root@zabbix-agent2 scripts]# chmod +x memcached.sh

[root@zabbix-agent2 scripts]# cd /etc/zabbix/zabbix_agentd.d/

[root@zabbix-agent2 zabbix_agentd.d]# vim memcache_status.conf

[root@zabbix-agent2 zabbix_agentd.d]# cat memcache_status.conf

UserParameter=memcached_status[*],/bin/bash /etc/zabbix/scripts/memcached_status.sh "$1"

Server端:

[root@zabbix-server ~]# zabbix_get -s 10.0.0.103 -k memcached_status[pid]

58548

[root@zabbix-server ~]# zabbix_get -s 10.0.0.103 -k memcached_status[curr_connections]

10

剩下就是导入模板和添加主机、关联主机。

监控Tomcat

tomcat:

[root@zabbix-agent2 package]# rz apache-tomcat-9.0.8.tar.gz

[root@zabbix-agent2 package]# tar xf apache-tomcat-9.0.8.tar.gz -C /application/

[root@zabbix-agent2 package]# cd ..

[root@zabbix-agent2 application]# ln -s /application/apache-tomcat-9.0.8/ /application/apache-tomcat

[root@zabbix-agent2 application]# ll

total 0

lrwxrwxrwx 1 root root  33 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 root  40 May 18 23:47 package

[root@zabbix-agent2 application]# yum install -y java

[root@zabbix-agent2 application]# cd apache-tomcat/bin/

[root@zabbix-agent2 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 CLASSPATH:    /application/apache-tomcat/bin/bootstrap.jar:/application/apache-tomcat/bin/tomcat-juli.jar

Tomcat started.

[root@zabbix-agent2 bin]# 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      

[root@zabbix-agent2 bin]# 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"

[root@zabbix-agent2 bin]# ./shutdown.sh

[root@zabbix-agent2 bin]# ./startup.sh

[root@zabbix-agent2 bin]# 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:

[root@zabbix-agent1 ~]# yum install -y zabbix-java-gateway java-1.8.0-openjdk

[root@zabbix-agent1 ~]# systemctl start zabbix-java-gateway

[root@zabbix-agent1 ~]# netstat -lntup|grep java

tcp6       0      0 :::10052       :::*        LISTEN      6934/java           

Zabbix-Server端:

[root@zabbix-server ~]# vim /etc/zabbix/zabbix_server.conf

JavaGateway=10.0.0.101

StartJavaPollers=5

[root@zabbix-server ~]# systemctl restart zabbix-server

Step1(配置-->主机-->创建主机):

技术分享图片

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

技术分享图片

技术分享图片

Step3:

技术分享图片

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

标签:zabbix cpu负载 nginx

原文地址:http://blog.51cto.com/13162375/2118472

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!