2

Smartmontools+python+zabbix实现WIN下智能硬盘监控

 4 weeks ago
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下智能硬盘监控

精选 原创

星星猫 2024-04-19 09:51:21 博主文章分类:Operations ©著作权

文章标签 Windows Python json 文章分类 运维 阅读数121

Smartmontools+python+zabbix实现WIN下智能硬盘监控_Python

Smartmontools(S.M.A.R.T. Monitoring Tools)是一组实用程序,包括smartctl和smartd两部分,用于控制和监控S.M.A.R.T信息,适用于大部分ATA、SCSI和NVMe硬盘

Smartmontools可以显示由S.M.A.R.T监测到的硬盘问题,通知即将可能会发生的故障,提示用户及时备份数据

# 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 的属性发现。它可以在主机或链接模板级别被覆盖。

CHANGE_IF_NEEDED

{$SMART.DISK.NAME.MATCHES}

此宏用于替代用于筛选 ID 的属性发现。它可以在主机或链接模板级别被覆盖。

CHANGE_IF_NEEDED

{$SMART.TEMPERATURE.MAX.CRIT}

此宏用于触发器表达式。它可以在主机或链接模板级别被覆盖。

65

{$SMART.TEMPERATURE.MAX.WARN}

此宏用于触发器表达式。它可以在主机或链接模板级别被覆盖。

50

关键和其他信息

发现 SMART 磁盘。

ZABBIX_PASSIVE

smart.disk.discovery

重写:

自检
- {#DISKTYPE} MATCHES_REGEX
- ITEM_PROTOTYPE LIKE - NO_DISCOVERnvmeSelf-test

不是 NVMe
- {#DISKTYPE} NOT_MATCHES_REGEX
- ITEM_PROTOTYPE REGEXP 'Medianvme

发现磁盘的 SMART 供应商特定属性。

ZABBIX_PASSIVE

smart.attribute.discovery

重写:

ID 筛选器
- {#ID} MATCHES_REGEX - {#NAME} MATCHES_REGEX
- ITEM_PROTOTYPE REGEXP '' - NO_DISCOVER{$SMART.ATTRIBUTE.ID.MATCHES}{$SMART.DISK.NAME.MATCHES}

将模板添加到Zabbix

进入主机配置 切换到模板页 增加"SMART by Zabbix agent 2"模板

Smartmontools+python+zabbix实现WIN下智能硬盘监控_Python_02

切换到自动发现规则页 执行SMART发现规则

Smartmontools+python+zabbix实现WIN下智能硬盘监控_json_03

自动发现SMART监控项成功, 获取数据正常

Smartmontools+python+zabbix实现WIN下智能硬盘监控_Windows_04

您也可以添加自己的数据项。

逻辑如下:数据元素将参数发送到脚本,脚本反过来解析 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

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK