Commit 444fb842 authored by Jenny's avatar Jenny
Browse files

Initial commit

parents
.idea
*pass.txt
*password.txt
from proxmoxer import ProxmoxAPI
from proxmoxer.backends.https import AuthenticationError
from os import path
import sys
import argparse
import getpass
import yaml
import re
def print_dw_table(vmdata):
print('^ Name ^ MAC-Adressen + VLANs ^ CPU-Sockets ^ CPU-Cores ^ RAM in MB ^ PVE-Benutzer ^ Bemerkung ^')
for vmid, data in vmdata.items():
line = '| '
if 'wikilink' in data:
line = line + data['wikilink']
else:
line = line + '[[technik:server:{0}|{0}]]'.format(data['name'])
line = line + ' | '
lastnet = len(data['net']) - 1
for idx, net in enumerate(data['net']):
if idx != lastnet:
line = line + net + ' \\\\ '
else:
line = line + net
line = line + ' | ' + str(data['sockets'])
line = line + ' | ' + str(data['cores'])
line = line + ' | ' + str(data['ram'])
line = line + ' | '
lastuser = len(data['users']) - 1
for idx, user in enumerate(data['users']):
if idx != lastuser:
line = line + user.replace('@pve', '') + ', '
else:
line = line + user.replace('@pve', '')
line = line + ' | '
if 'comment' in data:
line = line + data['comment']
line = line + ' |'
print(line)
print('^ Gesamt: ^ ^ ~~=sum(range(col(),1,col(),row()-1))~~ ^ ~~=sum(range(col(),1,col(),row()-1))~~ ^ '
'~~=sum(range(col(),1,col(),row()-1))~~ ^ ^ ^')
def generate_net_string(orgstring):
macmatch = re.search(r"([0-9A-Fa-f]{2}[:-]){5}([0-9A-Fa-f]{2})", orgstring)
if not macmatch:
return ""
mac = macmatch.group(0)
linkdown = re.search(r"link_down=1", orgstring)
tagmatch = re.search(r"tag=[0-9]+", orgstring)
if not tagmatch:
if not linkdown:
return "ALLE: {0}".format(mac)
else:
return "ALLE: {0} (down)".format(mac)
tag = tagmatch.group(0)
tag = tag.replace("tag=", "")
if not linkdown:
return "VLAN {0}: {1}".format(tag, mac)
else:
return "VLAN {0}: {1} (down)".format(tag, mac)
def main():
parser = argparse.ArgumentParser(description='Python-Script to generate Proxmox VM-Documentation')
parser.add_argument('--host', type=str, default='localhost',
help='[Optional] Proxmox host address')
parser.add_argument('--node', '-n', type=str,
help='Node to generate config for')
parser.add_argument('--user', '-u', type=str,
help='Proxmox API-User (user@realm)')
parser.add_argument('--password', '-p', type=str,
help='API-User password (optional when AskPass or PassFile is set)')
parser.add_argument('--passfile', type=str,
help='API-User password-file location')
parser.add_argument('--askpass', action='store_true', help='Ask for API-User password')
args = parser.parse_args()
if not args.user:
print('No API-User given!')
return 1
if not args.node:
print('No node given!')
return 1
if not args.password and not args.askpass and not args.passfile:
print('No API-User-Password given and AskPass or PassFile is not set!')
return 1
elif args.askpass:
password = getpass.getpass(prompt='API-User-Password: ')
elif args.passfile:
if not path.exists(args.passfile):
print('Given PassFile does not exist!')
return 1
file = open(args.passfile, 'r')
password = file.readlines()[0]
file.close()
else:
password = args.password
try:
pve = ProxmoxAPI(args.host, user=args.user, password=password, verify_ssl=False)
except AuthenticationError:
print('Login against Proxmox-API failed!')
return 1
vms = pve.nodes(args.node).qemu.get()
vmdata = dict()
for vm in vms:
if not vm['template']:
vmdata[vm['vmid']] = dict()
acls = pve.access.acl.get()
usedgroups = dict()
for acl in acls:
if acl['path'].startswith('/vms/') and acl['type'] == 'group':
usedgroups[acl['ugid']] = list()
for name, memberlist in usedgroups.items():
members = pve.access.groups(name).get()['members']
for member in members:
memberlist.append(member)
for vmid, data in vmdata.items():
config = pve.nodes(args.node).qemu(vmid).config.get()
data['ram'] = config['memory']
data['sockets'] = config['sockets']
data['cores'] = config['cores']
data['name'] = config['name']
data['net'] = list()
for key, val in config.items():
if not key.startswith('net'):
continue
netstr = generate_net_string(val)
if netstr:
data['net'].append(netstr)
if 'description' in config:
desc = yaml.safe_load(config['description'])
if 'wikilink' in desc:
data['wikilink'] = desc['wikilink']
if 'comment' in desc:
data['comment'] = desc['comment']
data['users'] = list()
for acl in acls:
if acl['path'] == '/vms/' + vmid and acl['type'] == 'user':
data['users'].append(acl['ugid'])
elif acl['path'] == '/vms/' + vmid and acl['type'] == 'group':
data['users'] = data['users'] + usedgroups[acl['ugid']]
print_dw_table(vmdata)
return 0
if __name__ == '__main__':
sys.exit(main())
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment