From 1e2af55c82fb4e4c369426b3cbc8fae62713998d Mon Sep 17 00:00:00 2001 From: Alex Waclawik Date: Tue, 20 Sep 2022 16:07:48 -0400 Subject: [PATCH] v1.2-final --- sumotool.py | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/sumotool.py b/sumotool.py index c40b646..a592592 100644 --- a/sumotool.py +++ b/sumotool.py @@ -1,3 +1,7 @@ +# SumoLogic Report Tool +# Created by Alex Waclawik +# Version 1.2 + import sys import time import configparser @@ -8,18 +12,23 @@ from pyfiglet import Figlet from lib.sumologic import SumoLogic +# sets the path cwd = pathlib.Path().resolve() +# initialize configparser config = configparser.ConfigParser() config.read("config.ini") +# initialize date and time, as well as declare date and time vars now = datetime.now() c_date = now.strftime("%y/%m/%d") c_time = now.strftime("%H:%M:%S") f_datetime = now.strftime("%y%m%d-%H%M%S") +# startup font f = Figlet(font='smslant') print(f.renderText('SumoLogic SDK')) print("The date is " + c_date + " and the time is currently " + c_time) +# initialize sumologic api args = sys.argv accessID = config['API']['accessID'] accessKey = config['API']['accessKey'] @@ -27,12 +36,15 @@ def main(): time.sleep(1) + # get number of sections in config then subtract one to get number of reports sections = config.sections() num_of_reports = len(sections) - 1 for x in range(0, num_of_reports): + # check if report is enabled reportName = 'REPORT_' + str(x) reportStatus = config[reportName].getboolean('enabled') if reportStatus == True: + # get the report job parameters from the config file name = config[reportName]['name'] print("\n|--< Starting Job for '" + name + "' >--|") dashID = config[reportName]['dashboardID'] @@ -40,8 +52,11 @@ def main(): exportFormat = config[reportName]['exportFormat'] timezone = config[reportName]['timezone'] template = config[reportName]['template'] + # start the report job reportID = sumo.start_report(actionType, exportFormat, timezone, template, dashID) + # wait for the report job to finish get_panel(reportID) + # rename and move the file to its final location savedFile = rename_and_move(name, exportFormat) print("SUCCESS: The Panel Report '" + savedFile + "' has been saved") else: @@ -52,8 +67,10 @@ def get_panel(reportID): keepGoing = True start_time = time.time() while (keepGoing): + # check the status of the report job reportStatus = sumo.report_status(reportID) if reportStatus == "InProgress": + # keeps track of the elapsed time, average is around 30 seconds elap_time = round((time.time() - start_time), 2) sys.stdout.write('...Job In Progress... Elapsed Time: %s\r' % (elap_time)) sys.stdout.flush() @@ -63,22 +80,28 @@ def get_panel(reportID): print("ERROR: Report Job Has Failed") keepGoing = False elif reportStatus == "Success": + # save the report and return reportGenerate = sumo.report_result(reportID) time.sleep(0.5) print("\nSUCCESS: Report Job Complete") keepGoing = False def rename_and_move(name, format): + # determine the format if format == "Png": fileExt = ".png" elif format == "Pdf": fileExt = ".pdf" + # concatenate the output filename filename = f_datetime + "_" + name + "_Report" + fileExt + # specificy source and destination src = str(cwd) + "\\result.png" dst = "../reports/" + # rename the file then copy it over to new destination os.rename(src, filename) report = "./" + filename shutil.copy(report, dst) + # remove the source file os.remove(filename) return(filename)