Smartmontools+python+zabbix实现WIN下智能硬盘监控
source link: https://blog.51cto.com/u_14249042/10518999
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
Smartmontools+python+zabbix实现WIN下智能硬盘监控
精选 原创Smartmontools(S.M.A.R.T. Monitoring Tools)是一组实用程序,包括smartctl和smartd两部分,用于控制和监控S.M.A.R.T信息,适用于大部分ATA、SCSI和NVMe硬盘
Smartmontools可以显示由S.M.A.R.T监测到的硬盘问题,通知即将可能会发生的故障,提示用户及时备份数据
- Windows系统
官网下载 已编译好的exe程序,直接执行安装即可 - Linux系统
使用在线源直接安装即可
# CentOS
yum install smartmontools -y
# Ubuntu
apt-get install smartmontools -y
SMART操作命令
1、状态查询
smartctl -a <device-path>:获取硬盘所有SMART信息
smartctl -i <device-path>:获取硬盘标识信息
smartctl -x <device-path>:获取硬盘所有信息
smartctl -A <device-path>:获取硬盘SMART厂家预设属性及数值
smartctl -H <device-path>:获取硬盘健康状态
注:PASSED表示硬盘健康状态良好
2、健康测试
smartctl -t short <device-path>:后台检测硬盘,消耗时间短
smartctl -t long <device-path>:后台检测硬盘,消耗时间长
smartctl -C -t short <device-path>:前台检测硬盘,消耗时间短
smartctl -C -t long <device-path>:前台检测硬盘,消耗时间长
不要忘记将 %username% 更改为系统中的用户名。
安装 Python 3.6.4时可以获取完整路径
不要忘记您需要以系统管理员身份运行安装。
该代码接受来自命令行的两个参数 — 磁盘名称和要打印的参数。
在没有参数的情况下运行,会检测到磁盘,输出是zabbix-server接受的现成格式。
将代码上传到任何可用的目录。
操作步骤
1.安装Zabbix Agent 2,Windows可以直接下载使用,Linux需要通过参数-enable-agent2编译安装。
2.安装Smartmontools,版本要求7.1+以上。
3.Windows平台zabbix_agentd.conf需要增加配置,定义smartctl的路径。
Plugins.Smart.Path="C:\ProgramFiles\smartmontools\bin\smartctl.exe"
方案二:使用第三方方案
地址:https://github.com/nikimaxim/zbx-smartmonitor
{$SMART。ATTRIBUTE.ID.MATCHES} | 此宏用于替代用于筛选 ID 的属性发现。它可以在主机或链接模板级别被覆盖。 |
|
{$SMART.DISK.NAME.MATCHES} | 此宏用于替代用于筛选 ID 的属性发现。它可以在主机或链接模板级别被覆盖。 |
|
{$SMART.TEMPERATURE.MAX.CRIT} | 此宏用于触发器表达式。它可以在主机或链接模板级别被覆盖。 |
|
{$SMART.TEMPERATURE.MAX.WARN} | 此宏用于触发器表达式。它可以在主机或链接模板级别被覆盖。 |
|
关键和其他信息 | |||
发现 SMART 磁盘。 | ZABBIX_PASSIVE | smart.disk.discovery 重写: 自检 不是 NVMe | |
发现磁盘的 SMART 供应商特定属性。 | ZABBIX_PASSIVE | smart.attribute.discovery 重写: ID 筛选器 |
将模板添加到Zabbix
进入主机配置 切换到模板页 增加"SMART by Zabbix agent 2"模板
切换到自动发现规则页 执行SMART发现规则
自动发现SMART监控项成功, 获取数据正常
您也可以添加自己的数据项。
逻辑如下:数据元素将参数发送到脚本,脚本反过来解析 smartctl 的输出,如果你想添加你自己的东西,没问题——代码中有注释可以帮助你做到这一点。
from subprocess import Popen, PIPE, check_output
import re
import json
import sys
import hashlib
path = '\"C:\\Program Files\\smartmontools\\bin\\smartctl\"' # for linux use 'sudo smartctl'
smart_params = ['Model_Family', 'Device_Model', 'Serial_Number', 'test_result', 'Firmware_Version'] # if u need more \
# add ur data to this list
codec = 'windows-1252' # for linux use utf8
def params(disk_name, raw_data=""):
# Pars output from smartctl
if raw_data not in smart_params and raw_data != "":
# Pars smartctl data from sensors (smartctl -A /dev/sd*)
out_data = re.findall(r'{}.*- *(\d+)'.format(raw_data),
Popen('{} -x /dev/{}'.format(path, disk_name), shell=True, stdout=PIPE,
).communicate()[0].decode(codec))
return out_data[0]
elif raw_data != "" and raw_data in smart_params:
# Pars smartctl information about disks (smartctl -i /dev/sd*)
out_data = re.findall(r'{}. *(.*)'.format(raw_data.replace('_', ' ')),
Popen('{} -x /dev/{}'.format(path, disk_name), shell=True, stdout=PIPE,
).communicate()[0].decode(codec))
return out_data[0]
elif raw_data == "":
# check sum of smartctl --scan
hash_object = hashlib.sha224(check_output(path + " --scan"))
return hash_object.hexdigest()
try:
# if no argumens from cli works as discovery
try:
if sys.argv[1] and sys.argv[2]:
print(params(sys.argv[1], sys.argv[2]))
except IndexError:
print(params(sys.argv[1]))
except IndexError:
# Discovery for disks
data = check_output(path + " --scan").decode(codec)
disks = set(re.findall(r'/dev/(.{3})', data))
output = []
for disk in disks:
smart = check_output(path + " -i /dev/{}".format(disk)).decode(codec)
if smart:
output.append({"{#DISKNAME}": disk, "{#SMART_ENABLED}": "1"})
else:
output.append({"{#DISKNAME}": disk, "{#SMART_ENABLED}": "0"})
output = {"data": output}
print(json.dumps(output))
UserParameter=uHDD.discovery,C:\\Users\\%username%\\AppData\\Local\\Programs\\Python\\Python36-32\\python.exe C:\\zabbix\\lld\\hdd_discovery.py
UserParameter=uHDD[*],C:\\Users\\%username%\\AppData\\Local\\Programs\\Python\\Python36-32\\python.exe C:\\zabbix\\lld\\hdd_discovery.py $1 $2
- 赞
- 收藏
- 评论
- 分享
- 举报
Recommend
-
73
官网提高的python版本监控redis很久没有更新了,今天按照官网操作的一遍,出现了好多报错,后来问了一个哥们,他给了我监控配置文件和脚本。下面我分享给大家参考参考。添加zabbix-agent配置文件[root@test-mq01redis]#cat/etc/zabbix/zabbix_agentd.d/redis.confU...
-
51
Linux监控平台介绍常见开源监控软件:cacti、nagios、zabbix、smokeping、open-falcon等等,其中cacti、smokeping偏向于基础监控,成图非常漂亮。cacti、nagios、zabbix服务端监控中心,需要php环境支持,其中zabbix和cacti都需要mysql作为数据存储,nagios不用存...
-
76
linux监控平台介绍常见开源监控软件:cacti,nagios,zabbix,smokeping,open-falcon等等cacti,smokeping偏向于基础监控,成图非常漂亮cacti,nagios,zabbix服务端监控中心,需要php环境支持,其中zabbix和cacti需要MySQL作为数据储存,agios不用存储历史数据,他注...
-
94
1、从https://pypi.python.org/pypi/docker-py#downloads这里下载docker-py-xxx.tar.gz到本地再传进去linux系统里面进行解压pythonsetup.pyinstall2、在agentd那里加入自定义模块UserParameter=docker_low_discovery[],/bin/bash/etc/zabbix/scripts/
-
51
利用zabbix自定义模块实现对mysql主从状态的监控,并实现邮件告警处理
-
2
本文转载自:使用 Smartmontools 检测硬盘坏道 1 在 Linux 上使用坏块工具检查坏道 坏块工具可以让用户扫描设备检查坏道或坏块,设备可以是一个磁盘或外...
-
6
【zabbix实现动环监控 二】 烟雾温湿度 根据机房建设要求
-
2
【zabbix实现动环监控 三】 漏水检测 机房漏水检测主要是检测漏水环境如下: 下雨天花板渗水 空调漏水的惨痛经历~~ 由于施工,空调排水管道堵塞了,大功率空调漏水的速度过快,直接从防静电木板...
-
5
【zabbix实现动环监控 四】 普通空调检测 机房监控中重要的一个指标就是温湿度控制,其中空调的运行状态将直接影响此指标,故对机房空调的状态监控尤为重要。机房空调有普通空调与精密空调,对于中小企业的公司来说,自建机房很多时候都是用的普通...
-
2
39-Zabbix实现SNMP网络设备监控及分布式Proxy代理实现和自动化运维 精选 原创 监控网络设备 SNMP即...
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK