From 9e4a54f1d06f8168eea07017a8139f8cfcacf50a Mon Sep 17 00:00:00 2001 From: Luke Barone Date: Tue, 4 Feb 2020 12:22:24 -0800 Subject: [PATCH 1/2] Added colour abilities --- README.md | 4 +- nagios-scraper.py | 107 +++++++++++++++++++++++++++++++++------------- 2 files changed, 80 insertions(+), 31 deletions(-) diff --git a/README.md b/README.md index df791fa..1188be6 100644 --- a/README.md +++ b/README.md @@ -51,6 +51,6 @@ exampleuser@https://www.example.com/nagios: ### Target Objectives (features to come) -- Colour coding the results (i.e. green for good, red for bad) +- Colour coding the results (i.e. green for good, red for bad) **COMPLETED** - Error handling -- Web page output \ No newline at end of file +- Web page output diff --git a/nagios-scraper.py b/nagios-scraper.py index 45c6cd3..6b6f2f5 100644 --- a/nagios-scraper.py +++ b/nagios-scraper.py @@ -7,6 +7,16 @@ import argparse +class bcolors: + HEADER = '\033[95m' + OKBLUE = '\033[94m' + OKGREEN = '\033[92m' + WARNING = '\033[93m' + FAIL = '\033[91m' + ENDC = '\033[0m' + BOLD = '\033[1m' + UNDERLINE = '\033[4m' + def get_url_response(url, user, password, auth_type): """Get the response from a URL. @@ -36,15 +46,23 @@ def print_stats(user, url, extracted_information): """ template = ( - '{user}@{url}:\n' + bcolors.HEADER + '{user}@{url}:\n' ' Hosts\n' 'Up\tDown\tUnreachable\tPending\tProblems\tTypes\n' - '{hosts_up}\t{hosts_down}\t{hosts_unreachable}\t\t{hosts_pending}\t' - '{hosts_problems}\t\t{hosts_types}\n' - ' Services\n' + + bcolors.ENDC + + bcolors.OKGREEN + '{hosts_up}\t' + + bcolors.FAIL + '{hosts_down}\t' + + bcolors.WARNING + '{hosts_unreachable}\t\t' + + bcolors.OKBLUE + '{hosts_pending}\t' + + bcolors.FAIL + '{hosts_problems}\t\t' + + bcolors.ENDC + '{hosts_types}\n' + + bcolors.HEADER + ' Services\n' 'OK\tWarning\tUnknown\tCritical\tProblems\tTypes\n' - '{service_ok}\t{service_warning}\t{service_unknown}\t' - '{service_critical}\t\t{service_problems}\t\t{service_types}\n') + + bcolors.OKGREEN + '{service_ok}\t' + + bcolors.WARNING + '{service_warning}\t' + + bcolors.OKBLUE + '{service_unknown}\t' + + bcolors.FAIL + '{service_critical}\t\t{service_problems}\t\t' + + bcolors.ENDC + '{service_types}\n') print(template.format(user=user, url=url, **extracted_information)) @@ -59,23 +77,37 @@ def print_all_stats(user, url, extracted_information): Returns: A table for each instance listing its stats """ - header = \ - """Hosts:\t\t\t\t\t\tServices: - Up\tDn\tUn\tPn\tPr\tTy\tOK\tWrn\tUnk\tCrt\tPr\tTy\t\tWhere - """ + header = ( + bcolors.HEADER + 'Hosts:\t\t\t\t\t\tServices:\n' + + bcolors.OKGREEN + 'Up\t' + + bcolors.FAIL + 'Dn\t' + + bcolors.WARNING + 'Un\t' + + bcolors.OKBLUE + 'Pn\t' + + bcolors.FAIL + 'Pr\t' + + bcolors.ENDC + 'Ty\t' + + bcolors.OKGREEN + 'OK\t' + + bcolors.WARNING + 'Wrn\t' + + bcolors.OKBLUE + 'Unk\t' + + bcolors.FAIL + 'Crt\tPr\t' + + bcolors.ENDC + 'Ty\t\t' + + bcolors.HEADER + 'Where\n') + print(header) data_to_print = "" for (a, b, c) in zip(user, url, extracted_information): - """fields = [hosts_up, hosts_down, hosts_unreachable, hosts_pending, - hosts_problems, hosts_types, service_ok, service_warning, - service_unknown, service_critical, service_problems, service_types, - "{user}@{url}".format(user=a, url=b, **c)]""" data_to_print += ( - '{hosts_up}\t{hosts_down}\t{hosts_unreachable}\t{hosts_pending}\t' - '{hosts_problems}\t{hosts_types}\t{service_ok}\t{service_warning}' - '\t{service_unknown}\t{service_critical}\t{service_problems}\t' - '{service_types}\t{user}@{url}\n'.format(user=a, url=b, **c)) - - print(header, data_to_print) + bcolors.OKGREEN + '{hosts_up}\t' + + bcolors.FAIL + '{hosts_down}\t' + + bcolors.WARNING + '{hosts_unreachable}\t' + + bcolors.OKBLUE + '{hosts_pending}\t' + + bcolors.FAIL + '{hosts_problems}\t' + + bcolors.ENDC + '{hosts_types}\t' + + bcolors.OKGREEN + '{service_ok}\t' + + bcolors.WARNING + '{service_warning}\t' + + bcolors.OKBLUE + '{service_unknown}\t' + + bcolors.FAIL + '{service_critical}\t{service_problems}\t' + + bcolors.ENDC + '{service_types}\t' + + bcolors.HEADER + '{user}@{url}\n').format(user=a, url=b, **c) + print(data_to_print) def print_tables(user, url, extracted_information): @@ -88,17 +120,34 @@ def print_tables(user, url, extracted_information): Returns: A table for each instance listing its stats """ - headers = ['H Up', 'H Down', 'H Unreachable', 'H Pending', 'H Problems', - 'Types', 'S OK', 'S Warning', 'S Unknown', 'S Critical', - 'S Problems', 'S Types', 'Where'] + headers = [ + bcolors.OKGREEN + 'H Up', + bcolors.FAIL + 'H Down', + bcolors.WARNING + 'H Unreach', + bcolors.OKBLUE + 'H Pend', + bcolors.FAIL + 'H Problems', + bcolors.ENDC + 'Types', + bcolors.OKGREEN + 'S OK', + bcolors.WARNING + 'S Warning', + bcolors.OKBLUE + 'S Unknown', + bcolors.FAIL + 'S Critical', 'S Problems', + bcolors.ENDC + 'S Types', + bcolors.HEADER + 'Where'] data = [] for (a, b, c) in zip(user, url, extracted_information): - data.append([c["hosts_up"], c["hosts_down"], c["hosts_unreachable"], - c["hosts_pending"], c["hosts_problems"], - c["hosts_types"], c["service_ok"], c["service_warning"], - c["service_unknown"], c["service_critical"], - c["service_problems"], c["service_types"], - '{user}@{url}'.format(user=a, url=b)]) + data.append([ + bcolors.OKGREEN + c["hosts_up"], + bcolors.FAIL + c["hosts_down"], + bcolors.WARNING + c["hosts_unreachable"], + bcolors.OKBLUE + c["hosts_pending"], + bcolors.FAIL + c["hosts_problems"], + bcolors.ENDC + c["hosts_types"], + bcolors.OKGREEN + c["service_ok"], + bcolors.WARNING + c["service_warning"], + bcolors.OKBLUE + c["service_unknown"], + bcolors.FAIL + c["service_critical"], c["service_problems"], + bcolors.ENDC + c["service_types"], + bcolors.HEADER + '{user}@{url}'.format(user=a, url=b)]) print(tabulate(data, headers=headers, tablefmt='simple', numalign='center')) From ccb05aba95505c4d87b7e6726a5c9e1107767ea9 Mon Sep 17 00:00:00 2001 From: Luke Barone Date: Tue, 4 Feb 2020 12:40:13 -0800 Subject: [PATCH 2/2] Changed spacing to satisfy PEP8 --- nagios-scraper.py | 109 +++++++++++++++++++++++----------------------- 1 file changed, 55 insertions(+), 54 deletions(-) diff --git a/nagios-scraper.py b/nagios-scraper.py index 6b6f2f5..b8ce3dc 100644 --- a/nagios-scraper.py +++ b/nagios-scraper.py @@ -16,7 +16,8 @@ class bcolors: ENDC = '\033[0m' BOLD = '\033[1m' UNDERLINE = '\033[4m' - + + def get_url_response(url, user, password, auth_type): """Get the response from a URL. @@ -51,18 +52,18 @@ def print_stats(user, url, extracted_information): 'Up\tDown\tUnreachable\tPending\tProblems\tTypes\n' + bcolors.ENDC + bcolors.OKGREEN + '{hosts_up}\t' - + bcolors.FAIL + '{hosts_down}\t' + + bcolors.FAIL + '{hosts_down}\t' + bcolors.WARNING + '{hosts_unreachable}\t\t' - + bcolors.OKBLUE + '{hosts_pending}\t' - + bcolors.FAIL + '{hosts_problems}\t\t' - + bcolors.ENDC + '{hosts_types}\n' - + bcolors.HEADER + ' Services\n' + + bcolors.OKBLUE + '{hosts_pending}\t' + + bcolors.FAIL + '{hosts_problems}\t\t' + + bcolors.ENDC + '{hosts_types}\n' + + bcolors.HEADER + ' Services\n' 'OK\tWarning\tUnknown\tCritical\tProblems\tTypes\n' + bcolors.OKGREEN + '{service_ok}\t' + bcolors.WARNING + '{service_warning}\t' - + bcolors.OKBLUE + '{service_unknown}\t' - + bcolors.FAIL + '{service_critical}\t\t{service_problems}\t\t' - + bcolors.ENDC + '{service_types}\n') + + bcolors.OKBLUE + '{service_unknown}\t' + + bcolors.FAIL + '{service_critical}\t\t{service_problems}\t\t' + + bcolors.ENDC + '{service_types}\n') print(template.format(user=user, url=url, **extracted_information)) @@ -78,35 +79,35 @@ def print_all_stats(user, url, extracted_information): """ header = ( - bcolors.HEADER + 'Hosts:\t\t\t\t\t\tServices:\n' - + bcolors.OKGREEN + 'Up\t' - + bcolors.FAIL + 'Dn\t' - + bcolors.WARNING + 'Un\t' - + bcolors.OKBLUE + 'Pn\t' - + bcolors.FAIL + 'Pr\t' - + bcolors.ENDC + 'Ty\t' - + bcolors.OKGREEN + 'OK\t' - + bcolors.WARNING + 'Wrn\t' - + bcolors.OKBLUE + 'Unk\t' - + bcolors.FAIL + 'Crt\tPr\t' - + bcolors.ENDC + 'Ty\t\t' - + bcolors.HEADER + 'Where\n') + bcolors.HEADER + 'Hosts:\t\t\t\t\t\tServices:\n' + + bcolors.OKGREEN + 'Up\t' + + bcolors.FAIL + 'Dn\t' + + bcolors.WARNING + 'Un\t' + + bcolors.OKBLUE + 'Pn\t' + + bcolors.FAIL + 'Pr\t' + + bcolors.ENDC + 'Ty\t' + + bcolors.OKGREEN + 'OK\t' + + bcolors.WARNING + 'Wrn\t' + + bcolors.OKBLUE + 'Unk\t' + + bcolors.FAIL + 'Crt\tPr\t' + + bcolors.ENDC + 'Ty\t\t' + + bcolors.HEADER + 'Where\n') print(header) data_to_print = "" for (a, b, c) in zip(user, url, extracted_information): data_to_print += ( - bcolors.OKGREEN + '{hosts_up}\t' - + bcolors.FAIL + '{hosts_down}\t' - + bcolors.WARNING + '{hosts_unreachable}\t' - + bcolors.OKBLUE + '{hosts_pending}\t' - + bcolors.FAIL + '{hosts_problems}\t' - + bcolors.ENDC + '{hosts_types}\t' - + bcolors.OKGREEN + '{service_ok}\t' - + bcolors.WARNING + '{service_warning}\t' - + bcolors.OKBLUE + '{service_unknown}\t' - + bcolors.FAIL + '{service_critical}\t{service_problems}\t' - + bcolors.ENDC + '{service_types}\t' - + bcolors.HEADER + '{user}@{url}\n').format(user=a, url=b, **c) + bcolors.OKGREEN + '{hosts_up}\t' + + bcolors.FAIL + '{hosts_down}\t' + + bcolors.WARNING + '{hosts_unreachable}\t' + + bcolors.OKBLUE + '{hosts_pending}\t' + + bcolors.FAIL + '{hosts_problems}\t' + + bcolors.ENDC + '{hosts_types}\t' + + bcolors.OKGREEN + '{service_ok}\t' + + bcolors.WARNING + '{service_warning}\t' + + bcolors.OKBLUE + '{service_unknown}\t' + + bcolors.FAIL + '{service_critical}\t{service_problems}\t' + + bcolors.ENDC + '{service_types}\t' + + bcolors.HEADER + '{user}@{url}\n').format(user=a, url=b, **c) print(data_to_print) @@ -121,33 +122,33 @@ def print_tables(user, url, extracted_information): Returns: A table for each instance listing its stats """ headers = [ - bcolors.OKGREEN + 'H Up', - bcolors.FAIL + 'H Down', - bcolors.WARNING + 'H Unreach', - bcolors.OKBLUE + 'H Pend', - bcolors.FAIL + 'H Problems', - bcolors.ENDC + 'Types', - bcolors.OKGREEN + 'S OK', - bcolors.WARNING + 'S Warning', - bcolors.OKBLUE + 'S Unknown', - bcolors.FAIL + 'S Critical', 'S Problems', - bcolors.ENDC + 'S Types', - bcolors.HEADER + 'Where'] + bcolors.OKGREEN + 'H Up', + bcolors.FAIL + 'H Down', + bcolors.WARNING + 'H Unreach', + bcolors.OKBLUE + 'H Pend', + bcolors.FAIL + 'H Problems', + bcolors.ENDC + 'Types', + bcolors.OKGREEN + 'S OK', + bcolors.WARNING + 'S Warning', + bcolors.OKBLUE + 'S Unknown', + bcolors.FAIL + 'S Critical', 'S Problems', + bcolors.ENDC + 'S Types', + bcolors.HEADER + 'Where'] data = [] for (a, b, c) in zip(user, url, extracted_information): data.append([ bcolors.OKGREEN + c["hosts_up"], - bcolors.FAIL + c["hosts_down"], + bcolors.FAIL + c["hosts_down"], bcolors.WARNING + c["hosts_unreachable"], - bcolors.OKBLUE + c["hosts_pending"], - bcolors.FAIL + c["hosts_problems"], - bcolors.ENDC + c["hosts_types"], + bcolors.OKBLUE + c["hosts_pending"], + bcolors.FAIL + c["hosts_problems"], + bcolors.ENDC + c["hosts_types"], bcolors.OKGREEN + c["service_ok"], bcolors.WARNING + c["service_warning"], - bcolors.OKBLUE + c["service_unknown"], - bcolors.FAIL + c["service_critical"], c["service_problems"], - bcolors.ENDC + c["service_types"], - bcolors.HEADER + '{user}@{url}'.format(user=a, url=b)]) + bcolors.OKBLUE + c["service_unknown"], + bcolors.FAIL + c["service_critical"], c["service_problems"], + bcolors.ENDC + c["service_types"], + bcolors.HEADER + '{user}@{url}'.format(user=a, url=b)]) print(tabulate(data, headers=headers, tablefmt='simple', numalign='center'))