-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreporter.py
377 lines (345 loc) · 15.5 KB
/
reporter.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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
# Author: AcidGo
import atexit
import datetime
import logging
import json
import sys
import time
from config import *
from pyVmomi import vim, vmodl
from pyVim.connect import SmartConnect, SmartConnectNoSSL, Disconnect
from sqlalchemy import create_engine
from sqlalchemy import BigInteger, Column, DateTime, Integer, JSON, String
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker
Base = declarative_base()
class VMReport(Base):
__tablename__ = "vmreport"
id = Column(Integer, primary_key=True, autoincrement=True, comment="自增主键")
create_time = Column(DateTime, nullable=False, comment="创建批次时间")
vm_vcenter_ip = Column(String(20), nullable=False, comment="虚拟机所在的 VC 的 IP")
vm_name = Column(String(100), nullable=False, comment="虚拟机名称")
vm_uuid = Column(String(80), nullable=False, default="", comment="虚拟机 UUID(uuid)'")
vm_smbios = Column(String(80), nullable=False, default="", comment="虚拟机 SMBIOS(instanceUuid)")
vm_datacenter = Column(String(30), nullable=False, comment="虚拟机所在数据中心")
vm_cluster = Column(String(30), nullable=False, comment="虚拟机所在集群")
vm_host = Column(String(20), nullable=False, comment="虚拟机所在主机")
vm_powerstate = Column(String(10), nullable=False, comment="虚拟机通电状况")
vm_boottime = Column(DateTime, comment="虚拟机启动的时间")
vm_vmtoolstatus = Column(String(30), nullable=False, default="", comment="虚拟机 vmtool 状态")
vm_guestfullname = Column(String(50), comment="虚拟机客户端操作系统")
vm_datastore_set = Column(String(100), nullable=False, comment="虚拟机使用的存储器集合")
vm_datastore_provision = Column(BigInteger, nullable=False, comment="虚拟机置备的磁盘空间,单位为 bytes")
vm_datastore_used = Column(BigInteger, nullable=False, comment="虚拟机使用的磁盘空间,单位为 bytes")
vm_cpu_hotadd = Column(String(20), nullable=False, comment="虚拟机 CPU 热添加特性")
vm_cpu_corenum = Column(Integer, nullable=False, comment="虚拟机 CPU 个数")
vm_cpu_corepersocket = Column(Integer, nullable=False, comment="虚拟机 CPU 每槽核数")
vm_mem_hotadd = Column(String(20), nullable=False, comment="虚拟机内存热添加特性")
vm_mem_size = Column(Integer, nullable=False, comment="虚拟机内存大小,单位为 MB")
vm_netinfo = Column(JSON, comment="虚拟机客户端获取的 IP 地址和 MAC")
vm_hostname = Column(String(100), nullable=False, default="", comment="虚拟机 DNS 名称")
vm_customvalue = Column(JSON, comment="虚拟机的自定义注释,格式为 {<kid>:<value>,...}")
vm_annotation = Column(String(200), nullable=False, default="", comment="虚拟机备注信息")
vm_modifiedtime = Column(DateTime, comment="虚拟机上一次修改的时间")
class VMWorker(object):
"""执行 vSphere 虚拟机相关工作。
"""
use_ssl = False
vm_properties = [
"config.instanceUuid",
"name",
"config.guestFullName",
"config.annotation",
"customValue",
"runtime.host",
"datastore",
"config.uuid",
"guest.net",
"runtime.powerState",
"guest.toolsStatus",
"guest.hostName",
"config.hardware.memoryMB",
"config.hardware.numCPU",
"config.hardware.numCoresPerSocket",
"runtime.bootTime",
"config.modified",
"config.cpuHotAddEnabled",
"config.memoryHotAddEnabled",
"runtime.dasVmProtection",
"storage.perDatastoreUsage",
]
def __init__(self):
self._vc_reset()
def _vc_reset(self):
self._args = {}
self._data = None
self._atexit_func = None
def vc_login(self, **kwargs):
"""登录 vCenter 的用户认证,可以直接对接 ESXi,但不建议。
Args:
kwargs: 登录认证信息。
"""
self._vc_reset()
connect = SmartConnect if self.use_ssl is True else SmartConnectNoSSL
try:
service_instance = connect(**kwargs)
except vim.fault.InvalidLogin as e:
raise Exception(e.msg)
self.service_instance = service_instance
# 注册程序退出后的 VC 连接断开
self._atexit_func = atexit.register(Disconnect, self.service_instance)
self._args.update({k: v for k, v in kwargs.items() if k.lower() != "pwd"})
def vc_logout(self):
"""
"""
if self.service_instance is None:
Disconnect(self.service_instance)
if self._atexit_func is not None:
atexit.unregister(self._atexit_func)
self._atexit_func = None
def set_data(self, data):
self._data = data
def get_data(self):
return self._data
def collect(self):
"""收集虚拟机的信息。
"""
if not self.service_instance:
raise Exception("unable to connect to host with supplied info")
view = self._get_container_view(obj_type=[vim.VirtualMachine])
vm_data = self._collect_properties(
view_ref = view,
obj_type = vim.VirtualMachine,
path_set = self.vm_properties,
include_mors = False,
)
for i in vm_data:
i.update({"vc.ip": self._args.get("host", "")})
self._data = vm_data
def _get_container_view(self, obj_type, container=None):
"""从指定层次获取指定对象的搜寻视图。
"""
if not container:
container = self.service_instance.content.rootFolder
view_ref = self.service_instance.content.viewManager.CreateContainerView(
container=container,
type=obj_type,
recursive=True
)
return view_ref
def _collect_properties(self, view_ref, obj_type, path_set=None, include_mors=False):
"""通过传入的视图遍历指定对象的相关属性。
"""
collector = self.service_instance.content.propertyCollector
obj_spec = vmodl.query.PropertyCollector.ObjectSpec()
obj_spec.obj = view_ref
obj_spec.skip = True
traversal_spec = vmodl.query.PropertyCollector.TraversalSpec()
traversal_spec.name = 'traverseEntities'
traversal_spec.path = 'view'
traversal_spec.skip = False
traversal_spec.type = view_ref.__class__
obj_spec.selectSet = [traversal_spec]
property_spec = vmodl.query.PropertyCollector.PropertySpec()
property_spec.type = obj_type
if not path_set:
property_spec.all = True
property_spec.pathSet = path_set
filter_spec = vmodl.query.PropertyCollector.FilterSpec()
filter_spec.objectSet = [obj_spec]
filter_spec.propSet = [property_spec]
props = collector.RetrieveContents([filter_spec])
data = []
_host_parent_mapping = {}
for obj in props:
properties = {}
for prop in obj.propSet:
if prop.name == "customValue":
tmp_dict = {}
for tmp_i in prop.val:
tmp_dict[tmp_i.key] = tmp_i.value
res = tmp_dict
elif prop.name == "guest.net":
tmp_list = []
for tmp_i in prop.val:
mac_ = tmp_i.macAddress
ip_ = None
if isinstance(tmp_i.ipAddress, list):
for j in tmp_i.ipAddress:
if j.count(".") >= 3:
ip_ = j
tmp_list.append((mac_, ip_))
res = tmp_list
elif prop.name == "datastore":
tmp_set = set()
for tmp_i in prop.val:
tmp_set.add(tmp_i.name)
res = tmp_set
elif prop.name == "runtime.host":
res = prop.val.name
if res not in _host_parent_mapping:
host_cluster = prop.val.parent.name
cluster_datacenter = prop.val.parent.parent.parent.name
_host_parent_mapping[res] = (cluster_datacenter, host_cluster)
properties["host.datacenter"] = _host_parent_mapping[res][0]
properties["host.cluster"] = _host_parent_mapping[res][1]
elif prop.name == "storage.perDatastoreUsage":
committed = 0
uncommitted = 0
unshared = 0
for tmp_i in prop.val:
committed += tmp_i.committed
uncommitted += tmp_i.uncommitted
unshared += tmp_i.unshared
properties["storage.perDatastoreUsage.provision"] = committed + uncommitted
properties["storage.perDatastoreUsage.committed"] = committed
continue
else:
res = prop.val
properties[prop.name] = res
if include_mors:
properties['obj'] = obj.obj
data.append(properties)
return data
class Reporter(VMWorker):
def report(self):
raise Exception("please overwrite me")
class DBReporter(Reporter):
def __init__(self):
super(DBReporter, self).__init__()
self._db_session = None
self._create_time = datetime.datetime.now()
def report(self):
if self._db_session is None:
raise Exception("not found mysql conn for reporting data")
logging.info("starting for report the data to database ......")
for line in self.get_data():
logging.debug("starting deal with getten data: {!s}".format(line))
row = VMReport(
create_time = self._create_time,
vm_vcenter_ip = line["vc.ip"],
vm_name = line["name"],
vm_uuid = line["config.uuid"],
vm_smbios = line["config.instanceUuid"],
vm_datacenter = line["host.datacenter"],
vm_cluster = line["host.cluster"],
vm_host = line["runtime.host"],
vm_powerstate = line["runtime.powerState"],
vm_boottime = line["runtime.bootTime"] if "runtime.bootTime" in line else None,
vm_vmtoolstatus = line.get("guest.toolsStatus", ""),
vm_guestfullname = line["config.guestFullName"],
vm_datastore_set = "|".join(line["datastore"]),
vm_datastore_provision = line["storage.perDatastoreUsage.provision"],
vm_datastore_used = line["storage.perDatastoreUsage.committed"],
vm_cpu_hotadd = line["config.cpuHotAddEnabled"],
vm_cpu_corenum = line["config.hardware.numCPU"],
vm_cpu_corepersocket = line["config.hardware.numCoresPerSocket"],
vm_mem_hotadd = line["config.memoryHotAddEnabled"],
vm_mem_size = line["config.hardware.memoryMB"],
vm_netinfo = {i[0]: i[1] for i in line["guest.net"]},
vm_hostname = line.get("guest.hostName", ""),
vm_customvalue = {k: v for k, v in line["customValue"].items()},
vm_annotation = line.get("config.annotation", ""),
vm_modifiedtime = line["config.modified"],
)
try:
self._db_session.add(row)
self._db_session.commit()
except Exception as e:
logging.error("get an err when add row for data: {!s}".format(e))
logging.exception(e)
else:
logging.debug("finished for report the row to database: {!s}".format(line["name"]))
logging.info("finished for report the data to database")
def db_login(self, db_urls):
"""
"""
# NOTICE: debug mode
# engine = create_engine(db_urls, echo=True)
# EOF NOTICE
engine = create_engine(
db_urls,
json_serializer = lambda obj: json.dumps(obj, ensure_ascii=False),
connect_args = {"connect_timeout": 10},
)
Base.metadata.create_all(engine)
self._db_session = sessionmaker(engine)()
logging.info("initializing database session is finished")
def init_logger(level, logfile=None):
"""日志功能初始化。
如果使用日志文件记录,那么则默认使用 RotatingFileHandler 的大小轮询方式,
默认每个最大 10 MB,最多保留 5 个。
Args:
level: 设定的最低日志级别。
logfile: 设置日志文件路径,如果不设置则表示将日志输出于标准输出。
"""
import os
import sys
from logging.handlers import RotatingFileHandler
if not logfile:
logging.basicConfig(
level = getattr(logging, level.upper()),
format = "%(asctime)s [%(levelname)s] %(message)s",
datefmt = "%Y-%m-%d %H:%M:%S"
)
else:
logger = logging.getLogger()
logger.setLevel(getattr(logging, level.upper()))
if logfile.lower() == "local":
logfile = os.path.join(sys.path[0], os.path.basename(os.path.splitext(__file__)[0]) + ".log")
handler = RotatingFileHandler(logfile, maxBytes=10*1024*1024, backupCount=5)
formatter = logging.Formatter("%(asctime)s [%(levelname)s] %(message)s", "%Y-%m-%d %H:%M:%S")
handler.setFormatter(formatter)
logger.addHandler(handler)
logging.info("Logger init finished.")
if __name__ == "__main__":
# ########## Self Test
# mock_data = [
# {
# "vc.ip": "196.1.1.101",
# "name": "192.168.66.31",
# "config.uuid": "abcd",
# "config.instanceUuid": "abcd",
# "host.datacenter": "abcd",
# "host.cluster": "abcd",
# "runtime.host": "abcd",
# "runtime.powerState": "poweredOn",
# "runtime.bootTime": None,
# "guest.toolsStatus": "",
# "config.guestFullName": "Red Hat Enterprise Linux 6 (64 位)",
# "datastore": "",
# "storage.perDatastoreUsage.provision": 100,
# "storage.perDatastoreUsage.committed": 200,
# "config.cpuHotAddEnabled": "",
# "config.hardware.numCPU": 2,
# "config.hardware.numCoresPerSocket": 3,
# "config.memoryHotAddEnabled": "",
# "config.hardware.memoryMB": 100,
# "guest.net": "",
# "guest.hostName": "",
# "customValue": {},
# "config.annotation": "XYZEVA",
# "config.modified": None,
# }
# ]
# ########## EOF Self Tes
init_logger("info")
try:
reporter = DBReporter()
reporter.db_login(REPORT_DB_URLS)
for vc, vc_login_args in VCCONFIG.items():
logging.info("starting deal with reporting for vc {!s}".format(vc))
try:
reporter.vc_login(**vc_login_args)
reporter.collect()
# NOTICE: only testing
# reporter.set_data(mock_data)
# EOF NOTICE
reporter.report()
except Exception as e:
logging.error("get an err when deal with reporting for vc {!s}: {!s}".format(vc, e))
logging.exception(e)
logging.info("finished deal with reporting for vc {!s}".format(vc))
except Exception as e:
logging.exception(e)
exit(1)