This repository has been archived by the owner on Nov 2, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 18
/
vcenter_export.py
executable file
·130 lines (115 loc) · 2.94 KB
/
vcenter_export.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
#!/usr/bin/env python3
"""
Process vCenter export for hostnames and IPs
"""
import argparse
import csv
import ipaddress
import os
import re
import sys
def parse_vms(vms, site, input_file):
#fieldnames = [
# 'Name',
# 'State',
# 'Guest OS',
# 'DNS Name',
# 'VM Storage Policies Compliance',
# 'Status',
# 'Managed By',
# 'Host',
# 'Host Type',
# 'Provisioned Space',
# 'Used Space',
# 'Host CPU',
# 'Host Mem',
# 'Guest Mem - %',
# 'Compatibility',
# 'Memory Size',
# 'Reservation',
# 'CPUs',
# 'NICs',
# 'Uptime',
# 'IP Address',
# 'VMware Tools Version Status',
# 'VMware Tools Running Status',
# 'EVC Mode',
# 'UUID',
# 'Notes',
# 'Alarm Actions',
# 'HA Protection',
# 'Needs Consolidation',
# 'Encryption',
# 'Datacenter',
# 'Cluster',
# 'Shares Value',
# 'Limit - IOPs',
# 'Datastore % Shares',
#]
vm_list = list()
with open(input_file, 'r', newline='') as csvfile:
reader = csv.DictReader(csvfile, dialect='unix')
for vm in reader:
print(
vm['Name'],
vm['State'],
vm['Guest OS'],
vm['DNS Name'],
vm['Provisioned Space'],
vm['Memory Size'],
vm['CPUs'],
vm['IP Address'],
vm['Cluster'],
)
print()
def write_vms(vms, output_file):
with open(output_file, 'w', newline='') as csvfile:
fieldnames = [
'name',
'status',
'cluster',
'role',
'tenant',
'platform',
'vcpus',
'memory',
'disk',
'comments',
]
writer = csv.DictWriter(csvfile, fieldnames=fieldnames, dialect='unix')
writer.writeheader()
writer.writerows(vms)
def main():
parser = argparse.ArgumentParser(
description='Parse vCenter export for hostnames and addresses',
)
parser.add_argument(
'input_file',
type=str,
help='file containing vCenter export',
)
parser.add_argument(
'output_file',
type=str,
help='location for output file ',
)
parser.add_argument(
'site',
type=str,
help='Netbox site',
)
args = parser.parse_args()
lines = list()
with open(args.input_file, 'r') as infile:
lines = infile.readlines()
if len(lines) == 0:
sys.exit(
'Unable to read {input_file} or {input_file} is empty'.format(
input_file=args.input_file,
)
)
vms = parse_vms(lines, args.site, args.input_file)
#write_vms(vms, args.output_file)
if __name__ == '__main__':
main()
# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4