Zabbix搭建与主机监控
前言
Zabbix是企业级分布式开源监控解决方案。可以对应用服务,数据库,网络,操作系统等提供7x24小时的监控,在6.x版本开始也新增了Kubernetes监控功能;本文主要介绍如何用zabbix的实现主机监控;
安装zabbix-server
这里用docker compose的方式搭建一个zabbix监控;
# 拉取镜像
docker pull zabbix/zabbix-server-mysql:6.4.1-centos
docker pull zabbix/zabbix-web-nginx-mysql:6.4.1-centos
# 创建数据目录
mkdir -p zabbix/{data,alertscripts}
mkdir -p zabbix/mysql/{data,conf}
cd zabbix/
# 添加数据库配置
cat > mysql/conf/my.cnf <<eof
[client]
default-character-set = utf8
[mysqld]
character-set-server = utf8
collation-server = utf8_bin
default_storage_engine = InnoDB
port = 3306
datadir = /var/lib/mysql
max_connections=1000
expire_logs_days = 15
relay_log_purge = 1
slow_query_log_file = /var/log/mysql/slow.log
log-error = /var/log/mysql/error.log
log-bin = /var/log/mysql/mysqld-bin
default_authentication_plugin = mysql_native_password
socket = /tmp/mysql.sock
lower_case_table_names = 1
sql_mode = NO_ENGINE_SUBSTITUTION
default-time_zone='+8:00'
eof
# 授权文件
chmod 644 mysql/conf/my.cnf
# 添加compose
cat > docker-compose.yml <<eof
version: '3.9'
networks:
zbx:
driver: bridge
services:
zabbix-mysql:
image: mysql:8.0.30
hostname: mysql
container_name: mysql
volumes:
- /etc/localtime:/etc/localtime:ro
- ./mysql/conf/my.cnf:/etc/my.cnf
- ./mysql/data:/var/lib/mysql
restart: always
privileged: true
command: --character-set-server=utf8 --collation-server=utf8_bin
environment:
- MYSQL_ROOT_PASSWORD=Egfi9Jnn9BzJf3Q6
- MYSQL_DATABASE=zabbix
- MYSQL_USER=zabbix
- MYSQL_PASSWORD=zabbix
- TZ=Asia/Shanghai
- LANG=en_US.UTF-8
ports:
- 3306:3306
networks:
- zbx
zabbix-server:
image: zabbix/zabbix-server-mysql:6.4.1-centos
container_name: zabbix-server
volumes:
- /etc/localtime:/etc/localtime:ro
- ./data:/var/lib/zabbix/
- ./alertscripts:/usr/lib/zabbix/alertscripts:ro
restart: always
privileged: true
environment:
- DB_SERVER_HOST=zabbix-mysql
- DB_SERVER_PORT=3306
- MYSQL_ROOT_PASSWORD=Egfi9Jnn9BzJf3Q6
- MYSQL_DATABASE=zabbix
- MYSQL_USER=zabbix
- MYSQL_PASSWORD=zabbix
- ZBX_CACHESIZE=1G
- ZBX_HISTORYCACHESIZE=512M
- ZBX_HISTORYINDEXCACHESIZE=32M
- ZBX_TRENDCACHESIZE=256M
- ZBX_VALUECACHESIZE=256M
- ZBX_STARTPINGERS=64
- ZBX_IPMIPOLLERS=1
- ZBX_ENABLE_SNMP_TRAPS=true
- ZBX_STARTTRAPPERS=1
ports:
- 10051:10051
networks:
- zbx
zabbix-web:
image: zabbix/zabbix-web-nginx-mysql:6.4.1-centos
container_name: zabbix-web
volumes:
- /etc/localtime:/etc/localtime:ro
restart: always
privileged: true
environment:
- ZBX_SERVER_NAME=Zabbix 6.0
- ZBX_SERVER_HOST=zabbix-server
- ZBX_SERVER_PORT=10051
- DB_SERVER_HOST=zabbix-mysql
- DB_SERVER_PORT=3306
- MYSQL_ROOT_PASSWORD=Egfi9Jnn9BzJf3Q6
- MYSQL_DATABASE=zabbix
- MYSQL_USER=zabbix
- MYSQL_PASSWORD=zabbix
- PHP_TZ=Asia/Shanghai
ports:
- 80:8080
networks:
- zbx
eof
# 启动服务
docker compose up zabbix-mysql -d && sleep 5
docker compose up -d
启动成功后,登陆页面,默认账号是 Admin / zabbix ;
安装zabbix-agent
zabbix-agent是采集主机指标的客户端,这里使用二进制包的安装方式,多台主机的情况下建议使用ansible去批量安装;
# 获取安装包: https://www.zabbix.com/cn/download
wget -c https://cdn.zabbix.com/zabbix/binaries/stable/6.4/6.4.1/zabbix_agent-6.4.1-linux-3.0-amd64-static.tar.gz
mkdir -p /usr/local/zabbix
tar xf zabbix_agent-6.4.1-linux-3.0-amd64-static.tar.gz -C /usr/local/zabbix
# 添加用户
useradd -s /sbin/nologin -M zabbix
chown zabbix.root -R /usr/local/zabbix
# 添加配置文件
rm -rf /usr/local/zabbix/conf/zabbix_agentd/*
cat > /usr/local/zabbix/conf/zabbix_agentd.conf <<eof
PidFile=/usr/local/zabbix/logs/zabbix_agentd.pid
LogFile=/usr/local/zabbix/logs/zabbix_agentd.log
LogFileSize=100
ListenPort=10050
StartAgents=2 # 启动的采集线程数
Server=172.16.110.169 # zabbix-server的地址
ServerActive=172.16.110.169 # zabbix-server的地址
HostMetadata=basic # 定义主机元数据,在做自动注册时会用到
Include=/usr/local/zabbix/conf/zabbix_agentd/*.conf
eof
添加system脚本管理agent服务;
# 创建脚本
cat > /usr/lib/systemd/system/zabbix-agent.service <<eof
[Unit]
Description=Zabbix Agent
After=syslog.target
After=network.target
[Service]
Type=simple
Restart=on-failure
PIDFile=/usr/local/zabbix/logs/zabbix_agentd.pid
KillMode=control-group
ExecStart=/usr/local/zabbix/sbin/zabbix_agentd -c /usr/local/zabbix/conf/zabbix_agentd.conf
ExecStop=/bin/kill -SIGTERM $MAINPID
RestartSec=10s
User=zabbix
Group=zabbix
[Install]
WantedBy=multi-user.targe
eof
# 启动服务
systemctl daemon-reload
systemctl enable zabbix-agent --now
自动注册
自动注册是由zabbix-agent端来自动的向服务端主动向服务端注册添加,这种方式能够减少服务端的资源消耗;
添加匹配条件,注册动作;
添加成功后,agent主机会自动向
制作仪表盘
zabbix的仪表盘默认并没有主机指标的展示,这里自己制作一个展示主机的指标图表;
添加一个前排主机面板,把要展示的主机组,指标加上;
效果展示;
集成ldap(可选)
zabbix默认支持ldap的认证集成,为了账号的统一可以配置一下,参考如下;
选择认证类型为ldap方式;
创建用户;(zabbix要求用户需要属于某个用户组和角色才能正常使用) ;
end~
(文章源于网络,版权归原作者所有,侵删)
微信扫码关注该文公众号作者
戳这里提交新闻线索和高质量文章给我们。
来源: qq
点击查看作者最近其他文章