1.对系统不间断实时监控
2.实时反馈系统当前状态
3.保证服务可靠性安全性
4.保证业务持续稳定运行
1.Zabbix
2.Open-Falcon 小米
3.Prometheus(普罗米修斯, Docker、 K8s)
1.硬件监控 路由器、交换机、防火墙
2.系统监控 CPU、内存、磁盘、网络、进程、 TCP
3.服务监控 nginx、 php、 tomcat、 redis、 memcache、 mysql
4.WEB 监控 请求时间、响应时间、加载时间、
5.日志监控 ELk(收集、存储、分析、展示) 日志易
6.安全监控 Firewalld、 WAF(Nginx+lua)、安全宝、牛盾云、安全狗
7.网络监控 smokeping 多机房
8.业务监控 活动引入多少流量、产生多少注册量、带来多大价值
CPU 监控命令: w、 top、 htop、 glances
%Cpu(s): 0.3 us, 0.3 sy, 0.0 ni, 99.3 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st
us 用户态: 跟用户的操作有关 35%
sy 系统态: 跟内核的处理有关 60%
id CPU 空闲:
内存监控命令: free
[root@m01 ~]# free -h
total used free shared buff/cache available
Mem: 977M 105M 724M 6.6M 148M 729M
Swap: 1.0G 0B 1.0G
磁盘监控命令: df、 iotop
Device: tps kB_read/s kB_wrtn/s kB_read kB_wrtn
sda 0.80 25.32 33.36 221034 291193
设备名 每秒传输次数 每秒读大小 每秒写大小 读的总大小 写的总大小
网络监控命令: ifconfig、 route、 glances、 iftop、 nethogs、 netstat
单位换算
Mbps 100Mbps/8
MB 12MB
iftop 中间的<= =>这两个左右箭头,表示的是流量的方向。
TX:发送流量、 RX:接收流量、 TOTAL:总流量
#查看 TCP11 中状态
netstat -an|grep ESTABLISHED
netstat -rn # 查看路由信息
netstat -lntup
2.随着时间的推移,用户不断的增多,服务随时可能扛不住会被 oom(out of memory),当系统内存不足的时候,会
触发 oom
1.当系统内存不足的时候就会大量使用 swap
2.当系统大量使用 swap 的时候,系统会特别卡
注意: 有时可能内存还有剩余 300Mb-500Mb,但会发现 swap 依然被使用
[root@ZabbixServer ~]# dd if=/dev/zero of=/dev/null bs=800M
[root@ZabbixServer ~]# tail -f /var/log/messages
Out of memory: Kill process 2227 (dd) score 778 or sacrifice child
Killed process 2227 (dd) total-vm:906724kB, anon-rss:798820kB, file-rss:0kB
3.那单机时代,如何使用 shell 脚本来实现服务器的监控
需求: 每隔 1 分钟监控一次内存,当你的可用内存低于 100m,发邮件报警,要求显示剩余内存
1.怎么获取内存可用的值 free -m|awk ‘/^Mem/{print $NF}‘
2.获取到内存可用的值如何和设定的阈值进行比较
3.比较如果大于 100m 则不处理,如果小于 100 则报警
4.如何每隔 1 分钟执行一次
[root@ZabbixServer ~]# cat free.sh
#!/usr/bin/bash
HostName=$(hostname)_$(hostname -i)
Date=$(date +%F)
while true;do
Free=$(free -m|awk ‘/^Mem/{print $NF}‘)
if [ $Free -le 100 ];then
echo "$Date: $HostName Mem Is < ${Free}MB"
fi
sleep 5
done
1.配置zabbix仓库
[root@m01 ~]# rpm -ivh https://mirrors.tuna.tsinghua.edu.cn/zabbix/zabbix/4.0/rhel/7/x86_64/zabbix-release-4.0-1.el7.noarch.rpm
[root@m01 ~]# sed -i ‘s#repo.zabbix.com#mirrors.tuna.tsinghua.edu.cn/zabbix#g‘ /etc/yum.repos.d/zabbix.repo
2.安装 Zabbix 程序包,以及 MySQL、 Zabbix-agent
[root@m01 ~]# yum install -y zabbix-server-mysql zabbix-web-mysql zabbix-agent mariadb-server
[root@m01 ~]# systemctl start mariadb.service && systemctl enable mariadb.service
3.创建 Zabbix 数据库以及用户
[root@m01 ~]# mysqladmin password 123456
[root@m01 ~]# mysql -uroot -p123456
MariaDB [(none)]> create database zabbix character set utf8 collate utf8_bin;
MariaDB [(none)]> grant all privileges on zabbix.* to zabbix@localhost identified by ‘zabbix‘;
MariaDB [(none)]> flush privileges;
4.导入 Zabbix 数据至数据库中
[root@m01 ~]# zcat /usr/share/doc/zabbix-server-mysql-4.0.11/create.sql.gz | mysql -uzabbix -pzabbix zabbix
5.编辑/etc/zabbix/zabbix_server.conf 文件,修改数据库配置
[root@m01 ~]# grep "^[a-Z]" /etc/zabbix/zabbix_server.conf
...............
DBHost=localhost
DBName=zabbix
DBUser=zabbix
DBPassword=zabbix
...............
6.启动 Zabbix 服务进程,并加入开机自启
[root@m01 ~]# systemctl start zabbix-server.service
[root@m01 ~]# systemctl enable zabbix-server.service
7.配置 Apache 的配置文件/etc/httpd/conf.d/zabbix.conf,修改时区
[root@m01 ~]# grep "Shanghai" /etc/httpd/conf.d/zabbix.conf
php_value date.timezone Asia/Shanghai
8.重启 Apache Web 服务器
[root@m01 ~]# systemctl start httpd
提示已成功地安装了 Zabbix 前端。配置文件/etc/zabbix/web/zabbix.conf.php 被创建。
默认登陆 ZabbixWeb 的用户名 Admin,密码 zabbix
打开图形之后会发现语言为乱码,原因是缺少字体
解决方法:安装字体并替换现有字体
[root@m01 ~]# yum install wqy-microhei-fonts -y
[root@m01 ~]# cp /usr/share/fonts/wqy-microhei/wqy-microhei.ttc /usr/share/zabbix/assets/fonts/graphfont.ttf
再次刷新发现已经变成中文了
zabbix-agent(数据采集)—>zabbix-server(数据分析|报警)—> 数据库(数据存储)<—zabbix web(数据展示)
[root@web01 ~]# rpm -ivh https://mirror.tuna.tsinghua.edu.cn/zabbix/zabbix/4.0/rhel/7/x86_64/zabbix-agent-4.0.11-1.el7.x86_64.rpm
[root@web01 ~]# grep "^[a-Z]" /etc/zabbix/zabbix_agentd.conf
PidFile=/var/run/zabbix/zabbix_agentd.pid
LogFile=/var/log/zabbix/zabbix_agentd.log
LogFileSize=0
Server=10.0.1.61
ServerActive=127.0.0.1
Hostname=Zabbix server
Include=/etc/zabbix/zabbix_agentd.d/*.conf
[root@web01 ~]# systemctl start zabbix-agent.service
[root@web01 ~]# systemctl enable zabbix-agent.service
[root@web01 ~]# netstat -lntup|grep 10050
tcp 0 0 0.0.0.0:10050 0.0.0.0:* LISTEN 10351/zabbix_agentd
tcp6 0 0 :::10050 :::* LISTEN 10351/zabbix_agentd
监控TCP11种状态集
[root@web01 ~]# netstat -ant|grep -c TIME_WAIT
55
[root@web01 ~]# netstat -ant|grep -c LISTEN
12
[root@web01 ~]# cat /etc/zabbix/zabbix_agentd.d/tcp_status.conf
UserParameter=tcp_state[*],netstat -ant|grep -c $1
root@web01 ~]# systemctl restart zabbix-agent.service
[root@m01 ~]# rpm -ivh https://mirrors.tuna.tsinghua.edu.cn/zabbix/zabbix/4.0/rhel/7/x86_64/zabbix-release-4.0-1.el7.noarch.rpm
[root@m01 ~]# yum install zabbix-get.x86_64 -y
[root@m01 ~]# zabbix_get -s 10.0.1.7 -k tcp_state[TIME_WAIT]
51
[root@m01 ~]# zabbix_get -s 10.0.1.7 -k tcp_state[LISTEN]
12
由于TCP有多种状态,需要添加多个监控项,我们可以使用克隆快速达到创建的效果
其他的状态依次添加即可
定制报警内容:
https://www.zabbix.com/documentation/4.0/zh/manual/appendix/macros/supported_by_location
参考博客
https://www.cnblogs.com/bixiaoyu/p/7302541.html
发送警告
报警邮件标题可以使用默认信息,亦可使用如下中文报警内容
名称:Action-Email
默认标题:故障{TRIGGER.STATUS},服务器:{HOSTNAME1}发生: {TRIGGER.NAME}故障!
告警主机:{HOSTNAME1}
告警时间:{EVENT.DATE} {EVENT.TIME}
告警等级:{TRIGGER.SEVERITY}
告警信息: {TRIGGER.NAME}
告警项目:{TRIGGER.KEY1}
问题详情:{ITEM.NAME}:{ITEM.VALUE}
当前状态:{TRIGGER.STATUS}:{ITEM.VALUE1}
事件ID:{EVENT.ID}
恢复警告
恢复标题:恢复{TRIGGER.STATUS}, 服务器:{HOSTNAME1}: {TRIGGER.NAME}已恢复!
恢复信息:
告警主机:{HOSTNAME1}
告警时间:{EVENT.DATE} {EVENT.TIME}
告警等级:{TRIGGER.SEVERITY}
告警信息: {TRIGGER.NAME}
告警项目:{TRIGGER.KEY1}
问题详情:{ITEM.NAME}:{ITEM.VALUE}
当前状态:{TRIGGER.STATUS}:{ITEM.VALUE1}
事件ID:{EVENT.ID}
[root@m01 ~]# grep "^AlertScriptsPath" /etc/zabbix/zabbix_server.conf
AlertScriptsPath=/usr/lib/zabbix/alertscripts
[root@m01 /usr/lib/zabbix/alertscripts]# ll
总用量 4
-rwxr-xr-x 1 root root 1344 8月 7 21:58 weixin.py
https://work.weixin.qq.com/wework_admin/loginpage_wx
1.登陆后在企业号上新建应用
2.上传logo,填写应用名称 ,应用介绍等
3.查看启动应用
同时会生成应用的AgentId以及Secret,这个在后面步骤会有用
4.接口调用测试
http://work.weixin.qq.com/api/devtools/devtool.php
这里的corpid为公司ID
Corpsecret就是刚才创建应用生成的Secrt,确认没问题填写进去然后下一步
如果没问题会显示200状态码
[root@m01 /usr/lib/zabbix/alertscripts]# cat weixin.py
..............
corpid=‘微信企业号corpid‘
appsecret=‘应用的Secret‘
agentid=应用的id
..............
[root@m01 /usr/lib/zabbix/alertscripts]# python weixin.py 你的账号 ‘发信测试‘ ‘微信测试消息’
虽然我们实现了发送到单个用户的功能,但是如果我们的用户比较多,这样还是麻烦的,不过我们可以发送到整个组,其实脚本里已经预留好了配置,只不过默认注释了。
将脚本修改为以下内容,注释掉用户,打开组设置
#!/usr/bin/env python
import requests
import sys
import os
import json
import logging
logging.basicConfig(level = logging.DEBUG, format = ‘%(asctime)s, %(filename)s, %(levelname)s, %(message)s‘,
datefmt = ‘%a, %d %b %Y %H:%M:%S‘,
filename = os.path.join(‘/tmp‘,‘weixin.log‘),
filemode = ‘a‘)
corpid=‘wwd26fdfb9940e7efa‘
appsecret=‘Btg89FnZfMu0k7l6b4iagmAR5Z9TCgKknYbx-SMQvmg‘
agentid=1000005
token_url=‘https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=‘ + corpid + ‘&corpsecret=‘ + appsecret
req=requests.get(token_url)
accesstoken=req.json()[‘access_token‘]
msgsend_url=‘https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=‘ + accesstoken
#touser=sys.argv[1]
toparty=sys.argv[1]
subject=sys.argv[2]
message=sys.argv[2] + "\n\n" +sys.argv[3]
params={
#"touser": touser,
"toparty": toparty,
"msgtype": "text",
"agentid": agentid,
"text": {
"content": message
},
"safe":0
}
req=requests.post(msgsend_url, data=json.dumps(params))
logging.info(‘sendto:‘ + toparty + ‘;;subject:‘ + subject + ‘;;message:‘ + message)
#!/bin/bash
num=$(echo $(($RANDOM%28+1)))
name=$(sed -n "${num}p" name.txt)
ok_boy=$(grep -v "${name}" name.txt)
for ok in ${ok_boy}
do
python weixin.py ${ok} "$1" "$2"
done
编写zabbix配置文件
[root@web01 /etc/zabbix/zabbix_agentd.d]# cat zbx_tcp.conf
UserParameter=ESTABLISHED,netstat -ant|grep -c ‘ESTABLISHED‘
UserParameter=SYN_SENT,netstat -ant|grep -c ‘SYN_SENT‘
UserParameter=SYN_RECV,netstat -ant|grep -c ‘SYN_RECV‘
UserParameter=FIN_WAIT1,netstat -ant|grep -c ‘FIN_WAIT1‘
UserParameter=FIN_WAIT2,netstat -ant|grep -c ‘FIN_WAIT2‘
UserParameter=TIME_WAIT,netstat -ant|grep -c ‘TIME_WAIT‘
UserParameter=CLOSE,netstat -ant|grep -c ‘CLOSE‘
UserParameter=CLOSE_WAIT,netstat -ant|grep -c ‘CLOSE_WAIT‘
UserParameter=LAST_ACK,netstat -ant|grep -c ‘LAST_ACK‘
UserParameter=LISTEN,netstat -ant|grep -c ‘LISTEN‘
UserParameter=CLOSING,netstat -ant|grep -c ‘CLOSING‘
[root@web01 ~]# systemctl restart zabbix-agent.service
使用zabbix-get命令测试
[root@m01 ~]# yum install zabbix-get.x86_64 -y
[root@m01 ~]# zabbix_get -s 10.0.1.7 -k ESTABLISHED
2
[root@m01 ~]# zabbix_get -s 10.0.1.7 -k LISTEN
12
[root@web01 ~]# cat /etc/nginx/conf.d/status.conf
server {
listen 80;
server_name localhost;
location /nginx_status {
stub_status on;
access_log off;
}
}
[root@web01 ~]# curl 127.0.0.1/nginx_status/
Active connections: 1
server accepts handled requests
6 6 6
Reading: 0 Writing: 1 Waiting: 0
[root@web01 /etc/zabbix/zabbix_agentd.d]# cat nginx_monitor.sh
#!/bin/bash
NGINX_COMMAND=$1
CACHEFILE="/tmp/nginx_status.txt"
CMD="/usr/bin/curl http://127.0.0.1/nginx_status/"
if [ ! -f $CACHEFILE ];then
$CMD >$CACHEFILE 2>/dev/null
fi
# Check and run the script
TIMEFLM=`stat -c %Y $CACHEFILE`
TIMENOW=`date +%s`
if [ `expr $TIMENOW - $TIMEFLM` -gt 60 ]; then
rm -f $CACHEFILE
fi
if [ ! -f $CACHEFILE ];then
$CMD >$CACHEFILE 2>/dev/null
fi
nginx_active(){
grep ‘Active‘ $CACHEFILE| awk ‘{print $NF}‘
exit 0;
}
nginx_reading(){
grep ‘Reading‘ $CACHEFILE| awk ‘{print $2}‘
exit 0;
}
nginx_writing(){
grep ‘Writing‘ $CACHEFILE | awk ‘{print $4}‘
exit 0;
}
nginx_waiting(){
grep ‘Waiting‘ $CACHEFILE| awk ‘{print $6}‘
exit 0;
}
nginx_accepts(){
awk NR==3 $CACHEFILE| awk ‘{print $1}‘
exit 0;
}
nginx_handled(){
awk NR==3 $CACHEFILE| awk ‘{print $2}‘
exit 0;
}
nginx_requests(){
awk NR==3 $CACHEFILE| awk ‘{print $3}‘
exit 0;
}
case $NGINX_COMMAND in
active)
nginx_active;
;;
reading)
nginx_reading;
;;
writing)
nginx_writing;
;;
waiting)
nginx_waiting;
;;
accepts)
nginx_accepts;
;;
handled)
nginx_handled;
;;
requests)
nginx_requests;
;;
*)
echo ‘Invalid credentials‘;
exit 2;
esac
[root@web01 ~]# cat /etc/zabbix/zabbix_agentd.d/nginx_status.conf
UserParameter=nginx_status[*],/bin/bash /etc/zabbix/zabbix_agentd.d/nginx_monitor.sh $1
[root@web01 ~]# systemctl restart zabbix-agent.service
[root@m01 ~]# zabbix_get -s 10.0.1.7 -k nginx_status[accepts]
7
[root@web01 ~]# tail -1 /etc/php-fpm.d/www.conf
pm.status_path = /php_status
[root@web01 ~]# cat /etc/nginx/conf.d/status.conf
server {
listen 80;
server_name localhost;
location /nginx_status {
stub_status on;
access_log off;
}
location /php_status {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME html$fastcgi_script_name;
include fastcgi_params;
}
}
[root@web01 ~]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@web01 ~]# systemctl restart nginx.service php-fpm.service
[root@web01 ~]# curl 127.0.0.1/php_status
pool: www
process manager: dynamic
start time: 08/Aug/2019:22:31:27 +0800
start since: 37
accepted conn: 1
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@web01 ~]# cat /etc/zabbix/zabbix_agentd.d/fpm.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"
ZBX_REQ_DATA_URL="$2"
# Nginx defaults
NGINX_STATUS_DEFAULT_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.91"
ERROR_NO_ACCESS="-0.92"
ERROR_WRONG_PARAM="-0.93"
ERROR_DATA="-0.94" # either can not connect / bad host / bad port
# Handle host and port if non-default
if [ ! -z "$ZBX_REQ_DATA_URL" ]; then
URL="$ZBX_REQ_DATA_URL"
else
URL="$NGINX_STATUS_DEFAULT_URL"
fi
# 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
#
#RESULT=$(echo "$NGINX_STATS" | awk ‘print $0;match($0, "^‘"$ZBX_REQ_DATA"‘:[[:space:]]+(.*)", a) { print a[1] }‘)
#RESULT=$(echo "$NGINX_STATS" | grep "$ZBX_REQ_DATA" | awk -F : ‘{print $2}‘)
RESULT=$(echo "$NGINX_STATS" | awk -F : "{if(\$1==\"$ZBX_REQ_DATA\") print \$2}")
if [ $? -ne 0 -o -z "$RESULT" ]; then
echo $ERROR_WRONG_PARAM
exit 1
fi
echo $RESULT
exit 0
[root@web01 ~]# bash /etc/zabbix/zabbix_agentd.d/fpm.sh "total processes" http://127.0.0.1/php_status
5
[root@web01 ~]# cat /etc/zabbix/zabbix_agentd.d/fpm.conf
UserParameter=php-fpm[*],/etc/zabbix/zabbix_agentd.d/fpm.sh "$1" "$2"
[root@web01 ~]# systemctl restart zabbix-agent.service
[root@m01 ~]# zabbix_get -s 10.0.1.7 -k php-fpm["total processes",http://127.0.0.1/php_status]
5
导入之后需要修改一下模版里的宏配置
需求,监控页面状态码
故障现象:
提示zabbix-server is not running
报错日志:
34983:20190807:202215.171 database is down: reconnecting in 10 seconds
34983:20190807:202225.172 [Z3001] connection to database ‘zabbix‘ failed: [1045] Access denied for user ‘zabbix‘@‘localhost‘ (using password: NO)
故障原因:
zabbix-server的配置文件里配有配置数据库密码
故障解决:
添加正确的数据库账号密码信息
[root@m01 ~]# grep "^DB" /etc/zabbix/zabbix_server.conf
DBHost=localhost
DBName=zabbix
DBUser=zabbix
DBPassword=zabbix
故障现象:微信报警失败
报错日志:
[root@m01 ~]# tail -f /var/log/zabbix/zabbix_server.log
Problem name: TIME_WAIT过多
Host: web01
Severity: Average
Original problem ID: 51
‘": Traceback (most recent call last):
File "/usr/lib/zabbix/alertscripts/weixin.py", line 7, in <module>
import requests
ImportError: No module named requests
问题原因:
缺少模块 requests
问题解决:
安装缺失的依赖包
[root@m01 ~]# yum install python-pip
[root@m01 ~]# pip install --upgrade pip
[root@m01 ~]# pip install requests
故障现象:
在server端使用zabbix_get命令测试键值命令时提示警告
[root@m01 ~]# zabbix_get -s 10.0.1.7 -k ESTABLISHED
(Not all processes could be identified, non-owned process info
will not be shown, you would have to be root to see it all.)
2
问题原因:
zabbix_agent是以普通用户zabbix运行的,而普通用户执行netstat -antp时会有警告,网上查找发现只要不是用p参数就可以以普通用户运行
解决方案:
监控脚本里的命令修改为netstat -ant
[root@m01 /data/soft]# wget https://dl.grafana.com/oss/release/grafana-6.3.2-1.x86_64.rpm
[root@m01 /data/soft]# yum localinstall grafana-6.3.2-1.x86_64.rpm -y
[root@m01 /data/soft]# systemctl start grafana-server.service
[root@m01 /data/soft]# systemctl enable grafana-server.service
访问grafana:http://10.0.0.61:3000
账号密码:admin admin
[root@m01 ~]# grafana-cli plugins list-remote|grep zabbix
id: alexanderzobnin-zabbix-app version: 3.10.4
[root@m01 ~]# grafana-cli plugins install alexanderzobnin-zabbix-app
[root@m01 ~]# systemctl restart grafana-server.service
网页操作-激活zabbix插件
网页操作-添加zabbix数据源
网页操作-导入模版
1.安装插件
在线安装方式
grafana-cli plugins install grafana-piechart-panel
离线安装方式
wget -nv https://grafana.com/api/plugins/grafana-piechart-panel/versions/latest/download -O /tmp/grafana-piechart-panel.zip
unzip -q /tmp/grafana-piechart-panel.zip -d /tmp
mv /tmp/grafana-piechart-panel-* /var/lib/grafana/plugins/grafana-piechart-panel
service grafana-server restart
2.配置图形
参考强哥的博客
https://www.qstack.com.cn/archives/213.html
percona需要php环境
[root@m01 /data/soft]# yum install php php-mysql -y
注意,安装完成后会有提示模版的路径位置
[root@m01 ~]# cd /data/soft/
[root@m01 /data/soft]# wget https://www.percona.com/downloads/percona-monitoring-plugins/percona-monitoring-plugins-1.1.8/binary/redhat/7/x86_64/percona-zabbix-templates-1.1.8-1.noarch.rpm
[root@m01 /data/soft]# rpm -ivh percona-zabbix-templates-1.1.8-1.noarch.rpm
警告:percona-zabbix-templates-1.1.8-1.noarch.rpm: 头V4 DSA/SHA1 Signature, 密钥 ID cd2efd2a: NOKEY
准备中... ################################# [100%]
正在升级/安装...
1:percona-zabbix-templates-1.1.8-1 ################################# [100%]
Scripts are installed to /var/lib/zabbix/percona/scripts
Templates are installed to /var/lib/zabbix/percona/templates
进入安装目录会发现有2个目录,一个是脚本目录,一个是模版目录
[root@m01 ~]# cd /var/lib/zabbix/percona/
[root@m01 /var/lib/zabbix/percona]# tree
.
├── scripts
│ ├── get_mysql_stats_wrapper.sh
│ └── ss_get_mysql_stats.php
└── templates
├── userparameter_percona_mysql.conf
└── zabbix_agent_template_percona_mysql_server_ht_2.0.9-sver1.1.8.xml
其中脚本目录里有2个脚本,用来获取数据库信息
[root@m01 /var/lib/zabbix/percona]# cd scripts/
[root@m01 /var/lib/zabbix/percona/scripts]# ls
get_mysql_stats_wrapper.sh ss_get_mysql_stats.php
修改get_mysql_stats_wrapper数据库登陆信息
第19行添加mysql账号密码
[root@m01 v]# sed -n ‘19p‘ get_mysql_stats_wrapper.sh
RES=`HOME=~zabbix mysql -uroot -p123456 -e ‘SHOW SLAVE STATUS\G‘ | egrep ‘(Slave_IO_Running|Slave_SQL_Running):‘ | awk -F: ‘{print $2}‘ | tr ‘\n‘
[root@m01 /var/lib/zabbix/percona/scripts]# sed -n ‘30,31p‘ ss_get_mysql_stats.php
$mysql_user = ‘root‘;
$mysql_pass = ‘123456‘;
[root@m01 ~]# cd /var/lib/zabbix/percona/templates/
[root@m01 /var/lib/zabbix/percona/templates]# cp userparameter_percona_mysql.conf /etc/zabbix/zabbix_agentd.d/
[root@m01 /var/lib/zabbix/percona/templates]# cd /etc/zabbix/zabbix_agentd.d/
[root@m01 /etc/zabbix/zabbix_agentd.d]# ls
userparameter_mysql.conf userparameter_percona_mysql.conf
[root@m01 ~]# systemctl restart zabbix-agent
[root@m01 ~]# zabbix_get -s 10.0.1.61 -k MySQL.Sort-scan
16
官方自带的模版有点问题,需要先装在2.x版本然后导出来,这里使用网友已经修改好的模版上传
http://pan.baidu.com/s/1pL1wDYj
查看监控发现没有数据显示不支持类型
查看zabbix-server发现因为tmp的文件没有权限,因为刚才手动执行了脚本,所以文件属性是root,将文件删除后由zabbix自己创建解决问题
报错日志如下:
2846:20190811:202708.785 item "Zabbix server:MySQL.State-init" became not supported: Value "rm: 无法删除"/tmp/localhost-mysql_cacti_stats.txt": 不允许的操作
0" of type "string" is not suitable for value type "Numeric (float)"
2843:20190811:202709.787 item "Zabbix server:MySQL.State-locked" became not supported: Value "rm: 无法删除"/tmp/localhost-mysql_cacti_stats.txt": 不允许的操作
0" of type "string" is not suitable for value type "Numeric (float)"
2844:20190811:202710.788 item "Zabbix server:MySQL.State-login" became not supported: Value "rm: 无法删除"/tmp/localhost-mysql_cacti_stats.txt": 不允许的操作
0" of type "string" is not suitable for value type "Numeric (float)"
web页面操作
修改zabbix-agent配置文件
[root@web02 ~]# cat /etc/zabbix/zabbix_agentd.conf
PidFile=/var/run/zabbix/zabbix_agentd.pid
LogFile=/var/log/zabbix/zabbix_agentd.log
LogFileSize=0
Server=10.0.1.61
ServerActive=10.0.1.61
Hostname=web02
Include=/etc/zabbix/zabbix_agentd.d/*.conf
web页面操作
刷新查看发现已经添加上了
默认为被动模式:100个监控项要100个来回,要的时候才返回
主动模式:100个监控项1个回合,将所需要的100个打包,然后一次发过去,发过去之后,客户端全部执行完再一次返回给服务端。
完全克隆原来被动模式的模版为主动模式
[root@web01 ~]# cat /etc/zabbix/zabbix_agentd.conf
PidFile=/var/run/zabbix/zabbix_agentd.pid
LogFile=/var/log/zabbix/zabbix_agentd.log
LogFileSize=0
Server=10.0.1.61
ServerActive=10.0.1.61
Hostname=web01
Include=/etc/zabbix/zabbix_agentd.d/*.conf
[root@web01 ~]# systemctl restart zabbix-agent.service
发现获取数据的时间是一样的
监控端口自动发现
系统自带的自动发现会显示红字,比如自带的磁盘分区发现规则
1.查看zabbbix所有的key过滤后展示
2.解析成json后的格式
3.过滤规则
实质上是从mount命令获取的分区名和类型
但是我们zabbix显示的并没有这么多
是因为做了正则表达式过滤
而正则表达式是在管理里面配置的
4.使用zabbix_get获取key
因为根据过滤规则,只发现了一个xfs的key,使用zabbix_get可以查看到这个key
[root@m01 ~]# zabbix_get -s 10.0.1.61 -k vfs.fs.size[{#FSNAME},free]
ZBX_NOTSUPPORTED: Cannot obtain filesystem information: [2] No such file or directory
[root@m01 ~]# zabbix_agentd -p|grep vfs.fs.size
vfs.fs.size[/,free] [u|15713636352]
[root@m01 ~]# zabbix_get -s 10.0.1.61 -k vfs.fs.size[/,free]
15713693696
1.查看网络自动发现规则
2.过滤规则
2.命令行过滤
[root@m01 ~]# zabbix_agentd -p|grep net.if.discovery
net.if.discovery [s|{"data":[{"{#IFNAME}":"tun0"},{"{#IFNAME}":"eth0"},{"{#IFNAME}":"eth1"},{"{#IFNAME}":"lo"}]}]
3.查看自动添加的监控项
我们会发现添加了四个监控项
2个eth0
2个eth1
4.查看key的值
[root@m01 ~]# zabbix_get -s 10.0.1.61 -k net.if.in[eth0]
2191453
[root@m01 ~]# zabbix_get -s 10.0.1.61 -k net.if.in[eth1]
7152
参考强哥博客
[https://www.qstack.com.cn/archives/108.html](https://www.qstack.com.cn/archives/108.html)
1.复制并修改数据库配置文件
[root@m01 ~]# cp /etc/my.cnf /etc/my3307.cnf
[root@m01 ~]# vim /etc/my3307.cnf
[root@m01 ~]# cat /etc/my3307.cnf
[mysqld]
datadir=/data/3307/
socket=/data/3307/mysql.sock
port=3307
user=mysql
symbolic-links=0
[mysqld_safe]
log-error=/data/3307/mysqld.log
pid-file=/data/3307/mysqld.pid
[root@m01 ~]# cp /etc/my3307.cnf /etc/my3308.cnf
[root@m01 ~]# sed -i ‘s#3307#3308#g‘ /etc/my3308.cnf
2.创建数据目录并初始化
[root@m01 ~]# mkdir /data/{3307,3308}
[root@m01 ~]# chown -R mysql:mysql /data/330*
[root@m01 ~]# mysql_install_db --user=mysql --defaults-file=/etc/my3307.cnf
[root@m01 ~]# mysql_install_db --user=mysql --defaults-file=/etc/my3308.cnf
3.启动多实例
[root@m01 ~]# mysqld_safe --defaults-file=/etc/my3307.cnf &
[root@m01 ~]# mysqld_safe --defaults-file=/etc/my3308.cnf &
4.检查端口
[root@m01 ~]# netstat -lntup|grep mysql
tcp 0 0 0.0.0.0:3306 0.0.0.0:* LISTEN 2042/mysqld
tcp 0 0 0.0.0.0:3307 0.0.0.0:* LISTEN 84790/mysqld
tcp 0 0 0.0.0.0:3308 0.0.0.0:* LISTEN 85439/mysqld
5.创建自动发现配置文件
[root@m01 ~]# vim /etc/zabbix/zabbix_agentd.d/mysql_discovery.conf
[root@m01 ~]# cat /etc/zabbix/zabbix_agentd.d/mysql_discovery.conf
UserParameter=mysql.discovery,/bin/bash /server/scripts/mysql_discovery.sh
6.创建自动发现多实例脚本
[root@m01 ~]# cat /server/scripts/mysql_discovery.sh
#!/bin/bash
#mysql low-level discovery
res=$(netstat -lntp|awk -F "[ :\t]+" ‘/mysqld/{print$5}‘)
port=($res)
printf ‘{‘
printf ‘"data":[‘
for key in ${!port[@]}
do
if [[ "${#port[@]}" -gt 1 && "${key}" -ne "$((${#port[@]}-1))" ]];then
printf ‘{‘
printf "\"{#MYSQLPORT}\":\"${port[${key}]}\"},"
else [[ "${key}" -eq "((${#port[@]}-1))" ]]
printf ‘{‘
printf "\"{#MYSQLPORT}\":\"${port[${key}]}\"}"
fi
done
printf ‘]‘
printf ‘}\n‘
7.测试自动发现脚本
[root@m01 ~]# bash /server/scripts/mysql_discovery.sh
{"data":[{"{#MYSQLPORT}":"3306"},{"{#MYSQLPORT}":"3307"},{"{#MYSQLPORT}":"3308"}]}
8.重启zabbix-agent
[root@m01 ~]# systemctl restart zabbix-agent.service
9.zabbix_get测试取key
[root@m01 ~]# zabbix_get -s 10.0.1.61 -k mysql.discovery
(Not all processes could be identified, non-owned process info
will not be shown, you would have to be root to see it all.)
{"data":[]}
这时我们发现取不出来并提示了个错误
原因是zabbix用户不能使用netstat的-p参数
解决方法为给netstat命令添加s权限
[root@m01 ~]# which netstat
/usr/bin/netstat
[root@m01 ~]# chmod u+s /usr/bin/netstat
然后再次测试就发现可以取到值了
[root@m01 ~]# zabbix_get -s 10.0.1.61 -k mysql.discovery
{"data":[{"{#MYSQLPORT}":"3306"},{"{#MYSQLPORT}":"3307"},{"{#MYSQLPORT}":"3308"}]}
10.web页面创建自动发现规则模版
11.模仿zabbix自带的mysql监控配置修改监控项
[root@m01 ~]# cat /etc/zabbix/zabbix_agentd.d/userparameter_mysql.conf
UserParameter=mysql.status[*],echo "show global status where Variable_name=‘$1‘;" | HOME=/var/lib/zabbix mysql -uroot -p123456 -P $2 -N | awk ‘{print $$2}‘
[root@m01 ~]# systemctl restart zabbix-agent.service
12.测试访问监控项
[root@m01 ~]# zabbix_get -s 10.0.1.61 -k mysql.status[Uptime,3307]
23202
[root@m01 ~]# zabbix_get -s 10.0.1.61 -k mysql.status[Uptime,3308]
23204
13.web页面添加监控项原型
12.web页面设置主机关联模版
13.查看是否已经自动添加成功
zabbix监控主机和监控项较少的时候,不需要优化
数据库 200台主机 * 200个监控项 = 40000监控项/30秒 = 1333次写入/每秒
写多 读少
1.mariadb 5.5 innodb 升级到mysql5.7 tokudb
2.去掉无用监控项,增加监控项的取值间隔,减少历史数据的保存周期
3.被动模式改为主动模式
4.针对zabbix-server进程数量调优
5.针对zabbix-server缓存调优,谁的剩余内存少,就加大他的缓存
TokuDB性能比InnoDB要好
实施步骤:
1.找一台机器安装好mysql5.7
2.将mariadb的数据导出,然后替换sql文件里的存储引擎为TokuDB
3.将替换之后的数据导入到mysql5.7
4.停掉mariadb
5.检查测试
可以人为制造进程繁忙,把自动发现调整IP范围为1-254
这个时候观察会发现自动发现进程变得繁忙了
修改进程数
[root@zabbix-11 ~]# grep "^StartDiscoverers" /etc/zabbix/zabbix_server.conf
StartDiscoverers=10
[root@zabbix-11 ~]# systemctl restart zabbix-server.service
调整之后发现进程不这么繁忙了
调整配置文件
[root@zabbix-11 ~]# grep "^Cache" /etc/zabbix/zabbix_server.conf
CacheSize=128M
思路:
2台zabbix-server使用keepavied做高可用
数据库做主从复制
keepalived两端都做backup角色,设置不抢占VIP
keepalived设置如果发生改变就将自身的从库数据库修改为主库设置
然后另一台修复上线后,手动介入重新做主从同步,变成从库
原文:https://www.cnblogs.com/alaska/p/12655995.html