20
20
#
21
21
22
22
from HTMLParser import HTMLParser
23
+ from distutils .version import StrictVersion
23
24
import operator
24
25
import sys
25
26
import urllib
26
27
import curses
27
28
import traceback
28
29
import getopt
29
30
import time
31
+ import re
30
32
31
33
32
34
class ApacheStatusParser (HTMLParser ):
33
35
"""
34
36
Clase que parseja la sortida del handler server-status de apache
35
37
"""
36
38
39
+ apache_version = 0
37
40
performance_info = 2
38
41
scoreboard = 3
39
42
proceses = 4
@@ -56,6 +59,15 @@ def __init__(self):
56
59
self .append = False
57
60
self .status = 1
58
61
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
+
59
71
def handle_starttag (self , tag , attrs ):
60
72
if tag == "b" :
61
73
return
@@ -141,7 +153,7 @@ def usage(exit = 1):
141
153
print main .__doc__
142
154
sys .exit (exit )
143
155
144
- def print_screen (screen , url , show_scoreboard ):
156
+ def print_screen (screen , url , show_scoreboard , apache_version ):
145
157
screen = stdscr .subwin (0 , 0 )
146
158
screen .nodelay (1 )
147
159
@@ -156,6 +168,7 @@ def print_screen(screen, url, show_scoreboard):
156
168
while not end :
157
169
try :
158
170
data = ApacheStatusParser ()
171
+ data .set_apache_version (apache_version )
159
172
statusdata = urllib .urlopen (url ).read ()
160
173
data .feed (statusdata )
161
174
data .eval_data ()
@@ -165,9 +178,22 @@ def print_screen(screen, url, show_scoreboard):
165
178
screen .clear ()
166
179
167
180
# 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 ])
171
197
172
198
# evaluar scoreboard
173
199
if show_scoreboard :
@@ -309,7 +335,7 @@ def print_process(y,x,screen,process,columns,show_only_active,width):
309
335
else :
310
336
return 0
311
337
312
- def main (url , stdscr , show_scoreboard ):
338
+ def main (url , stdscr , show_scoreboard , apache_version ):
313
339
"""Shows the actual status of the Apache web server using the server-status
314
340
url. It needs the ExtendedStatus flag
315
341
@@ -351,7 +377,7 @@ def main(url, stdscr, show_scoreboard):
351
377
}
352
378
353
379
try :
354
- print_screen (stdscr ,url ,show_scoreboard )
380
+ print_screen (stdscr ,url ,show_scoreboard , apache_version )
355
381
except :
356
382
raise
357
383
@@ -380,6 +406,10 @@ def main(url, stdscr, show_scoreboard):
380
406
usage ()
381
407
382
408
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 )
383
413
# Initialize curses
384
414
stdscr = curses .initscr ()
385
415
# Turn off echoing of keys, and enter cbreak mode,
@@ -392,7 +422,7 @@ def main(url, stdscr, show_scoreboard):
392
422
# a special value like curses.KEY_LEFT will be returned
393
423
stdscr .keypad (1 )
394
424
try :
395
- main (url ,stdscr ,show_scoreboard ) # Enter the main loop
425
+ main (url ,stdscr ,show_scoreboard , apache_version ) # Enter the main loop
396
426
except :
397
427
raise
398
428
# Set everything back to normal
0 commit comments