Hello,

>  For data this predictable, simple regex matching will probably work fine.

I thought that too...

Anyway - here's what I've come up with:

#!/usr/bin/python

import urllib, sgmllib, re

mod_status = urllib.urlopen("http://10.1.2.201/server-status";)
status_info = mod_status.read()
mod_status.close()

class StatusParser(sgmllib.SGMLParser):
    def parse(self, string):
        self.feed(string)
        self.close()

    def __init__(self, verbose=0):
        sgmllib.SGMLParser.__init__(self, verbose)
        self.information = []
        self.inside_dt_element = False

    def start_dt(self, attributes):
        self.inside_dt_element = True

    def end_dt(self):
        self.inside_dt_element = False

    def handle_data(self, data):
        if self.inside_dt_element:
            self.information.append(data)

    def get_data(self):
        return self.information


status_parser = StatusParser()
status_parser.parse(status_info)

rps_pattern = re.compile( '(\d+\.\d+) requests/sec' )
connections_pattern = re.compile( '(\d+) requests\D*(\d+) idle.*' )

for line in status_parser.get_data():
    rps_match = rps_pattern.search( line )
    connections_match =  connections_pattern.search( line )
    if rps_match:
        rps = float(rps_match.group(1))
    elif connections_match:
        connections = int(connections_match.group(1)) +
int(connections_match.group(2))

rps_threshold = 10
connections_threshold = 100

if rps > rps_threshold:
    print "CRITICAL: %s Requests per second" % rps
else:
    print "OK: %s Requests per second" % rps

if connections > connections_threshold:
    print "CRITICAL: %s Simultaneous Connections" % connections
else:
    print "OK: %s Simultaneous Connections" % connections

Comments and criticism please.

S.
_______________________________________________
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor

Reply via email to