一、参考
参考另一篇博文配置Windows代理程序
Zabbix5系列-监控Windows主机 (三)
二、CPU温度监控(自动发现版)
2.1 由于Windows无法直接读取温度信息,所以用C#写了个脚本调用OpenHardwareMonitor库来进行温度读取,并匹配自动发现
编译好的程序:CPU温度zabbix自动发现脚本
源代码:
using Newtonsoft.Json.Linq;
using OpenHardwareMonitor.Hardware;
using System;
using System.IO;
namespace cpu_temp
{
class Program
{
static void Main(string[] args)
{
if (args.Length >= 1)
{
if (args[0] == "discovery")
{
Computer computer = new Computer();
computer.CPUEnabled = true;
computer.Open();
JObject r = new JObject();
JArray data = new JArray();
// 遍历所有硬件
for (int i = 0; i < computer.Hardware.Length; i++)
{
// 如果发现CPU
if (computer.Hardware[i].HardwareType == HardwareType.CPU)
{
// 遍历传感器
for (int j = 0; j < computer.Hardware[i].Sensors.Length; j++)
{
// 如果发现温度
if (computer.Hardware[i].Sensors[j].SensorType == SensorType.Temperature)
{
JObject cpu = new JObject();
cpu.Add("{#INDEX}", data.Count + 1);
cpu.Add("{#CPUID}", i.ToString());
cpu.Add("{#CPUNAME}", computer.Hardware[i].Sensors[j].Name);
cpu.Add("{#CPUTEMP}", computer.Hardware[i].Sensors[j].Value.ToString());
data.Add(cpu);
}
}
}
}
computer.Close();
r.Add("data", data);
Console.Write(r.ToString());
File.WriteAllText("cpu.temp", r.ToString());
}else{
if (File.Exists("cpu.temp"))
{
JObject json = Newtonsoft.Json.JsonConvert.DeserializeObject<JObject>(File.ReadAllText("cpu.temp"));
foreach (var item in json.Value<JArray>("data"))
{
if (args[0] == item.Value<string>("{#INDEX}"))
{
Console.Write(item.Value<string>("{#CPUTEMP}"));
}
}
}
}
}
}
}
}
2.2 复制程序至agent2安装目录
2.3 打开命令提示符测试读取
cd /d "C:\Program Files\Zabbix Agent 2"
cpu_temp_discovery.exe discovery
cpu_temp_discovery.exe 1
2.4 打开zabbix_agent2.conf配置文件增加用户自定义参数
参考:用户自定义参数
自定义参数:
UserParameter=cpu.temp.discovery[*],"C:\Program Files\Zabbix Agent 2\cpu_temp_discovery.exe" $1
2.5 打开命令提示符重启服务
sc stop "Zabbix Agent 2"
sc start "Zabbix Agent 2"
2.6 保存模板内容为windows_temperature.yaml并导入zabbix
windows_temperature.yaml模板:
zabbix_export:
version: '5.4'
date: '2022-06-10T10:47:13Z'
groups:
-
uuid: 7df96b18c230490a9a0a9e2307226338
name: Templates
templates:
-
uuid: 4f0aa5b74bfa4d3993b40074ed8e8604
template: 'Windows Temperature by Zabbix agent'
name: 'Windows Temperature by Zabbix agent'
groups:
-
name: Templates
discovery_rules:
-
uuid: 983f5adea86d4cee9560141b7ebb5cec
name: CPU温度自动发现
key: 'cpu.temp.discovery[discovery]'
lifetime: 3d
item_prototypes:
-
uuid: 89f8911e181c4eb0b382730b102a6906
name: 'CPU {#CPUID}: {#CPUNAME} 温度'
key: 'cpu.temp.discovery[{#INDEX}]'
history: 30d
value_type: FLOAT
units: °C
tags:
-
tag: CPU
value: 温度
trigger_prototypes:
-
uuid: 4824f569e1f3428391b39854120222cf
expression: 'avg(/Windows Temperature by Zabbix agent/cpu.temp.discovery[{#INDEX}],5m)<{$CPU_TEMP_CRIT_LOW:""}'
recovery_mode: RECOVERY_EXPRESSION
recovery_expression: 'min(/Windows Temperature by Zabbix agent/cpu.temp.discovery[{#INDEX}],5m)>{$CPU_TEMP_CRIT_LOW:""}+3'
name: 'CPU {#CPUID}: {#CPUNAME} 温度太低: >{$CPU_TEMP_CRIT_LOW:""}℃'
opdata: '{ITEM.LASTVALUE1}'
priority: AVERAGE
-
uuid: 8344c07558274769a08abd4c3f9b2e22
expression: 'avg(/Windows Temperature by Zabbix agent/cpu.temp.discovery[{#INDEX}],5m)>{$CPU_TEMP_CRIT:""}'
recovery_mode: RECOVERY_EXPRESSION
recovery_expression: 'max(/Windows Temperature by Zabbix agent/cpu.temp.discovery[{#INDEX}],5m)<{$CPU_TEMP_CRIT:""}-3'
name: 'CPU {#CPUID}: {#CPUNAME} 温度高于临界值: >{$CPU_TEMP_CRIT:""}℃'
opdata: '{ITEM.LASTVALUE1}'
priority: AVERAGE
-
uuid: 2d0c380a8c61489eabfdb0ef2a5de4e5
expression: 'avg(/Windows Temperature by Zabbix agent/cpu.temp.discovery[{#INDEX}],5m)>{$CPU_TEMP_WARN:""}'
recovery_mode: RECOVERY_EXPRESSION
recovery_expression: 'max(/Windows Temperature by Zabbix agent/cpu.temp.discovery[{#INDEX}],5m)<{$CPU_TEMP_WARN:""}-3'
name: 'CPU {#CPUID}: {#CPUNAME} 温度高于警告值: >{$CPU_TEMP_WARN:""}℃'
opdata: '{ITEM.LASTVALUE1}'
priority: WARNING
dependencies:
-
name: 'CPU {#CPUID}: {#CPUNAME} 温度高于临界值: >{$CPU_TEMP_CRIT:""}℃'
expression: 'avg(/Windows Temperature by Zabbix agent/cpu.temp.discovery[{#INDEX}],5m)>{$CPU_TEMP_CRIT:""}'
recovery_expression: 'max(/Windows Temperature by Zabbix agent/cpu.temp.discovery[{#INDEX}],5m)<{$CPU_TEMP_CRIT:""}-3'
graph_prototypes:
-
uuid: 83f34ee492224bb893839fa2f45d15ab
name: 'CPU {#CPUID}: {#CPUNAME} 温度'
yaxismin: '-5'
yaxismax: '120'
ymin_type_1: FIXED
ymax_type_1: FIXED
graph_items:
-
sortorder: '1'
drawtype: GRADIENT_LINE
color: E64A19
item:
host: 'Windows Temperature by Zabbix agent'
key: 'cpu.temp.discovery[{#INDEX}]'
macros:
-
macro: '{$CPU_TEMP_CRIT}'
value: '85'
description: 'CPU高温度临界值 单位: ℃'
-
macro: '{$CPU_TEMP_CRIT_LOW}'
value: '10'
description: 'CPU低温度临界值 单位: ℃'
-
macro: '{$CPU_TEMP_WARN}'
value: '70'
description: 'CPU高温度警告值 单位: ℃'
导入模板
2.7 新建主机并关联模板
2.8 等待几分钟后成功发现监控项、触发器、图形
2.9 查看数据是否获取成功