2018-1-24

salt-api获取内存

def send_main(param):
    salt = SaltApi(SALT_API_CON,param)
    result1 = salt.salt_command()

    #以内存为例,对获取结果做处理
    mem_dict = {}
    for host, value in result1.items():
        if not value:
            print(host, "内存信息获取失败,请验证dmidecode工具")
            continue
        if host not  in mem_dict.keys():
            mem_dict[host] = {}
        memory = 0
        li = value.split('\n\t')
        for item in li:
            if 'Memory Device' in item:
                continue
            k, v = item.split(':')
            if k == 'Size':
                num, company = v.rsplit(" ",maxsplit=1)
                try:
                    num = int(num)/1024
                except ValueError:
                    continue
                memory += num
        mem_dict[host]['memory']=memory
    return mem_dict

获取内存的mem.py代码修改为

class Mem(Base):
    def run(self):
        self.method = "cmd.run"
        self.tgt = "*"
        self.arg = "dmidecode -q -t 17 2>/dev/null"      #获取内存的命令

        return {"client": "local", "fun": self.method, "tgt": self.tgt, "arg":self.arg }

猜你喜欢

转载自blog.csdn.net/wushan1992/article/details/87731932