Shutdown.py, how to check if scrub is running?

Status
Not open for further replies.

fanix

Dabbler
Joined
Oct 10, 2013
Messages
20
Hi peeps,

I have a python script that runs every 15minutes, it checks if two computers on my network is online, if neither is on, it shutdown FreeNAS. Works perfectly. I want to improve the script so that it checks if a scrub is running, because I dont want it to shutdown in this instance. Is there a command I can use to check if scrub is currently running?

Here is my script:

Code:
#!/usr/bin/env python
 
from subprocess import Popen, PIPE
import os, sys
 
ip_list = []
 
###### IP addresses #####
# IP addresses go underneath this line, one on each line in the format: ip_list.append('x.x.x.x')
ip_list.append('192.168.1.200')
ip_list.append('192.168.1.50')
###### End IP addresses
 
shutdown = True
 
for ip in ip_list:
    cmd = ['/sbin/ping', '-s 0', '-W 1', '-q', '-n' , '-c 1', ip]
    p1 = Popen(cmd, stdout=PIPE, stderr=PIPE)
    p1.wait()
    statusText = p1.communicate()[0].split('\n')[3].strip()
    if statusText.find(', 0.0% packet loss') > 0:
        shutdown = False
 
if shutdown:
    cmd = ['/sbin/shutdown', '-p', 'now']
    p1 = Popen(cmd)
 

Dusan

Guru
Joined
Jan 29, 2013
Messages
1,165
You need to parse the output of zpool status. Something like this should work:
Code:
p = Popen(["zpool", "status", zpool_name], stdout=PIPE)
stdout = p.communicate()[0]
if stdout.find("scrub in progress") != -1:
    # scrub_is_running

You can also omit the zpool_name and it will check if any pool is being scrubbed.

Edit: I just remembered seeing a 9.2.1 changelist that will ask you if you really want to shutdown/reboot while scrub/resilver is in progress. This is how FreeNAS checks it:
https://github.com/freenas/freenas/blob/master/gui/middleware/notifier.py?source=c#L3861
So, you may also want to scan for "resilver" in the output.
The entire change: https://github.com/freenas/freenas/commit/fd8c7fc198300aeacbc7dd6936154c1ecc4d40a6
 
Status
Not open for further replies.
Top