-
Notifications
You must be signed in to change notification settings - Fork 1
/
apache-stats.py
91 lines (84 loc) · 2.96 KB
/
apache-stats.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#!/usr/bin/python
# Copyright (C) 2009 Glen Pitt-Pladdy
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#
#
# See: https://www.pitt-pladdy.com/blog/_20091114-134615_0000_Apache_stats_on_Cacti_via_SNMP_/
CACHETIME = 30
CACHEFILE = '/var/cache/snmp/apache'
# check for cache file newer CACHETIME seconds ago
import os
import time
if os.path.isfile ( CACHEFILE ) \
and ( time.time() - os.stat ( CACHEFILE )[8] ) < CACHETIME:
# use cached data
f = open ( CACHEFILE, 'r' )
data = f.read()
f.close()
else:
# grab the status URL (fresh data)
# need debian package python-urlgrabber
from urlgrabber import urlread
data = urlread ( 'https://<SERVER>/server-status?auto',
user_agent = 'SNMP Apache Stats' )
# write file
f = open ( CACHEFILE+'.TMP.'+`os.getpid()`, 'w' )
f.write ( data )
f.close()
os.rename ( CACHEFILE+'.TMP.'+`os.getpid()`, CACHEFILE )
# dice up the data
scoreboardkey = [ '_', 'S', 'R', 'W', 'K', 'D', 'C', 'L', 'G', 'I', '.' ]
params = {}
for line in data.splitlines():
if line == '<SERVER>':
continue
if line == 'TLSSessionCacheStatus':
continue
fields = line.split( ': ' )
if fields[0] == 'Scoreboard':
# count up the scoreboard into states
states = {}
for state in scoreboardkey:
states[state] = 0
for state in fields[1]:
states[state] += 1
elif fields[0] == 'Total kBytes':
# turn into base (byte) value
params[fields[0]] = int(fields[1])*1024
else:
# just store everything else
params[fields[0]] = fields[1]
# output the data in order (this is because some platforms don't have them all)
dataorder = [
'Total Accesses',
'Total kBytes',
'CPULoad',
'Uptime',
'ReqPerSec',
'BytesPerSec',
'BytesPerReq',
'BusyWorkers',
'IdleWorkers'
]
for param in dataorder:
try:
print params[param]
except: # not all Apache's have all stats
print 'U'
# print the scoreboard
for state in scoreboardkey:
print states[state]
# End of file