zabbix
2019-12-24 本文已影响0人
吃可爱长大鸭
1.为什么要使用监控
1.对系统不间断实时监控
2.实时反馈系统当前状态
3.保证服务可靠性安全性
4.保证业务持续稳定运行
2.如何进行监控,比如我们需要监控磁盘的使用率
1.如何查看磁盘使用率 df -h
2.监控磁盘的那些指标 block、 inode
3.如何获取具体的信息 df -h|awk
4.获取的数值到达多少报警 80%
3.流行的监控工具
1.Zabbix
2.Lepus(天兔)数据库监控系统
3.Open-Falcon 小米
4.Prometheus(普罗米修斯, Docker、 K8s)
4.如果去到一家新公司,如何入手监控
1.硬件监控 路由器、交换机、防火墙
2.系统监控 CPU、内存、磁盘、网络、进程、 TCP
3.服务监控 nginx、 php、 tomcat、 redis、 memcache、 mysql
4.WEB 监控 请求时间、响应时间、加载时间、
5.日志监控 ELk(收集、存储、分析、展示) 日志易
6.安全监控 Firewalld、 WAF(Nginx+lua)、安全宝、牛盾云、安全狗
7.网络监控 smokeping 多机房
8.业务监控 活动引入多少流量、产生多少注册量、带来多大价值
第一章zabbix安装
1.配置zabbix仓库
[root@zabbix ~]# rpm -ivh https://mirrors.tuna.tsinghua.edu.cn/zabbix/zabbix/4.0/rhel/7/x86_64/zabbix-release-4.0-1.el7.noarch.rpm
2.安装 Zabbix 程序包,、 Zabbix-agent
[root@zabbix ~]# yum install -y zabbix-server-mysql zabbix-web-mysql zabbix-agent
3.安装MySQL
mysql部署安装
4.启动MySQL加入开机自启
[root@db01 ~]# systemctl start mysqld && systemctl enable mysqld
5.创建 Zabbix 数据库以及用户
[root@db01 ~]# mysqladmin password 123456
[root@db01 ~]# mysql -uroot -p123456
mysql> create database zabbix character set utf8 collate utf8_bin;
mysql> grant all privileges on zabbix.* to zabbix@localhost identified by 'zabbix';
mysql> flush privileges;
6.导入zabbix数据到数据库
[root@zabbix ~]# scp -r /usr/share/doc/zabbix-server-mysql-4.0.11/create.sql.gz 10.0.0.51:/
[root@db01 ~]# zcat /usr/share/doc/zabbix-server-mysql*/create.sql.gz | mysql -uzabbix -pzabbix zabbix
7.编辑/etc/zabbix/zabbix_server.conf 文件,修改数据库配置
[root@zabbix ~]# vim /etc/zabbix/zabbix_server.conf
...............
DBHost=10.0.0.51
DBName=zabbix
DBUser=zabbix
DBPassword=zabbix
...............
8.启动 Zabbix 服务进程,并加入开机自启
[root@zabbix ~]# systemctl start zabbix-server.service
[root@zabbix ~]# systemctl enable zabbix-server.service
9.配置 Apache 的配置文件/etc/httpd/conf.d/zabbix.conf,修改时区
[root@zabbix ~]# vim /etc/httpd/conf.d/zabbix.conf
...............
php_value date.timezone Asia/Shanghai
...............
10.启动httpd并加入开机自启
[root@zabbix ~]# systemctl start httpd && systemctl enable httpd
web操作步骤
1.浏览器打开地址:http://10.0.0.71/zabbix/setup.php
2.登陆zabbix Username:Admin Password:zabbix

3.调整字符集背景颜色

4.修复中文乱码
解决方法:
[root@zabbix ~]# yum install wqy-microhei-fonts -y
[root@zabbix ~]# cp /usr/share/fonts/wqy-microhei/wqy-microhei.ttc /usr/share/zabbix/assets/fonts/graphfont.ttf

第二章zabbix监控主机
1.安装zabbix-agent
rpm -ivh https://mirrors.tuna.tsinghua.edu.cn/zabbix/zabbix/4.0/rhel/7/x86_64/zabbix-release-4.0-1.el7.noarch.rpm
2.配置zabbix-agent
[root@web01 ~]# vim /etc/zabbix/zabbix_agentd.conf
PidFile=/var/run/zabbix/zabbix_agentd.pid
LogFile=/var/log/zabbix/zabbix_agentd.log
LogFileSize=0
Server=10.0.0.71
ServerActive=10.0.0.51
HostMetadata=web
Include=/etc/zabbix/zabbix_agentd.d/*.conf
3.启动zabbix-agent并检查
[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
4.zabbix-web页面添加主机




第三章自定义监控tcp11种状态
1.配置监控值
[root@web01 ~]# vim /etc/zabbix/zabbix_agentd.d/tcp.conf
UserParameter=TCP_STATUS[*],netstat -ant|grep -c $1
#重启客户段
[root@web01 ~]# systemctl restart zabbix-agent.service
2.server端进行测试
[root@zabbix ~]# 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@zabbix ~]# yum install zabbix-get.x86_64 -y
[root@zabbix ~]# zabbix_get -s 10.0.1.7 -k tcp_state[TIME_WAIT]
3
[root@zabbix ~]# zabbix_get -s 10.0.1.7 -k tcp_state[LISTEN]
7
3.web端添加


这里监控11种状态太多为了快速监控可以选择克隆跟上面操作一样只需要克隆改下键值和名称。
4.创建图形

5.触发器




第四章 邮件报警
1.定义发件人



2.定义收件人


3.自定义报警内容过
发送警告
默认标题:故障{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}

恢复报警
告警主机:{HOSTNAME1}
告警时间:{EVENT.DATE} {EVENT.TIME}
告警等级:{TRIGGER.SEVERITY}
告警信息: {TRIGGER.NAME}
告警项目:{TRIGGER.KEY1}
问题详情:{ITEM.NAME}:{ITEM.VALUE}
当前状态:{TRIGGER.STATUS}:{ITEM.VALUE1}
事件ID:{EVENT.ID}

第五章微信报警
1.查看配置文件里的脚本目录路径
[root@zabbix ~]# grep "^AlertScriptsPath" /etc/zabbix/zabbix_server.conf
AlertScriptsPath=/usr/lib/zabbix/alertscripts
2.将weixin.py放在zabbix特定目录
[root@zabbix /usr/lib/zabbix/alertscripts]# ll
总用量 4
-rwxr-xr-x 1 root root 1344 8月 7 21:58 weixin.py
3.配置发信人


4.配置收信人

5.登陆企业微信公众号添加账户
https://work.weixin.qq.com/wework_admin/loginpage_wx
1.登陆后在企业号上新建应用

2.接口调用测试
http://work.weixin.qq.com/api/devtools/devtool.php

这里的corpid为公司ID

Corpsecret就是刚才创建应用生成的Secrt,确认没问题填写进去然后下一步
如果没问题会显示200状态码

3.添加成员

4.关注公众号

5.查看自己账号

6.修改脚本里的信息
[root@m01 /usr/lib/zabbix/alertscripts]# cat weixin.py
..............
corpid='微信企业号corpid'
appsecret='应用的Secret'
agentid=应用的id
..............
7.发信测试
[root@zabbix /usr/lib/zabbix/alertscripts]# python weixin.py 你的账号 '你好骚' '我好喜欢'
8.微信查看
......
9.整个微信组接收消息
#!/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)
报错日志:
[root@zabbix ~]# 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@zabbix ~]# yum install python-pip
[root@zabbix ~]# pip install --upgrade pip
[root@zabbix ~]# pip install requests
第六章 自定义监控模板
1.创建模板
2.链接模板
第七章 自定义模版监控nginx状态
1.开启监控页面并访问测试
[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
2.准备nginx监控状态脚本
[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
3.编写zabbix监控配置文件
[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
4.使用zabbix_get取值
[root@zabbix ~]# zabbix_get -s 10.0.1.7 -k nginx_status[accepts]
7
4.导入模版
5.链接模版
6.链接模板
7.查看数据
故障1
故障现象:
提示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@zabbix ~]# grep "^DB" /etc/zabbix/zabbix_server.conf
DBHost=10.0.0.51
DBName=zabbix
DBUser=zabbix
DBPassword=zabbix
第八章 自定义模版监控php状态
1.开启监控页面
[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
2.访问测试
[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
3.准备访问脚本
[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
4.准备zabbix配置文件
[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
4.使用zabbix_get取值
[root@zabbix ~]# zabbix_get -s 10.0.1.7 -k php-fpm["total processes",http://127.0.0.1/php_status]
5
5.导入模版
导入之后需要修改一下模版里的宏配置

第九章 WEB监控



第十章 zabbix 自愈


添加远程执行命令
[root@web01 ~]# visudo
%zabbix ALL=(ALL) NOPASSWD: ALL
[root@web01 ~]# usermod -G wheel zabbix
[root@web01 ~]# vim /etc/zabbix/zabbix_agentd.conf
EnableRemoteCommands=1
[root@web01 ~]# systemctl restart zabbix-agent.service