forked from jon-tobin/rhv-reporting
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoverall_report_3.7.py
124 lines (102 loc) · 3.89 KB
/
overall_report_3.7.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
import argparse
import base64
import configparser
import datetime
import logging
import os
import mysql.connector
import ovirtsdk4 as sdk
logging.basicConfig(level=logging.DEBUG, filename='example.log')
config = configparser.ConfigParser(interpolation=None)
config.read('/etc/workloadmgr/workloadmgr.conf')
rhv_connection = sdk.Connection(
url=str(config['ovirt_authtoken']['rhv_auth_url'] + "/api"),
username=str(config['ovirt_authtoken']['rhv_username']),
password=base64.b85decode(config['ovirt_authtoken']['rhv_password']).decode("utf-8"),
insecure=True,
ca_file="",
)
db_url = config['DEFAULT']['sql_connection']
user = db_url.split("//")[1].split(":")[0]
password = db_url.split("@")[0].split(":")[-1]
host = db_url.split("@")[1].split("/")[0]
database = db_url.split("/")[-1].split("?")[0]
mydb = mysql.connector.connect(
host=host,
user=user,
passwd=password,
database=database
)
def check_dir(directory):
if not os.path.isdir(directory):
return False
return True
def get_rhv_vms():
vms_service = rhv_connection.system_service().vms_service()
vms = vms_service.list()
return vms
def get_protected_vms():
mycursor = mydb.cursor()
mycursor.execute("select vm_id from workload_vms where deleted=0;")
myresult = mycursor.fetchall()
vm_ids = []
for row in myresult:
vm_ids.append(row[0])
return vm_ids
def get_current_date():
now = datetime.datetime.now()
dt_string = now.strftime("%d-%m-%Y-%H:%M:%S")
return dt_string
def print_delimeter(file):
file.write("\n")
file.write("#"*70+"\n")
def get_workloads():
mycursor = mydb.cursor()
mycursor.execute("select id, display_name from workloads;")
myresult = mycursor.fetchall()
workload_details = {}
for row in myresult:
workload_details[row[0]]=row[1]
return workload_details
def get_snapshots(TVM_report):
successful_snapshots_count = 0
failed_workloads = []
mycursor = mydb.cursor()
mycursor.execute("SELECT id, workload_id, status from snapshots where deleted=0 and created_at > DATE_SUB(NOW(), INTERVAL 24 HOUR)")
myresult = mycursor.fetchall()
total_snapshots = len(myresult)
TVM_report.write("Number of Snapshots last 24h : %s" % (total_snapshots))
print_delimeter(TVM_report)
for row in myresult:
if row[2]!="available":
failed_workloads.append(row[1])
TVM_report.write("Number of successful Snapshots last 24h : %s" % (total_snapshots-len(failed_workloads)))
print_delimeter(TVM_report)
TVM_report.write("Number of failed Snapshots last 24h : %s" % (len(failed_workloads)))
print_delimeter(TVM_report)
workload_details = get_workloads()
TVM_report.write("Failed workloads in last 24h..\n")
if not failed_workloads:
TVM_report.write("None")
for failed_workload in failed_workloads:
TVM_report.write("%s:%s\n"%(failed_workload,workload_details[failed_workload]))
print_delimeter(TVM_report)
if __name__== "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("--output_dir", help="Specify output directory where reports should go",default="/home/stack")
args = parser.parse_args()
directory_path = args.output_dir
status = check_dir(directory_path)
if not status:
print("Provided directory path is invalid or not present.")
else:
report_file_name = "TVM_Overall_Backup_Report-%s.txt" %(get_current_date())
report_file_name=os.path.join(directory_path,report_file_name)
TVM_report = open(report_file_name, "w")
print_delimeter(TVM_report)
TVM_report.write(" "*4 +"Report generated on %s" % (get_current_date()))
print_delimeter(TVM_report)
TVM_report.write("VMs existing: %s" % (len(get_rhv_vms())))
TVM_report.write("\nVMs protected: %s" % (len(get_protected_vms())))
print_delimeter(TVM_report)
get_snapshots(TVM_report)