2020-04-26 10:01:51 +02:00
|
|
|
# -*- coding: UTF-8 -*-
|
|
|
|
|
|
|
|
# smart function inspired by py-SMART https://github.com/freenas/py-SMART
|
|
|
|
# under Copyright (C) 2015 Marc Herndon and GPL2
|
|
|
|
|
|
|
|
"""Displays HDD smart status of different drives or all drives
|
|
|
|
|
|
|
|
Parameters:
|
2020-04-26 10:02:05 +02:00
|
|
|
* smartstatus.display: how to display (defaults to 'combined', other choices: 'seperate' or 'singles')
|
2020-05-07 06:18:17 +02:00
|
|
|
* smartstatus.drives: in the case of singles which drives to display, separated comma list value, multiple accepted (defaults to 'sda', example:'sda,sdc')
|
2020-05-08 16:12:34 +02:00
|
|
|
* smartstatus.show_names: boolean in the form of "True" or "False" to show the name of the drives in the form of sda, sbd, combined or none at all.
|
2020-04-26 10:01:51 +02:00
|
|
|
"""
|
|
|
|
|
|
|
|
import os
|
|
|
|
|
2020-04-26 10:07:17 +02:00
|
|
|
import shutil
|
2020-04-26 10:01:51 +02:00
|
|
|
|
2020-04-26 10:07:17 +02:00
|
|
|
import core.module
|
|
|
|
import core.decorators
|
2020-04-26 10:01:51 +02:00
|
|
|
|
2020-04-26 10:07:17 +02:00
|
|
|
import util.cli
|
2020-05-08 16:12:34 +02:00
|
|
|
import util.format
|
2020-04-26 10:07:17 +02:00
|
|
|
|
2020-05-03 11:15:52 +02:00
|
|
|
|
2020-04-26 10:07:17 +02:00
|
|
|
class Module(core.module.Module):
|
|
|
|
@core.decorators.every(minutes=5)
|
2020-04-26 16:39:24 +02:00
|
|
|
def __init__(self, config, theme):
|
|
|
|
super().__init__(config, theme, [])
|
2020-04-26 10:01:51 +02:00
|
|
|
|
|
|
|
self.devices = self.list_devices()
|
2020-05-03 11:15:52 +02:00
|
|
|
self.display = self.parameter("display", "combined")
|
|
|
|
self.drives = self.parameter("drives", "sda")
|
2020-05-08 16:12:34 +02:00
|
|
|
self.show_names = util.format.asbool(self.parameter("show_names", True))
|
2020-05-09 10:57:44 +02:00
|
|
|
self.create_widgets()
|
2020-04-26 10:01:51 +02:00
|
|
|
|
|
|
|
def create_widgets(self):
|
2020-05-03 11:15:52 +02:00
|
|
|
if self.display == "combined":
|
2020-05-09 10:57:44 +02:00
|
|
|
widget = self.add_widget()
|
2020-05-03 11:15:52 +02:00
|
|
|
widget.set("device", "combined")
|
|
|
|
widget.set("assessment", self.combined())
|
2020-04-26 10:01:51 +02:00
|
|
|
self.output(widget)
|
|
|
|
else:
|
|
|
|
for device in self.devices:
|
2020-05-03 11:15:52 +02:00
|
|
|
if self.display == "singles" and device not in self.drives:
|
2020-04-26 10:01:51 +02:00
|
|
|
continue
|
2020-05-09 10:57:44 +02:00
|
|
|
widget = self.add_widget()
|
2020-05-03 11:15:52 +02:00
|
|
|
widget.set("device", device)
|
|
|
|
widget.set("assessment", self.smart(device))
|
2020-04-26 10:01:51 +02:00
|
|
|
self.output(widget)
|
|
|
|
|
2020-04-26 10:07:17 +02:00
|
|
|
def update(self):
|
|
|
|
for widget in self.widgets():
|
2020-05-03 11:15:52 +02:00
|
|
|
device = widget.get("device")
|
|
|
|
if device == "combined":
|
|
|
|
widget.set("assessment", self.combined())
|
2020-04-26 10:01:51 +02:00
|
|
|
self.output(widget)
|
|
|
|
else:
|
2020-05-03 11:15:52 +02:00
|
|
|
widget.set("assessment", self.smart(device))
|
2020-04-26 10:01:51 +02:00
|
|
|
self.output(widget)
|
|
|
|
|
|
|
|
def output(self, widget):
|
2020-05-03 11:15:52 +02:00
|
|
|
device = widget.get("device")
|
|
|
|
assessment = widget.get("assessment")
|
2020-05-08 16:12:34 +02:00
|
|
|
if self.show_names:
|
2020-05-07 06:18:17 +02:00
|
|
|
widget.full_text("{}: {}".format(device, assessment))
|
2020-05-08 16:12:34 +02:00
|
|
|
else:
|
|
|
|
widget.full_text("{}".format(assessment))
|
2020-04-26 10:01:51 +02:00
|
|
|
|
|
|
|
def state(self, widget):
|
|
|
|
states = []
|
2020-05-03 11:15:52 +02:00
|
|
|
assessment = widget.get("assessment")
|
|
|
|
if assessment == "Pre-fail":
|
|
|
|
states.append("warning")
|
|
|
|
if assessment == "Fail":
|
|
|
|
states.append("critical")
|
2020-04-26 10:01:51 +02:00
|
|
|
return states
|
|
|
|
|
|
|
|
def combined(self):
|
|
|
|
for device in self.devices:
|
|
|
|
result = self.smart(device)
|
2020-05-03 11:15:52 +02:00
|
|
|
if result == "Fail":
|
|
|
|
return "Fail"
|
|
|
|
if result == "Pre-fail":
|
|
|
|
return "Pre-fail"
|
|
|
|
return "OK"
|
2020-04-26 10:01:51 +02:00
|
|
|
|
|
|
|
def list_devices(self):
|
2020-05-03 11:15:52 +02:00
|
|
|
for (root, folders, files) in os.walk("/dev"):
|
|
|
|
if root == "/dev":
|
|
|
|
devices = {
|
|
|
|
"".join(filter(lambda i: i.isdigit() == False, file))
|
|
|
|
for file in files
|
|
|
|
if "sd" in file
|
|
|
|
}
|
|
|
|
nvme = {
|
|
|
|
file for file in files if ("nvme0n" in file and "p" not in file)
|
|
|
|
}
|
2020-04-26 10:01:51 +02:00
|
|
|
devices.update(nvme)
|
|
|
|
return devices
|
|
|
|
|
|
|
|
def smart(self, disk_name):
|
2020-05-03 11:15:52 +02:00
|
|
|
smartctl = shutil.which("smartctl")
|
2020-04-26 10:01:51 +02:00
|
|
|
assessment = None
|
2020-04-26 10:07:17 +02:00
|
|
|
|
2020-05-03 11:15:52 +02:00
|
|
|
output = util.cli.execute(
|
|
|
|
"sudo {} --health {}".format(smartctl, os.path.join("/dev/", disk_name))
|
|
|
|
)
|
|
|
|
output = output.split("\n")
|
2020-04-26 10:07:17 +02:00
|
|
|
line = output[4]
|
2020-05-03 11:15:52 +02:00
|
|
|
if "SMART" in line:
|
|
|
|
if any([i in line for i in ["PASSED", "OK"]]):
|
|
|
|
assessment = "OK"
|
2020-04-26 10:01:51 +02:00
|
|
|
else:
|
2020-05-03 11:15:52 +02:00
|
|
|
assessment = "Fail"
|
2020-04-26 10:01:51 +02:00
|
|
|
|
2020-05-03 11:15:52 +02:00
|
|
|
if assessment == "OK":
|
|
|
|
output = util.cli.execute(
|
|
|
|
"sudo {} -A {}".format(smartctl, os.path.join("/dev/", disk_name))
|
|
|
|
)
|
|
|
|
output = output.split("\n")
|
2020-04-26 10:07:17 +02:00
|
|
|
for line in output:
|
2020-05-03 11:15:52 +02:00
|
|
|
if "Pre-fail" in line:
|
|
|
|
assessment = "Pre-fail"
|
2020-04-26 10:01:51 +02:00
|
|
|
return assessment
|
2020-04-26 10:07:17 +02:00
|
|
|
|
2020-05-03 11:15:52 +02:00
|
|
|
|
2020-04-26 10:07:17 +02:00
|
|
|
# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4
|