Skip to content

Commit

Permalink
wrapper-scripts/*: support py3.12, use raw strings for regexes
Browse files Browse the repository at this point in the history
Only regexes that include escapes and such cause errors, but use r""
everywhere for consistency.

Closes: eLvErDe#146
Signed-off-by: Hank Leininger <hlein@korelogic.com>
  • Loading branch information
hlein committed Jun 23, 2024
1 parent 8ee64ed commit 906a315
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 56 deletions.
10 changes: 5 additions & 5 deletions wrapper-scripts/aacraid-status
Original file line number Diff line number Diff line change
Expand Up @@ -107,15 +107,15 @@ def returnArrayInfo(output, ctrl_id="?"):
# Cut on : and re-join everything except first part
line = ":".join(line.split(":")[1:]).strip()
# Extract everything between ()
device_state = re.search("\s+\((.*)\)\s+", line).group(1)
device_state = re.search(r"\s+\((.*)\)\s+", line).group(1)
# Coma separated
device_attrs = device_state.split(",")
device_attrs = [x.strip() for x in device_attrs]
# Some magic here...
# Strip out from the list element not matching Xyz:12
# Split on column: now we have and array of arrays like [['Controller', '1'], ['Connector', '1'], ['Device', '0']]
# Casting to dict will turn Controller as key and 1 as value
device_attrs = dict([x.split(":") for x in device_attrs if re.match("\w+:\d+", x)])
device_attrs = dict([x.split(":") for x in device_attrs if re.match(r"\w+:\d+", x)])

# Extract id for this device
if "Controller" in device_attrs:
Expand Down Expand Up @@ -174,7 +174,7 @@ def returnDisksInfo(output, controllerid):
for line in output:
if re.match(r"^Reported Channel,Device(\(T:L\))?\s+: [0-9]+,[0-9]+(\([0-9]+:[0-9]+\))?$", line.strip()):
diskid = re.split("\s:\s", line)[1].strip()
diskid = re.sub("\(.*\)", "", diskid)
diskid = re.sub(r"\(.*\)", "", diskid)
diskid = str(controllerid) + "," + diskid
if re.match(r"^State\s+:.*$", line.strip()):
state = line.split(":")[1].strip()
Expand Down Expand Up @@ -290,8 +290,8 @@ if printarray:
# Usually it should return either [0-9] or Simple_Volume but...
# It can also return "6 Reed-Solomon" so we need to handle this too...
# So let's match [0-9] followed by a space or EOL.
if re.match("^[0-9]+(\s|$)", arrayinfo[0]):
raidtype = re.sub("^", "RAID", arrayinfo[0])
if re.match(r"^[0-9]+(\s|$)", arrayinfo[0]):
raidtype = re.sub(r"^", "RAID", arrayinfo[0])
else:
raidtype = arrayinfo[0]
for tasks in tasksinfo:
Expand Down
62 changes: 31 additions & 31 deletions wrapper-scripts/megaclisas-status
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ def returnROCTemp(output):
for line in output:
if re.match(r"^ROC temperature :.*$", line.strip()):
tmpstr = line.split(":")[1].strip()
ROCtemp = re.sub(" +.*$", "", tmpstr)
ROCtemp = re.sub(r" +.*$", "", tmpstr)
if ROCtemp != "":
return str(str(ROCtemp) + "C")
else:
Expand All @@ -254,7 +254,7 @@ def returnBBUPresence(output):
for line in output:
if re.match(r"^BBU +:.*$", line.strip()):
tmpstr = line.split(":")[1].strip()
BBU = re.sub(" +.*$", "", tmpstr)
BBU = re.sub(r" +.*$", "", tmpstr)
break
if BBU != "":
return str(BBU)
Expand All @@ -268,7 +268,7 @@ def returnBBUStatus(output):
for line in output:
if re.match(r"^ *Battery Replacement required +:.*$", line.strip()):
tmpstr = line.split(":")[1].strip()
BBUStatus = re.sub(" +.*$", "", tmpstr)
BBUStatus = re.sub(r" +.*$", "", tmpstr)
break
if BBUStatus == "Yes":
return str("REPL")
Expand Down Expand Up @@ -374,23 +374,23 @@ def returnArrayInfo(output, controllerid, arrayid, arrayindex):
diskperspan = int(line.strip().split(":")[1].strip())
elif re.match(r"^Current Cache Policy.*?:.*$", line.strip()):
props = line.strip().split(":")[1].strip()
if re.search("ReadAdaptive", props):
if re.search(r"ReadAdaptive", props):
properties += "ADRA"
if re.search("ReadAhead", props):
if re.search(r"ReadAhead", props):
properties += "RA"
if re.match("ReadAheadNone", props):
if re.match(r"ReadAheadNone", props):
properties += "NORA"
if re.search("WriteBack", props):
if re.search(r"WriteBack", props):
properties += ",WB"
if re.match("WriteThrough", props):
if re.match(r"WriteThrough", props):
properties += ",WT"
elif re.match(r"^Disk Cache Policy.*?:.*$", line.strip()):
props = line.strip().split(":")[1].strip()
if re.search("Disabled", props):
if re.search(r"Disabled", props):
dskcache = "Disabled"
if re.search("Disk.s Default", props):
if re.search(r"Disk.s Default", props):
dskcache = "Default"
if re.search("Enabled", props):
if re.search(r"Enabled", props):
dskcache = "Enabled"
elif re.match(r"^Ongoing Progresses.*?:.*$", line.strip()):
operationlinennumber = linenumber
Expand Down Expand Up @@ -458,7 +458,7 @@ def returnDiskInfo(output, controllerid):
for line in output:
if re.match(r"^Span: [0-9]+ - Number of PDs:", line.strip()):
spanid = line.split(":")[1].strip()
spanid = re.sub(" - Number of PDs.*", "", spanid)
spanid = re.sub(r" - Number of PDs.*", "", spanid)
elif re.match(r"Enclosure Device ID: .*$", line.strip()):
# We match here early in the analysis so reset the vars if this is a new disk we're reading..
oldenclid = enclid
Expand All @@ -472,8 +472,8 @@ def returnDiskInfo(output, controllerid):
lsidid = "Unknown"
elif re.match(r"^Coerced Size: ", line.strip()):
dsize = line.split(":")[1].strip()
dsize = re.sub(" \[.*\.*$", "", dsize)
dsize = re.sub("[0-9][0-9] GB", " Gb", dsize)
dsize = re.sub(r" \[.*\.*$", "", dsize)
dsize = re.sub(r"[0-9][0-9] GB", " Gb", dsize)
elif re.match(r"^(CacheCade )?Virtual (Disk|Drive): [0-9]+.*$", line.strip()):
arrayindex += 1
arrayid = line.split("(")[0].split(":")[1].strip()
Expand All @@ -487,11 +487,11 @@ def returnDiskInfo(output, controllerid):
slotid = line.split(":")[1].strip()
elif re.match(r"Firmware state: .*$", line.strip()):
fstate = line.split(":")[1].strip()
subfstate = re.sub("\(.*", "", fstate)
subfstate = re.sub(r"\(.*", "", fstate)
dbgprint("Firmware State: " + str(fstate) + " " + str(subfstate))
elif re.match(r"Inquiry Data: .*$", line.strip()):
model = line.split(":")[1].strip()
model = re.sub(" +", " ", model)
model = re.sub(r" +", " ", model)

# re-define our "sub-code"
# our seagate drives have an ID string of
Expand All @@ -508,10 +508,10 @@ def returnDiskInfo(output, controllerid):
continue

# Sub code
manuf = re.sub(" .*", "", model)
dtype = re.sub(manuf + " ", "", model)
dtype = re.sub(" .*", "", dtype)
hwserial = re.sub(".*" + dtype + " *", "", model)
manuf = re.sub(r" .*", "", model)
dtype = re.sub(manuf + r" ", "", model)
dtype = re.sub(r" .*", "", dtype)
hwserial = re.sub(r".*" + dtype + r" *", "", model)
elif re.match(r"^Media Type: .*$", line.strip()):
mtype = line.split(":")[1].strip()
if mtype == "Hard Disk Device":
Expand All @@ -529,7 +529,7 @@ def returnDiskInfo(output, controllerid):
else:
# Drive temp is amongst the last few lines matched, decide here if we add information to the table..
temp = line.split(":")[1].strip()
temp = re.sub(" \(.*\)", "", temp)
temp = re.sub(r" \(.*\)", "", temp)
if model != "Unknown":
dbgprint("Disk Info: " + str(arrayid) + " " + str(diskid) + " " + str(oldenclid))
if subfstate == "Rebuild":
Expand Down Expand Up @@ -578,8 +578,8 @@ def returnUnconfDiskInfo(output, controllerid):

elif re.match(r"^Coerced Size: ", line.strip()):
dsize = line.split(":")[1].strip()
dsize = re.sub(" \[.*\.*$", "", dsize)
dsize = re.sub("[0-9][0-9] GB", " Gb", dsize)
dsize = re.sub(r" \[.*\.*$", "", dsize)
dsize = re.sub(r"[0-9][0-9] GB", " Gb", dsize)
elif re.match(r"^Drive.s posi*tion: DiskGroup: [0-9]+,.*$", line.strip()):
arrayid = line.split(",")[1].split(":")[1].strip()
elif re.match(r"^Device Id: [0-9]+.*$", line.strip()):
Expand All @@ -588,11 +588,11 @@ def returnUnconfDiskInfo(output, controllerid):
slotid = line.split(":")[1].strip()
elif re.match(r"Firmware state: .*$", line.strip()):
fstate = line.split(":")[1].strip()
subfstate = re.sub("\(.*", "", fstate)
subfstate = re.sub(r"\(.*", "", fstate)
dbgprint("Firmware State: " + str(fstate) + " " + str(subfstate))
elif re.match(r"Inquiry Data: .*$", line.strip()):
model = line.split(":")[1].strip()
model = re.sub(" +", " ", model)
model = re.sub(r" +", " ", model)

# re-define our "sub-code"
# our seagate drives have an ID string of
Expand All @@ -608,10 +608,10 @@ def returnUnconfDiskInfo(output, controllerid):
model = "{0} {1:>10} {2}".format(m.group(2), m.group(4), m.group(1))
continue

manuf = re.sub(" .*", "", model)
dtype = re.sub(manuf + " ", "", model)
dtype = re.sub(" .*", "", dtype)
hwserial = re.sub(".*" + dtype + " *", "", model)
manuf = re.sub(r" .*", "", model)
dtype = re.sub(manuf + r" ", "", model)
dtype = re.sub(r" .*", "", dtype)
hwserial = re.sub(r".*" + dtype + r" *", "", model)
elif re.match(r"^Media Type: .*$", line.strip()):
mtype = line.split(":")[1].strip()
if mtype == "Hard Disk Device":
Expand All @@ -629,7 +629,7 @@ def returnUnconfDiskInfo(output, controllerid):
temp = "N/A"
else:
temp = line.split(":")[1].strip()
temp = re.sub("\(.*\)", "", temp)
temp = re.sub(r"\(.*\)", "", temp)
if arrayid == False:
if subfstate == "Unconfigured":
dbgprint("Unconfigured Disk: Arrayid: " + str(arrayid) + " DiskId: " + str(diskid) + " " + str(olddiskid) + " " + str(fstate))
Expand Down Expand Up @@ -826,7 +826,7 @@ if totaldrivenumber:
for array in arraydisk:
diskname = str(controllerid) + array[8] + array[9]
dbgprint("Disk c" + diskname + " status : " + array[5])
if re.match("|".join(["^Online$", "^Online, Spun Up$", "^Rebuilding \(.*"]), array[5]):
if re.match(r"|".join([r"^Online$", r"^Online, Spun Up$", r"^Rebuilding \(.*"]), array[5]):
if AddDisk(NagiosGoodDisks, diskname):
nagiosgooddisk += 1
else:
Expand Down
8 changes: 4 additions & 4 deletions wrapper-scripts/megaide-status
Original file line number Diff line number Diff line change
Expand Up @@ -62,18 +62,18 @@ def returnDiskInfo():
f = open("/etc/megaide-status.conf")
table = []
for line in f:
if re.match("^c[0-9]+u[0-9]+p[0-9]+$", line.strip()):
if re.match(r"^c[0-9]+u[0-9]+p[0-9]+$", line.strip()):
# Valid disk entry
controllerid = line.split("u")[0].strip().strip("c")
diskid = line.split("p")[1].strip()
id = line.strip()
f2 = open("/proc/megaide/" + controllerid + "/physicaldrives/phy_drv_" + diskid + "_info")
for line in f2:
if re.match("^Model No :.*$", line.strip()):
if re.match(r"^Model No :.*$", line.strip()):
model = line.split(":")[1].strip()
if re.match("^Status :.*$", line.strip()):
if re.match(r"^Status :.*$", line.strip()):
state = line.split()[2].strip()
if re.match("^Drive is Not Present.*$", line.strip()):
if re.match(r"^Drive is Not Present.*$", line.strip()):
model = "Unknown"
state = "OFFLINE"
f2.close()
Expand Down
32 changes: 16 additions & 16 deletions wrapper-scripts/sas2ircu-status
Original file line number Diff line number Diff line change
Expand Up @@ -44,17 +44,17 @@ def getCtrlList():
res = getOutput(cmd)
list = []
for line in res:
if re.match("^[0-9]+.*$", line):
if re.match(r"^[0-9]+.*$", line):
ctrlnmbr, ctrlname = int(line.split()[0]), line.split()[1]
# Check if it's a RAID controller and a volume exists
cmd = binarypath + " " + str(ctrlnmbr) + " DISPLAY"
res = getOutput(cmd)
raid = False
validarray = False
for line in res:
if re.match("^RAID Support\s+:\s+Yes$", line):
if re.match(r"^RAID Support\s+:\s+Yes$", line):
raid = True
if re.match("^IR volume [0-9]+.*$", line):
if re.match(r"^IR volume [0-9]+.*$", line):
validarray = True
if raid and validarray:
list.append([ctrlnmbr, ctrlname])
Expand All @@ -72,21 +72,21 @@ def getArrayList(ctrlnmbr):
raidlevel = ""
size = ""
for line in res:
if re.match("^IR volume [0-9]+.*$", line):
if re.match(r"^IR volume [0-9]+.*$", line):
if arrayid == -1:
arrayid = arrayid + 1
else:
list.append([arrayid, raidlevel, size, arraystatus, disklist])
arrayid = arrayid + 1
disklist = []
if re.match("Status of volume.*$", line):
if re.match(r"Status of volume.*$", line):
arraystatus = line.split(":")[1].strip()
if re.match("RAID level.*$", line):
if re.match(r"RAID level.*$", line):
raidlevel = line.split(":")[1].strip()
if re.match("Size \(in MB\)\s+.*$", line):
if re.match(r"Size \(in MB\)\s+.*$", line):
size = line.split(":")[1].strip()
size = str(int(round((float(size) / 1000)))) + "G"
if re.match("^PHY\[[0-9]+\] Enclosure#/Slot#.*$", line):
if re.match(r"^PHY\[[0-9]+\] Enclosure#/Slot#.*$", line):
disksid = ":".join(line.split(":")[1:]).strip()
disksid = disksid.split(":")
disklist.append(disksid)
Expand All @@ -108,25 +108,25 @@ def getDiskList(ctrlnmbr):
realid = ["", ""]
for line in res:
if (
re.match("^Device is a Hard disk.*$", line)
or re.match("^Device is a Enclosure services device.*$", line)
or re.match("^Device is a unknown device.*$", line)
re.match(r"^Device is a Hard disk.*$", line)
or re.match(r"^Device is a Enclosure services device.*$", line)
or re.match(r"^Device is a unknown device.*$", line)
):
if diskid == -1:
diskid = diskid + 1
else:
list.append([diskid, diskstatus, diskmodel, diskserial, realid])
diskid = diskid + 1
if re.match("Enclosure #.*$", line):
if re.match(r"Enclosure #.*$", line):
enclid = line.split(":")[1].strip()
if re.match("Slot #.*$", line):
if re.match(r"Slot #.*$", line):
slotid = line.split(":")[1].strip()
realid = [enclid, slotid]
if re.match("^State.*$", line):
if re.match(r"^State.*$", line):
diskstatus = line.split(":")[1].strip()
if re.match("^Model Number.*$", line):
if re.match(r"^Model Number.*$", line):
diskmodel = line.split(":")[1].strip()
if re.match("^Serial No.*$", line):
if re.match(r"^Serial No.*$", line):
diskserial = line.split(":")[1].strip()
# ie: [[0, 'Optimal (OPT)', 'Hitachi HUA72202', 'JK1151YAHUYAZZ', ['1', '0']], [1, 'Optimal (OPT)', 'Hitachi HUA72202', 'JK1151YAHUW1DZ', ['1', '1']]]
list.append([diskid, diskstatus, diskmodel, diskserial, realid])
Expand Down

0 comments on commit 906a315

Please sign in to comment.