Skip to content
This repository was archived by the owner on Jun 22, 2022. It is now read-only.

Commit 025bde5

Browse files
committed
Rename hits to requests.
Correctly calculate requests per second. Do not rely on built in RPS since that calculates average since the server was started
1 parent 83e8a34 commit 025bde5

2 files changed

Lines changed: 37 additions & 11 deletions

File tree

apache_status/conf.d/apache_status.conf

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,8 +95,8 @@ collection_group {
9595
}
9696

9797
metric {
98-
name = "ap_hits"
99-
title = "Hits"
98+
name = "ap_requests"
99+
title = "Requests"
100100
value_threshold = 0.0
101101
}
102102
}

apache_status/python_modules/apache_status.py

Lines changed: 35 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99

1010
# global to store state for "total accesses"
1111
last_total_accesses = 0
12+
last_update = 0
13+
last_uptime = 0
1214

1315
descriptors = list()
1416
Desc_Skel = {}
@@ -80,31 +82,47 @@ def update_status(self):
8082
scline = l.split(": ", 1)[1].rstrip()
8183
for sck in scline:
8284
self.status[ Scoreboard_bykey[sck] ] += 1
83-
elif l.find("ReqPerSec:") == 0:
84-
scline = l.split(": ", 1)[1].rstrip()
85-
self.status["ap_rps"] = float(scline)
8685
elif l.find("Total Accesses:") == 0:
87-
global last_total_accesses
86+
global last_total_accesses, last_update
8887
new_value = int(l.split(": ", 1)[1].rstrip())
88+
now = time.time()
8989
if (last_total_accesses == 0):
9090
# if we don't have a value from last time, record a 0,
9191
# otherwise we'll cause an enormous spike in the graph
9292
# by recording the total value of the counter
93-
self.status["ap_hits"] = 0
93+
self.status["ap_requests"] = 0
94+
self.status["ap_rps"] = 0
9495
else:
9596
# subtract counter's old value from the new value and
9697
# write it
9798
hits = new_value - last_total_accesses
98-
self.status["ap_hits"] = hits
99+
self.status["ap_requests"] = hits
100+
# Calculate the average requests per second
101+
self.status["ap_rps"] = float(hits) / ( now - last_update )
99102
# store for next time
100103
last_total_accesses = new_value
104+
last_update = now
101105

102106
elif l.find("BusyWorkers:") == 0:
103107
scline = l.split(": ", 1)[1].rstrip()
104108
self.status["ap_busy_workers"] = int(scline)
105109
elif l.find("IdleWorkers:") == 0:
106110
scline = l.split(": ", 1)[1].rstrip()
107111
self.status["ap_idle_workers"] = int(scline)
112+
elif l.find("Uptime:") == 0:
113+
global last_uptime
114+
scline = l.split(": ", 1)[1].rstrip()
115+
uptime = int(scline)
116+
# Check whether uptime is less than what it used to be. If it is Apache
117+
# was restarted so we should set ap_requests and ap_rps to 0. Otherwise
118+
# we'll get a huge spike
119+
if ( last_uptime == 0 or uptime < last_uptime):
120+
print "Zero out ap_requests since Apache has been restarted"
121+
self.status["ap_requests"] = 0
122+
self.status["ap_rps"] = 0
123+
last_uptime = uptime
124+
self.status["ap_uptime"] = uptime
125+
108126

109127
except urllib2.URLError:
110128
traceback.print_exc()
@@ -170,7 +188,7 @@ def metric_init(params):
170188
}))
171189

172190
descriptors.append(create_desc({
173-
"name" : "ap_hits",
191+
"name" : "ap_requests",
174192
"value_type" : "uint",
175193
"units" : "hits",
176194
"format" : "%u",
@@ -193,6 +211,15 @@ def metric_init(params):
193211
"description": "Idle threads",
194212
}))
195213

214+
descriptors.append(create_desc({
215+
"name" : "ap_uptime",
216+
"value_type" : "uint",
217+
"units" : "seconds",
218+
"format" : "%u",
219+
"description": "Uptime",
220+
}))
221+
222+
196223
for k,v in Scoreboard.iteritems():
197224
descriptors.append(create_desc({
198225
"name" : k,
@@ -216,10 +243,9 @@ def metric_cleanup():
216243
for d in descriptors:
217244
v = d['call_back'](d['name'])
218245
if d['name'] == "ap_rps":
219-
print 'value for %s is %.3f' % (d['name'], v)
246+
print 'value for %s is %.4f' % (d['name'], v)
220247
else:
221248
print 'value for %s is %u' % (d['name'], v)
222249
time.sleep(15)
223250
except KeyboardInterrupt:
224-
time.sleep(0.2)
225251
os._exit(1)

0 commit comments

Comments
 (0)