-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdelete.py
78 lines (66 loc) · 2.61 KB
/
delete.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
'''
Created on 2017年8月16日
@author: ning.lin
'''
import json
import salt.client as sc
print("123")
###salt调用
local = sc.LocalClient()
###目标主机指定
tgt = "*"
###获取grains,disk信息
grains = local.cmd(tgt,"grains.items")
diskusage = local.cmd(tgt,"disk.usage")
###主要应用列表即文件开头
app_name = ["tomcat","zookeeper","redis","mysql","nginx"]
cols = "主机名,IP地址,内存(GB),CPU核数,操作系统,数据盘/data(GB),所属项目,主要应用"
###打开一个.csv文件,以便写入
ret_file = open("ret.csv","w")
###首先写入开头,有点字段名的意思
ret_file.write(cols + "\n")
try:
for i in grains.keys():
###打印信息可注释掉
print (grains["nodename"])
print ("ipv4" + ":" ,grains["ipv4"])
print ("mem_total" + ":" , grains["mem_total"] / 1024 + 1)
print ("num_cpus" + ":" , grains["num_cpus"])
print ("osfullname" + ":" , grains["osfullname"])
print ("release" + ":" , grains["lsb_distrib_release"])
###可能一些主机没有/data数据盘1048576是1024x1024
if "/data" not in diskusage:
print ("diskusage" + ":" + "have no /data disk")
else:
data_vol = int(diskusage["/data"]["1K-blocks"])
print ("diskusage" + ":" , data_vol / 1048576 )
###去掉127.0.0.1这个地址
ipv4 = str(grains["ipv4"]).replace(", '127.0.0.1'","")
###因为一些历史遗留问题,这里取得不是主机名,而是salt-minion的id名,用以判断主要应用
hostname = grains["id"]
ipv4 = str(grains["ipv4"]).replace(", '127.0.0.1'","")
ipv4 = ipv4.replace(",","and")
mem = grains["mem_total"] / 1024 + 1
num_cpu = grains["num_cpus"]
OS = grains["osfullname"] + grains["lsb_distrib_release"]
if "/data" not in diskusage:
disk_data = "None"
else:
disk_data = data_vol / 1048576
###项目名为空
project = ""
###通过minion ID名来判断主要运行服务,比如xx-mysql-1,则运行mysql
for j in app_name:
if j in hostname.lower():
app = j
break
else:
app = "undefined"
c = ","
###连接并写入
line = hostname + c + ipv4 + c + str(mem) + c + str(num_cpu) + c + str(OS) + c + str(disk_data) + c + project + c + app
ret_file.write(line + "\n")
except Exception as e:
print ("Exception:\n",e)
finally:
ret_file.close()