hwraid/wrapper-scripts/megaraidsas-status

109 lines
2.9 KiB
Plaintext
Raw Permalink Normal View History

2013-08-14 23:03:24 +02:00
#!/usr/bin/python
import os
import re
import sys
binarypath = "/usr/sbin/megasasctl"
if len(sys.argv) > 2:
2021-08-17 09:42:37 +02:00
print("Usage: megaraidsas-status [-d]")
2013-08-14 23:03:24 +02:00
sys.exit(1)
printarray = True
if len(sys.argv) > 1:
if sys.argv[1] == "-d":
2013-08-14 23:03:24 +02:00
printarray = False
else:
2021-08-17 09:42:37 +02:00
print("Usage: megaraidsas-status [-d]")
2013-08-14 23:03:24 +02:00
sys.exit(1)
# Check binary exists (and +x), if not print an error message
if os.path.exists(binarypath) and os.access(binarypath, os.X_OK):
pass
2013-08-14 23:03:24 +02:00
else:
sys.exit(3)
2013-08-14 23:03:24 +02:00
# Get command output
def getOutput(cmd):
output = os.popen(cmd)
lines = []
for line in output:
if not re.match(r"^$", line.strip()):
2013-08-14 23:03:24 +02:00
lines.append(line.strip())
return lines
2013-08-14 23:03:24 +02:00
def returnDiskList(output):
lines = []
for line in output:
if re.match(r"^[a-z][0-9]+[a-z][0-9\*]+[a-z][0-9]+\s.*$", line.strip()):
2013-08-14 23:03:24 +02:00
list = line.split()
# Let's hack... Some disk may report smart error after status
# Get theses errors, join them into one list item and place it
# before status
errindex = False
try:
errindex = list.index("errs:")
2013-08-14 23:03:24 +02:00
except ValueError:
pass
if errindex:
list.insert(errindex - 1, " ".join(list[errindex:]))
list = list[: errindex + 1]
2013-08-14 23:03:24 +02:00
lines.append(list)
if fake_failure:
lines[0][-1] = "BAD"
2013-08-14 23:03:24 +02:00
return lines
2013-08-14 23:03:24 +02:00
def returnArrayList(output):
lines = []
for line in output:
if re.match(r"^[a-z][0-9]+[a-z][0-9]+\s.*$", line.strip()):
2013-08-14 23:03:24 +02:00
lines.append(line.split())
if fake_failure:
lines[0][-1] = "DEGRADED"
2013-08-14 23:03:24 +02:00
return lines
2013-08-14 23:03:24 +02:00
# A way to force a fake failure
fake_failure = False
if os.path.exists("/root/fake_megaraid_failure"):
2013-08-14 23:03:24 +02:00
fake_failure = True
cmd = binarypath + " -v"
2013-08-14 23:03:24 +02:00
output = getOutput(cmd)
disklist = returnDiskList(output)
arraylist = returnArrayList(output)
if printarray:
2021-08-17 09:42:37 +02:00
print("-- Arrays informations --")
print("-- ID | Type | Size | Status")
2013-08-14 23:03:24 +02:00
for array in arraylist:
2021-08-17 09:42:37 +02:00
print(array[0] + " | " + array[2] + " " + array[3] + " | " + array[1] + " | " + array[-1])
print("")
2013-08-14 23:03:24 +02:00
2021-08-17 09:42:37 +02:00
print("-- Disks informations")
print("-- ID | Model | Status | Warnings")
2013-08-14 23:03:24 +02:00
for disk in disklist:
# Check if there's some smart non critical warnings
if re.match(r"^errs:.*$", disk[-2]):
2013-08-14 23:03:24 +02:00
# Some disk may have vendor or model containing spaces
2021-08-17 09:42:37 +02:00
print(disk[0] + " | " + " ".join(disk[1:-3]) + " | " + disk[-1] + " | " + disk[-2])
2013-08-14 23:03:24 +02:00
else:
2021-08-17 09:42:37 +02:00
print(disk[0] + " | " + " ".join(disk[1:-2]) + " | " + disk[-1])
2013-08-14 23:03:24 +02:00
# Check if there's a bad disk
bad = False
for array in arraylist:
if not array[-1] == "optimal":
2013-08-14 23:03:24 +02:00
bad = True
for disk in disklist:
if disk[-1] not in ["online", "hotspare", "ready"]:
2013-08-14 23:03:24 +02:00
bad = True
if bad:
2021-08-17 09:42:37 +02:00
print("\nThere is at least one disk/array in a NOT OPTIMAL state.")
2013-08-14 23:03:24 +02:00
sys.exit(1)