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 }