Пример #1
0
def get_irq_info():
    irq = {
        "irq": 0,
        "softIrq": 0,
    }
    DSTAT_TOOL = "/usr/bin/dstat"
    if not os.path.exists(DSTAT_TOOL):
        ERROR("dstat tool not installed")
        return irq

    ret, data = OCT_SYSTEM("%s -c --nocolor --noheaders 1 5" % DSTAT_TOOL)
    if ret != 0:
        ERROR("get dstat info error %d" % ret)
        return irq

    hi = 0
    si = 0
    lines = data.split("\n")
    for line in lines:
        segs = line.split()
        if len(segs) != 6 or segs[0] == "usr":
            continue
        hi = hi + int(segs[4])
        si = si + int(segs[5])

    irq["irq"] = round(hi / 5, 2)
    irq["softIrq"] = round(si / 5, 2)

    return irq
Пример #2
0
def msgqueue_send(msgType, payload=None):
    credentials = pika.PlainCredentials(RABBITMQ_USER, RABBITMQ_PASSWD)
    parameters = pika.ConnectionParameters(host='localhost',
                                           port=5672,
                                           virtual_host=RABBITMQ_RVM_VHOST,
                                           credentials=credentials)
    try:
        connection = pika.BlockingConnection(parameters)
    except:
        ERROR("connect to server error %s,%s" %
              (str(parameters), str(credentials)))
        return False

    if (connection == None):
        ERROR("create connection to localhost error %s" % (str(parameters)))
        return True

    channel = connection.channel()
    properties = pika.BasicProperties(content_type='text/plain',
                                      delivery_mode=1)

    channel.basic_publish(exchange=RABBITMQ_RVM_EXCHANGE,
                          routing_key=routingkeys.get(msgType),
                          body=build_payload(msgType, payload),
                          properties=properties)

    connection.close()
Пример #3
0
    def post(self, *args, **kwargs):
        argObj = getArgObj(self.request)

        # import time
        # yield tornado.gen.Task(tornado.ioloop.IOLoop.instance().add_timeout, time.time() + 10)

        if (not argObj.get("api")):
            ERROR("not a valid api, no api exist")
            self.write(buildFailureReply(INVALID_PARAS))
            self.finish()
            return

        (status, session) = self.checkSession(argObj)
        if (not status):
            ERROR("check session failed %s " % str(argObj))
            self.write(buildFailureReply(NO_AUTH_SKEY))
            self.finish()
            return

        (role, accountId) = self.getAccountInfo(session)
        argObj["paras"]["role"] = role

        # IF accountId Specified, just use it
        if not argObj["paras"].get("accountId"):
            argObj["paras"]["accountId"] = accountId

        if argObj["api"].split(".")[-1] == "APILogOut":
            argObj["centerSessionID"] = self.get_cookie("centerSessionID",
                                                        "").replace("%22", "")
            DEBUG(argObj)

        retObj = doDispatching(argObj, session, API_PROTOS)
        self.write(buildReply(retObj))
        self.finish()
Пример #4
0
def get_meminfo_fromdb(db, dbname):
    totalMem = 0
    vmTotalMem = 0
    vmUsedMem = 0

    if dbname == "octframe":
        ret = db.select("tb_vm", dbname=dbname)
        if ret == -1:
            ERROR("select tb_vm from db err")

        for dur in db.cur:
            obj = row_to_dict("tb_vm", dur, dbname)
            basic = transToObj(obj['V_Basic'].replace('\n', '\\n'))
            if basic == None:
                continue

            memory = int(basic["memory"])  #unit MByte
            vmTotalMem += memory * 1024 * 1024

            state = int(obj['V_State'])
            if state == 2:  #running
                vmUsedMem += memory * 1024 * 1024  #unit Byte

        ret = db.select("tb_host", dbname=dbname)
        if ret == -1:
            ERROR("select tb_host from db err")

        for dur in db.cur:
            obj = row_to_dict("tb_host", dur, dbname)
            memObj = transToObj(obj['H_Memory'].replace('\n', '\\n'))
            if memObj == None:
                continue

            tmp = int(memObj["total"])  #unit MByte
            totalMem += tmp * 1024 * 1024  #unit Byte

    else:
        ret = db.select("tb_vm", dbname=dbname)
        if ret == -1:
            ERROR("select tb_vm from db err")

        for dur in db.cur:
            obj = row_to_dict("tb_vm", dur, dbname)
            memory = int(obj["V_Memory"])  #unit MByte
            status = obj["V_Status"]

            vmTotalMem += memory * 1024 * 1024
            if status == "Running":
                vmUsedMem += memory * 1024 * 1024  #unit Byte

        ret = db.select("v_host", dbname=dbname)
        if ret == -1:
            ERROR("select v_host from db err")

        for dur in db.cur:
            obj = row_to_dict("v_host", dur, dbname)
            tmp = int(obj["MemoryTotal"])  #unit KB
            totalMem += tmp * 1024  #unit Byte

    return (totalMem, vmTotalMem, vmUsedMem)
Пример #5
0
def get_cpuinfo_fromdb(db, dbname):
    vcpuTotal = 0
    vcpuUsed = 0
    vcpuAlloc = 0

    if dbname == "octframe":
        ret = db.select("tb_vm", dbname=dbname)
        if ret == -1:
            ERROR("select tb_vm from db err")

        for dur in db.cur:
            obj = row_to_dict("tb_vm", dur, dbname)
            basic = transToObj(obj['V_Basic'].replace('\n', '\\n'))
            if basic == None:
                continue

            state = int(obj['V_State'])

            vcpuAlloc += int(basic["cpu"])
            if state == 2:  #running
                vcpuUsed += int(basic["cpu"])

        ret = db.select("tb_host", dbname=dbname)
        if ret == -1:
            ERROR("select tb_host from db err")

        for dur in db.cur:
            obj = row_to_dict("tb_host", dur, dbname)
            other = transToObj(obj['H_Others'].replace('\n', '\\n'))

            vcpuTotal = int(other["cpu_core"]) * 4

    else:
        ret = db.select("tb_vm", dbname=dbname)
        if ret == -1:
            ERROR("select tb_vm from db err")

        for dur in db.cur:
            obj = row_to_dict("tb_vm", dur, dbname)
            cpunum = int(obj["V_CpuNum"])
            status = obj["V_Status"]

            vcpuAlloc += cpunum
            if status == "Running":
                vcpuUsed += cpunum

        ret = db.select("v_host", dbname=dbname)
        if ret == -1:
            ERROR("select v_host from db err")

        for dur in db.cur:
            obj = row_to_dict("v_host", dur, dbname)
            vcpuTotal += int(obj["VCpuTotal"])

    return (vcpuTotal, vcpuUsed, vcpuAlloc)
Пример #6
0
def get_mem_info():
    mem_flags = {
        "totalmemory": "total",
        "usedmemory": "used",
        "available": "available",
        "inactivememory": "cache",
        "freememory": "free",
        "buffermemory": "buffer",
        "swapcache": "swapCache",
        "totalswap": "totalSwap",
        "usedswap": "usedSwap",
        "freeswap": "freeSwap"
    }

    info = {}

    if not os.path.exists(VMSTAT_TOOL):
        ERROR("vmstat tool not exist")
        return info

    ret, data = OCT_SYSTEM(MEMINFO_CMD)
    if ret != 0:
        ERROR("get meminfo error with cmd %s, %d\n" % (MEMINFO_CMD, ret))
        return info

    lines = data.split("\n")
    for line in lines:
        segs = line.replace(" ", "").split("K")
        flag = mem_flags.get(segs[1])
        if not flag:
            continue
        info[flag] = int(segs[0]) * 1024

    info["shared"] = 0  # TBD
    info["available"] = info["free"] + info["buffer"]

    if not info["totalSwap"]:
        info["pfreeSwap"] = 0
        info["pusedSwap"] = 0
    else:
        info["pfreeSwap"] = round(info["freeSwap"] / info["totalSwap"] * 100.0,
                                  2)
        info["pusedSwap"] = round(info["usedSwap"] / info["totalSwap"] * 100.0,
                                  2)

    if not info["total"]:
        info["pused"] = 0
        info["pavailable"] = 0
    else:
        info["pused"] = round(
            (info["total"] - info["free"]) / info["total"] * 100.0, 2)
        info["pavailable"] = round(info["available"] / info["total"] * 100.0,
                                   2)

    return info
Пример #7
0
def web_logout(db, env, arg):
    sessionId = arg.get("centerSessionID")
    if not sessionId:
        ERROR("no session uuid to logout %s" % str(arg))
        return buildRetObj(OCT_SUCCESS)

    session = getSession(db, sessionId)
    if not session:
        ERROR("get session error to logout %s" % sessionId)
        return buildRetObj(OCT_SUCCESS)

    return buildRetObj(OCT_SUCCESS)
Пример #8
0
def get_cpuinfo_byvirsh():
    cpuinfo = {}

    CMD = "cat /proc/cpuinfo | grep processor | wc -l"
    ret, data = OCT_SYSTEM(CMD)
    if ret:
        ERROR("exec cmdline [%s] error" % CMD)
        return None

    cpuinfo["vcpuTotal"] = int(data) * 4

    CMD = "virsh list --all | sed -n '3,$p' | sed '$d'"
    ret, data = OCT_SYSTEM(CMD)
    if ret:
        ERROR("exec cmdline [%s] error" % CMD)
        return None

    if data:
        vcpuAlloc = 0
        vcpuUsed = 0

        domainList = data.split('\n')
        for domain in domainList:
            domid = domain.split()[1]

            CMD = "virsh dominfo %s" % domid
            ret, data = OCT_SYSTEM(CMD)
            if ret:
                ERROR("exec cmdline [%s] error" % CMD)
                continue

            cpus = 0
            state = ""
            data = data.split('\n')
            for line in data:
                if 'CPU(s)' in line:
                    cpus = int(line.split()[1])
                if 'State' in line:
                    state = line.split()[1]

            vcpuAlloc += cpus
            if state == 'running':
                vcpuUsed += cpus

        cpuinfo['vcpuUsed'] = vcpuUsed
        cpuinfo['vcpuAlloc'] = vcpuAlloc

    else:
        cpuinfo['vcpuUsed'] = 0
        cpuinfo['vcpuAlloc'] = 0

    return cpuinfo
Пример #9
0
def get_meminfo_byvirsh():
    meminfo = {}

    CMD = "virsh list --all | sed -n '3,$p' | sed '$d'"
    ret, data = OCT_SYSTEM(CMD)
    if ret:
        ERROR("exec cmdline [%s] error" % CMD)
        return None

    if data:

        vmTotalMem = 0
        vmUsedMem = 0

        domainList = data.split('\n')
        for domain in domainList:
            domid = domain.split()[1]

            CMD = "virsh dominfo %s" % domid
            ret, data = OCT_SYSTEM(CMD)
            if ret:
                ERROR("exec cmdline [%s] error" % CMD)
                continue

            maxMem = 0
            usedMem = 0
            data = data.split('\n')
            for line in data:
                if 'Max memory' in line:  #unit KB
                    maxMem = int(line.split()[2])
                if 'Used memory' in line:  #unit KB
                    usedMem = int(line.split()[2])

            vmTotalMem += maxMem
            vmUsedMem += usedMem

        meminfo['vmTotalMem'] = vmTotalMem * 1024  #unit Byte
        meminfo['vmUsedMem'] = vmUsedMem * 1024  #unit Byte
    else:
        meminfo['vmTotalMem'] = 0
        meminfo['vmUsedMem'] = 0

    CMD = 'cat /proc/meminfo | grep MemTotal'
    ret, data = OCT_SYSTEM(CMD)
    if ret:
        ERROR("exec cmdline [%s] error" % CMD)
        return None

    meminfo["totalMem"] = int(data.split()[1]) * 1024

    return meminfo
Пример #10
0
def get_agents(db, arg):
    listObj = {
        "total": 0,
        "list": [],
    }

    start = arg["paras"].get("start") or 0
    limit = arg["paras"].get("limit") or 100

    cond = "WHERE 1=1 "
    ret = db.select(TB_AGENT, cond=cond, limit=int(limit), offset=int(start))
    if ret == -1:
        ERROR("get agent list error")
        return (DB_ERR, None)

    for dur in db.cur:
        obj = dbmysql.row_to_dict(TB_AGENT, dur)
        item = Agent(db, dbObj=obj)
        item.loadFromObj()

        listObj["list"].append(item.toObj())

    listObj["total"] = getAgentCount(db)

    return (OCT_SUCCESS, listObj)
Пример #11
0
def get_agent(db, arg):
    agentId = arg["paras"]["id"]
    agent = getAgent(db, agentId)
    if not agent:
        ERROR("agent %s not exist" % agentId)
        return (SEGMENT_NOT_EXIST, None)
    return (OCT_SUCCESS, agent.toObj())
Пример #12
0
def callWebServiceEX(*args):
	function = args[-4]
	session = args[-3]
	arg = args[-2]
	apiProto = args[-1]

	if (apiProto):
		arg["apiName"] = apiProto["name"]

	env = web_get_env(session)
	db = dbmysql.mysqldb()

	try:
		funret = function(db, env, arg)
	except Exception as e:
		errorLog = "Error in %s: [%s],\n Import module failed. [%s]" % (function, e, traceback.format_exc(limit=100))
		errorLog = errorLog.replace("\n", "")
		errorLog = errorLog.replace("\"", "\\\"")
		ERROR(errorLog)

		retObj = buildResult(OCT_SYSTEM_ERR, errorLog=errorLog)
		if (arg.get("async") != True):
			retObj = writeApiWithResult(db, env, arg, retObj, apiProto, False)
			retObj["session"] = session

		del db, env
		return retObj

	retObj = makeFunctionResult(funret)
	if (arg.get("async") != True):
		retObj = writeApiWithResult(db, env, arg, retObj, apiProto, False)
		retObj["session"] = session
	del db, env

	return retObj
Пример #13
0
def get_syncmsg(db, arg):
    listObj = {
        "total": 0,
        "list": [],
    }

    paras = arg["paras"]

    start = arg["paras"].get("start") or 0
    limit = arg["paras"].get("limit") or 100

    cond = "WHERE 1=1 "
    agentId = paras["agentId"]
    type = paras["type"]

    if agentId:
        cond += "AND M_AgentId='%s' " % agentId
    if type:
        cond += "AND M_Type='%s' " % type

    cond += "ORDER BY M_Time DESC"
    ret = db.select(TB_MSG, cond=cond, limit=int(limit), offset=int(start))
    if ret == -1:
        ERROR("get agent list error")
        return (DB_ERR, None)

    for dur in db.cur:
        obj = dbmysql.row_to_dict(TB_MSG, dur)
        item = Msg(db, dbObj=obj)
        item.loadFromObj()
        listObj["list"].append(item.toObj())

    listObj["total"] = getMsgCount(db, cond)

    return (OCT_SUCCESS, listObj)
Пример #14
0
def get_net_stat():

    netstat = {}

    fd = open(NETINFO_FILE, "r")
    if not fd:
        ERROR("open file %s error\n" % NETINFO_FILE)
        return netstat

    line = fd.readline()
    while line:

        if ": " in line:
            segs = line.replace("\t", "").replace("\n", "").split(": ")

            interface = segs[0].replace(" ", "")

            data = segs[1].split()
            inbytes = data[0]
            outbytes = data[8]

            netstat[interface] = {"inbytes": inbytes, "outbytes": outbytes}

        line = fd.readline()

    fd.close()
    return netstat
Пример #15
0
def get_products(db, paras):
    listObj = {
        "items": [],
        "total": 0,
    }
    type = paras["type"]

    cond = "WHERE 1=1 "
    if type:
        cond += "AND P_Type='%s' " % type

    state = paras["state"]
    if state:
        cond += "AND P_State='%s' " % state

    cond += "ORDER BY P_CreateTime "

    ret = db.select(TB_PRODUCT, cond=cond)
    if ret == -1:
        ERROR("get user list error")
        return (DB_ERR, None)

    for dur in db.cur:
        obj = dbmysql.row_to_dict(TB_PRODUCT, dur)
        product = Product(db, dbObj=obj)
        product.loadFromObj()

        listObj["items"].append(product.toObj())

    listObj["items"].sort(key=lambda x: x["infoObj"]["id"])

    listObj["total"] = len(listObj["items"])

    return (OCT_SUCCESS, listObj)
Пример #16
0
    def post(self):

        # Step 1, Login with default account
        argObj = getArgObj(self.request)
        paras = {
            "account": self.get_argument("username"),
            "password": self.get_argument("password"),
            "role": 7,
        }
        argObj["api"] = "octlink.tundra.v1.account.APILoginByAccount"
        argObj["paras"] = paras

        self.db = dbmysql.mysqldb()
        session = getSessionObj(self.db,
                                sessionId="00000000000000000000000000000000")
        del self.db

        argObj["session"] = session
        retObj = doDispatching(argObj, session, API_PROTOS)
        if retObj["RetCode"] != OCT_SUCCESS:
            ERROR("login error %s" % str(retObj))
            self.redirect("/login/?error=true")
        else:
            sessionObj = retObj["RetObj"]["session"]
            self.set_cookie("rvmusercookie", sessionObj["id"])
            self.set_cookie("username", retObj["RetObj"]["name"])
            self.set_cookie("userid", retObj["RetObj"]["id"])
            self.redirect("/")
Пример #17
0
def get_cpu_cores():
	ret, data = OCT_SYSTEM("cat /proc/cpuinfo | grep -w processor | wc -l")
	if ret != 0:
		ERROR("get processor number error")
		return 1

	return int(data.replace("\n", ""))
Пример #18
0
def get_cores_per_cpu():
    ret, data = OCT_SYSTEM("cat %s | grep 'cpu cores'| uniq" % HW_CPUINFO_FILE)
    if ret != 0:
        ERROR("get processor number error")
        return 2

    return int(data.replace("\n", ""))
Пример #19
0
def get_cpu_time():
    timeinfo = {
        "user": 0,
        "system": 0,
        "nice": 0,
        "idle": 100.00,
        "steal": 0,
        "iowait": 0,
    }
    IOSTAT_TOOL = "/usr/bin/iostat"
    if not os.path.exists(IOSTAT_TOOL):
        ERROR("iostat tool not installed")
        return timeinfo

    ret, data = OCT_SYSTEM("%s -c 1 5" % IOSTAT_TOOL)
    if ret != 0:
        ERROR("get iostat info error %d" % ret)
        return timeinfo

    user = 0
    nice = 0
    system = 0
    iowait = 0
    steal = 0
    idle = 0
    lines = data.split("\n")
    for line in lines:
        segs = line.split()
        if len(segs) != 6 or not segs[0] or segs[0] == "Linux":
            continue

        user = user + float(segs[0])
        nice = nice + float(segs[1])
        system = system + float(segs[2])
        iowait = iowait + float(segs[3])
        steal = steal + float(segs[4])
        idle = idle + float(segs[5])

    timeinfo["user"] = round(user / 5, 2)
    timeinfo["nice"] = round(nice / 5, 2)
    timeinfo["system"] = round(system / 5, 2)
    timeinfo["iowait"] = round(iowait / 5, 2)
    timeinfo["steal"] = round(steal / 5, 2)
    timeinfo["idle"] = round(idle / 5, 2)

    return timeinfo
Пример #20
0
def doServerDispatching(arg, API_LIST):
    apiProto = API_LIST.get(arg.get("api"))
    if (not apiProto):
        ERROR("func of %s not exist" % arg.get("api"))
        return buildRetObj(SEGMENT_NOT_EXIST)

    ret, errorMsg = checkParas(arg.get("paras"), apiProto)
    if (not ret):
        ERROR("check paras error [%s]" % errorMsg)
        return buildRetObj(INVALID_PARAS, errorLog=errorMsg)

    arg["async"] = False  # TBD

    if (arg.get("async")):
        return writeServerApi(arg, apiProto)
    else:
        return callServerWebServiceDir(apiProto["func"], arg, apiProto)
Пример #21
0
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
Пример #22
0
def get_cpu_number():
    ret, data = OCT_SYSTEM(
        "cat %s | grep 'physical id' | sort | uniq | wc -l" % HW_CPUINFO_FILE)
    if ret != 0:
        ERROR("get processor number error")
        return 1

    return int(data.replace("\n", ""))
Пример #23
0
def makeFunctionResult(funret):
	RetCode = funret.get('RetCode')
	if type(RetCode) != int:
		errorLog = "Wrong formatstr in function return data ret[%s]" % (funret)
		ERROR(errorLog)
		return buildResult(OCT_SYSTEM_ERR, errorLog=errorLog)

	return buildResult(RetCode, funret.get("RetObj"), funret.get("ErrorLog"))
Пример #24
0
def get_disk_info():

    diskinfo = parse_disk_info()
    if not diskinfo:
        ERROR("parse disk info from df command error")
        return info

    return diskinfo
Пример #25
0
	def changePassword(self, oldPassword, newPassword):
		
		if (not newPassword or not oldPassword):
			ERROR("new password or old password not specified when doing password change operation")
			return NOT_ENOUGH_PARAS
		
		ret = self.auth(oldPassword)
		if (ret != OCT_SUCCESS):
			ERROR("old password %s not right" % oldPassword)
			return USER_PASSWD_ERR
		
		ret = self.resetPassword(newPassword)
		if (ret != 0):
			ERROR("modify password error,mayby old pass not right")
			return USER_PASSWD_ERR
		
		return OCT_SUCCESS
Пример #26
0
def get_cpu_cores():
    ret, data = OCT_SYSTEM("cat %s | grep -w processor | wc -l" %
                           HW_CPUINFO_FILE)
    if ret != 0:
        ERROR("get processor number error")
        return 1

    return int(data.replace("\n", ""))
Пример #27
0
def parse_disk_stat():
	info = {}

	if not os.path.exists(IOSTAT_TOOL):
		ERROR("iostat tool not installed")
		return stat

	ret, data = OCT_SYSTEM("iostat -d -k 1 5")
	if ret != 0:
		ERROR("exec iostat command error")
		return stat
		
	paras = data.split("Device:")[1:]

	dev_num = len([tmp for tmp in paras[0].split("\n")[1:] if tmp])

	devices = []
	for i in range(dev_num):
		device = []
		for para in paras:
			lines = [tmp for tmp in para.split("\n")[1:] if tmp]
			device.append(lines[i])

		devices.append(device)

	for device in devices:

		tps_total = 0
		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
Пример #28
0
def remove_agent(db, arg):
    paras = arg["paras"]
    agent = getAgent(db, paras["id"])
    if not agent:
        ERROR("no agent to remove %s" % paras["id"])
        return SEGMENT_NOT_EXIST

    ret = agent.delete()

    return (ret, None)
Пример #29
0
def getSession(db, sessionId=None):

    session = Session(db, sessionId)
    if (session.init()):
        ERROR("get session error %s" % sessionId)
        return {}

    session.update()

    return session.toObj()
Пример #30
0
	def init(self):

		cond = "WHERE ID='%s' " % (self.myId)
		dbObj = self.db.fetchone(TB_QUOTA, cond)
		if (not dbObj):
			ERROR("init quota of %s error" % self.myId)
			return -1

		self.dbObj = dbObj
		self.loadFromObj()
		return 0