def getLoads(dl): result = 0 loads = {} cmd = "uptime" rval, lines = core.system(cmd, 3 <= dl) if rval == 0 and len(lines) == 1: parts = lines[0].split("load average: ") if len(parts) == 2: load_parts = parts[1].split(', ') if len(load_parts) == 3: loads = { "1min": load_parts[0], "5min": load_parts[1], "15min": load_parts[2] } cmd = "nproc" rval, lines = core.system(cmd, 3 <= dl) if rval == 0 and len(lines) == 1: loads["ncore"] = lines[0] else: loads["ncore"] = 0 return result, loads
def run (self): cmd = "cd " + self.dir + "; " + self.cmd rval = system_piped (cmd, self.pipe, self.dl <= DL) if rval == 0: rval2, lines = system ("touch " + self.dir + "/obs.finished") else: rval2, lines = system ("touch " + self.dir + "/obs.failed") return rval
def run(self): cmd = "cd " + self.dir + "; " + self.cmd rval = system_piped(cmd, self.pipe, self.dl <= DL) if rval == 0: rval2, lines = system("touch " + self.dir + "/obs.finished") else: rval2, lines = system("touch " + self.dir + "/obs.failed") return rval
def run(self): cmd = "cd " + self.dir + "; " + self.cmd self.script.log(2, "UWBProcThread::run " + self.cmd) rval = self.script.system_piped(cmd, self.pipe, 2, self.env) self.script.log(2, "UWBProcThread::run rval=" + str(rval)) if rval == 0: rval2, lines = system("touch " + self.dir + "/obs.finished") else: rval2, lines = system("touch " + self.dir + "/obs.failed") return rval
def run (self): cmd = "cd " + self.dir + "; " + self.cmd self.script.log (2, "UWBProcThread::run " + self.cmd) rval = self.script.system_piped (cmd, self.pipe, 2, self.env) self.script.log (2, "UWBProcThread::run rval=" + str(rval)) if rval == 0: rval2, lines = system ("touch " + self.dir + "/obs.finished") else: rval2, lines = system ("touch " + self.dir + "/obs.failed") return rval
def test_fluxcal(target, fluxcal_on_file, fluxcal_off_file): # check if the target matches the fluxcal off file cmd = "grep " + target + " " + fluxcal_on_file + " | wc -l" rval, lines = system(cmd) if rval == 0 and len(lines) == 1 and int(lines[0]) > 0: return (True, "") # check if the target matches the fluxcal off file cmd = "grep " + target + " " + fluxcal_on_file + " | wc -l" rval, lines = system(cmd) if rval == 0 and len(lines) == 1 and int(lines[0]) > 0: return (True, "") return (False, target + " did not exist in fluxcal files")
def test_fluxcal (target, fluxcal_on_file, fluxcal_off_file): target = clean_target(target) # check if the target matches the fluxcal off file cmd = "grep " + target + " " + fluxcal_on_file + " | wc -l" rval, lines = system (cmd) if rval == 0 and len(lines) == 1 and int(lines[0]) > 0: return (True, "valid fluxcal on") # check if the target matches the fluxcal off file cmd = "grep " + target + " " + fluxcal_off_file + " | wc -l" rval, lines = system (cmd) if rval == 0 and len(lines) == 1 and int(lines[0]) > 0: return (True, "valid fluxcal off") return (False, target + " did not exist in fluxcal files")
def getDBState (self, key): cmd = "dada_dbmetric -k " + key rval, lines = system (cmd, False) if rval == 0: a = lines[0].split(',') dat = {'nbufs':a[0], 'full':a[1], 'clear':a[2], 'written':a[3],'read':a[4]} hdr = {'nbufs':a[5], 'full':a[6], 'clear':a[7], 'written':a[8],'read':a[9]} return 0, hdr, dat else: return 1, {}, {}
def get_parfile_param (target, param): target = clean_target(target) (ok, message) = get_par_file(target) if not ok: return (False, message) par_file = message cmd = "grep -i " + param + " " + par_file + " | awk '{print $2}'" rval, lines = system (cmd) if rval != 0 or len(lines) <= 0: return (False, "could not read parfile") return (True, lines[0])
def get_psrcat_param(target, param): cmd = "psrcat -all " + target + " -c " + param + " -nohead -o short" rval, lines = system(cmd) if rval != 0 or len(lines) <= 0: return ("fail", "could not use psrcat") if lines[0].startswith("WARNING"): return ("fail", "pulsar " + target + " did not exist in catalog " + lines[0]) parts = lines[0].split() if len(parts) == 2 and parts[0] == "1": return ("ok", parts[1])
def getLoads (dl): result = 0 loads = {} cmd = "uptime" rval, lines = core.system (cmd, 3 <= dl) if rval == 0 and len(lines) == 1: parts = lines[0].split("load average: ") if len(parts) == 2: load_parts = parts[1].split(', ') if len(load_parts) == 3: loads = {"1min" : load_parts[0], "5min": load_parts[1], "15min": load_parts[2]} cmd = "nproc" rval, lines = core.system (cmd, 3 <= dl) if rval == 0 and len(lines) == 1: loads["ncore"] = lines[0] else: loads["ncore"] = 0 return result, loads
def getDiskCapacity (dirs, dl): result = 0 disks = {} for dir in dirs: cmd = "df " + dir + " -B 1048576 -P | tail -n 1 | awk '{print $2,$3,$4}'" rval, lines = core.system (cmd, 3 <= dl) result += rval if rval == 0 and len(lines) == 1: parts = lines[0].split() disks[dir] = {"size": parts[0], "used": parts[1], "available": parts[2]} return result, disks
def get_psrcat_param (target, param): target = clean_target(target) cmd = "psrcat -all " + target + " -c " + param + " -nohead -o short" rval, lines = system (cmd) if rval != 0 or len(lines) <= 0: return (False, "could not use psrcat") if lines[0].startswith("WARNING"): return (False, "pulsar " + target + " did not exist in catalog") parts = lines[0].split() if len(parts) == 2 and parts[0] == "1": return (True, parts[1]) else: return (False, "pulsar " + target + " did not match only 1 entry")
def get_psrcat_param(target, param): # remove the _R suffix if target.endswith('_R'): target = target[:-2] cmd = "psrcat -all " + target + " -c " + param + " -nohead -o short" rval, lines = system(cmd) if rval != 0 or len(lines) <= 0: return (False, "could not use psrcat") if lines[0].startswith("WARNING"): return (False, "pulsar " + target + " did not exist in catalog") parts = lines[0].split() if len(parts) == 2 and parts[0] == "1": return (True, parts[1]) else: return (False, "pulsar " + target + " did not match only 1 entry")
def getNTPSynced (dl): result = 0 if os.path.isfile ("/usr/sbin/ntpq"): cmd = "/usr/sbin/ntpq -c 'rv 0 offset'" elif os.path.isfile ("/usr/bin/ntpq"): cmd = "/usr/bin/ntpq -c 'rv 0 offset'" else: return (0, True) rval, lines = core.system (cmd, 3 <= dl) if rval == 0 and len(lines) == 1: if lines[0] != "/usr/sbin/ntpq: read: Connection refused": parts = lines[0].split("=") offset = float(parts[1]) # offset in milliseconds if offset < 5: return (result, True) return (result, False)
def getDBState(self, key): cmd = "dada_dbmetric -k " + key rval, lines = system(cmd, False) if rval == 0: a = lines[0].split(',') dat = { 'nbufs': a[0], 'full': a[1], 'clear': a[2], 'written': a[3], 'read': a[4] } hdr = { 'nbufs': a[5], 'full': a[6], 'clear': a[7], 'written': a[8], 'read': a[9] } return 0, hdr, dat else: return 1, {}, {}
def getIPMISensors(dl): sensors = {} cmd = "ipmitool sensor" (rval, lines) = core.system(cmd, 3 <= dl) if rval == 0 and len(lines) > 0: for line in lines: data = line.split("|") metric_name = data[0].strip().lower().replace("+", "").replace( " ", "_") value = data[1].strip() lower = data[6].strip() upper = data[7].strip() # Skip missing sensors if value == 'na': continue state = "na" metric_units = "na" metric_value = "na" # if sensor is a boolean value if re.search("(0x)", value): if value == "0x1": metric_value = "ok" metric_units = "na" state = "ok" else: metric_value = "fail" metric_units = "na" state = "fail" else: # Extract out a float value vmatch = re.search("([0-9.]+)", value) if not vmatch: continue metric_value = vmatch.group(1) metric_units = data[2].strip().replace("degrees C", "C") state = data[3] #lnr = float(data[4].strip()) #lcr = float(data[5].strip()) #lnc = float(data[6].strip()) #unc = float(data[7].strip()) #ucr = float(data[8].strip()) #unr = float(data[9].strip()) #state = "ok" #if value <= lnr or value >= unr: # state = "non recoverable" #if value <= lcr or value >= ucr: # state = "critical" #if value <= lnc or value >= unc: # state = "non critical" sensors[metric_name] = { "value": metric_value, "units": metric_units, "state": state, "lower": lower, "upper": upper } return rval, sensors
def getIPMISensors (dl): sensors = {} return (0, sensors) cmd = "ipmitool sensor" (rval, lines) = core.system (cmd, 3 <= dl) if rval == 0 and len(lines) > 0: for line in lines: data = line.split("|") metric_name = data[0].strip().lower().replace("+", "").replace(" ", "_") value = data[1].strip() lower = data[6].strip() upper = data[7].strip() # Skip missing sensors if value == 'na': continue state = "na" metric_units = "na" metric_value = "na" # if sensor is a boolean value if re.search("(0x)", value): if value == "0x1": metric_value = "ok" metric_units = "na" state = "ok" else: metric_value = "fail" metric_units = "na" state = "fail" else: # Extract out a float value vmatch = re.search("([0-9.]+)", value) if not vmatch: continue metric_value = vmatch.group(1) metric_units = data[2].strip().replace("degrees C", "C") state = data[3] #lnr = float(data[4].strip()) #lcr = float(data[5].strip()) #lnc = float(data[6].strip()) #unc = float(data[7].strip()) #ucr = float(data[8].strip()) #unr = float(data[9].strip()) #state = "ok" #if value <= lnr or value >= unr: # state = "non recoverable" #if value <= lcr or value >= ucr: # state = "critical" #if value <= lnc or value >= unc: # state = "non critical" sensors[metric_name] = { "value": metric_value, "units": metric_units, "state": state, "lower":lower, "upper":upper } return rval, sensors
Daemon.conclude(self) ############################################################################### # if __name__ == "__main__": if len(sys.argv) != 2: print "ERROR: 1 command line argument expected" sys.exit(1) cfg = sys.argv[1] cfg_dir = os.environ.get('SPIP_ROOT') + "/share" cmd = "cp " + cfg_dir + "/" + cfg + "/*.cfg " + cfg_dir + "/" system(cmd, False) hostname = getHostNameShort() script = LMCDaemon("spip_lmc", hostname) state = script.configure(DAEMONIZE, DL, "lmc", "lmc") if state != 0: sys.exit(state) script.log(1, "STARTING SCRIPT") try: script.main() except: