From 1c16d7eb1d6c8aeed088347c2c98d9c054eb84c4 Mon Sep 17 00:00:00 2001 From: Kepi Date: Fri, 4 Dec 2015 19:12:39 +0100 Subject: [PATCH 1/2] Initial support for apache 2.4 Original apache-top doesn't work on apache 2.4 because of small diferences in output. There is new table with thread listing on top of output which cause troubles. This initial support just ignore this table so everything works in same way as in 2.2 and older. --- apache-top.py | 44 +++++++++++++++++++++++++++++++++++++------- 1 file changed, 37 insertions(+), 7 deletions(-) diff --git a/apache-top.py b/apache-top.py index c54608b..ac6dc15 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: @@ -309,7 +335,7 @@ 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 @@ -351,7 +377,7 @@ def main(url, stdscr, show_scoreboard): } try: - print_screen(stdscr,url,show_scoreboard) + print_screen(stdscr,url,show_scoreboard,apache_version) except: raise @@ -380,6 +406,10 @@ def main(url, stdscr, show_scoreboard): usage() try: + data = ApacheStatusParser() + statusdata = urllib.urlopen(url).read() + # detect apache version + apache_version = re.search('Server Version: Apache/([^ ]+)', statusdata).group(1) # Initialize curses stdscr=curses.initscr() # Turn off echoing of keys, and enter cbreak mode, @@ -392,7 +422,7 @@ 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 + main(url,stdscr,show_scoreboard,apache_version) # Enter the main loop except: raise # Set everything back to normal From 08c1525e354a9c022c66b5a7ba9a81e32ccf7faf Mon Sep 17 00:00:00 2001 From: Kepi Date: Fri, 4 Dec 2015 19:21:00 +0100 Subject: [PATCH 2/2] Code cleanup: convert tabs to spaces On couple of places there are tabs instead of spaces. --- apache-top.py | 54 +++++++++++++++++++++++++++------------------------ 1 file changed, 29 insertions(+), 25 deletions(-) diff --git a/apache-top.py b/apache-top.py index ac6dc15..72879ad 100755 --- a/apache-top.py +++ b/apache-top.py @@ -261,9 +261,9 @@ def print_screen(screen, url, show_scoreboard, apache_version): 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: @@ -282,8 +282,8 @@ def print_screen(screen, url, show_scoreboard, apache_version): message = "Normal sorting" c = "" - except IndexError: - raise + except IndexError: + raise except: pass @@ -341,22 +341,22 @@ def main(url, stdscr, show_scoreboard, apache_version): 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 """ @@ -377,9 +377,9 @@ def main(url, stdscr, show_scoreboard, apache_version): } try: - print_screen(stdscr,url,show_scoreboard,apache_version) + print_screen(stdscr,url,show_scoreboard,apache_version) except: - raise + raise if __name__ == "__main__": @@ -405,11 +405,15 @@ def main(url, stdscr, show_scoreboard, apache_version): print "*** ERROR: Url missing\n" usage() + # detect apache version try: - data = ApacheStatusParser() statusdata = urllib.urlopen(url).read() - # detect apache version 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() # Turn off echoing of keys, and enter cbreak mode, @@ -422,9 +426,9 @@ def main(url, stdscr, show_scoreboard, apache_version): # a special value like curses.KEY_LEFT will be returned stdscr.keypad(1) try: - main(url,stdscr,show_scoreboard,apache_version) # 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) @@ -439,4 +443,4 @@ def main(url, stdscr, show_scoreboard, apache_version): 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"