-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgetvms-from-vcs.py
executable file
·89 lines (72 loc) · 2.91 KB
/
getvms-from-vcs.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
#!/usr/bin/env python3
#
from pyVim import connect
from pyVmomi import vim
import psycopg2
import datetime
#scandate = datetime.datetime.now()
dbconn = psycopg2.connect("dbname=findmyvmdb user=findmyvmuser")
cur_s = dbconn.cursor()
cur_i = dbconn.cursor()
cur_s.execute ("SELECT vc_host,vc_user,vc_pwd,ignore FROM vc WHERE ignore = false ORDER BY vc_host ASC")
rows = cur_s.fetchall()
for row in rows:
scandate = datetime.datetime.now()
try: # CONNECT_VC
vc_instance = connect.SmartConnectNoSSL(host=row[0],user=row[1],pwd=row[2])
content = vc_instance.RetrieveContent()
containter = content.rootFolder
viewType = [vim.VirtualMachine]
recursive = True
containerView = content.viewManager.CreateContainerView (containter, viewType, recursive)
children = containerView.view
for child in children: # FOR_CHILD
try: # GET_CHILD
summary = child.summary
cpn = "None"
if child.parent:
cpn = str(child.parent.name)
macs = ''
ipall = ''
try: # GET_IP
for nic in child.guest.net:
if '02:00:4c:4f:4f:50' in nic.macAddress: ##NPCAP LOOPBACK
pass
elif len(macs) < 2030:
macs += nic.macAddress + ";"
else:
pass
if ( summary.runtime.powerState == 'poweredOn' ):
addresses = nic.ipConfig.ipAddress
for adr in addresses:
if ":" in adr.ipAddress or "169.254." in adr.ipAddress:
pass
elif len(ipall) < 2033:
ipall += adr.ipAddress + ';'
else:
pass
else:
ipall = '0.0.0.0;'
except Exception as e1: # GET_IP
print ("E1: Failed network add for %s %s" %(summary.vm, row[0]))
print (e1)
pass
storage_bytes = 0
if summary.storage.committed:
storage_bytes = summary.storage.committed
cur_i.execute ("INSERT INTO vms VALUES ( %s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s )", # 18
(scandate, str(summary.vm), summary.config.name, summary.config.vmPathName, summary.config.guestFullName, summary.runtime.powerState,
summary.guest.ipAddress, summary.guest.toolsStatus, str(summary.runtime.host.name), summary.config.memorySizeMB, summary.config.numCpu, row[0],
cpn, macs, ipall, summary.config.uuid, summary.config.instanceUuid, storage_bytes ))
except Exception as e2: # GET_CHILD
print ("E2: Failed to add %s %s" % ( str(summary.vm), summary.config.name ) )
print (e2)
pass
# FOR_CHILD
except Exception as e3: # CONNECT_VC
cur_i.execute("UPDATE vc SET ignore = True WHERE vc_host = '" + row[0] + "'")
print ("E3: %s" % (row[0]))
print (e3)
pass
cur_i.execute("UPDATE vc SET scandate = %s WHERE vc_host = %s", (scandate,row[0]))
dbconn.commit()