Last active
August 29, 2015 14:25
-
-
Save lalomartins/fcee89b391d33143ce3a to your computer and use it in GitHub Desktop.
Watch progress of a zpool (zfs) resilver — Python 3, Qt5
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
import re, subprocess, sys | |
from PyQt5.QtCore import Qt, QTimer | |
from PyQt5.QtWidgets import ( | |
QApplication, | |
QWidget, | |
QLabel, | |
QProgressBar, | |
QHBoxLayout, | |
QVBoxLayout, | |
) | |
percent_re = re.compile(r'([\d.]+)%') | |
def get_status(pool): | |
output = subprocess.check_output(['gksudo', 'zpool', 'status', pool], | |
universal_newlines=True) | |
prev = percent = percent_match = None | |
for line in output.split('\n'): | |
if '%' in line: | |
percent_match = percent_re.search(line) | |
break | |
prev = line | |
if percent_match is None: | |
return None | |
percent = float(percent_match.group(1)) | |
return prev.strip(), percent | |
def init_ui(pool): | |
# bit of a hack but it allows us to reload the module without a segfault | |
global app, main_window, details_label, progress_bar | |
try: | |
app | |
except NameError: | |
app = QApplication(sys.argv) | |
else: | |
main_window.destroy() | |
main_window = QWidget() | |
main_window.resize(250, 150) | |
main_window.move(300, 300) | |
main_window.setWindowTitle('Resilver progress of {}'.format(pool)) | |
hbox = QHBoxLayout() | |
# hbox.addStretch(1) | |
vbox = QVBoxLayout() | |
# vbox.addStretch(1) | |
hbox.addLayout(vbox) | |
main_window.setLayout(hbox) | |
details_label = QLabel('loading…') | |
details_label.setAlignment(Qt.AlignCenter) | |
vbox.addWidget(details_label) | |
progress_bar = QProgressBar() | |
progress_bar.setAlignment(Qt.AlignCenter) | |
vbox.addWidget(progress_bar) | |
def update_status(pool): | |
details, percent = get_status(pool) | |
details_label.setText(details) | |
progress_bar.setValue(percent) | |
main_window.setWindowTitle('{}% — Resilver progress of {}'.format(percent, pool)) | |
def start_timer(pool, interval=300000): | |
global timer | |
timer = QTimer() | |
timer.setInterval(interval) | |
def handler(): | |
update_status(pool) | |
timer.timeout.connect(handler) | |
timer.start() | |
if __name__ == '__main__': | |
pool = sys.argv[1] | |
init_ui(pool) | |
main_window.show() | |
update_status(pool) | |
start_timer(pool) | |
sys.exit(app.exec_()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment