[modules/smartstatus] Updated to latest API

This commit is contained in:
tobi-wan-kenobi 2020-04-26 10:07:17 +02:00
parent 8e29bf8f9e
commit ec2b2a8a4b
2 changed files with 29 additions and 29 deletions

View file

@ -64,6 +64,7 @@ def main():
core.event.trigger('stop') core.event.trigger('stop')
if __name__ == "__main__": if __name__ == "__main__":
main()
try: try:
main() main()
except Exception as e: except Exception as e:

View file

@ -12,17 +12,19 @@ Parameters:
import os import os
import bumblebee.util import shutil
import bumblebee.output
import bumblebee.engine
from shutil import which import core.module
from subprocess import Popen, PIPE import core.widget
import core.decorators
import util.cli
class Module(core.module.Module):
@core.decorators.every(minutes=5)
def __init__(self, config):
super().__init__(config, [])
class Module(bumblebee.engine.Module):
def __init__(self, engine, config):
super(Module, self).__init__(engine, config, None)
self.devices = self.list_devices() self.devices = self.list_devices()
self.display = self.parameter('display', 'combined') self.display = self.parameter('display', 'combined')
self.drives = self.parameter('drives', 'sda') self.drives = self.parameter('drives', 'sda')
@ -31,7 +33,7 @@ class Module(bumblebee.engine.Module):
def create_widgets(self): def create_widgets(self):
widgets = [] widgets = []
if self.display == 'combined': if self.display == 'combined':
widget = bumblebee.output.Widget() widget = core.widget.Widget()
widget.set('device', 'combined') widget.set('device', 'combined')
widget.set('assessment', self.combined()) widget.set('assessment', self.combined())
self.output(widget) self.output(widget)
@ -40,15 +42,15 @@ class Module(bumblebee.engine.Module):
for device in self.devices: for device in self.devices:
if self.display == 'singles' and device not in self.drives: if self.display == 'singles' and device not in self.drives:
continue continue
widget = bumblebee.output.Widget() widget = core.widget.Widget()
widget.set('device', device) widget.set('device', device)
widget.set('assessment', self.smart(device)) widget.set('assessment', self.smart(device))
self.output(widget) self.output(widget)
widgets.append(widget) widgets.append(widget)
return widgets return widgets
def update(self, widgets): def update(self):
for widget in widgets: for widget in self.widgets():
device = widget.get('device') device = widget.get('device')
if device == 'combined': if device == 'combined':
widget.set('assessment', self.combined()) widget.set('assessment', self.combined())
@ -89,16 +91,14 @@ class Module(bumblebee.engine.Module):
return devices return devices
def smart(self, disk_name): def smart(self, disk_name):
SMARTCTL_PATH = which('smartctl') smartctl = shutil.which('smartctl')
assessment = None assessment = None
cmd = Popen(
['sudo', SMARTCTL_PATH, '--health', os.path.join('/dev/', disk_name)], output = util.cli.execute('sudo {} --health {}'.format(
stdout=PIPE, smartctl, os.path.join('/dev/', disk_name)
stderr=PIPE, ))
) output = output.split('\n')
_stdout, _stderr = [i.decode('utf8') for i in cmd.communicate()] line = output[4]
_stdout = _stdout.split('\n')
line = _stdout[4]
if 'SMART' in line: if 'SMART' in line:
if any([i in line for i in ['PASSED', 'OK']]): if any([i in line for i in ['PASSED', 'OK']]):
assessment = 'OK' assessment = 'OK'
@ -106,14 +106,13 @@ class Module(bumblebee.engine.Module):
assessment = 'Fail' assessment = 'Fail'
if assessment == 'OK': if assessment == 'OK':
cmd = Popen( output = util.cli.execute('sudo {} -A {}'.format(
['sudo', SMARTCTL_PATH, '-A', os.path.join('/dev/', disk_name)], smartctl, os.path.join('/dev/', disk_name)
stdout=PIPE, ))
stderr=PIPE, output = output.split('\n')
) for line in output:
_stdout, _stderr = [i.decode('utf8') for i in cmd.communicate()]
_stdout = _stdout.split('\n')
for line in _stdout:
if 'Pre-fail' in line: if 'Pre-fail' in line:
assessment = 'Pre-fail' assessment = 'Pre-fail'
return assessment return assessment
# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4