从0部署一台能够收集Windows硬件信息的Zabbix服务器
部署Zabbix
部署MySQL
- 下载MySQL bundle
1
wget https://dev.mysql.com/get/Downloads/MySQL-8.4/mysql-server_8.4.6-1ubuntu24.04_amd64.deb-bundle.tar
- 解压
1
tar -xvf mysql-server_8.4.6-1ubuntu24.04_amd64.deb-bundle.tar
- 安装MySQL的deb包安装百分百会失败,失败就失败了,接着执行
1
dpkg -i *.deb
执行后会自动下载缺失的依赖,下载安装好后会让设置root密码。1
apt --fix-broken install -y
- 确保MySQL正确运行
1
systemctl enable --now mysql
部署Zabbix
打开Download and install Zabbix 7.4 for Ubuntu 24.04 Noble
(amd64, arm64), MySQL, Nginx,然后按照页面提示安装。
默认用户名和密码是Admin
和zabbix
,安装好后务必修改默认用户名和密码。
处理语言包不存在的问题
报错提示”You are not able to choose some of the languages, because locales for them are not installed on the web server.”,以及首页语言不可选择。
安装必要的依赖包:
1 | apt install -y locales dialog |
配置语言和区域:
1 | dpkg-reconfigure locales |
然后选择你需要的语言。如果不知道选啥就勾选”en_US.UTF-8”和”zh_CN.UTF-8”,然后回车,在选择系统环境的区域语言时选择”en_US.UTF-8”,回车等待安装完成即可。
安装好后重启一下Zabbix的服务
1 | systemctl restart zabbix-server zabbix-agent nginx php8.3-fpm |
处理仪表盘图表中文缺字的问题
- 定位到如下路径:下面有个
1
/usr/share/zabbix/ui/assets/fonts
graphfont.ttf
,是个软链接到/etc/alternatives/zabbix-frontend-font
。 - 删除
graphfont.ttf
,然后下载一个别的ttf字体,放到/usr/share/zabbix/ui/assets/fonts
目录下,重命名为graphfont.ttf
- 重启Zabbix服务
1
systemctl restart zabbix-server zabbix-agent nginx php8.3-fpm
安装好微软雅黑的字体后图表的效果:
Windows 安装 Agent
获得 Agent
- 打开Download pre-compiled Zabbix agent binaries,下方“PACKAGING”选择“Archive”,下载Zabbix agent 2的ZIP包到本地,解压后把配置文件和可执行文件都放一起,现在应该至少有4个文件:
1
2
3
4zabbix_agent2.conf
zabbix_agent2.exe
zabbix_get.exe
zabbix_sender.exe - 创建一个
script
文件夹,稍后我们额外收集信息的脚本都放在这里。 - 下载一些工具:
GnuWin Files,下载gawk-3.1.6-1-bin.zip
;
UnxUtils Files,下载UnxUtils.zip
;
下载后都放在刚才解压好的文件夹下。
截至目前,目录结构应该是这样的:
1 | ├─Zabbix |
省略的文件不一定是不需要的,建议都一起带着。
配置文件
打开上面根目录下的zabbix_agent2.conf
文件,里面的内容我进行了精简:
1 | # 服务器IP地址 |
自定义脚本
自定义收集项的一共需要添加三部分内容:
- 本地添加脚本;
- 配置文件添加
UserParameter
项; - Zabbix后台添加监控项。
- CPU 型号
Key:cpu_hardware_model
配置项:UserParameter=cpu_hardware_model,C:\zabbix\script\cpu_hardware_model.bat
1
2
3@echo off
wmic cpu get name|C:\\zabbix\\gawk-3.1.6-1-bin\\bin\\awk.exe "NR==2 { print }" > C:\\zabbix\\script\\cpu_hardware_model.txt
type C:\\zabbix\\script\\cpu_hardware_model.txt - CPU 物理数量
Key:cpu_hardware_number
配置项:UserParameter=cpu_hardware_number,C:\zabbix\script\cpu_hardware_number.bat
1
2
3@echo off
wmic cpu get DeviceID | findstr /V "DeviceID" |findstr /V "^$" | C:\\zabbix\\UnxUtils\\usr\\local\\wbin\\grep.exe "CPU" |C:\\zabbix\\UnxUtils\\usr\\local\\wbin\\wc.exe -l | C:\\zabbix\\UnxUtils\\usr\\local\\wbin\\sed.exe "s/ //g" > C:\\zabbix\\script\\cpu_hardware_number.txt
type C:\\zabbix\\script\\cpu_hardware_number.txt - CPU核心数量
Key:cpu_hardware_core
配置项:UserParameter=cpu_hardware_core,C:\zabbix\script\cpu_hardware_core.bat
1
2
3@echo off
wmic cpu get NumberOfCores | C:\\zabbix\\gawk-3.1.6-1-bin\\bin\\awk.exe "NR==2 { print }" > C:\\zabbix\\script\\cpu_hardware_core.txt
type C:\\zabbix\\script\\cpu_hardware_core.txt - 内存完整信息
Key:memory_info_all
配置项:UserParameter=memory_info_all,C:\zabbix\script\memory_info_all.bat
1
2
3@echo off
wmic memorychip get BankLabel, Capacity, Manufacturer, PartNumber, SerialNumber, Speed > C:\\zabbix\\script\\memory_info_all.txt
type C:\\zabbix\\script\\memory_info_all.txt - 主板序列号
Key:board_sn
配置项:UserParameter=board_sn,C:\zabbix\script\board_sn.bat
1
2
3@echo off
wmic baseboard get SerialNumber |C:\\zabbix\\gawk-3.1.6-1-bin\\bin\\awk.exe "NR==2 { print }" > C:\\zabbix\\script\\board_sn.txt
type C:\\zabbix\\script\\board_sn.txt - 主板品牌
Key:board_brand
配置项:UserParameter=board_brand,C:\zabbix\script\board_brand.bat
1
2
3@echo off
wmic baseboard get Manufacturer |C:\\zabbix\\gawk-3.1.6-1-bin\\bin\\awk.exe "NR==2 { print }" > C:\\zabbix\\script\\board_brand.txt
type C:\\zabbix\\script\\board_brand.txt - 主板型号
Key:board_model
配置项:UserParameter=board_model,C:\zabbix\script\board_model.bat
1
2
3@echo off
wmic baseboard get Product |C:\\zabbix\\gawk-3.1.6-1-bin\\bin\\awk.exe "NR==2 { print }" > C:\\zabbix\\script\\board_model.txt
type C:\\zabbix\\script\\board_model.txt - 主板MAC地址
Key:nic_mac
配置项:UserParameter=nic_mac,C:\zabbix\script\nic_mac.bat
1
2
3@echo off
wmic nic where "MACAddress is not null and NetEnabled=true" get MACAddress > C:\\zabbix\\script\\nic_mac.txt
type C:\\zabbix\\script\\nic_mac.txt - 磁盘信息
Key:disk_info
配置项:UserParameter=disk_info,C:\zabbix\script\disk_info.bat
1
2
3@echo off
wmic diskdrive get Model, SerialNumber, Size > C:\\zabbix\\script\\disk_info.txt
type C:\\zabbix\\script\\disk_info.txt - 显卡信息
Key:video_controller_info
配置项:UserParameter=video_controller_info,C:\zabbix\script\video_controller_info.bat
1
2
3@echo off
wmic path win32_VideoController get Name, AdapterCompatibility, DriverVersion > C:\\zabbix\\script\\video_controller_info.txt
type C:\\zabbix\\script\\video_controller_info.txt - 显示器信息(已弃用)
Key:monitor_info
配置项:UserParameter=monitor_info,C:\zabbix\script\monitor_info.bat
1
2
3@echo off
wmic desktopmonitor get Caption, MonitorType, Name, PNPDeviceID > C:\\zabbix\\script\\monitor_info.txt
type C:\\zabbix\\script\\monitor_info.txt
整理一下
现在的目录结构应该是:
1 | ├─Zabbix |
然后配置文件看起来应该是这样的:
1 | Server=192.168.8.192 |
安装 Agent 为服务
将整个文件夹复制到C盘根目录下面,然后以管理员权限打开命令提示符执行如下命令安装:
1 | C:\zabbix\zabbix_agent2.exe -c C:\zabbix\zabbix_agent2.conf -i |
启动服务:
1 | net start "Zabbix Agent 2" |
如果有开启防火墙的话创建一条防火墙放通规则:
1 | netsh advfirewall firewall add rule name=ZabbixInbond dir=in action=allow protocol=TCP localport=10050 profile=any |
此时即可使用Zabbix添加这台主机。
批量部署
Win11 24H2移除了WMIC命令,使得我们上面的脚本大部分都没法用了,在操作系统版本复杂的环境里批量部署很容易遇见类似的问题。
所以我写了个批量部署的脚本,能够自动替换配置文件中的主机名,能够自动检测当前本机是否有WMIC存在,如不存在则自动安装。以管理员权限执行即可。
1 | @echo off |