Reputation:
Curious whether there's a way to return or output JSON contents into the terminal or somehow call them without using the print statement?
I'm writing a Python script to create a custom sensor in PRTG. The main goal of the script is to retrieve JSON data from Rubrik's API, extract specific key values from the JSON file and write them to a new JSON file. The contents of the second JSON file should be somehow outputted. PRTG requires not to use any sort of print statements in the script as it may corrupt the JSON.
I tried using various different methods, though did not find any success.
This is the code that I'm using:
import rubrik_cdm
import urllib3
import json
import sys
from datetime import datetime, timedelta
# Disable warnings
urllib3.disable_warnings()
# Authenticate by providing the node_ip and api_token
NODE_IP = ""
API_TOKEN = ""
# Establish a connection to Rubrik
rubrik = rubrik_cdm.Connect(
node_ip=NODE_IP,
api_token=API_TOKEN)
# Get Rubrik's failed archives from the past 24 hours and write them to a JSON file
def get_rubrik_failed_archives():
current_date = datetime.today() - timedelta(days=1) # <-- Get the datetime
datetime_to_string = current_date.strftime("%Y-%m-%dT%H:%M:%S")
get_failed_archives = rubrik.get("v1", f"/event/latest?event_status=Failure&event_type=Archive&before_date={datetime_to_string}&limit=1000")
with open("get_failed_archives.json", "w") as file:
json.dump(get_failed_archives, file, indent=3)
# Write first JSON file specific contents to a new JSON file
def get_rubrik_failed_archives_main():
with open("get_failed_archives.json") as json_file:
json_data = json.load(json_file)
failed_archives_data = []
for archive_data in json_data["data"]:
failed_archives_data.append({
"objectName": archive_data["latestEvent"]["objectName"],
"time": archive_data["latestEvent"]["time"],
"eventType": archive_data["latestEvent"]["eventType"],
"eventStatus": archive_data["latestEvent"]["eventStatus"],
})
with open("rubrik_failed_archives.json", "w") as file:
json.dump(failed_archives_data, file, indent=4, sort_keys=True)
return failed_archives_data
get_rubrik_failed_archives()
get_rubrik_failed_archives_main()
JSON file contents:
[
{
"eventStatus": "Failure",
"eventType": "Archive",
"objectName": "W12 BO Template",
"time": "2022-08-23T10:09:33.092Z"
},
{
"eventStatus": "Failure",
"eventType": "Archive",
"objectName": "W12 BO Template",
"time": "2022-08-23T09:06:33.786Z"
},
{
"eventStatus": "Failure",
"eventType": "Archive",
"objectName": "W12 BO Template",
"time": "2022-08-23T08:03:35.118Z"
},
{
"eventStatus": "Failure",
"eventType": "Archive",
"objectName": "W12 BO Template",
"time": "2022-08-23T07:00:32.683Z"
}
]
So, is there a way to return or get an output of JSON contents without using the print statement in Python?
Upvotes: 1
Views: 206