2020#
2121
2222from HTMLParser import HTMLParser
23+ from distutils .version import StrictVersion
2324import operator
2425import sys
2526import urllib
2627import curses
2728import traceback
2829import getopt
2930import time
31+ import re
3032
3133
3234class ApacheStatusParser (HTMLParser ):
3335 """
3436 Clase que parseja la sortida del handler server-status de apache
3537 """
3638
39+ apache_version = 0
3740 performance_info = 2
3841 scoreboard = 3
3942 proceses = 4
@@ -56,6 +59,15 @@ def __init__(self):
5659 self .append = False
5760 self .status = 1
5861
62+ def set_apache_version (self , version ):
63+ self .apache_version = version
64+ if self .apache_new ():
65+ self .scoreboard = 4
66+ self .proceses = 5
67+
68+ def apache_new (self ):
69+ return StrictVersion (self .apache_version ) >= StrictVersion ('2.4.0' )
70+
5971 def handle_starttag (self , tag , attrs ):
6072 if tag == "b" :
6173 return
@@ -141,7 +153,7 @@ def usage(exit = 1):
141153 print main .__doc__
142154 sys .exit (exit )
143155
144- def print_screen (screen , url , show_scoreboard ):
156+ def print_screen (screen , url , show_scoreboard , apache_version ):
145157 screen = stdscr .subwin (0 , 0 )
146158 screen .nodelay (1 )
147159
@@ -156,6 +168,7 @@ def print_screen(screen, url, show_scoreboard):
156168 while not end :
157169 try :
158170 data = ApacheStatusParser ()
171+ data .set_apache_version (apache_version )
159172 statusdata = urllib .urlopen (url ).read ()
160173 data .feed (statusdata )
161174 data .eval_data ()
@@ -165,9 +178,22 @@ def print_screen(screen, url, show_scoreboard):
165178 screen .clear ()
166179
167180 # imprimim el header
168- 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 ])
169- screen .addstr (1 ,0 ,data .performance_info_data [7 ])
170- 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 ])
181+ if data .apache_new ():
182+ data_uptime = 7
183+ data_restart = 4
184+ data_cpu = 10
185+ data_reqs = 11
186+ data_procs = 12
187+ else :
188+ data_uptime = 5
189+ data_restart = 3
190+ data_cpu = 7
191+ data_reqs = 8
192+ data_procs = 9
193+
194+ 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 ])
195+ screen .addstr (1 ,0 ,data .performance_info_data [data_cpu ])
196+ 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 ])
171197
172198 # evaluar scoreboard
173199 if show_scoreboard :
@@ -309,7 +335,7 @@ def print_process(y,x,screen,process,columns,show_only_active,width):
309335 else :
310336 return 0
311337
312- def main (url , stdscr , show_scoreboard ):
338+ def main (url , stdscr , show_scoreboard , apache_version ):
313339 """Shows the actual status of the Apache web server using the server-status
314340url. It needs the ExtendedStatus flag
315341
@@ -351,7 +377,7 @@ def main(url, stdscr, show_scoreboard):
351377 }
352378
353379 try :
354- print_screen (stdscr ,url ,show_scoreboard )
380+ print_screen (stdscr ,url ,show_scoreboard , apache_version )
355381 except :
356382 raise
357383
@@ -380,6 +406,10 @@ def main(url, stdscr, show_scoreboard):
380406 usage ()
381407
382408 try :
409+ data = ApacheStatusParser ()
410+ statusdata = urllib .urlopen (url ).read ()
411+ # detect apache version
412+ apache_version = re .search ('Server Version: Apache/([^ ]+)' , statusdata ).group (1 )
383413 # Initialize curses
384414 stdscr = curses .initscr ()
385415 # Turn off echoing of keys, and enter cbreak mode,
@@ -392,7 +422,7 @@ def main(url, stdscr, show_scoreboard):
392422 # a special value like curses.KEY_LEFT will be returned
393423 stdscr .keypad (1 )
394424 try :
395- main (url ,stdscr ,show_scoreboard ) # Enter the main loop
425+ main (url ,stdscr ,show_scoreboard , apache_version ) # Enter the main loop
396426 except :
397427 raise
398428 # Set everything back to normal
0 commit comments