2017-10-09 09:18:37 -04:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
|
|
|
|
# Email alerts about disks if there are any errors found.
|
|
|
|
# It can take a LONG time depending on the speed/type of disk and size of disk.
|
|
|
|
# Should probably only cron it once a week or so.
|
|
|
|
|
2017-10-09 14:38:33 -04:00
|
|
|
import datetime
|
2017-10-09 09:18:37 -04:00
|
|
|
import os
|
|
|
|
import subprocess
|
2017-10-09 14:38:33 -04:00
|
|
|
import sys
|
|
|
|
|
|
|
|
# needs badblocks, smartctl, and parted installed also
|
2017-10-09 09:18:37 -04:00
|
|
|
|
|
|
|
def getDisks():
|
|
|
|
disks = []
|
2017-10-09 09:42:26 -04:00
|
|
|
with open(os.devnull, 'w') as _DEVNULL:
|
|
|
|
_rawlist = subprocess.run(['parted',
|
|
|
|
'--list',
|
|
|
|
'--machine',
|
|
|
|
'--script'],
|
|
|
|
stdout = subprocess.PIPE,
|
|
|
|
stderr = _DEVNULL).stdout.decode('utf-8')
|
|
|
|
for l in _rawlist.splitlines():
|
|
|
|
if l in ('', 'BYT;'):
|
|
|
|
continue # Skip empty lines and markers for new devices
|
|
|
|
elif l.startswith('/'):
|
|
|
|
# It's a device path.
|
|
|
|
_l = l.split(':')
|
|
|
|
if _l[2] not in ('md', 'dm'): # Skip non-block devices like MDADM arrays, LVM volumes
|
|
|
|
if _l[0] not in disks:
|
|
|
|
disks.append(_l[0])
|
2017-10-09 09:18:37 -04:00
|
|
|
return(disks)
|
|
|
|
|
|
|
|
def chkDisk(disk):
|
2017-10-09 14:45:37 -04:00
|
|
|
d = '.'.join(os.path.split(disk)[1:])
|
|
|
|
os.makedirs('/var/log/badblocks', exist_ok = True)
|
|
|
|
if os.path.isfile('/var/log/badblocks/{0}.log'.format(d)):
|
2017-10-09 14:38:33 -04:00
|
|
|
# for some reason this file was just created within the past 24 hours,
|
|
|
|
# so we better play it safe and write to a different log file
|
|
|
|
now = datetime.datetime.now()
|
2017-10-09 14:45:37 -04:00
|
|
|
modified = datetime.datetime.fromtimestamp(os.path.getmtime('/var/log/badblocks/{0}.log'.format(d)))
|
2017-10-09 14:38:33 -04:00
|
|
|
diff = now - modified
|
|
|
|
timedelta = datetime.timedelta(days = 1)
|
|
|
|
if not diff >= timedelta:
|
|
|
|
d += '_secondary'
|
|
|
|
bb = ['badblocks',
|
2017-10-09 14:46:47 -04:00
|
|
|
'-o', '/var/log/badblocks/{0}.log'.format(d),
|
2017-10-09 14:38:33 -04:00
|
|
|
disk]
|
|
|
|
smctl = ['smartctl',
|
2017-10-09 14:46:47 -04:00
|
|
|
'-t', 'long',
|
|
|
|
'-d', 'sat',
|
2017-10-09 14:38:33 -04:00
|
|
|
disk]
|
2017-10-09 14:56:19 -04:00
|
|
|
with open(os.devnull, 'w') as DEVNULL:
|
|
|
|
for c in (bb, smctl):
|
|
|
|
subprocess.run(c, stdout = DEVNULL)
|
2017-10-09 14:38:33 -04:00
|
|
|
return()
|
2017-10-09 09:18:37 -04:00
|
|
|
|
|
|
|
def userChk():
|
|
|
|
# Needs to be run as root/with sudo, because of e.g. cryptsetup, etc.
|
|
|
|
if os.geteuid() != 0:
|
|
|
|
raise PermissionError('This script must be run with root privileges.')
|
|
|
|
|
2017-10-09 16:32:13 -04:00
|
|
|
def main():
|
|
|
|
userChk()
|
|
|
|
for d in getDisks():
|
|
|
|
chkDisk(d)
|
|
|
|
|
2017-10-09 09:18:37 -04:00
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|