def get_os_info(salt_url, salt_user, salt_passwd,tgt): os_argvs = ['localhost', 'kernel', 'kernelrelease', 'cpu_model', 'num_cpus', 'productname', 'os', 'osrelease', 'mem_total'] os_module = 'grains.items' salt = salt_api.SaltAPI(salt_url, salt_user, salt_passwd) hosts = ",".join(tgt) result = salt.salt_run(hosts,os_module) interface_module = 'network.interfaces' interface_result = salt.salt_run(hosts, interface_module,) os_info_dict = {} for ip in tgt: try: host_info = {} for i in os_argvs: host_info[i] = result[ip][i] interface_info = [] for j in interface_result[ip].keys(): inter_info = interface_result[ip][j] try: interface_info.append({"hwaddr": inter_info['hwaddr'], "ipaddr": inter_info['inet'][0]["address"], "label": inter_info['inet'][0]["label"],"netmask": inter_info['inet'][0]["netmask"]}) except: continue host_info["interface"] = interface_info os_info_dict[ip] = host_info except: continue return os_info_dict
def get_disk_info(salt_url,salt_user,salt_passwd,tgt): argv = "fdisk -l | grep -E 'sda:|sdb:|sdc:|vda:|vdb:|vdc:' |awk -F ',' '{print $1}'" disk_module = 'cmd.run' salt = salt_api.SaltAPI(salt_url, salt_user, salt_passwd) hosts = ",".join(tgt) result = salt.salt_run_arg(hosts, disk_module, argv) disk_dict = {} for i in result.keys(): disk_infos = [] if re.search("Disk", result[i]): disk_infos = result[i].split("\n") else: disk_module = 'disk.usage' result_w = salt.salt_run(i, disk_module) for j in result_w.keys(): for k in result_w[j].keys(): disk_size = result_w[j][k]["1K-blocks"] if disk_size: disk = k.strip("\\") + str(disk_size / 1024 / 1024) + " GB" disk_infos.append(disk) else: pass disk_info = sorted(disk_infos) disk_dict[i] = {"disk_info": disk_info} return disk_dict
def get_mem_info(salt_url,salt_user,salt_passwd,tgt): module = 'status.meminfo' salt = salt_api.SaltAPI(salt_url, salt_user, salt_passwd) hosts = ",".join(tgt) result = salt.salt_run(hosts, module) mem_info = {} for ip in tgt: try: SwapTotal = int(result[ip]['SwapTotal']['value']) / 1024 except: SwapTotal = 0 mem_info[ip] = {"SwapTotal": SwapTotal} return mem_info
def get_sofeware(salt_url, salt_user, salt_passwd, tgt, tag): """ :param salt_url: :param salt_user: :param salt_passwd: :param tgt: host list :param tag: server name list :return: dict """ module = 'cmd.run' n_tag = "|".join(tag) argv = "netstat -tnlp | grep -E '%s' |awk '{print $4,$7}'|sed -s 's/:\{1,3\}/\//' |sed -s 's/ /\//' | awk -F '/' '{print $2,$3,$NF}'" % n_tag salt = salt_api.SaltAPI(salt_url, salt_user, salt_passwd) hosts = ",".join(tgt) result = salt.salt_run_arg(hosts, module, argv) info_ditc = {} for i in result.keys(): if re.search("\d+", result[i]): infos = result[i].split("\n") n_infos = [] for j in infos: b = j.split() n_infos.append({ "port": b[0], "pid": b[1], "name": b[2].strip(":") }) info_ditc[i] = n_infos else: pass data = {} for i in info_ditc.keys(): info = {} for j in info_ditc[i]: key = j['name'] j['port'] = [j['port']] if key in info.keys(): try: if j['port'][0] not in info[key]['port']: info[key]['port'] = info[key]['port'] + j['port'] else: pass except: pass else: info[key] = j for k in info.keys(): argv = "find /usr/ -name %s | grep -E '/sbin|/bin|/src'" % k result = salt.salt_run_arg(i, module, argv) cmd = result[i].split("\n")[0] argv = "%s --version ||%s -version ||%s -v||%s -V||%s version" % ( cmd, cmd, cmd, cmd, cmd) result = salt.salt_run_arg(i, module, argv) version_infos = result[i] if re.search("\d+.\d+.\d+", version_infos): info[k]["version"] = re.search("\d+.\d+.\d+", version_infos).group() else: info[k]["version"] = "Unkonwn" argv = "php --version" result = salt.salt_run_arg(i, module, argv) version_infos = result[i] if re.search("\d+.\d+.\d+", version_infos): info["php"] = {} info["php"]["version"] = re.search("\d+.\d+.\d+", version_infos).group() info["php"]["name"] = "php" info["php"]["port"] = "None" data[i] = info return data