def getPs1(self): fObj = open("/root/.bashrc") fc = fObj.readlines() fObj.close() for l in fc: if "PS1" in l: ps1 = l.split("@")[1].split(" ")[0] self.ps1 = ps1 writeToFile("ps1", ps1)
def getRoleType(self): fObj = open(DIRs['ROLE_TYPE_FILE']) fCon = fObj.readlines() for el in fCon: if el.startswith("srvRole"): srvRoleType = el.strip()[8:] fObj.close() if srvRoleType == "Single": role_type = "one" else: role_type = "oneMerge" writeToFile("role_type", role_type)
def getSrvInfo(self): ## 服务器供应商 vCmd = "hal-get-property --udi /org/freedesktop/Hal/devices/computer --key system.vendor 2>/dev/null" try: p = subprocess.Popen(vCmd,shell=True,stdout=subprocess.PIPE,stderr=subprocess.PIPE) out, err = p.communicate() ser_vendor = "unKnown" if not out else out.strip() finally: pass ## 服务器型号 pCmd = "hal-get-property --udi /org/freedesktop/Hal/devices/computer --key system.hardware.product 2>/dev/null" try: p = subprocess.Popen(pCmd,shell=True,stdout=subprocess.PIPE,stderr=subprocess.PIPE) out, err = p.communicate() ser_product = "unKnown" if not out else out.strip() finally: pass writeToFile("ser_vendor", ser_vendor) writeToFile("ser_product", ser_product)
def getIP(self): fObj = open(DIRs['ROLE_TYPE_FILE']) fCon = fObj.readlines() for el in fCon: if el.startswith("telIP"): self.telIP = el.split("=")[1].strip() if el.startswith("cncIP"): self.cncIP = el.split("=")[1].strip() if el.startswith("lanIP"): lanIP = el.split("=")[1].strip() if el.startswith("dbOneIp"): dbOneIp = el.split("=")[1].strip() if el.startswith("dbTwoIp"): dbTwoIp = el.split("=")[1].strip() fObj.close() writeToFile("telIP", self.telIP) writeToFile("cncIP", self.cncIP) writeToFile("lanIP", lanIP) writeToFile("dbOneIp", dbOneIp) writeToFile("dbTwoIp", dbTwoIp)
def getEth(self): for eFile in self.ethFileLists: if self.telIP in open(eFile).read(): ethName = os.path.basename(eFile) if ":" in ethName: mBit = ethName.index(":") netCard = ethName[6:mBit] else: netCard = ethName[6:] ## 网卡类型 tCmd = "/sbin/ethtool -i %s|grep 'driver'|awk -F':' '{print $2}'|xargs echo" % netCard p = subprocess.Popen(tCmd,shell=True,stdout=subprocess.PIPE,stderr=subprocess.PIPE) out, err = p.communicate() net_adapter_type = out.strip() ## 网卡驱动 dCmd = "/sbin/ethtool -i %s|egrep ^version|awk -F':' '{print $2}'|xargs echo" % netCard p = subprocess.Popen(dCmd,shell=True,stdout=subprocess.PIPE,stderr=subprocess.PIPE) out, err = p.communicate() net_adapter_driver_version = out.strip() ## 网卡速率 wCmd = "/sbin/ethtool %s|grep Speed|awk -F':' '{print $2}'|xargs echo" % netCard p = subprocess.Popen(wCmd,shell=True,stdout=subprocess.PIPE,stderr=subprocess.PIPE) out, err = p.communicate() net_bandwidth = out.strip() writeToFile("net_adapter_type", net_adapter_type) writeToFile("net_adapter_driver_version", net_adapter_driver_version) writeToFile("net_bandwidth", net_bandwidth)
def getDisk(self): mountPoints, diskTotal = [], 0 #diskTotal累加 parts = psutil.disk_partitions() #分区信息 for p in parts: mp = str(p).split(",")[1].split("=")[1] mountPoints.append(mp) for x in mountPoints: pTotal = psutil.disk_usage(x.strip("'")).total #单个分区容量 diskTotal += pTotal diskTotal = changeUnit(int(diskTotal)) rootSize, rootRate = psutil.disk_usage('/').free, psutil.disk_usage('/').percent #获取/分区的剩余空间和使用率 writeToFile("totalDisk", diskTotal) writeToFile("rootSize", changeUnit(int(rootSize))) writeToFile("rootRate", rootRate) if float(rootRate) > float(85): """ON开关,防止多次重复报警;报警一次之后设为关闭""" if self.sDb.read(self.telIP, "rootAlarm") == "ON": ## 状态为"NO"且开关为"ON"时,报故障 sub = "DBTX ROOT partition Alarm:%s" % self.ps1 msg = "DateTime: %s\n根分区已使用%s%%, 超过85%%, 请警惕!!!" % (getTimeNow(), rootRate) saveLog.warning(msg) sendMail(sub, msg) self.sDb.update(self.telIP, "rootAlarm", "OFF") ## 报警后,将开关置为"OFF" else: if self.sDb.read(self.telIP, "rootAlarm") == "OFF": ## 状态OK且开关为"OFF"时,报恢复 sub = "DBTX ROOT partition is OK:%s" % self.ps1 msg = "DateTime: %s\n根分区可用空间充足%s." % (getTimeNow(), changeUnit(int(rootSize))) saveLog.info(msg) sendMail(sub, msg) self.sDb.update(self.telIP, "rootAlarm", "ON") ## 使用率不足85%时,将开关置为"ON"
def getMem(self): totMem = changeUnit(int(psutil.TOTAL_PHYMEM)) #获取物理内存总数,diskTotal()函数用来单位转换 writeToFile("totalMem", totMem)
def getCpu(self): cpuCores = psutil.NUM_CPUS #获取CPU数量 writeToFile("cpuCores", cpuCores)