Created
March 31, 2023 07:40
-
-
Save mritsurgeon/6309b08d673331ab5c03c12e1f23987a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import requests | |
import base64 | |
import re | |
import xml.etree.ElementTree as ElementTree | |
import xml.etree.ElementTree as ET | |
from datetime import datetime, timedelta | |
from requests.packages.urllib3.exceptions import InsecureRequestWarning | |
import pandas as pd | |
# Disable SSL certificate validation warning | |
requests.packages.urllib3.disable_warnings(InsecureRequestWarning) | |
# Set credentials for authentication | |
user = "administrator" | |
password = 'Password' | |
pair = f"{user}:{password}" | |
encoded_creds = base64.b64encode(pair.encode('ascii')).decode('ascii') | |
basic_auth_value = f"Basic {encoded_creds}" | |
# Set headers for authentication | |
headers = { | |
'Authorization': basic_auth_value | |
} | |
# Disable SSL certificate validation | |
import ssl | |
ssl._create_default_https_context = ssl._create_unverified_context | |
# Authenticate and obtain session key | |
logon = requests.post('https://localhost:9398/api/sessionMngr/?v=latest', headers=headers, verify=False) | |
response_headers = logon.headers | |
session_key = response_headers.get('X-RestSvcSessionId'.upper()) | |
# Set the URL to the BackupTaskSession API endpoint | |
url = 'https://localhost:9398/api/query?type=BackupTaskSession' | |
# Set headers with the session key | |
headers2 = { | |
'X-RestSvcSessionId': session_key | |
} | |
# Send a GET request to the URL with the session key | |
response = requests.get(url, headers=headers2, verify=False) | |
# Create an empty DataFrame with the required columns | |
columns = ['Name', 'State', 'Result', 'CreationTimeUTC', 'EndTimeUTC', 'TotalSize', 'Reason'] | |
df = pd.DataFrame(columns=columns) | |
# Parse the XML data | |
xml = ET.fromstring(response.content) | |
# Loop through each link and retrieve data | |
for link in xml.iter('{http://www.veeam.com/ent/v1.0}Link'): | |
if link.get('Href').endswith('?format=Entity'): | |
response = requests.get(link.get('Href'), headers=headers2, verify=False) | |
xml_response = ET.fromstring(response.content) | |
for backup_task_session in xml_response.iter('{http://www.veeam.com/ent/v1.0}BackupTaskSession'): | |
reason_element = backup_task_session.find('{http://www.veeam.com/ent/v1.0}Reason') | |
reason_value = reason_element.text if reason_element is not None else "N/A" | |
result_element = backup_task_session.find('{http://www.veeam.com/ent/v1.0}Result') | |
result_value = result_element.text if result_element is not None else "N/A" | |
state_element = backup_task_session.find('{http://www.veeam.com/ent/v1.0}State') | |
state_value = state_element.text if state_element is not None else "N/A" | |
CreationTimeUTC_element = backup_task_session.find('{http://www.veeam.com/ent/v1.0}CreationTimeUTC') | |
CreationTimeUTC_value = CreationTimeUTC_element.text if CreationTimeUTC_element is not None else "N/A" | |
EndTimeUTC_element = backup_task_session.find('{http://www.veeam.com/ent/v1.0}EndTimeUTC') | |
EndTimeUTC_value = EndTimeUTC_element.text if EndTimeUTC_element is not None else "N/A" | |
TotalSize_element = backup_task_session.find('{http://www.veeam.com/ent/v1.0}TotalSize') | |
TotalSize_value = TotalSize_element.text if TotalSize_element is not None else "N/A" | |
name_element = name_value = xml_response.attrib['VmDisplayName'] | |
name_value = name_element if name_element is not None else "N/A" | |
# Append the values to the DataFrame | |
df = pd.concat([df, pd.DataFrame({ | |
'Name': name_value, | |
'State': state_value, | |
'Result': result_value, | |
'CreationTimeUTC': CreationTimeUTC_value, | |
'EndTimeUTC': EndTimeUTC_value, | |
'TotalSize': TotalSize_value, | |
'Reason': reason_value | |
}, index=[0])], ignore_index=True) | |
print(df) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment