Print html

Welcome, Rytis<br/> <span class="subText">

You are able to update your Telegraph.co.uk profile from this page </span> </h2> [... ]

The HTML page that has been retrieved shows that I have successfully logged on to the system, and the web page that has been returned to me is the user-profile/account-management page. Now let's try visiting the log-off page, which is supposed to log us out from the site, effectively invalidating the session cookie that we retrieved earlier:

>>> url_logon = 'https://auth.telegraph.co.uk/sam-ui/login.htm' >>> url_logoff = 'https://auth.telegraph.co.uk/sam-ui/logoff.htm' >>> import urllib, urllib2

>>> data = urllib.urlencode({'email': '[email protected]', 'password': 'secret'})

>>> opener = urllib2.build_opener(urllib2.HTTPCookieProcessor())

>>> urllib2.install_opener(opener)

>>> res = opener.open(url_logoff) >>> html_logoff = res.read() >>> res.close()

Now let's see if the <span> tag of the class subText is a mark that can be used to distinguish between the registration page (meaning that we have logged on successfully) and the main landing page (meaning we logged off successfully):

>>> from BeautifulSoup import BeautifulSoup >>> soup_logon = BeautifulSoup(html_logon) >>> soup_logoff = BeautifulSoup(html_logoff) >>> len(soup_logon.findAll('span', 'subText')) 1

>>> len(soup_logoff.findAll('span', 'subText'))

And indeed, this proves to be a reasonably valid test. So we have a way to authenticate ourselves to the web site by submitting the required information in the POST data request. You can use the same method to submit large forms as well. For example, you may want to build an automated check to test the registration functionality of your web site or the comment system.

The check script for Nagios system is quite similar to the one that we wrote for the navigation test. Listing 8-5 shows the complete script.

Listing 8-5. The site logon/logoff check script #!/usr/bin/env python import sys import urllib2, urllib import time from BeautifulSoup import BeautifulSoup from optparse import OptionParser

NAGIOS_OK = 0 NAGIOS_WARNING = 1 NAGIOS_CRITICAL = 2

WEBSITE_LOGON = 'https://auth.telegraph.co.uk/sam-ui/login.htm' WEBSITE_LOGOFF = 'https://auth.telegraph.co.uk/sam-ui/logoff.htm' WEBSITE_USER = '[email protected]' WEBSITE_PASS = 'secret'

def test_logon_logoff():

opener = urllib2.build_opener(urllib2.HTTPCookieProcessor()) urllib2.install_opener(opener)

data = urllib.urlencode({'email': WEBSITE_USER, 'password': WEBSITE_PASS})

try:

result = opener.open(WEBSITE_LOGON, data) html_logon = result.read() result.close()

result = opener.open(WEBSITE_LOGOFF) html_logoff = result.read() result.close()

soup_logon = BeautifulSoup(html_logon) soup_logoff = BeautifulSoup(html_logoff) if len(soup_logon.findAll('span', 'subText')) == 1 and \ len(soup_logoff.findAll('span', 'subText')) == 0: status = [NAGIOS_OK, 'Logon/logoff operation'] else:

status = [NAGIOS_CRITICAL,

'ERROR: Failed to logon and then logoff to the web site']

except:

status = [NAGIOS_CRITICAL, 'ERROR: Failure in the logon/logoff test'] return status def main():

parser = OptionParser()

parser.add_option('-w', dest='time_warn', default=3.8, help="Warning threshold in seconds, defaul: %default") parser.add_option('-c', dest='time_crit', default=5.8, help="Critical threshold in seconds, default: %default") (options, args) = parser.parse_args() if float(options.time_crit) < float(options.time_warn):

options.time_warn = options.time_crit start = time.time() code, message = test_logon_logoff() elapsed = time.time() - start if code != 0:

print message sys.exit(code) else:

if elapsed < float(options.time_warn):

print "OK: Performed %s sucessfully in %f seconds" % (message, elapsed) sys.exit(NAGIOS_OK) elif elapsed < float(options.time_crit):

print "WARNING: Performed %s sucessfully in %f seconds" % (message, elapsed)

sys.exit(NAGIOS_WARNING) else:

print "CRITICAL: Performed %s sucessfully in %f seconds" % (message, elapsed)

sys.exit(NAGIOS_CRITICAL)

You need to add this script to the commands. cfg file and create the appropriate host, hostgroup, service, and service group definitions in the Nagios configuration files, just as we did with the site navigation script. Once you have added this configuration, restart the Nagios process and after a short time you should see the check status appearing in the Nagios console.

Was this article helpful?

0 0

Post a comment