Zabbix监控平台对接modbus tcp设备

背景:

1、公司机房没有专用的温度监控设备,以往是通过snmp功能get服务器的CPU或是主板温度,根据偏差值算出机房的大致温度(温度计值与服务器的差值),可最近研发部门经常在服务器跑高负载任务,导致计算出的环境温度极度不准

2、网上虽然有成套的温度报警产品,但从以下方面讲不符合公司要求:

2-1:现有产品多是通过邮箱或是手机语音/手机短信、微信报警,而我司需要对接到内部平台统一报警

2-2:网上的设备大多在200~400元价格,支持云功能的还要续平台费(每年要提采购、报销审批,内部流程很麻烦)

实现过程:

1、首先是购买硬件设备,网关+温度传感器大概需要200~220元左右,如下图所示:

· 左侧图片为modbus tcp网关,实现485或是modbus rtu协议转modbus tcp协议

· 右侧图片为温湿度探头,支持modbus rtu协议,通过485连接网关、然后转为tcp协议

2、系统及软件方面:

· 在linux系统安装5.2版本以上的Zabbix-Server (5.2开始支持modbus协议)

· zabbix-agent2 (当前仅zabbix-agent2版支持modbus,后期可能会增加zabbix-agent的支持)

3、在这里理顺一下逻辑,否则各位在配置时可能会出现错误或是疑问:

· 虽然Zabbix-Server可以直接对被监控主机做一些例如ping、snmp、service.perf、port.perf等的监控,但是modbus数据的获取是通过zabbix-agent2实现的,也就是说agent2获取到被控主机的modbus数据,再将结果回传到Zabbix-Server。如果modbus设备自身、或是所在网络有防火墙策略的话,需要放行agent2主机访问(Zabbix的server和agent可以安装到同一个linux系统上)

4、部署Zabbix-Server,因为官网有十分详尽的教程、在此不做描述,这里要提一句:官网命令中安装的数据库并不完整,需要自行手动安装下mariadb

· 官网链接:官网安装教程

5、安装zabbix-agent2,这里着重说明一下:modbus需要zabbix-agent2的支持,而我们通过命令yum install zabbix-agent默认安装的是v1版,Zabbix-Server自带的也是v1版agent

· 首先停用或是卸载旧版agent(如果存在的话),拿centos7来说,可以执行systemctl stop zabbix-agent; yum autoremove zabbix-agent进行停用后的卸载

· 因为zabbix-agent2部分使用go语言编写,所以它依赖go环境,这里我们通过一键安装脚本部署(基于Centos安装,如果是debian的话需要提前安装sudo):

 引用链接:一键安装go环境 

git clone https://gitee.com/xuthus5/golang-install-shell.git (如果报错需要安装git)
cd golang-install-shell
chmod +x install.sh
./install.sh

· 出现以下安装界面后,选择0 安装golang环境,然后按默认参数安装即可:

----------------------------------
0) 安装golang环境
1) 安装gopm跨墙
2) 安装web框架
3) 安装包
4) 退出
----------------------------------
输入操作编号:0
----------------------------------
选择安装版本:
1) 1.11
2) 1.10.4
3) 1.9.7
4) 1.8.7
----------------------------------
输入安装版本编号(默认1):

~~~~~~~~~~~~~~~~~~~~~~省略很多软件安装的信息~~~~~~~~~~~~~~~~~~~~~~~

输入程序安装目录GOROOT(默认 /usr/local):
输入GOPATH目录(默认 /root/go):

·  安装完成后,选择4 退出

go version go1.11 linux/amd64
环境变量配置成功!请执行 source /root/.bashrc 确保配置生效
----------------------------------
0) 安装golang环境
1) 安装gopm跨墙
2) 安装web框架
3) 安装包
4) 退出
----------------------------------
输入操作编号:4

· 安装zabbix-agent2:

# 添加官网源
rpm -Uvh https://repo.zabbix.com/zabbix/5.4/rhel/7/x86_64/zabbix-release-5.4-1.el7.noarch.rpm

# 清除yum缓存
yum clean all 

# 安装agent2,安装完后程序会自动运行、并自动配置开机自启动功能
yum install zabbix-agent2

· 安装成功后编辑配置文件:

# 编辑配置文件
vi /etc/zabbix/zabbix_agent2.conf

# 根据实际环境修改以下项目
Hostname=
Server=
ServerActive=

# 配置完成后重启agent2
systemctl restart zabbix-agent2

· 在Zabbix-Server服务器添加安装了zabbix-agent2的主机,并添加modbus监控项

· 这里引用官网对modbus监控项的详细介绍及访问链接:官网介绍

Parameter Description Defaults Example
endpoint Protocol and address of the endpoint, defined as protocol://connection_string

Possible protocol values: rtuascii (Agent 2 only), tcp

Connection string format:

with tcp - address:port
with serial line: rtuascii - port_name:speed:params
where
'speed' - 1200, 9600 etc
'params' - data bits (5,6,7 or 8), parity (n,e or o for none/even/odd), stop bits (1 or 2)
protocol: none

rtu/ascii protocol:
port_name: none
speed: 115200
params: 8n1

tcp protocol:
address: none
port: 502
tcp://192.168.6.1:511
tcp://192.168.6.2
tcp://[::1]:511
tcp://::1
tcp://localhost:511
tcp://localhost
rtu://COM1:9600:8n
ascii://COM2:1200:7o2
rtu://ttyS0:9600
ascii://ttyS1
slave id Modbus address of the device it is intended for (1 to 247), see MODBUS Messaging Implementation Guide (page 23)

tcp device (not GW) will ignore the field
serial: 1

tcp: 255 (0xFF)
2
function Empty or value of a supported function:

1 - Read Coil,
2 - Read Discrete Input,
3 - Read Holding Registers,
4 - Read Input Registers
empty 3
address Address of the first registry, coil or input.

If 'function' is empty, then 'address' should be in range for:
Coil - 00001 - 09999
Discrete input - 10001 - 19999
Input register - 30001 - 39999
Holding register - 40001 - 49999

If 'function' is not empty, the 'address' field will be from 0 till 65535 and used without modification (PDU)
empty function: 00001

non-empty function: 0
9999
count Count of sequenced 'type' which will be read from device, where:

for Coil or Discrete input the 'type' = 1 bit
for other cases: (count*type)/2 = real count of registers for reading
If 'offset' is not 0, the value will be added to 'real count'
Acceptable range for 'real count' is 1:65535
1 2
type Data type:

for Read Coil and Read Discrete Input - bit

for Read Holding Registers and Read Input Registers:
int8 - 8bit
uint8 - 8bit (unsigned)
int16 - 16bit
uint16 - 16bit (unsigned)
int32 - 32bit
uint32 - 32bit (unsigned)
float - 32bit
uint64 - 64bit (unsigned)
double - 64bit
bit
uint16
uint64
endianness Endianness type:
be - Big Endian
le - Little Endian
mbe - Mid-Big Endian
mle - Mid-Little Endian

Limitations:
for 1 bit - be
for 8 bits - be,le
for 16 bits - be,le
be le
offset Number of registers, starting from 'address', the result of which will be discarded.

The size of each register is 16bit (needed to support equipment that does not support random read access).
0 4

· 这是根据实际环境修改后的参照表:

说明 监控项
官方给出的监控项 modbus.get[endpoint,<slaveid>,<function>,<address>,<count>,<type>,<endianness>,<offset>]
对上述的解释 modbus.get[通讯协议及地址,设备地址,功能码,寄存器地址,读取个数,数据类型,字节序,抵消] 备注:最后的字节序、抵消没弄明白是啥意思
根据实际环境修改

modbus.get[tcp://192.168.1.202:502,1,3,512,1,int16,,]

· 使用tcp的502端口访问192.168.1.202网关

· 1表示设备地址为1

· 3表示功能码,为读取保持寄存器

· 512是十进制寄存器地址,表示十六进制的02 00

· 1是读取个数

· int16表示网关返回的数据类型

· 末尾的<字节序>和<抵消>不知道啥作用,留空即可

备注:以上参数需要使用10进制

· 我所用到的温度传感器给出的温度值是实际值的10倍,所以在zabbix进程中要除以10(也就是乘以0.1)

· 设置完监控项后,agent端要等1分钟左右才能使用新的监控项,如果着急的话可以手动执行systemctl restart zabbix-agent2,让agent2强制重启并立即获取新的监控项:

这是获取到的温度值:

· 在调试过程中如果遇到modbus网关没响应、zabbix报网关错误等,可以下载调试工具、将本机作为网关服务器,然后让zabbix-agent2访问本机,根据接收信息里面的数据分析是哪里出现的错误:

工具下载地址:调试助手

猜你喜欢

转载自blog.csdn.net/baishikele006/article/details/123145869