def add(self): self.myId = getUuid() self.startTime = get_current_time() self.finishTime = get_current_time() obj = { "ID": self.myId, "AT_AccountId": self.accountId, "AT_ApiId": self.apiId, "AT_Type": self.type, "AT_Name": self.name, "AT_State": self.state, "AT_User": self.user or "", "AT_Request": transToStr(self.request), "AT_Reply": transToStr(self.reply), "AT_StartTime": get_current_time(), "AT_CreateTime": get_current_time(), } if (self.state == API_STATE_FINISHED or self.state == API_STATE_FAILED): obj["AT_FinishTime"] = get_current_time() ret = self.db.insert(TB_APITRACE, obj) if ret == -1: WARNING("add api %s error for db operation" % self.myId) return DB_ERR return OCT_SUCCESS
def collectMsg(): DEBUG("### Collect msg thread start...") while True: for func in funcList: now = get_current_time() if (now - func["lastCollect"]) > func["CollectInterval"] * 1000: func["lastCollect"] = now tmp = func["func"]() if lock.acquire(): msg[func["type"]].append(tmp) if (len(msg[func["type"]]) > 5): msg[func["type"]] = msg[func["type"]][-5:] lock.release() if lock.acquire(): fd = open(MSG_FILE, "w") fd.write(transToStr(msg, indent=4)) fd.close() lock.release() sleep(5)
def updatePrice(self): userObj = { "P_Info": transToStr(self.infoObj), "P_LastSync": get_current_time(), "P_Description": self.desc } cond = "WHERE ID='%s'" % self.myId ret = self.db.update(TB_PRODUCT, userObj, cond=cond) if (ret == -1): WARNING("update user %s error for db operation" % self.name) return DB_ERR return 0
def add(self): self.myId = getUuid() obj = { "ID": self.myId, "QR_Name": self.name, "QR_Type": self.type, "QR_Price": self.price, "QR_Points": self.points, "QR_Info": transToStr(self.info), "QR_Paras": transToStr(self.paras), "QR_CreateTime": get_current_time(), "QR_Description": self.desc, } ret = self.db.insert(TB_QUERYRESULT, obj) if (ret == -1): WARNING("add user %s error for db operation" % self.name) return DB_ERR DEBUG(obj) return OCT_SUCCESS
def updateReply(self): obj = { "AT_Reply": transToStr(self.reply), "AT_State": API_STATE_FINISHED, "AT_FinishTime": get_current_time() } cond = "WHERE ID='%s'" % self.myId ret = self.db.update(TB_APITRACE, obj, cond=cond) if ret == -1: WARNING("update api request %s error for db operation" % self.apiId) return DB_ERR return 0
def createSql(productType, obj): header = "INSERT INTO tb_product (ID, P_Name, P_Type, P_TypeName, P_Info, " header += "P_Description, P_LastSync, P_CreateTime) VALUES (" typeName = obj["name"] products = obj["products"] for product in products: sql = "'%s'" % getUuid() sql += ",'%s'" % product["name"] sql += ",'%s'" % productType sql += ",'%s'" % typeName sql += ",'%s'" % transToStr(product) sql += ",'%s'" % product.get("desc") or "" sql += ",'%ld'" % now sql += ",'%ld'" % now print(header + sql + ");")
def add(self): self.myId = getUuid() self.createTime = get_current_time() self.expireTime = get_current_time() + SESSION_EXPIRE_TIME obj = { "ID": self.myId, "S_UserId": self.userId, "S_UserName": self.username, "S_UserType": self.role, "S_Cookie": transToStr(self.cookie), "S_CreateTime": self.createTime, "S_ExpireTime": self.expireTime, } ret = self.db.insert(TB_SESSION, obj) if ret == -1: WARNING("add session %s error for db operation" % self.myId) return DB_ERR return OCT_SUCCESS
def add(self): self.myId = getUuid() obj = { "ID": self.myId, "P_Name": self.name, "P_Type": self.type, "P_TypeName": PRODUCT_TYPES[self.type], "P_Description": self.desc, "P_Info": transToStr(self.infoObj), "P_CreateTime": get_current_time(), "P_LastSync": get_current_time() } ret = self.db.insert(TB_PRODUCT, obj) if (ret == -1): WARNING("add product %s error for db operation" % self.name) return DB_ERR DEBUG(obj) return OCT_SUCCESS
def sendMsg(): DEBUG("### Send msg thread start...") if not os.path.exists(CONFIG_FILE_PATH): ERROR("agent config file not exist") return obj = fileToObj(CONFIG_FILE_PATH) serverIp = obj.get("serverIp") if not serverIp: ERROR("can't find server ip in config file") return agentId = obj.get("agentId") if not agentId: agentId = getUuid() obj["agentId"] = agentId fd = open(CONFIG_FILE_PATH, "w") fd.write(transToStr(obj, indent=4)) fd.close() while True: if lock.acquire(): for func in funcList: msg_type = func["type"] if len(msg[msg_type]) < 1: continue now = get_current_time() if (now - func["lastSend"]) > func["SendInterval"] * 1000: func["lastSend"] = now api = "octlink.quantum.v1.sync.APISyncMsg" paras = { "agentId": agentId, "type": msg_type, "data": transToStr(msg[msg_type][-1]), "timeout": 0 } session_uuid = "00000000000000000000000000000000" (retCode, retObj) = api_call(serverIp, "9999", api, paras, session_key=session_uuid, async=False, https=False) if (retCode): ERROR("connect to server error") continue DEBUG("send msg OK!") lock.release() sleep(60)
#unit bps, bits per second def get_net_info(): netinfo = {} netstat_before = get_net_stat() time.sleep(5) netstat_after = get_net_stat() for k in netstat_before: inspeed = (int(netstat_after[k]["inbytes"]) - int(netstat_before[k]["inbytes"])) / 5 * 8 outspeed = (int(netstat_after[k]["outbytes"]) - int(netstat_before[k]["outbytes"])) / 5 * 8 tmp = {"incoming": int(inspeed), "outgoing": int(outspeed)} netinfo[k] = tmp return netinfo if __name__ == "__main__": netinfo = get_net_info() print(transToStr(netinfo))
read_total = 0 write_total = 0 for i in device: tps_total += float(i.split()[1]) read_total += float(i.split()[2]) write_total += float(i.split()[3]) dev_name = i.split()[0] dev = { "tps": round(tps_total/5, 2), "readSpeed": round(read_total/5, 2), # unit KB/s "writeSpeed": round(write_total/5, 2) # unit KB/s } info[dev_name] = dev return info def get_disk_stat(): stat = parse_disk_stat() if not stat: ERROR("parse disk stat info use iostat command error") return disk_stat return stat if __name__ == "__main__": diskstat = get_disk_stat() print(transToStr(diskstat))
"maxUserProcesses": 10000, } """ def get_sys_info(): sysinfo = { "os": platform.system() + " Debian " + get_debian_version(), "kernel": get_kernel_info(), "cmdline": get_cmdline(), "maxOpenFiles": get_max_open_files(), "maxUserProcesses": get_max_processes(), "hostname": platform.node() } uptime = get_uptime() if not uptime: ERROR("got uptime error") return sysinfo sysinfo["upTime"] = "%d 天 %d 时 %d 分 %d 秒" % (uptime["day"], uptime["hour"], uptime["minute"], uptime["second"]) sysinfo["freeRate"] = uptime["freeRate"] rebootTime = get_current_time() - uptime["seconds"] * 1000 sysinfo["lastRebootTime"] = getStrTime(rebootTime) return sysinfo if __name__ == "__main__": info = get_sys_info() print(transToStr(info))
data = data.split("\n")[1:] for i in data: filesystem = i.split()[0] disk = { "total": int(i.split()[1]), "used": int(i.split()[2]), "available": int(i.split()[3]), "pused": i.split()[4], "mount": i.split()[5] } diskinfo[filesystem] = disk return diskinfo def get_disk_info(): diskinfo = parse_disk_info() if not diskinfo: ERROR("parse disk info from df command error") return info return diskinfo if __name__ == "__main__": diskinfo = get_disk_info() print(transToStr(diskinfo))