def __init__(self, sess): self.sess = sess self.cmdCtx = CmdCtx() self.cmdHdlrClasses = [] classes = CmdHdlr.__subclasses__() for clazz in classes: self.cmdHdlrClasses.append(clazz) classes2 = clazz.__subclasses__() if classes2: classes.extend(classes2)
if tokens[1] != 'disk': continue if tokens[2] == 'iDRAC': continue if rslt is None: rslt = [] rslt.append((tokens[-1], tokens[0][1:-1])) return rslt if __name__ == '__main__': ''' Test this module here ''' from kd.ep.cmd_ctx import CmdCtx cmdCtx = CmdCtx() cmdCtx.cmdMsg = "/bin/lsscsi" cmdCtx.rspMsg = """/bin/lsscsi .... [0:0:0:0] cd/dvd QEMU QEMU DVD-ROM 1.5. /dev/sr0 [2:0:0:0] disk QEMU QEMU HARDDISK 1.5. /dev/sda [2:0:0:1] disk QEMU QEMU HARDDISK 1.5. /dev/sdi [2:0:0:2] disk QEMU QEMU HARDDISK 1.5. /dev/sdg [2:0:0:3] disk QEMU QEMU HARDDISK 1.5. /dev/sde [2:0:0:4] disk QEMU QEMU HARDDISK 1.5. /dev/sdc [2:0:0:5] disk QEMU QEMU HARDDISK 1.5. /dev/sdh [2:0:0:6] disk QEMU QEMU HARDDISK 1.5. /dev/sdf [2:0:0:7] disk QEMU QEMU HARDDISK 1.5. /dev/sdd [2:0:0:8] disk QEMU QEMU HARDDISK 1.5. /dev/sdb""" if LsscsiHdlr.canParseRslt(cmdCtx): cmdCtx.rslt = LsscsiHdlr.parseRslt(cmdCtx)
continue if line.startswith("/bin/mount ") or line.startswith("mount "): continue words = line.split() devInfo = [words[0], words[2], words[4]] if rslt is None: rslt = [devInfo] else: rslt.append(devInfo) return rslt if __name__ == '__main__': ''' Test this module here ''' cmdCtx = CmdCtx() cmdCtx.cmdMsg = "mount -l | grep docknode" cmdCtx.rspMsg = """mount -l | grep docknode /dev/sdb on /kodiak/dock/myDock/docknode/1/mnt/0 type xfs (rw,relatime,seclabel,attr2,inode64,noquota) /dev/sdc on /kodiak/dock/myDock/docknode/1/mnt/1 type xfs (rw,relatime,seclabel,attr2,inode64,noquota) /dev/sdd on /kodiak/dock/myDock/docknode/1/mnt/2 type xfs (rw,relatime,seclabel,attr2,inode64,noquota) /dev/sde on /kodiak/dock/myDock/docknode/1/mnt/3 type xfs (rw,relatime,seclabel,attr2,inode64,noquota) /dev/sdg on /kodiak/dock/myDock/docknode/1/mnt/4 type xfs (rw,relatime,seclabel,attr2,inode64,noquota) /dev/sdh on /kodiak/dock/myDock/docknode/1/mnt/5 type xfs (rw,relatime,seclabel,attr2,inode64,noquota) /dev/sdi on /kodiak/dock/myDock/docknode/1/mnt/6 type xfs (rw,relatime,seclabel,attr2,inode64,noquota)""" if MountHdlr.canParseRslt(cmdCtx): cmdCtx.rslt = MountHdlr.parseRslt(cmdCtx) print "canParse:\n %s" % cmdCtx else: print "Failed to parse it!!!"
rslt = None for line in cmdCtx.rspMsg.splitlines(): if line == "" or line.isspace(): continue if line.startswith("/usr/sbin/pidof ") or line.startswith("pidof "): continue for name in line.split(): if name.strip() != '': if rslt is None: rslt = [ int(name) ] else: rslt.append(int(name)) return rslt if __name__ == '__main__': ''' Test this module here ''' cmdCtx = CmdCtx() cmdCtx.cmdMsg = "pidof kdtkcd" cmdCtx.rspMsg = """1234 5678""" if PidofHdlr.canParseRslt(cmdCtx): cmdCtx.rslt = PidofHdlr.parseRslt(cmdCtx) print "canParse:\n %s" % cmdCtx else: print "Failed to parse it!!!"
@staticmethod def parseRslt(cmdCtx): rslt = None for line in cmdCtx.rspMsg.splitlines(): if line == "" or line.isspace(): continue if line.startswith("/usr/sbin/lsmod ") or line.startswith( "lsmod "): continue print line words = line.split() rslt = [words[0], int(words[2])] return rslt if __name__ == '__main__': ''' Test this module here ''' cmdCtx = CmdCtx() cmdCtx.cmdMsg = "lsmod | grep --color=never kodiak" cmdCtx.rspMsg = """lsmod | grep --color=never kodiak kodiak 118041 0 """ if LsmodHdlr.canParseRslt(cmdCtx): cmdCtx.rslt = LsmodHdlr.parseRslt(cmdCtx) print "canParse:\n %s" % cmdCtx else: print "Failed to parse it!!!"
def canParseRslt(cmdCtx): if cmdCtx.cmdMsg.startswith("print "): return True return False @staticmethod def parseRslt(cmdCtx): for line in cmdCtx.rspMsg.splitlines(): if line == "" or line.isspace(): continue if line.startswith("print "): continue words = line.split('=') rslt = words[1].strip() return rslt if __name__ == '__main__': ''' Test this module here ''' cmdCtx = CmdCtx() cmdCtx.cmdMsg = "print aaa" cmdCtx.rspMsg = """$65997 = 65996 """ if GcmdHdlr.canParseRslt(cmdCtx): print "canParse" cmdCtx.rslt = GcmdHdlr.parseRslt(cmdCtx) print cmdCtx else: print "Failed to parse it!!!"
line.startswith("enp") or \ line.startswith("eno") or \ line.startswith("virbr"): tokens = line.split() if rslt is None: rslt = [] rslt.append(tokens[0]) return rslt if __name__ == '__main__': from kd.ep.cmd_ctx import CmdCtx ''' Test this module here ''' cmdCtx = CmdCtx() cmdCtx.cmdMsg = "/usr/sbin/ifconfig -s" cmdCtx.rspMsg = """/usr/sbin/ifconfig -s .... Iface MTU RX-OK RX-ERR RX-DRP RX-OVR TX-OK TX-ERR TX-DRP TX-OVR Flg eth0 1500 299706 75 0 0 15949 0 0 0 BMRU eth1 1500 0 0 0 0 0 0 0 0 BMU eth2 1500 132601 0 0 0 8273 0 0 0 BMRU eth3 1500 0 0 0 0 0 0 0 0 BMU lo 65536 10 0 0 0 10 0 0 0 LRU virbr0 1500 29749044 0 0 0 7320954 0 0 0 BMRU virbr1 1500 10 0 0 0 9 0 0 0 BMRU virbr2 1500 124383 0 0 0 35 0 0 0 BMRU virbr3 1500 10 0 0 0 9 0 0 0 BMRU vnet0 1500 11423 0 0 0 264318 0 0 0 BMRU vnet1 1500 10 0 0 0 16 0 0 0 BMRU vnet2 1500 10 0 0 0 123947 0 0 0 BMRU
pass @staticmethod def canParseRslt(cmdCtx): return cmdCtx.cmdMsg.startswith("diff -q ") @staticmethod def parseRslt(cmdCtx): rslt = None for line in cmdCtx.rspMsg.splitlines(): if line == "" or line.startswith("diff -q ") or line.isspace(): continue cmdCtx.setRC(RC.MISMATCH, line) break return rslt if __name__ == '__main__': ''' Test this module here ''' cmdCtx = CmdCtx() cmdCtx.cmdMsg = "diff -q new.txt old.txt" cmdCtx.rspMsg = """Files new.txt and old.txt differ""" if DiffHdlr.canParseRslt(cmdCtx): print "canParse" DiffHdlr.parseRslt(cmdCtx) print cmdCtx else: print "Failed to parse it!!!"
if line == "" or line.isspace(): continue if line.startswith("/bin/ls ") or line.startswith("/bin/ls:"): continue if line.find("No such file or directory") >= 0: cmdCtx.setRC(RC.NOT_FOUND, line) break for name in line.split(): if name.strip() != '': if rslt is None: rslt = [name] else: rslt.append(name) return rslt if __name__ == '__main__': ''' Test this module here ''' cmdCtx = CmdCtx() cmdCtx.cmdMsg = "/bin/ls --color=none /dev" cmdCtx.rspMsg = """autofs cpu_dma_latency input mem pts sr0 tty14 tty24 tty34 tty44 tty54 tty7 usbmon1 vcsa""" if LsHdlr.canParseRslt(cmdCtx): cmdCtx.rslt = LsHdlr.parseRslt(cmdCtx) print "canParse:\n %s" % cmdCtx else: print "Failed to parse it!!!"
rslt = None for line in cmdCtx.rspMsg.splitlines(): if line == "" or line.isspace(): continue if line.startswith("/bin/systemctl"): continue if line.startswith("systemctl"): continue rslt = True if line == 'active' else False break return rslt if __name__ == '__main__': ''' Test this module here ''' from kd.ep.cmd_ctx import CmdCtx cmdCtx = CmdCtx() cmdCtx.cmdMsg = "systemctl is-active kodiak-data-engine" cmdCtx.rspMsg = cmdCtx.cmdMsg + """ .... active""" if SystemctlIsactive.canParseRslt(cmdCtx): cmdCtx.rslt = SystemctlIsactive.parseRslt(cmdCtx) print "canParse:\n %s" % cmdCtx else: print "Failed to parse it!!!"
@staticmethod def parseRslt(cmdCtx): rslt = None for line in cmdCtx.rspMsg.splitlines(): if line == "" or line.isspace(): continue if line.startswith("/usr/sbin/pvdisplay"): continue if line.startswith("pvdisplay"): continue rslt = line.strip().split(':') break return rslt if __name__ == '__main__': from kd.ep.cmd_ctx import CmdCtx ''' Test this module here ''' cmdCtx = CmdCtx() cmdCtx.cmdMsg = "/usr/sbin/pvdisplay -c" cmdCtx.rspMsg = """/usr/sbin/pvdisplay .... /dev/sda2:centos_kodiak-cube2-c71-br4:66082816:-1:8:8:-1:4096:8066:11:8055:82FBVl-xqTS-C9Ja-m5J7-xhaw-bgeL-9eQ6Q9""" if PvdisplayHdlr.canParseRslt(cmdCtx): cmdCtx.rslt = PvdisplayHdlr.parseRslt(cmdCtx) print "canParse:\n %s" % cmdCtx else: print "Failed to parse it!!!"
@staticmethod def parseRslt(cmdCtx): rslt = None if cmdCtx.cmdMsg.startswith("echo $?"): for line in cmdCtx.rspMsg.splitlines(): if line == "" or line == cmdCtx.cmdMsg or line.isspace(): continue if line == "0": cmdCtx.setRC(RC.OK) else: rslt = line cmdCtx.setRC(RC.ERROR, "Fail to execute previous cmd") break return rslt if __name__ == '__main__': ''' Test this module here ''' cmdCtx = CmdCtx() cmdCtx.cmdMsg = "echo $?" cmdCtx.rspMsg = """0 """ if LcmdHdlr.canParseRslt(cmdCtx): print "canParse" LcmdHdlr.parseRslt(cmdCtx) print cmdCtx else: print "Failed to parse it!!!"
class EpCtx(object): ''' Endpoint context. The endpoint uses the session object to interact with remote entity. ''' def __init__(self, sess): self.sess = sess self.cmdCtx = CmdCtx() self.cmdHdlrClasses = [] classes = CmdHdlr.__subclasses__() for clazz in classes: self.cmdHdlrClasses.append(clazz) classes2 = clazz.__subclasses__() if classes2: classes.extend(classes2) def run(self, cmdMsg, timeout=-1, tryParse=True, seps=None, noRsp=False): ''' Run this cmdMsg against this endpoint. ''' self.cmdCtx.setRC(RC.OK) self.cmdCtx.cmdMsg = cmdMsg self.sess.sendline(cmdMsg) self.cmdCtx.rslt = None if noRsp: self.cmdCtx.setRC(RC.OK) return self.cmdCtx.rcMsg.rc rc, rspMsg = self.sess.expect(seps, timeout=timeout) if rc == RC.OK: self.cmdCtx.setRC(RC.OK) if isinstance(rspMsg, basestring): self.cmdCtx.rspMsg = rspMsg.strip().replace("\r", "") else: self.cmdCtx.rspMsg = rspMsg if tryParse: for clazz in self.cmdHdlrClasses: if clazz.canParseRslt(self.cmdCtx): self.cmdCtx.rslt = clazz.parseRslt(self.cmdCtx) break else: self.cmdCtx.setRC(rc, rspMsg) return self.cmdCtx.rcMsg.rc def close(self): self.sess.close() def getSess(self): return self.sess def getCmdCtx(self): return self.cmdCtx def getRcMsg(self): return self.cmdCtx.rcMsg def getRC(self): return self.cmdCtx.rcMsg.rc def getRslt(self): return self.cmdCtx.rslt def __str__(self): return self.__class__.__name__ __repr__ = __str__
if "Active:" not in line: continue tokens = line.split() if tokens[1] != 'disk': continue if rslt is None: rslt = [] rslt.append( (tokens[-1], tokens[0][1:-1]) ) return rslt if __name__ == '__main__': ''' Test this module here ''' from kd.ep.cmd_ctx import CmdCtx cmdCtx = CmdCtx() cmdCtx.cmdMsg = "systemctl status kodiak-data-engine" cmdCtx.rspMsg = """systemctl status kodiak-data-engine .... Loaded: loaded (/usr/lib/systemd/system/kodiak-data-engine.service; disabled; vendor preset: disabled) Active: inactive (dead) """ if SystemctlStatus.canParseRslt(cmdCtx): cmdCtx.rslt = SystemctlStatus.parseRslt(cmdCtx) print "canParse:\n %s" % cmdCtx else: print "Failed to parse it!!!"
cmdCtx.setRC(RC.ERROR, line) break continue if line.endswith('records in') or line.endswith('records out'): continue if "bytes (" not in line: continue m = re.findall('([-+]?\d+[\.]?\d*)', line) rslt = [int(m[0]), float(m[2])] break return rslt if __name__ == '__main__': ''' Test this module here ''' cmdCtx = CmdCtx() cmdCtx.cmdMsg = "/bin/dd new.txt old.txt" cmdCtx.rspMsg = """/bin/dd if=/dev/zero of=/dev/kdblk0 bs=8192 seek=0 cou nt=1 oflag=direct,seek_bytes /bin/dd: error writing /dev/kdblk0: Input/output error 1+0 records in 0+0 records out 0 bytes (0 B) copied, 29.5568 s, 0.0 kB/s""" if DdHdlr.canParseRslt(cmdCtx): cmdCtx.rslt = DdHdlr.parseRslt(cmdCtx) print "canParse:\n %s" % cmdCtx else: print "Failed to parse it!!!"
if 'grep' in line: continue words = line.split() if rslt == None: rslt = [ words[1] ] else: rslt.append(words[1]) if rslt == None: cmdCtx.setRC(RC.NOT_FOUND) return rslt if __name__ == '__main__': ''' Test this module here ''' cmdCtx = CmdCtx() cmdCtx.cmdMsg = "ps aux | grep --color=never kdpkd.*1" cmdCtx.rspMsg = """\ root 6647 0.2 1.7 697336 32400 ? Ssl 01:06 0:00 node ./kdpkd_main.js kdpkd.myDock.1 root 8445 0.0 0.0 112644 996 pts/0 S+ 01:13 0:00 grep --color=auto --color=never kdpkd.*1 """ if PsHdlr.canParseRslt(cmdCtx): cmdCtx.rslt = PsHdlr.parseRslt(cmdCtx) print "canParse:\n %s" % cmdCtx else: print "Failed to parse it!!!"