一. 项目背景
监控异地局域网主机(主机内有物联5G卡 可以单方面向特定的云服务器传输信息)这里采用 zabbix 5xx系列 agent2 -6.2 版本 主动模式,即客户端向服务端注册。二. 部署服务端
2.1 安装docker
#01 安装docker
# step 1: 安装必要的一些系统工具
sudo yum install -y yum-utils device-mapper-persistent-data lvm2
# Step 2: 添加软件源信息
sudo yum-config-manager --add-repo https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
# Step 3
sudo sed -i 's+download.docker.com+mirrors.aliyun.com/docker-ce+' /etc/yum.repos.d/docker-ce.repo
# Step 4: 更新并安装Docker-CE
sudo yum makecache fast
sudo yum -y install docker-ce
# Step 4: 开启Docker服务
sudo service docker start
#02 设置镜像加速
sudo mkdir -p /etc/docker
sudo tee /etc/docker/daemon.json <<-'EOF'
{
"registry-mirrors": ["https://9akof579.mirror.aliyuncs.com"]
}
EOF
sudo systemctl daemon-reload
sudo systemctl restart docker
- 安装docker-compose
#01 下载docker-compose
curl -L https://get.daocloud.io/docker/compose/releases/download/1.29.1/docker-compose-`uname -s`-`uname -m` > /usr/local/bin/docker-compose
#02 给docker-compose目录授权 s
udo chmod +x /usr/local/bin/docker-compose
#03 查看一下version 显示有版本号那就说明安装成功了
docker-compose version
-
部署服务
#01 创建数据目录
mkdir -p /opt/zabbix
cd /opt/zabbix/
#02 docker-compose 文件
[root@centos7 zabbix]# cat docker-compose.yml
version: '3'
services:
zabbix-web-nginx-mysql:
image: zabbix/zabbix-web-nginx-mysql:centos-5.2-latest
restart: always
environment:
- DB_SERVER_HOST=zabbix-mysql
- MYSQL_DATABASE=zabbix
- MYSQL_USER=zabbix
- MYSQL_PASSWORD=zabbix
- MYSQL_ROOT_PASSWORD=123qwe
- ZBX_SERVER_HOST=zabbix-server-mysql
ports:
- 8080:8080
volumes:
- /etc/localtime:/etc/localtime
- /opt/zabbix/fonts/DejaVuSans.ttf:/usr/share/zabbix/assets/fonts/DejaVuSans.ttf
networks:
- zbx_net
depends_on:
- zabbix-server-mysql
- zabbix-mysql
zabbix-mysql:
image: mysql:8.0.23
restart: always
ports:
- 3306:3306
environment:
- MYSQL_DATABASE=zabbix
- MYSQL_USER=zabbix
- MYSQL_PASSWORD=zabbix
- MYSQL_ROOT_PASSWORD=123qwe
command:
- mysqld
- --default-authentication-plugin=mysql_native_password
- --character-set-server=utf8
- --collation-server=utf8_bin
volumes:
- /etc/localtime:/etc/localtime
- /opt/zabbix/db:/var/lib/mysql
networks:
- zbx_net
zabbix-java-gateway:
image: zabbix/zabbix-java-gateway:centos-5.2-latest
restart: always
volumes:
- /etc/localtime:/etc/localtime
networks:
- zbx_net
zabbix-server-mysql:
image: zabbix/zabbix-server-mysql:centos-5.2-latest
restart: always
volumes:
- zabbix-server-vol:/etc/zabbix
- /opt/zabbix/alertscripts:/usr/lib/zabbix/alertscripts
- /etc/localtime:/etc/localtime
ports:
- 10051:10051
environment:
- DB_SERVER_HOST=zabbix-mysql
- MYSQL_DATABASE=zabbix
- MYSQL_USER=zabbix
- MYSQL_PASSWORD=zabbix
- MYSQL_ROOT_PASSWORD=123qwe
- ZBX_JAVAGATEWAY=zabbix-java-gateway
- ZBX_JAVAGATEWAY_ENABLE=true
- ZBX_JAVAGATEWAYPORT=10052
depends_on:
- zabbix-mysql
networks:
- zbx_net
zabbix-agent:
image: zabbix/zabbix-agent:centos-5.2-latest
restart: always
ports:
- 10050:10050
environment:
- ZBX_HOSTNAME=Zabbix server
- ZBX_SERVER_HOST=zabbix-server-mysql
- ZBX_SERVER_PORT=10051
networks:
- zbx_net
networks:
zbx_net:
driver: bridge
volumes:
zabbix-server-vol:
#03 创建目录
mkdir alertscripts db fonts
#04 安装中文语言包
cd fonts
wget https://dl.cactifans.com/zabbix_docker/msty.ttf
mv msty.ttf DejaVuSans.ttf
#05 启动服务
docker-compose up -d
-
启动检查
- 浏览器访问 主机IP+8080端口 默认账户密码:Admin zabbix
- 系统语言汉化(图一)
- 我们要修改一下配置,将ip更换为容器名称,不然会检测不到(图二,删除IP 换成 dns名称加端口)
-
配置自动发现
三. 客户端配置
3.1 安装agent2
## 监控客户端服务器
1) 安装服务 并修改配置文件
yum install -y zabbix-agent2
# 由于网络原因 这里选择wget安装
wget https://mirrors.tuna.tsinghua.edu.cn/zabbix/zabbix/5.0/rhel/7/x86_64/zabbix-agent2-5.0.13-1.el7.x86_64.rpm
yum localinstall -y zabbix-agent2-5.0.13-1.el7.x86_64.rpm
2) 修改systemctl管理的单元文件
# 修改agent2配置文件,修改如下,填写zabbix-server主机ip和主机名
[root@centos7 zabbix]# egrep -v '^#|^$' /etc/zabbix/zabbix_agent2.conf
PidFile=/var/run/zabbix/zabbix_agent2.pid
LogFile=/var/log/zabbix/zabbix_agent2.log
LogFileSize=0
Server=192.168.5.249 # 服务端ip
ServerActive=192.168.5.249 # 服务端ip
Hostname=
HostnameItem=system.hostname # 客户端主机名
HostMetadataItem=system.uname # 客户端系统信息
Include=/etc/zabbix/zabbix_agent2.d/*.conf
ControlSocket=/tmp/agent.soc
3)重启服务
[root@db01 ~]$ systemctl restart zabbix-agent2.service
4)服务端去验证
zabbix_get -s 10.0.0.7 -p 10050 -k 'agent.ping'
3.2 页面验证
监控 nginx 11种状态# tcp的11种状态每种状态的数量
1) 需要的命令
root@web01 ~]# netstat -tna
[root@web01 ~]# netstat -tna | awk '{print $NF}' | sort | uniq -c | sort -rn | awk 'BEGIN{print "TCP状态 \t","连接数量"} {print $2"\t",$1} ' |column -t
TCP状态 连接数量
LISTEN 11
TIME_WAIT 10
SYN_SENT 1
State 1
ESTABLISHED 1
established) 1
四. MQQT 协议收集消息
##注意
MQQT服务器为中转服务器,即客户端和服务端都订阅该服务器
1)zabbix服务端
2)zabbix客户端
3)mqqt服务器
#01 安装MQTT服务器(mosquitto)
1) 安装编译环境
yum install gcc-c++ cmake openssl-devel -y
2)下载mosquitto
wget http://mosquitto.org/files/source/mosquitto-1.6.8.tar.gz
3)解压 编译 安装
echo "解压"
tar -zxvf mosquitto-1.6.8.tar.gz
echo "进入mosquitto-1.6.8"cd mosquitto-1.6.8
echo "编译"
make
echo "安装"
sudo make install
4)修改链接路径,重新加载动态链接库
sudo ln -s /usr/local/lib/libmosquitto.so.1 /usr/lib/libmosquitto.so.1
sudo ldconfig
5)创建配置文件 和用户 用户组
cp /etc/mosquitto/mosquitto.conf.example /etc/mosquitto/mosquitto.conf
sudo groupadd mosquitto
sudo useradd -g mosquitto mosquitto -s /sbin/nologin
6) 启动、查看、关闭程序
echo "运行程序"
mosquitto -c /etc/mosquitto/mosquitto.conf -d
echo "ps查看"
ps -aux | grep mosquitto
echo "关闭程序"kill -9 $(pidof mosquitto)
7)本地简单测试
# 打开一个订阅者
mosquitto_sub -t topic
# 打开一个发布者
mosquitto_pub -t topic -m "发布内容"
https://zhuanlan.zhihu.com/p/164930347
- Python 实现订阅和发布
##订阅
# -*- coding: utf-8 -*-
import paho.mqtt.client as mqtt
def on_connect(client, userdata, flags, rc):
print("Connected with result code: " + str(rc))
def on_message(client, userdata, msg):
print(msg.topic + " " + str(msg.payload))
client = mqtt.Client()
#设置用户名和密码
# client.username_pw_set("mosquitto", "mosquitto")
client.on_connect = on_connect
client.on_message = on_message
#client.on_disconnect = on_disconnect
#连接 IP port keepalive
client.connect('101.200.170.7', 1833, 600)
#订阅的 topic
client.subscribe('test', qos=0)
client.loop_forever()
###发布
# -*- coding: utf-8 -*-
import paho.mqtt.client as mqtt
def on_connect(client, userdata, flags, rc):
print("Connected with result code: " + str(rc))
def on_message(client, userdata, msg):
print(msg.topic + " " + str(msg.payload))
client = mqtt.Client()
#设置用户名和密码
#client.username_pw_set("mosquitto", "mosquitto")
client.on_connect = on_connect
client.on_message = on_message
#连接 IP port keepalive
client.connect('101.200.170.7', 1833, 600)
#发布 topic 内容
client.publish('test', payload='111ing', qos=0)
- 添加监控项
#01 监控项参数
mqtt.get["tcp://192.168.5.249:1833","test"]
mqqt服务器地址和端口:"tcp://192.168.5.249:1833"
topic 名称:test (自定义 需要先创建订阅)
例如:mosquitto_sub -t topic
- 图示