diff --git a/apache-top.py b/apache-top.py index c54608b..72879ad 100755 --- a/apache-top.py +++ b/apache-top.py @@ -20,6 +20,7 @@ # from HTMLParser import HTMLParser +from distutils.version import StrictVersion import operator import sys import urllib @@ -27,6 +28,7 @@ import traceback import getopt import time +import re class ApacheStatusParser(HTMLParser): @@ -34,6 +36,7 @@ class ApacheStatusParser(HTMLParser): Clase que parseja la sortida del handler server-status de apache """ + apache_version = 0 performance_info = 2 scoreboard = 3 proceses = 4 @@ -56,6 +59,15 @@ def __init__(self): self.append = False self.status = 1 + def set_apache_version(self, version): + self.apache_version = version + if self.apache_new(): + self.scoreboard = 4 + self.proceses = 5 + + def apache_new(self): + return StrictVersion(self.apache_version) >= StrictVersion('2.4.0') + def handle_starttag(self, tag, attrs): if tag == "b": return @@ -141,7 +153,7 @@ def usage(exit = 1): print main.__doc__ sys.exit(exit) -def print_screen(screen, url, show_scoreboard): +def print_screen(screen, url, show_scoreboard, apache_version): screen = stdscr.subwin(0, 0) screen.nodelay(1) @@ -156,6 +168,7 @@ def print_screen(screen, url, show_scoreboard): while not end: try: data = ApacheStatusParser() + data.set_apache_version(apache_version) statusdata = urllib.urlopen(url).read() data.feed(statusdata) data.eval_data() @@ -165,9 +178,22 @@ def print_screen(screen, url, show_scoreboard): screen.clear() # imprimim el header - screen.addstr(0,0,data.performance_info_data[5].replace("Server uptime: ","Uptime:").replace(" days","d").replace(" day","d").replace(" hours","h").replace(" hour","h").replace(" minutes","m").replace(" minute","m").replace(" seconds","s").replace("second","s") + ", " + data.performance_info_data[3]) - screen.addstr(1,0,data.performance_info_data[7]) - screen.addstr(2,0,data.performance_info_data[8].replace("request","req").replace("second","sec") + ", Active/Idle: " + data.performance_info_data[9].split()[0] + "/" + data.performance_info_data[9].split()[5]) + if data.apache_new(): + data_uptime = 7 + data_restart = 4 + data_cpu = 10 + data_reqs = 11 + data_procs = 12 + else: + data_uptime = 5 + data_restart = 3 + data_cpu = 7 + data_reqs = 8 + data_procs = 9 + + screen.addstr(0,0,data.performance_info_data[data_uptime].replace("Server uptime: ","Uptime:").replace(" days","d").replace(" day","d").replace(" hours","h").replace(" hour","h").replace(" minutes","m").replace(" minute","m").replace(" seconds","s").replace("second","s") + ", " + data.performance_info_data[data_restart]) + screen.addstr(1,0,data.performance_info_data[data_cpu]) + screen.addstr(2,0,data.performance_info_data[data_reqs].replace("request","req").replace("second","sec") + ", Active/Idle: " + data.performance_info_data[data_procs].split()[0] + "/" + data.performance_info_data[data_procs].split()[5]) # evaluar scoreboard if show_scoreboard: @@ -235,9 +261,9 @@ def print_screen(screen, url, show_scoreboard): show_scoreboard = 1 message = "Showing mod_status scoreboard" else: - show_scoreboard = 0 - message = "Hiding mod_status scoreboard" - y = 0 + show_scoreboard = 0 + message = "Hiding mod_status scoreboard" + y = 0 elif c == "a": # mostra els actius if show_only_active: @@ -256,8 +282,8 @@ def print_screen(screen, url, show_scoreboard): message = "Normal sorting" c = "" - except IndexError: - raise + except IndexError: + raise except: pass @@ -309,28 +335,28 @@ def print_process(y,x,screen,process,columns,show_only_active,width): else: return 0 -def main(url, stdscr, show_scoreboard): +def main(url, stdscr, show_scoreboard, apache_version): """Shows the actual status of the Apache web server using the server-status url. It needs the ExtendedStatus flag Usage: apache-top [-s] -u url -u url Url where apache-status is located - Example: apache-top.py -u http://www.domain.com/server-status + Example: apache-top.py -u http://www.domain.com/server-status -s Show scoreboard Interactive keys: - q Exit - P Sort by PID - C Sort by CPU usage - S Sort by Seconds since beginning of most recent request - V Sort by VirtualHost - M Sort by Mopde of operation - R Sort by Request - I Sort by Ip - s Show/Hide mod_status scoreboard - a Switch between show all processes and show only active processes (default) - r Reverse sort + q Exit + P Sort by PID + C Sort by CPU usage + S Sort by Seconds since beginning of most recent request + V Sort by VirtualHost + M Sort by Mopde of operation + R Sort by Request + I Sort by Ip + s Show/Hide mod_status scoreboard + a Switch between show all processes and show only active processes (default) + r Reverse sort """ @@ -351,9 +377,9 @@ def main(url, stdscr, show_scoreboard): } try: - print_screen(stdscr,url,show_scoreboard) + print_screen(stdscr,url,show_scoreboard,apache_version) except: - raise + raise if __name__ == "__main__": @@ -379,6 +405,14 @@ def main(url, stdscr, show_scoreboard): print "*** ERROR: Url missing\n" usage() + # detect apache version + try: + statusdata = urllib.urlopen(url).read() + apache_version = re.search('Server Version: Apache/([^ ]+)', statusdata).group(1) + except: + print "ERROR parsing the data. Please, make sure you are alowed to read the server-status page and you have ExtendedStatus flag activated" + sys.exit(2) + try: # Initialize curses stdscr=curses.initscr() @@ -392,9 +426,9 @@ def main(url, stdscr, show_scoreboard): # a special value like curses.KEY_LEFT will be returned stdscr.keypad(1) try: - main(url,stdscr,show_scoreboard) # Enter the main loop - except: - raise + main(url,stdscr,show_scoreboard,apache_version) # Enter the main loop + except: + raise # Set everything back to normal curses.curs_set(1) stdscr.keypad(0) @@ -409,4 +443,4 @@ def main(url, stdscr, show_scoreboard): curses.nocbreak() curses.endwin() #traceback.print_exc() # Print the exception - print "ERROR parsing the data. Please, make sure you are alowed to read the server-status page and you have ExtendedStatus flag activated" + print "ERROR parsing the data. Please, make sure you are alowed to read the server-status page and you have ExtendedStatus flag activated"