Skip to content

Commit

Permalink
Merge branch 'master' of github.com:lukebarone/nagios-scraper
Browse files Browse the repository at this point in the history
No idea what changed yet
  • Loading branch information
lukebarone committed Jan 16, 2021
2 parents 59af679 + ccb05ab commit e46522c
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 31 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
- Web page output
108 changes: 79 additions & 29 deletions nagios-scraper.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,17 @@
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.
Expand Down Expand Up @@ -36,15 +47,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))


Expand All @@ -59,23 +78,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):
Expand All @@ -88,17 +121,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'))

Expand Down

0 comments on commit e46522c

Please sign in to comment.