1、搭建zabbix监控服务
server端:192.168.79.77
agent端:192.168.79.87
[root@localhost ~]# rpm -Uvh https://repo.zabbix.com/zabbix/4.2/rhel/7/x86_64/zabbix-release-4.2-2.el7.noarch.rpm #zabbix'仓库
[root@localhost ~]# yum clean all #清除yum缓存
[root@localhost ~]# yum -y install zabbix-server-mysql zabbix-web-mysql zabbix-agent #使用yum安装zabbix相关组件,因选用mysql数据库用作存储数据库,故使用zabbix-server-mysql与zabbix-web-mysql
[root@localhost ~]# rpm -ql zabbix-server-mysql
/etc/logrotate.d/zabbix-server #zabbix-server滚动日志相关设置
/etc/zabbix/zabbix_server.conf #zabbix-server主配置文件
/usr/lib/systemd/system/zabbix-server.service #UNITfile
/usr/lib/tmpfiles.d/zabbix-server.conf
/usr/lib/zabbix/alertscripts
/usr/lib/zabbix/externalscripts
/usr/sbin/zabbix_server_mysql
/usr/share/doc/zabbix-server-mysql-4.2.5
/usr/share/doc/zabbix-server-mysql-4.2.5/AUTHORS
/usr/share/doc/zabbix-server-mysql-4.2.5/COPYING
/usr/share/doc/zabbix-server-mysql-4.2.5/ChangeLog
/usr/share/doc/zabbix-server-mysql-4.2.5/NEWS
/usr/share/doc/zabbix-server-mysql-4.2.5/README
/usr/share/doc/zabbix-server-mysql-4.2.5/create.sql.gz #sql文件,用于创建zabbix数据库
/usr/share/man/man8/zabbix_server.8.gz
/var/log/zabbix
/var/run/zabbix
[root@localhost ~]# mysql -uroot -psigeling #需自行安装mysql或mariadb
mysql> create database zabbix character set utf8 collate utf8_bin; #创建数据库
mysql> grant all privileges on zabbix.* to zabbix@localhost identified by 'sigeling'; #设定zabbix用户的权限,此用户专用于访问zabbix库
mysql> quit;
[root@localhost ~]# zcat /usr/share/doc/zabbix-server-mysql*/create.sql.gz | mysql -uzabbix -p zabbix #创建zabbix数据库环境
[root@localhost ~]# vim /etc/zabbix/zabbix_server.conf #更改配置文件中数据库密码为之前设置的zabbix用户的密码
DBPassword=sigeling
[root@localhost ~]# vim /etc/httpd/conf.d/zabbix.conf #更改时区
php_value date.timezone Asia/Shanghai
[root@localhost ~]# systemctl restart zabbix-server zabbix-agent httpd
[root@localhost ~]# systemctl enable zabbix-server zabbix-agent httpd
[root@localhost ~]# ss -ntl
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 *:10050 *:* zabbix-agent
LISTEN 0 128 *:10051 *:* zabbix-server
LISTEN 0 50 *:3306 *:*
LISTEN 0 128 :::80 :::*
2、添加memory监控项,并出图
[root@localhost ~]# rpm -Uvh https://repo.zabbix.com/zabbix/4.2/rhel/7/x86_64/zabbix-release-4.2-2.el7.noarch.rpm
[root@localhost ~]# yum install zabbix-agent zabbix-sender -y
[root@localhost ~]# vim /etc/zabbix/zabbix_agentd.conf
##### Passive checks related
Server=192.168.79.77
ListenIP=0.0.0.0
# ListenPort=10050
StartAgents=3
##### Active checks related
ServerActive=192.168.79.77
Hostname=192.168.79.87
[root@localhost ~]# vim /etc/zabbix/zabbix_agentd.conf
### Option: UserParameter
# User-defined parameter to monitor. There can be several user-defined par
ameters.
# Format: UserParameter=<key>,<shell command>
# See 'zabbix_agentd' directory for examples.
#
# Mandatory: no
# Default:
# UserParameter=
UserParameter=system.memory.free,awk '/^MemFree/{print $2}' /proc/meminfo
[root@localhost ~]# systemctl restart zabbix-agent.service
3、实现报警
以监控httpd服务进程为例,server为192.168.79.77,agent为192.168.79.87。
[root@localhost ~]# yum -y install httpd
[root@localhost ~]# vim /var/www/html/index.html
<h1>test</h1>
[root@localhost ~]# systemctl start httpd
[root@localhost ~]# ss -ntl
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 *:10050 *:*
LISTEN 0 128 *:111 *:*
LISTEN 0 5 192.168.122.1:53 *:*
LISTEN 0 128 *:22 *:*
LISTEN 0 128 127.0.0.1:631 *:*
LISTEN 0 100 127.0.0.1:25 *:*
LISTEN 0 128 :::111 :::*
LISTEN 0 128 :::80 :::*
LISTEN 0 128 :::22 :::*
LISTEN 0 128 ::1:631 :::*
LISTEN 0 100 ::1:25 :::*
4、添加tomcat监控模版
5、搭建zabbix-proxy,并测试
zabbix proxy可以代替zabbix server收集agent端的数据,存入proxy的数据库中,之后将数据同步给server端,可以减少server端的压力。
使用场景:
监控远程区域设备
监控本地网络不稳定区域
当zabbix监控数量庞大的设备时,可以用来减轻server的压力
简化维护过程
zabbix分布式监控的好处:
减少连接数和端口暴露在公网上的风险
server --> proxy:同步配置
proxy --> server:同步数据
拓扑结构如下图所示:
zabbix-server在上文已配置完毕
#zabbix-proxy配置段
[root@localhost ~]# yum -y install mariadb-server
[root@localhost ~]# yum -y install zabbix-proxy-mysql zabbix-get zabbix-agent
[root@localhost ~]# systemctl start mariadb
[root@localhost ~]# mysql_secure_installation
[root@localhost ~]# mysql -uroot -psigeling
mysql> create database zabbix_proxy charset 'utf8';
mysql> grant all on zabbix_proxy.* to 'zbxproxy'@'192.168.%' identified by 'zbxpass';
mysql> flush privileges;
mysql> exit
[root@localhost ~]# zcat /usr/share/doc/zabbix-proxy-mysql-4.2.5/schema.sql.gz | mysql -uzbxproxy -p zabbix_proxy -h 192.168.79.97
[root@localhost ~]# vim /etc/zabbix/zabbix_proxy.conf
ProxyMode=0 #0为主动模式,1为被动模式,针对被监控主机来说,主动模式为被监控主机主动向server/proxy发送数据,被动模式为server/proxy隔一段时间后主动向被监控主机采集数据
Server=192.168.79.77 #当代理模式为主动模式时,此处的地址为获取配置并且同步数据的目标主机。当代理模式为被动模式时,仅有此处列出来的主机地址可被允许连接。
ServerPort=10051 #proxy监听的端口
Hostname=192.168.79.97 #此处的hostname大小写敏感,且必须是独一无二的,server端通过此处的名称来判断proxy主机是否合法
DBHost=192.168.79.97 #proxy专用的数据库主机地址
DBName=zabbix_proxy #proxy数据库的库名称
DBUser=zbxproxy #使用何用户去连接数据库
DBPassword=zbxpass #数据库用户密码
HeartbeatFrequency=60 #心跳探测周期,server端每隔一个心跳探测周期会探测proxy工作是否正常
ConfigFrequency=3600 #主动模式下zabbix_proxy从server端同步配置的周期
[root@localhost ~]# systemctl restart zabbix-proxy
#zabbix-agent配置段,此处与之前的配置无任何不同,仅仅是将之前指向server端的ip地址更改为proxy端的地址即可
[root@localhost ~]# vim /etc/zabbix/zabbix_agentd.conf
Server=192.168.79.97
ServerActive=192.168.79.97
[root@localhost ~]# systemctl restart zabbix-agent
[root@localhost ~]# zabbix_get -s 192.168.79.87 -k "net.if.in[ens33,bytes]" #在proxy端测试使用zabbix_get命令是否能够获取到数据
74807637
可在主机列表中看到刚刚添加的主机,前面为proxy节点的ip后面跟上agent端主机。
链接一个简单的OS Linux模板进行测试
可以看到功能正常
注意:
1.在server端添加的proxy名称要与proxy配置文件中hostname的值保持一致,ip对ip或主机名对主机名
2.通过proxy监控的主机,agent要授权proxy有监控权限;
server=proxy_server_ip
3.数据库用户授权连接要注意与配置文件中相同。最好保持proxy与server、agent为统一版本,此外若proxy可以获取到数据但同步不到server’上时,可以看看/var/log/zabbix/zabbix-proxy.log中的日志内容进行排错。
6、添加redis监控模版(自动发现的方式,一台主机,多个redis端口)
在agent端配置redis多实例,端口分别为6379和6380
[root@localhost ~]# yum -y install redis
[root@localhost ~]# mkdir /etc/redis
[root@localhost ~]# cp -a /etc/redis.conf /etc/redis/redis-6380.conf
[root@localhost ~]# cp -a /etc/redis.conf /etc/redis/redis-6379.conf
[root@localhost ~]# vim /etc/redis/redis-6380.conf #只更改下面几项可以达到实验效果,其余的根据情况进行修改i
bind 0.0.0.0 #允许远程访问
port 6380 #监听端口
pidfile /var/run/redis_6380.pid #pidfile
logfile /var/log/redis/redis_6380.log #日志文件
[root@localhost ~]# redis-server /etc/redis/redis6379.conf &
[root@localhost ~]# redis-server /etc/redis/redis6380.conf &
[root@localhost ~]# ss -ntl
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 *:10050 *:*
LISTEN 0 128 *:6379 *:*
LISTEN 0 128 *:6380 *:*
[root@localhost ~]# vim /etc/zabbix/zabbix_agentd.conf
UserParameter=redis.discovery,sudo python /data/zabbix/externalscripts/redis_port.py
UserParameter=redis[*],sudo /data/zabbix/externalscripts/redis_stats.sh $1 $2
[root@localhost ~]# vim /data/zabbix/externalscripts/redis_port.py
#!/usr/bin/python2.7
import os
import json
p=os.popen("""sudo netstat -anp|awk -F: '/redis-server/&&/LISTEN/{sub(/ .*/,"",$2);if($2)print $2}' """)
ports = []
for port in p.readlines():
r = os.path.basename(port.strip())
ports += [{'{#REDISPORT}':r}]
print json.dumps({'data':ports},sort_keys=True,indent=4,separators=(',',':'))
[root@localhost ~]# vim /data/zabbix/externalscripts/redis_stats.sh
#!/bin/bash
METRIC="$1"
PORT="${2:-6379}"
CACHE_FILE="/data/zabbix/externalscripts/redis_$PORT.cache"
redis-cli -p ${PORT} -a 'USLF93SUVwtSF$$#@W' info > ${CACHE_FILE} 2>/dev/null || exit 1
awk -F'[:,]+' '/'"${METRIC}"':/{gsub(/^[^0-9]+/,"",$2);print $2}' ${CACHE_FILE}
[root@localhost ~]# systemctl restart zabbix-agent.service
[root@localhost ~]# vim /etc/sudoers
zabbix ALL=(ALL) NOPASSWD: ALL
[root@localhost data]# zabbix_get -s 192.168.79.87 -k redis.discovery
{
"data":[
{
"{#REDISPORT}":"6379"
},
{
"{#REDISPORT}":"6380"
}
]
}
[root@localhost data]# zabbix_get -s 192.168.79.87 -k redis[used_memory,6380]
813544