-
Notifications
You must be signed in to change notification settings - Fork 0
/
agent_cirros.py
114 lines (90 loc) · 3.37 KB
/
agent_cirros.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
import os
import socket
import json
from collections import OrderedDict
try:
import requests
except ImportError:
os.system('pip install requests')
import requests
try:
import psutil
except ImportError:
os.system('pip install psutil')
import psutil
#------------------------------------------------------------
#--------------------- CREATE FOLDERS ----------------------
#------------------------------------------------------------
if os.path.exists('/home/cirros/export'):
pass
else:
os.system('mkdir /home/cirros/export')
os.system('touch /home/cirros/export/specs.json')
#------------------------------------------------------------
#------------------ VM's RAM, DISK, CPUs --------------------
#------------------------------------------------------------
cpu = psutil.cpu_count()
mem = psutil.virtual_memory()
ram_total = mem.total / (1024 ** 3)
ram_used = mem.used / (1024 ** 3)
ram_free = mem.free / (1024 ** 3)
disk = psutil.disk_usage('/')
disk_total = disk.total / (1024 ** 3)
disk_used = disk.used / (1024 ** 3)
disk_free = disk.free / (1024 ** 3)
net = psutil.net_if_addrs()
hostname = socket.gethostname()
#------------------------------------------------------------
#-------------------- CREATE JSON FILE ----------------------
#------------------------------------------------------------
speclist = []
vmspec = OrderedDict()
vmspec['OS Name'] = os.name
vmspec['Hostname'] = hostname
vmspec['VCPUs'] = cpu
ramlst = []
ramspec = OrderedDict()
ramspec['total'] = round(ram_total,2)
ramspec['used'] = round(ram_used,2)
ramspec['free'] = round(ram_free,2)
ramlst.append(ramspec)
vmspec['RAM'] = ramlst
disklst = []
diskspec = OrderedDict()
diskspec['total'] = round(disk_total,2)
diskspec['used'] = round(disk_used,2)
diskspec['free'] = round(disk_free,2)
disklst.append(diskspec)
vmspec['Disk'] = disklst
niclst = []
for i in net:
nicspec = OrderedDict()
nicspec['name'] = i
nicspeclst = []
for j in range(len(net[i])):
nicspecfam = OrderedDict()
if str(net[i][j].family) == "AddressFamily.AF_INET":
nicspecfam['ip version'] = 'IPv4'
elif str(net[i][j].family) == 'AddressFamily.AF_INET6':
nicspecfam['ip version'] = 'IPv6'
elif str(net[i][j].family) == 'AddressFamily.AF_PACKET':
nicspecfam['ip version'] = 'Hardware'
nicspecfam['address'] = str(net[i][j].address)
#management net ip
if '10.0.10' in nicspecfam['address']:
ip = nicspecfam['address']
nicspecfam['netmask'] = str(net[i][j].netmask)
nicspecfam['broadcast'] = str(net[i][j].broadcast)
nicspeclst.append(nicspecfam)
nicspec['specs'] = nicspeclst
niclst.append(nicspec)
vmspec['NIC'] = niclst
vmspec['management_net ip'] = ip
speclist.append(vmspec)
with open('/home/cirros/export/specs.json','w') as f1:
json.dump(speclist,f1, indent=4, separators=(',',':'), sort_keys=False)
#add trailing newline for POSIX compatibility
f1.write('\n')
url = 'http://10.0.100.151:6868/api'
response = requests.post(url, json = speclist)
print(response)