组件 | 说明 |
---|---|
Zabbix Server | 负责接收Agent发送报告信息的核心组件,所有的配置、数据统计、数据操作都由它组织进行 |
Database storage | 负责存储所有的配置信息以及收集的数据 |
Web interface | 是zabbix的GUI接口,通常情况下与Zabbix Server运行在同一台主机上 |
Pxory | 属于可选组件,常用于分布式监控环境中,代理Server收集部分数据,然后转发到 Server,可以减轻Server的压力 |
Agent | 部署在被监控的主机(客户端)上,负责收集被监控端主机的数据,如CPU、内存、 数据库等数据,然后发送到Server端或Proxy端 |
进程 | 说明 |
---|---|
zabbix_gentd | 客户端守护进程,此进程用于收集被监控端的数据,如CPU负载、内存、硬盘使用情况等 |
zabbix_get | zabbix工具是单独使用的命令。zabbix_get是在server或者proxy端执行获取远程客户端信息的命令,主要用于排错 |
zabbix_sender | 用于发送数据给sen/印或者proxy,通常用于耗时比较长的检查 |
zabbix_server | zabbix 服务端守护进程。zabbix_agentd、zabbix_getv zabbix_sender、zabbix_proxy、 zabbixjava_gateway的数据最终都是提交到servero数据并不都是主动提交给zabbix_server 的,也有被动提交数据的,即server主动去获取数据 |
zabbix_proxy | zabbix代理守护进程。功能类似server,唯一不同的是它只是一个中转站,需要把收集到的数据主动(或被动)地提交到server上 |
zabbix_java_gateway | 在zabbix2.0之后引入的一个功能。顾名思义:Java网关,类似agentd,但是只用于Java方面。需要特别注意的是,它只能主动去获取数据,而不能被动获取数据。 它的数据最终会提交到server或者proxy |
架构 | 说明 |
---|---|
server-client架构 | 是zabbix的最简单的架构,监控机和被监控机之间不经过任何代理,直接在 zabbix server和zabbix agentd之间进行数据交互,适用于网络比较简单,设备比较少的监控环境 |
master-node-client架构 | 是Zabbix最复杂的监控架构,适用于跨网络、跨机房、设备较多的大型环境。每个node同时也是一个server端,node下面可以接proxy,也可以直接接cliento node有自已 的配置文件和数据库,其要做的是将配置信息和监控数据向master同步。当master发生故障或损坏, node可以保证架构的完整性 |
master-proxy-client架构 | 是server、client之间沟通的一个桥梁,proxy本身没有前端,而且其本身并不存放数据, 只是将agentd发来的数据暂时存放,而后再提交给servero该架构经常是和master-node-client架构 做比较的架构,一般适用于跨机房、跨网络的中型网络架构的监控 |
[root@nginx-server ~]# rpm -Uvh https://repo.zabbix.com/zabbix/4.4/rhel/7/x86_64/zabbix-release-4.4-1.el7.noarch.rpm //官方源
[root@nginx-server ~]# vim /etc/yum.repos.d/zabbix.repo //配置阿里源
[zabbix]
name=Zabbix Official Repository - $basearch
#baseurl=http://repo.zabbix.com/zabbix/4.4/rhel/7/$basearch/
baseurl=https://mirrors.aliyun.com/zabbix/zabbix/4.4/rhel/7/$basearch/
enabled=1
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-ZABBIX-A14FE591
[zabbix-debuginfo]
name=Zabbix Official Repository debuginfo - $basearch
#baseurl=http://repo.zabbix.com/zabbix/4.4/rhel/7/$basearch/debuginfo/
baseurl=https://mirrors.aliyun.com/zabbix/zabbix/4.4/rhel/7/$basearch/
enabled=0
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-ZABBIX-A14FE591
gpgcheck=1
[zabbix-non-supported]
name=Zabbix Official Repository non-supported - $basearch
#baseurl=http://repo.zabbix.com/non-supported/rhel/7/$basearch/
baseurl=https://mirrors.aliyun.com/zabbix/non-supported/rhel/7/$basearch/
enabled=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-ZABBIX
gpgcheck=1
[root@nginx-server ~]# yum clean all
[root@nginx-server ~]# yum -y install zabbix-server-mysql zabbix-web-mysql zabbix-agent mariadb-server
[root@localhost ~]# yum -y install mariadb-server
[root@nginx-server ~]# systemctl start mariadb
[root@nginx-server ~]# systemctl enable mariadb
[root@nginx-server ~]# vim /etc/my.cnf
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
user=mysql
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
character-set-server=utf8 #设置字符集为UTF-8
innodb_file_per_table=1 #让InnoDB的每个表文件单独存储
[mysqld_safe]
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid
[root@nginx-server ~]# systemctl restart mariadb
[root@nginx-server ~]# mysql_secure_installation
[root@nginx-server ~]# mysql -uroot -p123456
MariaDB [(none)]> create database zabbix character set utf8 collate utf8_bin;
Query OK, 1 row affected (0.00 sec)
MariaDB [(none)]> grant all privileges on zabbix.* to zabbix@'localhost' identified by 'zabbix';
Query OK, 0 rows affected (0.00 sec)
MariaDB [(none)]> grant all privileges on zabbix.* to zabbix@'127.0.0.1' identified by 'zabbix';
Query OK, 0 rows affected (0.01 sec)
MariaDB [(none)]> flush privileges;
[root@nginx-server ~]# zcat /usr/share/doc/zabbix-server-mysql-4.4.10/create.sql.gz | mysql -uzabbix -pzabbix zabbix //导入数据库SQL脚本
[root@nginx-server ~]# cp /etc/zabbix/zabbix_server.conf /etc/zabbix/zabbix_server.conf.bak
[root@nginx-server ~]# vim /etc/zabbix/zabbix_server.conf
DBHost=localhost
DBName=zabbix
DBUser=zabbix
DBPassword=zabbix
[root@localhost ~]# cp /etc/zabbix/zabbix_agentd.conf /etc/zabbix/zabbix_agentd.conf.bak
[root@localhost ~]# vim /etc/zabbix/zabbix_agentd.conf
PidFile=/var/run/zabbix/zabbix_agentd.pid #pid文件路径
LogFile=/var/log/zabbix/zabbix_agentd.log #日志文件路径
LogFileSize=0 #日志切割大小,0表示不切割
Server=127.0.0.1,192.168.1.10 #被动模式,Zabbix-Server的IP地址
ServerActive=127.0.0.1,192.168.1.10 #主动模式,Zabbix-Server的IP地址
Hostname=Zabbix server #本机的Hostname,使用主动模式则必须配置
Include=/etc/zabbix/zabbix_agentd.d/ #包含的子配置文件
UnsafeUserParameters=1 #启用特殊字符,用于自定义监控
[root@nginx-server ~]# systemctl start zabbix-server
[root@nginx-server ~]# systemctl enable zabbix-server
[root@nginx-server ~]# vim /etc/httpd/conf.d/zabbix.conf
php_value date.timezone Asia/Shanghai
[root@nginx-server ~]# systemctl restart httpd
[root@nginx-server ~]# systemctl enable httpd
[root@nginx-server ~]# yum -y install wqy-microhei-fonts
[root@nginx-server ~]# cp /usr/share/fonts/wqy-microhei/wqy-microhei.ttc /usr/share/zabbix/assets/fonts/graphfont.ttf
[root@nginx-agent ~]# rpm -ivh https://mirrors.aliyun.com/zabbix/zabbix/4.4/rhel/7/x86_64/zabbix-agent-4.4.10-1.el7.x86_64.rpm
[root@nginx-agent ~]# vim /etc/zabbix/zabbix_agentd.conf
Server=192.168.1.10
[root@nginx-agent ~]# systemctl start zabbix-agent
[root@nginx-agent ~]# systemctl enable zabbix-agent
打开Zabbix前端安装向导
浏览器打开地址:http://192.168.1.10/zabbix/setup.php
在登录界面输入默认的用户名Admin,密码zabbix即可登录到Zabbix服务器
创建主机群组
添加组名"网络设备"
创建主机
添加主机名称为"R1",在群组中选择"网络设备",移除默认的"agent代理程序的接口 ",添加"SNMP接口 ",并在地址栏中添加交换机的管理地址"192.168.10.110”
添加模板
将宏{$SNMP_COMMUNITY)的可用值修改成与交换机上配置的相同,为"centos"
创建图形
在图形配置界面,"监控项"栏中单击"添加"按钮
[root@nginx-agent ~]# rpm -ivh https://mirrors.aliyun.com/zabbix/zabbix/4.4/rhel/7/x86_64/zabbix-agent-4.4.10-1.el7.x86_64.rpm
[root@nginx-agent ~]# vim /etc/zabbix/zabbix_agentd.conf
Server=192.168.1.10 //zabbix主机仍为127.0.0.1
ServerActive=192.168.1.10 //被监控主机IP
Hostname=web1
[root@nginx-agent ~]# systemctl start zabbix-agent
[root@nginx-agent ~]# systemctl enable zabbix-agent
测试网络连接情况
[root@server ~]# yum -y install zabbix-get
[root@server ~]# zabbix_get -s 192.168.1.20 -k agent.hostname
web1
下载 Zabbix-Agent 的 Windows 版本,下载链接https://cdn.zabbix.com/zabbix/binaries/stable/4.4/4.4.10/zabbix_agent-4.4.10-windows-amd64-openssl.msi
建议Remote command默认关闭
安装完成后已自动启动
到Zabbix服务器上添加主机
选择模板
[root@nginx-agent ~]# vim /etc/zabbix/zabbix_agentd.d/check_httpd.sh
#!/bin/bash
result=`ps -ef | grep httpd | grep -v grep`
if [ -n "$result" ]
then
echo '1'
else
echo '0'
fi
[root@nginx-agent ~]# vim /etc/zabbix/zabbix_agentd.conf
UnsafeUserParameters=1 //用户自定义的脚本中可以包含特殊字符
设置自定义键值为"check_httpd", 要执行的命令为执行check_httpd. sh这个脚本
[root@nginx-agent ~]# vim /etc/zabbix/zabbix_agentd.d/userparameter_httpd.conf
UserParameter=check_httpd.sh /etc/zabbix/zabbix_scripts/check_httpd.sh
[root@nginx-agent ~]# systemctl restart zabbix-agent
[root@nginx-agent ~]# vim /etc/zabbix/zabbix_agentd.d/iostat.conf
UserParameter=iostat,iostat |awk '/^sda/{print $2}'
[root@nginx-agent ~]# systemctl restart zabbix-agent
[root@nginx-agent ~]# zabbix_agentd -p | grep iostat
iostat [t|15.99]
[root@mysql ~]# yum -y install mariadb-server
[root@mysql ~]# systemctl start mariadb
[root@mysql ~]# mysql_secure_installation
[root@mysql ~]# mysql -uroot -p123456
MariaDB [(none)]> create database zabbix character set utf8 collate utf8_bin;
Query OK, 1 row affected (0.00 sec)
MariaDB [(none)]> grant all on zabbix.* to zabbix@'%' identified by '123.com';
Query OK, 0 rows affected (0.00 sec)
MariaDB [(none)]> exit
Bye
[root@nginx-server ~]# mysqldump -uroot -p123456 --databases zabbix > `date +%F-%H`_zabbix.sql
[root@nginx-server ~]# cat 2020-07-04-18_zabbix.sql | mysql -h 192.168.1.30 -uzabbix -p123.com zabbix
[root@nginx-server ~]# vim /etc/zabbix/zabbix_server.conf
DBHost=192.168.1.30
DBName=zabbix
DBUser=zabbix
DBPassword=123.com
[root@nginx-server ~]# vim /etc/zabbix/web/zabbix.conf.php
$DB['TYPE'] = 'MYSQL';
$DB['SERVER'] = '192.168.1.30';
$DB['PORT'] = '0';
$DB['DATABASE'] = 'zabbix';
$DB['USER'] = 'zabbix';
$DB['PASSWORD'] = '123.com';
[root@nginx-server ~]# systemctl restart zabbix-server httpd
[root@nginx-server ~]# systemctl stop postfix
[root@nginx-server ~]# systemctl disable postfix
Removed symlink /etc/systemd/system/multi-user.target.wants/postfix.service.
[root@nginx-server ~]# yum -y install mailx
[root@nginx-server ~]# vim /etc/mail.rc
set from=[email protected]
set smtp=smtps://smtp.XX.com:465
set smtp-auth-user=[email protected]
set smtp-auth-password=123456
set smtp-auth=login
set ssl-verify=ignore
set nss-config-dir=/etc/pki/nssdb/
[root@nginx-server ~]# yum -y install dos2unix
[root@nginx-server ~]# cd /usr/lib/zabbix/alertscripts/
[root@nginx-server alertscripts]# vim email_notice.sh
#!/bin/bash
#export UTF-8
FILE=/tmp/mail.txt
echo "$3" > $FILE
dos2unix -k $FILE
/bin/mail -s "$2" $1 < $FILE
[root@nginx-server alertscripts]# chmod +x email_notice.sh
参数添加3个:{ALERT.SENDTO}、{ALERT.SUBJECT}、{ALERT.MESSAGE}”
Python脚本
[root@nginx-server ~]# vim /usr/local/zabbix/alertscripts/zabbix_sendijnail.py
#!/usr/bin/python
#_*_ coding:utf-8 _*_
import smtplib
from email.mime.text import MIMEText
from email.header import Header
from smtplib import SMTP_SSL
import sys
smtpaddr='smtp.163.com'
myemail='邮箱地址'
password='----------'
#f=open('/usr/local/zabbix/.passwd','r')
#password=f.readline().strip()
recvmail=sys.argv[1]
subject=sys.argv[2]
content=sys.argv[3]
msg=MIMEText('''%s'''%(content),"plain","utf-8")
msg["Subject"]=Header(subject,'utf-8').encode()
msg["From"]=myemail
msg["To"]=recvmail
try:
smtp=SMTP_SSL(smtpaddr)
smtp.login(myemail,password)
smtp.sendmail(myemail,recvmail.split(','),msg.as_string())
smtp.quit()
print("success")
except Exception as e:
print("fail:"+str(e))
Problem: {EVENT.NAME}故障{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@nginx-server ~]# cd /usr/lib/zabbix/alertscripts/
[root@nginx-server alertscripts]# vim weixin.py
#!/usr/bin/env python
#-*- coding: utf-8 -*-
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='微信企业号corpid'
appsecret='应用的Secret'
agentid=应用的id
#获取accesstoken
token_url='https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=' + corpid + '&corpsecret=' + appsecret
req=requests.get(token_url)
accesstoken=req.json()['access_token']
[root@nginx-server alertscripts]# chmod 777 weixin.py
[root@nginx-server ~]# cd /usr/lib/zabbix/alertscripts/
[root@nginx-server alertscripts]# dos2unix -k weixin.py
dos2unix: converting file weixin.py to Unix format ...
[root@nginx-server alertscripts]# touch /tmp/weixin.log
[root@nginx-server alertscripts]# chown zabbix:zabbix /tmp/weixin.log
[root@nginx-server ~]# /usr/local/zabbix/alertscripts/zabbix_dd.py
#!/usr/bin/python
# -*- coding: utf-8 -*-
#curl 'https://oapi.dingtalk.com/gettoken?corpid=xxx&corpsecret=xxx'
import json,urllib2,sys
appkey = 'xxx'
appsecret = 'xxx'
agentid = 'xxx'
touser = sys.argv[1]
content = sys.argv[2]
tockenurl = 'https://oapi.dingtalk.com/gettoken?corpid=' + appkey + "&corpsecret=" + appsecret
tockenresponse = urllib2.urlopen(tockenurl)
tockenresult = json.loads(tockenresponse.read().decode('utf-8'))
tocken = tockenresult['access_token']
sendurl = 'https://oapi.dingtalk.com/message/send?access_token=' + tocken
headers = {
'Content-Type':'application/json'
}
main_content = {
"touser": touser,
"toparty": "",
"agentid": agentid,
"msgtype": "text",
"text": {
"content": content
}
}
main_content = json.dumps(main_content)
req = urllib2.Request(sendurl,headers=headers)
response = urllib2.urlopen(req, main_content.encode('utf8'))
print(response.read().decode('utf-8'))
参数添加2个:{ALERT.SENDTO}、{ALERT.SUBJECT}
zabbix模板文件下载:
[root@zabbix-mysql ~]# yum -y install php php-mysql
[root@mysql ~]# mkdir -p /data/soft
[root@mysql ~]# cd /data/soft/
[root@mysql 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@mysql soft]# rpm -ivh percona-zabbix-templates-1.1.8-1.noarch.rpm
[root@mysql ~]# cd /var/lib/zabbix/percona/scripts/
[root@mysql scripts]# vim 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@mysql scripts]# vim ss_get_mysql_stats.php
$mysql_user = 'root';
$mysql_pass = '123456';
[root@zabbix-mysql scripts]# cd ../templates/
[root@zabbix-mysql templates]# cp userparameter_percona_mysql.conf /etc/zabbix/zabbix_agentd.d/
[root@zabbix-mysql templates]# systemctl restart zabbix-agent
[root@zabbix-nginx ~]# yum -y install epel-release
[root@zabbix-nginx ~]# yum -y install nginx
[root@zabbix-nginx ~]# vim /etc/nginx/nginx.conf
http {
server {
......
location /nginx_status {
stub_status on;
access_log off;
}
......
[root@zabbix-nginx ~]# rpm -ivh https://mirrors.aliyun.com/zabbix/zabbix/4.4/rhel/7/x86_64/zabbix-agent-4.4.10-1.el7.x86_64.rpm
Server=192.168.1.10
[root@zabbix-nginx ~]# systemctl start zabbix-agent
[root@zabbix-nginx ~]# cd /etc/zabbix/zabbix_agentd.d/
[root@zabbix-nginx ~]# vim 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@zabbix-nginx zabbix_agentd.d]# chmod +x nginx_monitor.sh
[root@zabbix-nginx zabbix_agentd.d]# vim nginx_status.conf
UserParameter=nginx_status[*],/bin/bash /etc/zabbix/zabbix_agentd.d/nginx_monitor.sh $1
[root@zabbix-nginx ~]# systemctl restart zabbix-agent
[root@zabbix-nginx ~]# touch /tmp/nginx_status.txt
[root@zabbix-nginx ~]# chown zabbix:zabbix /tmp/nginx_status.txt
[root@zabbix-nginx ~]# systemctl start nginx
zabbix导入zbx_nginx_templates模板文件
默认为被动模式:100个监控项要100个来回,要的时候才返回
主动模式:100个监控项1个回合,将所需要的100个打包,然后一次发过去,发过去之后,客户端全部执行完再一次返回给服务端。
更改为主动模式: