I am attempting to learn Python. It was suggested to me to try a web scraper, so I thought to get myself to look at multiple Nagios instances. I have not programmed in Python before, but learned from other code reviews to adapt my standards to PEP8. The online checker says the code passes.
from bs4 import BeautifulSoup
import requests
from requests.auth import HTTPDigestAuth
from requests.auth import HTTPBasicAuth
import urllib.request
import time
allNagiosInfo = [
('http://example.com/nagios', 'user', 'password', 'Basic'),
('https://network2.example.com/nagios', 'user', 'password', 'Digest'),
('https://anothernetwork.example.com/nagios', 'user', 'password', 'Basic')]
for nagiosEntry in allNagiosInfo:
nagiosBaseURL = nagiosEntry[0]
nagiosUsername = nagiosEntry[1]
nagiosPassword = nagiosEntry[2]
nagiosAuthType = nagiosEntry[3]
# print("Current values: {}\t{}\t{}\t{}".format(
# nagiosBaseURL, nagiosUsername, nagiosPassword, nagiosAuthType))
if nagiosAuthType == "Basic":
response = requests.get(nagiosBaseURL + "/cgi-bin/status.cgi?host=all",
auth=HTTPBasicAuth(nagiosUsername,
nagiosPassword))
if nagiosAuthType == "Digest":
response = requests.get(nagiosBaseURL + "/cgi-bin/status.cgi?host=all",
auth=HTTPDigestAuth(nagiosUsername,
nagiosPassword))
html = BeautifulSoup(response.text, "html.parser")
for i, items in enumerate(html.select('td')):
if i == 3:
hostsAll = items.text.split('\n')
hostsUp = hostsAll[12]
hostsDown = hostsAll[13]
hostsUnreachable = hostsAll[14]
hostsPending = hostsAll[15]
hostsProblems = hostsAll[24]
hostsTypes = hostsAll[25]
if i == 12:
serviceAll = items.text.split('\n')
serviceOK = serviceAll[13]
serviceWarning = serviceAll[14]
serviceUnknown = serviceAll[15]
serviceCritical = serviceAll[16]
serviceProblems = serviceAll[26]
serviceTypes = serviceAll[27]
# print(i, items.text) ## To get the index and text
print("Nagios Server at {}@{}:".format(nagiosUsername, nagiosBaseURL))
print(" Hosts")
print("\tUp - {}".format(hostsUp))
print("\tDown - {}".format(hostsDown))
print("\tUnreachable - {}".format(hostsUnreachable))
print("\tPending - {}".format(hostsPending))
print("\tProblems - {}".format(hostsProblems))
print("\tTypes - {}".format(hostsTypes))
print(" Services")
print("\tOK - {}".format(serviceOK))
print("\tWarning - {}".format(serviceWarning))
print("\tUnknown - {}".format(serviceUnknown))
print("\tCritical - {}".format(serviceCritical))
print("\tProblems - {}".format(serviceProblems))
print("\tTypes - {}".format(serviceTypes))
# print("Request returned:\n\n{}".format(html.text))
# To get the full request
Currently, it outputs my three instances properly:
Nagios Server at user@http://example.com/nagios:
Hosts
Up - 24
Down - 0
Unreachable - 0
Pending - 0
Problems - 0
Types - 24
Services
OK - 142
Warning - 0
Unknown - 0
Critical - 0
Problems - 0
Types - 142
Nagios Server at user@https://network2.example.com/nagios:
Hosts
Up - 3
Down - 0
Unreachable - 0
Pending - 0
Problems - 0
Types - 3
Services
OK - 28
Warning - 0
Unknown - 0
Critical - 0
Problems - 0
Types - 28
Nagios Server at user@https://anothernetwork.example.com/nagios:
Hosts
Up - 56
Down - 0
Unreachable - 0
Pending - 0
Problems - 0
Types - 56
Services
OK - 130
Warning - 0
Unknown - 0
Critical - 0
Problems - 0
Types - 130
I understand I do not have error checking (i.e. is the username/password correct? Auth Type correct? Server URL right?), so that will be my next thing to learn. I also understand I should not have variables (especially with passwords) hard-coded into the code, but I do not know what is the "proper" way of dealing with secure variables.