/
vcenter_usage_stats.py
executable file
·158 lines (134 loc) · 5.12 KB
/
vcenter_usage_stats.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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
import sys
import time
from optparse import OptionParser
from psphere.client import Client
from psphere.managedobjects import HostSystem
class Host_info:
def __init__(self, host_ins):
self.host = host_ins
self.total_memory = (self.host.summary.hardware.memorySize) / (1024*1024)
self.total_cpu = self.host.summary.hardware.cpuMhz * self.host.summary.hardware.numCpuCores
class Moniter:
def __init__(self, options):
self.hosts = []
if options.server_ip:
self.server_ip = options.server_ip
else:
print "Please provide server ip"
sys.exit(1)
if options.username:
self.username = options.username
else:
print "Please provide user name"
sys.exit(1)
if options.password:
self.password = options.password
else:
print "Please provide password"
sys.exit(1)
if options.host_system_ips:
hosts = options.host_system_ips.split(",")
self.hosts.extend(hosts)
print "Moniter hosts:", self.hosts
else:
print "Please provide Host Sytem IPs to moniter"
sys.exit(1)
self.hostsconn_fds = []
def start(self, interval):
self.client = self.connect_server()
self.initialize_hosts()
self.collect_data(interval)
def connect_server(self):
try:
client = Client(self.server_ip, self.username, self.password)
print "connection established"
return client
except Exception, e:
print "exception:", e
sys.exit(1)
def create_log(self, h):
fd = open(str(h)+".log", "w")
fd.write("=" * 100 + "\n")
fd.write("START TIME:" + str(time.strftime("%a, %d %b %Y %H:%M:%S", time.gmtime()))+ "\n")
fd.write("=" * 100 + "\n")
val = "TIME".ljust(18)
fd.write(val)
val = "OverallCPU usage".ljust(18)
fd.write(val)
val = "OverallMem usage".ljust(18)
fd.write(val)
return fd
def initialize_hosts(self):
for h in self.hosts:
fd = self.create_log(h)
host_ins = HostSystem.get(self.client, name=h)
host_obj = Host_info(host_ins)
self.hostsconn_fds.append((fd, host_obj))
def collect_data(self, interval):
try:
for fd,host_obj in self.hostsconn_fds:
total_mem = str(host_obj.total_memory)
total_cpu = str(host_obj.total_cpu)
fd.write("(TOTAL MEMORY:" + total_mem + " TOTAL CPU:" + total_cpu + ")" + "\n")
fd.write("*" * 100 + "\n")
initial_time = time.time()
while True:
temp_fds = []
if (time.time() - initial_time) > int(interval):
for fd,host_obj in self.hostsconn_fds:
host_obj.host.update()
cpu = str(host_obj.host.summary.quickStats.overallCpuUsage).ljust(18)
mem = str(host_obj.host.summary.quickStats.overallMemoryUsage).ljust(18)
cur_time = str(time.strftime("%H:%M:%S", time.gmtime())).ljust(18)
#print "cpu:", cpu
#print "mem:", mem
#print "fd:", fd
fd.write(cur_time + cpu + mem + "\n")
fd.close()
fd = open(str(host_obj.host.name)+".log", "a")
temp_fds.append((fd, host_obj))
initial_time = time.time()
if temp_fds:
self.hostsconn_fds = []
self.hostsconn_fds = temp_fds
except Exception, e:
print "Exception in collect_data", str(e)
sys.exit(1)
finally:
#self.client.logout()
fd.close()
if __name__ == "__main__":
parser = OptionParser()
parser.add_option( "-i", "--server_ip",
action="store",
type="string",
dest="server_ip",
default="",
help="Vcenter IP Address")
parser.add_option( "-u", "--username",
action="store",
type="string",
dest="username",
default="",
help="Vcenter User name")
parser.add_option( "-p", "--password",
action="store",
type="string",
dest="password",
default="",
help="Vcenter Password")
parser.add_option( "-n", "--interval",
action="store",
type="string",
dest="interval",
default="300",
help="Time interval to collect data, default is 300(in secs)")
parser.add_option( "-s", "--host_system_ips",
action="store",
type="string",
dest="host_system_ips",
default="",
help="Host System IPs")
options, remainder = parser.parse_args()
moniter = Moniter(options)
moniter.start(options.interval)