-
Notifications
You must be signed in to change notification settings - Fork 23
/
rhev-vm-applist.py
executable file
·134 lines (104 loc) · 4.34 KB
/
rhev-vm-applist.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
#!/usr/bin/env python
#
# Author: Pablo Iranzo Gomez ([email protected])
# Description: Script for accessing RHEV-M DB for gathering app list for a given VM
#
# Requires rhevm-sdk to work and psycopg2 (for PG access)
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, version 2 of the License.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
import optparse
import psycopg2
from rhev_functions import *
description = """
rhev-vm-applist is a script for gathering statistics about VM usage that can be used to tax usage
"""
# Option parsing
p = optparse.OptionParser("rhev-vm-applist.py [arguments]", description=description)
p.add_option("-u", "--user", dest="username", help="Username to connect to RHEVM API", metavar="admin@internal",
default="admin@internal")
p.add_option("-w", "--password", dest="password", help="Password to use with username", metavar="admin",
default="redhat")
p.add_option("-k", action="store_true", dest="keyring", help="use python keyring for user/password", metavar="keyring",
default=False)
p.add_option("-W", action="store_true", dest="askpassword", help="Ask for password", metavar="admin", default=False)
p.add_option("-s", "--server", dest="server", help="RHEV-M server address/hostname to contact", metavar="server",
default="127.0.0.1")
p.add_option("--dbuser", dest="dbuser", help="RHEV-M database user", metavar="dbuser", default="engine")
p.add_option("--dbpass", dest="dbpass", help="RHEV-M database password", metavar="dbpass", default="redhat")
p.add_option("-p", "--port", dest="port", help="API port to contact", metavar="443", default="443")
p.add_option('-v', "--verbosity", dest="verbosity", help="Show messages while running", metavar='[0-n]', default=0,
type='int')
p.add_option("-n", "--name", dest="name", help="VM name", metavar="name")
(options, args) = p.parse_args()
options.username, options.password = getuserpass(options)
baseurl = "https://%s:%s/ovirt-engine/api" % (options.server, options.port)
api = apilogin(url=baseurl, username=options.username, password=options.password)
con = psycopg2.connect(database='engine', user=options.dbuser, password=options.dbpass)
try:
value = api.vms.list()
except:
print("Error accessing RHEV-M api, please check data and connection and retry")
sys.exit(1)
# FUNCTIONS
def gathervmdata(vmname):
"""Obtans VM data from Postgres database and RHEV api
@param vmname: VM name to get information for
"""
# Get VM ID for the query
vmid = api.vms.get(name=vmname).id
# sql Query for gathering date from range
sql = "select app_list as vm_apps from vm_dynamic where vm_guid='%s' ;" % vmid
cur.execute(sql)
rows = cur.fetchall()
return rows[0]
def vmdata(vm):
"""Returns a list of VM data
@param vm: object identifying VM and return information from it
"""
# VMNAME, VMRAM, VMRAMAVG, VMCPU, VMCPUAVG, VMSTORAGE, VMSIZE
vmdata = [vm.name, gathervmdata(vm.name)]
return vmdata
def htmlrow(lista):
"""Returns an HTML row for a table
@param lista: Elements to put as diferent columns to construct a row
"""
table = "<tr>"
for elem in lista:
table += "<td>%s</td>" % elem
table += "</tr>"
return table
def htmltable(listoflists):
"""Returns an HTML table based on Rows
@param listoflists: Contains a list of all table rows to generate a table
"""
table = "<table>"
for elem in listoflists:
table += htmlrow(elem)
table += "</table>"
return table
# MAIN PROGRAM
if __name__ == "__main__":
# Open connection
cur = con.cursor()
print("<html>")
print("<head><title>VM Table</title></head><body>")
if not options.name:
data = [["Name", "App list"]]
for vm in paginate(api.vms):
try:
data.append(vmdata(vm))
except:
skip = 1
else:
data = [["VMNAME", "APP list"], vmdata(api.vms.get(name=options.name))]
print(htmltable(data))
if con:
con.close()
print("</body></html>")