一、参考
参考另一篇博文配置Zabbix-Agent2代理程序
Zabbix5系列-监控Linux主机 (四)
二、安装传感器工具
# 安装lm-sensors
yum install lm_sensors
# debian等系统使用apt安装
#apt install lm-sensors
# 扫描传感器 一路下一步扫描所有安全的传感器 或者根据实际情况控制扫描
sensors-detect
# 查看传感器温度
sensors -u
三、编写自动发现脚本
# 安装python3
yum install python3
# 创建脚本文件
vi /etc/zabbix/sensors.discovery.py
sensors.discovery.py脚本内容:
#!/usr/bin/env python3
# coding:utf8
import os
import json
def handle35(strdata):
temp = json.loads(strdata)
data = []
for i in temp:
for j in temp[i]:
for v in temp[i][j]:
if v.find('input')>-1:
data.append(
{
'{#INDEX}': len(data) + 1,
'{#DEVICE}': i,
'{#SENSOR}': v.replace('_input',''),
'{#SITE}': j
}
)
r = {'data': data}
print(json.dumps(r))
def handle34(strdata):
data = []
sensors = strdata.split('\n\n')
for i in sensors:
line = i.split('\n')
if len(line) > 2:
site = None
for j in range(len(line)):
if j == 0:
continue
elif j == 1 and line[j].find('Adapter') == 0:
continue
elif line[j][:2] != ' ':
site = line[j][:-1]
elif line[j][:2] == ' ':
if line[j].find('_input') > -1:
data.append(
{
'{#INDEX}': len(data) + 1,
'{#DEVICE}': line[0],
'{#SENSOR}': line[j][2:line[j].find('_input')],
'{#SITE}': site
}
)
site = None
r = {'data': data}
print(json.dumps(r))
try:
version = os.popen("sensors -v").read()
if version.find('version') > -1:
if version.find('3.5') > -1 or version.find('3.6') > -1:
temp = os.popen("sensors -u -j").read()
handle35(temp)
else:
temp = os.popen("sensors -u").read()
handle34(temp)
else:
print('Error')
except:
print('Error')
# 修改脚本所有者
chown -R zabbix /etc/zabbix/sensors.discovery.py
# 赋予脚本执行权
chmod +x /etc/zabbix/sensors.discovery.py
# 测试执行脚本
/etc/zabbix/sensors.discovery.py
成功读取到传感器信息:
四、 添加自定义参数
# zabbix-agent2配置文件插入自定义参数
echo "UserParameter=sensors.discovery,/etc/zabbix/sensors.discovery.py">>/etc/zabbix/zabbix_agent2.conf
# 重启zabbix-agent2服务
systemctl restart zabbix-agent2
五、 导入模板
保存模板文件为Linux_sensors_by_Zabbix_agent.yaml
模板内容:
zabbix_export:
version: '5.4'
date: '2022-06-11T08:35:50Z'
groups:
-
uuid: 7df96b18c230490a9a0a9e2307226338
name: Templates
templates:
-
uuid: 09228ac755b641f781075b5ccd043b82
template: 'Linux sensors by Zabbix agent'
name: 'Linux sensors by Zabbix agent'
groups:
-
name: Templates
discovery_rules:
-
uuid: d7fc58bf4a824c1b9491ac63e77ff6d9
name: 传感器自动发现
key: sensors.discovery
delay: 1d
filter:
evaltype: AND
conditions:
-
macro: '{#DEVICE}'
value: '.*'
formulaid: A
-
macro: '{#INDEX}'
value: '.*'
formulaid: B
-
macro: '{#SENSOR}'
value: '.*'
formulaid: C
-
macro: '{#SITE}'
value: Package
formulaid: D
item_prototypes:
-
uuid: 1a0892f533d041dd836e409c4f844f22
name: '传感器 {#DEVICE}: {#SITE} 温度'
key: 'sensor[{#DEVICE},{#SENSOR}]'
history: 30d
value_type: FLOAT
units: ℃
tags:
-
tag: 环境
value: 传感器
trigger_prototypes:
-
uuid: f9786cd5b3b44e8bbfc3a0aac9c6eff5
expression: 'avg(/Linux sensors by Zabbix agent/sensor[{#DEVICE},{#SENSOR}],5m)<{$SENSOR_TEMP_CRIT_LOW:""}'
recovery_mode: RECOVERY_EXPRESSION
recovery_expression: 'min(/Linux sensors by Zabbix agent/sensor[{#DEVICE},{#SENSOR}],5m)>{$SENSOR_TEMP_CRIT_LOW:""}+3'
name: '传感器 {#DEVICE}: {#SITE} 温度太低: >{$SENSOR_TEMP_CRIT_LOW:""}℃'
opdata: '{ITEM.LASTVALUE1}'
priority: AVERAGE
-
uuid: 14f7f762a2c543b9948398b8eff68d2b
expression: 'avg(/Linux sensors by Zabbix agent/sensor[{#DEVICE},{#SENSOR}],5m)>{$SENSOR_TEMP_CRIT:""}'
recovery_mode: RECOVERY_EXPRESSION
recovery_expression: 'max(/Linux sensors by Zabbix agent/sensor[{#DEVICE},{#SENSOR}],5m)<{$SENSOR_TEMP_CRIT:""}-3'
name: '传感器 {#DEVICE}: {#SITE} 温度高于临界值: >{$SENSOR_TEMP_CRIT:""}℃'
opdata: '{ITEM.LASTVALUE1}'
priority: AVERAGE
-
uuid: 040cfea9b7b64f6791018662ad3fcf63
expression: 'avg(/Linux sensors by Zabbix agent/sensor[{#DEVICE},{#SENSOR}],5m)>{$SENSOR_TEMP_WARN:""}'
recovery_mode: RECOVERY_EXPRESSION
recovery_expression: 'max(/Linux sensors by Zabbix agent/sensor[{#DEVICE},{#SENSOR}],5m)<{$SENSOR_TEMP_WARN:""}-3'
name: '传感器 {#DEVICE}: {#SITE} 温度高于警告值: >{$SENSOR_TEMP_WARN:""}℃'
opdata: '{ITEM.LASTVALUE1}'
priority: WARNING
dependencies:
-
name: '传感器 {#DEVICE}: {#SITE} 温度高于临界值: >{$SENSOR_TEMP_CRIT:""}℃'
expression: 'avg(/Linux sensors by Zabbix agent/sensor[{#DEVICE},{#SENSOR}],5m)>{$SENSOR_TEMP_CRIT:""}'
recovery_expression: 'max(/Linux sensors by Zabbix agent/sensor[{#DEVICE},{#SENSOR}],5m)<{$SENSOR_TEMP_CRIT:""}-3'
graph_prototypes:
-
uuid: 5006a3e1db3f4d70a070370ebc52c10c
name: '传感器 {#DEVICE}: {#SITE} 温度'
yaxismin: '-10'
yaxismax: '120'
ymin_type_1: FIXED
ymax_type_1: FIXED
graph_items:
-
sortorder: '1'
drawtype: GRADIENT_LINE
color: FF7043
item:
host: 'Linux sensors by Zabbix agent'
key: 'sensor[{#DEVICE},{#SENSOR}]'
macros:
-
macro: '{$SENSOR_TEMP_CRIT}'
value: '85'
description: '传感器高温度临界值 单位: ℃'
-
macro: '{$SENSOR_TEMP_CRIT_LOW}'
value: '10'
description: '传感器低温度临界值 单位: ℃'
-
macro: '{$SENSOR_TEMP_WARN}'
value: '70'
description: '传感器高温度警告值 单位: ℃'''
导入模板
六、 创建主机并关联模板
创建主机
关联模板
七、测试结果
测试自动发现 成功获取传感器信息
手动执行自动发现
成功发现监控项
成功读取到温度
八、 修改传感器过滤器
切换到自动发现的过滤器页 对应sensors -u的信息 进行过滤