监控CPU负载NginxTCPPHPMemcachedRedisMysqlTomcat
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了监控CPU负载NginxTCPPHPMemcachedRedisMysqlTomcat相关的知识,希望对你有一定的参考价值。
监控CPU负载
Agent端:
[[email protected] ~]# 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端:
[[email protected] ~]# zabbix_get -s 10.0.0.101 -k cpu_load1
0.00
[[email protected] ~]# zabbix_get -s 10.0.0.101 -k cpu_load5
0.01
[[email protected] ~]# zabbix_get -s 10.0.0.101 -k cpu_load15
0.05
Step1(配置-->模板-->创建模板):
Step2(创建克隆监控项):
Step3(创建克隆触发器):
Step4(创建图形):
Step5(添加模板如下图):
ab压测使CPU有波动
Server端:
[[email protected] ~]# yum install -y httpd-tools
[[email protected] ~]# ab -n 10000 -c 100 http://10.0.0.101/index
Agent端:
[[email protected] ~]# yum install -y httpd
[[email protected] ~]# systemctl start httpd
[[email protected] ~]# vim /var/www/html/index.html
[[email protected] ~]# curl --head http://10.0.0.101/index.html
监控nginx服务
Agent端:
[[email protected] ~]# vim /etc/yum.repos.d/nginx.repo
[[email protected] ~]# cat /etc/yum.repos.d/nginx.repo
[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/centos/7/x86_64/
gpgcheck=0
enabled=1
[[email protected] ~]# rpm -qa | grep centos-release
centos-release-7-4.1708.el7.centos.x86_64
[[email protected] ~]# yum install -y nginx
[[email protected] ~]# systemctl start nginx ##启动前查看是否有其他服务占用80端口(Apache)
[[email protected] ~]# netstat -lntup|grep nginx
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 76182/nginx: master
[[email protected] ~]# vim /etc/nginx/conf.d/default.conf
location /nginx_status {
stub_status on;
access_log off;
allow 127.0.0.1;
deny all;
}
[[email protected] ~]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[[email protected] ~]# nginx -s reload
浏览器查看http://10.0.0.101/nginx_status或者
[[email protected] ~]# curl http://10.0.0.101/nginx_status
Active connections: 2
server accepts handled requests
123 123 129
Reading: 0 Writing: 1 Waiting: 1
[[email protected] ~]# mkdir -p /etc/zabbix/scripts
[[email protected] ~]# cd /etc/zabbix/scripts
[[email protected] 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
[[email protected] scripts]# chmod +x nginx_status.sh
[[email protected] scripts]# systemctl restart zabbix-agent
[[email protected] scripts]# cd /etc/zabbix/zabbix_agentd.d
[[email protected] zabbix_agentd.d]# vim nginx_status.conf
[[email protected] zabbix_agentd.d]# cat nginx_status.conf
UserParameter=nginx_status[*],/bin/bash /etc/zabbix/scripts/nginx_status.sh "$1"
Server端检测:
[[email protected] ~]# zabbix_get -s 10.0.0.101 -k nginx_status[accepts]
177
Step1(配置-->模板-->创建模板):
Step2(添加克隆监控项):
Step3(添加触发器):
Add部分
Step4(添加图形):
Step5(为主机添加模板):
监控TCP信息状态
Agent端:
[[email protected] ~]# cd /etc/zabbix/scripts/
[[email protected] scripts]#vim tcp_status.sh
[[email protected] scripts]# chmod +x tcp_status.sh
[[email protected] 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
[[email protected] zabbix_agentd.d]# vim tcp_status.conf
[[email protected] zabbix_agentd.d]# cat tcp_status.conf
UserParameter=tcp_status[*],/bin/bash /etc/zabbix/scripts/tcp_status.sh "$1"
[[email protected] zabbix_agentd.d]# systemctl restart zabbix-agent
Server端
[[email protected] ~]# 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
[[email protected] ~]# zabbix_get -s 10.0.0.101 -k tcp_status[ESTAB] ##检验键值
2
Step1(创建模板):
Step2(创建克隆监控项):
Step3(创建图形):
Step4(关联主机):
中文字符集乱码问题
[[email protected] ~]# cd /usr/share/zabbix/fonts/
[[email protected] fonts]# rz simkai.ttf
[[email protected] 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
[[email protected]bbix-server fonts]# systemctl restart zabbix-server
监控php-fpm状态
Agent端:
[[email protected] ~]# yum install -y php php-fpm
[[email protected] ~]# systemctl start php-fpm
[[email protected] ~]# netstat -lntup|grep 9000
tcp 0 0 127.0.0.1:9000 0.0.0.0:* LISTEN 64086/php-fpm: mast
[[email protected] ~]# 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;
}
[[email protected] ~]# vim /usr/share/nginx/html/index.php
<?php
phpinfo();
?>
浏览器http://10.0.0.101/index.php能否成功
[[email protected] ~]# vim /etc/php-fpm.d/www.conf
pm.status_path = /phpfpm_status
[[email protected] ~]# systemctl restart php-fpm
[[email protected] ~]# 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;
}
[[email protected] ~]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[[email protected] ~]# nginx -s reload
浏览器http://10.0.0.101/phpfpm_status或者curl http://10.0.0.101/phpfpm_status
[[email protected] ~]# 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
[[email protected] ~]# curl -s http://10.0.0.101/phpfpm_status|awk '/accepted conn:/ {print $NF}'
7
[[email protected] ~]# cd /etc/zabbix/scripts
[[email protected] scripts]# vim phpfpm_status.sh
[[email protected] scripts]# chmod +x phpfpm_status.sh
[[email protected] scripts]# cd ..
[[email protected] zabbix]# cd zabbix_agentd.d/
[[email protected] zabbix_agentd.d]# vim phpfpm_status.conf
UserParameter=phpfpm_status[*],/bin/bash /etc/zabbix/scripts/phpfpm_status.sh "$1"
[[email protected] ~]# systemctl restart zabbix-agent
Server端:
[[email protected] zabbix]# zabbix_get -s 10.0.0.101 -k phpfpm_status[accepted_conn]
20
Step1(创建模板):
Step2(关联主机):
监控mysql
Agent端:
[[email protected] ~]# yum install -y mariadb-server
[[email protected] ~]# systemctl start mariadb
[[email protected] ~]# netstat -lntup|grep 3306
tcp 0 0 0.0.0.0:3306 0.0.0.0:* LISTEN 31688/mysqld
[[email protected] ~]# yum install -y https://www.percona.com/redir/downloads/percona-release/redhat/latest/percona-release-0.1-4.noarch.rpm
[[email protected] ~]# 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
[[email protected] ~]# yum install -y percona-zabbix-templates
[[email protected] ~]# 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
[[email protected] ~]# vim /var/lib/zabbix/percona/scripts/ss_get_mysql_stats.php
$mysql_user = 'root';
$mysql_pass = '';
$mysql_port = 3306;
[[email protected] ~]# cp /var/lib/zabbix/percona/templates/userparameter_percona_mysql.conf /etc/zabbix/zabbix_agentd.d/
[[email protected] ~]# systemctl restart zabbix-agent
由于agent端mariadb没有数据,监控也没有值,只能在server端测试
[[email protected] ~]# cd /var/lib/
[[email protected] lib]# scp -r zabbix/ [email protected]:/var/lib/
Server端:
[[email protected] ~]# ls /var/lib/zabbix
Percona
[[email protected] scripts]# zabbix_get -s 10.0.0.102 -k MySQL.Threads-connected
zabbix_get [82941]: Check access restrictions in Zabbix agent configuration
[[email protected] ~]# vim /etc/zabbix/zabbix_agentd.conf
Server=10.0.0.102
[[email protected] scripts]# systemctl restart zabbix-agent
[[email protected] scripts]# zabbix_get -s 10.0.0.102 -k MySQL.Threads-connected
25
剩下就是导入模板和添加主机、关联主机。
监控Redis
Agent端:
[[email protected] ~]# wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
[[email protected] ~]# yum install -y redis
[[email protected] ~]# systemctl start redis
[[email protected] ~]# redis-cli
127.0.0.1:6379> info
127.0.0.1:6379> quit
[[email protected] ~]# cd /etc/zabbix/scripts/
[[email protected] scripts]# vim redis_status.sh
[[email protected] scripts]# chmod +x redis_status.sh
[[email protected] scripts]# cd /etc/zabbix/zabbix_agentd.d/
[[email protected] zabbix_agentd.d]# vim redis_status.conf
UserParameter=redis_status[*],/bin/bash /etc/zabbix/scripts/redis_status.sh "$1"
[[email protected] zabbix_agentd.d]# systemctl restart zabbix-agent
Server端:
[[email protected] scripts]# zabbix_get -s 10.0.0.101 -k redis_status[used_cpu_sys]
0.10
剩下就是导入模板和添加主机、关联主机。
监控Memcached
Agent端:
[[email protected] ~]# yum install -y memcached
[[email protected] ~]# systemctl start memcached
[[email protected] ~]# 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
[[email protected] ~]# mkdir -p /etc/zabbix/scripts
[[email protected] ~]# cd /etc/zabbix/scripts/
[[email protected] scripts]# vim memcached_status.sh
[[email protected] scripts]# chmod +x memcached.sh
[[email protected] scripts]# cd /etc/zabbix/zabbix_agentd.d/
[[email protected] zabbix_agentd.d]# vim memcache_status.conf
[[email protected] zabbix_agentd.d]# cat memcache_status.conf
UserParameter=memcached_status[*],/bin/bash /etc/zabbix/scripts/memcached_status.sh "$1"
Server端:
[[email protected] ~]# zabbix_get -s 10.0.0.103 -k memcached_status[pid]
58548
[[email protected] ~]# zabbix_get -s 10.0.0.103 -k memcached_status[curr_connections]
10
剩下就是导入模板和添加主机、关联主机。
监控Tomcat
tomcat端:
[[email protected] package]# rz apache-tomcat-9.0.8.tar.gz
[[email protected] package]# tar xf apache-tomcat-9.0.8.tar.gz -C /application/
[[email protected] package]# cd ..
[[email protected] application]# ln -s /application/apache-tomcat-9.0.8/ /application/apache-tomcat
[[email protected] 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
[[email protected] application]# yum install -y java
[[email protected] application]# cd apache-tomcat/bin/
[[email protected] 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.
[[email protected] 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
[[email protected] 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"
[[email protected] bin]# ./shutdown.sh
[[email protected] bin]# ./startup.sh
[[email protected] 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端:
[[email protected] ~]# yum install -y zabbix-java-gateway java-1.8.0-openjdk
[[email protected] ~]# systemctl start zabbix-java-gateway
[[email protected] ~]# netstat -lntup|grep java
tcp6 0 0 :::10052 :::* LISTEN 6934/java
Zabbix-Server端:
[[email protected] ~]# vim /etc/zabbix/zabbix_server.conf
JavaGateway=10.0.0.101
StartJavaPollers=5
[[email protected] ~]# systemctl restart zabbix-server
Step1(配置-->主机-->创建主机):
Step2(添加主机-->添加模板):
Step3:
以上是关于监控CPU负载NginxTCPPHPMemcachedRedisMysqlTomcat的主要内容,如果未能解决你的问题,请参考以下文章