From 906a31509899b17a888526648289f50dee8ff62e Mon Sep 17 00:00:00 2001 From: Hank Leininger Date: Sun, 23 Jun 2024 16:15:51 -0600 Subject: [PATCH] wrapper-scripts/*: support py3.12, use raw strings for regexes Only regexes that include escapes and such cause errors, but use r"" everywhere for consistency. Closes: https://github.com/eLvErDe/hwraid/issues/146 Signed-off-by: Hank Leininger --- wrapper-scripts/aacraid-status | 10 ++--- wrapper-scripts/megaclisas-status | 62 +++++++++++++++---------------- wrapper-scripts/megaide-status | 8 ++-- wrapper-scripts/sas2ircu-status | 32 ++++++++-------- 4 files changed, 56 insertions(+), 56 deletions(-) diff --git a/wrapper-scripts/aacraid-status b/wrapper-scripts/aacraid-status index 600f93f..973f856 100644 --- a/wrapper-scripts/aacraid-status +++ b/wrapper-scripts/aacraid-status @@ -107,7 +107,7 @@ 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] @@ -115,7 +115,7 @@ def returnArrayInfo(output, ctrl_id="?"): # 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: @@ -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() @@ -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: diff --git a/wrapper-scripts/megaclisas-status b/wrapper-scripts/megaclisas-status index 071e514..d17d0c1 100755 --- a/wrapper-scripts/megaclisas-status +++ b/wrapper-scripts/megaclisas-status @@ -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: @@ -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) @@ -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") @@ -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 @@ -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 @@ -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() @@ -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 @@ -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": @@ -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": @@ -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()): @@ -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 @@ -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": @@ -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)) @@ -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: diff --git a/wrapper-scripts/megaide-status b/wrapper-scripts/megaide-status index e918c49..fcb9e96 100755 --- a/wrapper-scripts/megaide-status +++ b/wrapper-scripts/megaide-status @@ -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() diff --git a/wrapper-scripts/sas2ircu-status b/wrapper-scripts/sas2ircu-status index eec897d..2b2ff1f 100755 --- a/wrapper-scripts/sas2ircu-status +++ b/wrapper-scripts/sas2ircu-status @@ -44,7 +44,7 @@ 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" @@ -52,9 +52,9 @@ def getCtrlList(): 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]) @@ -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) @@ -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])