Projet

Général

Profil

Paste
Télécharger au format
Statistiques
| Branche: | Révision:

root / plugins / cherokee / munin-plugin-for-cherokee @ 29bdf34e

Historique | Voir | Annoter | Télécharger (4,07 ko)

1
#!/usr/bin/env python
2

    
3
# Munin Plugin for Cherokee in Python
4
# Based on the work of Mateusz Pawlowski: http://www.mobygeek.net/projects/files/cherokee__type_.rb
5
# Coded by the not-python-programmer Jose Luis Salas <<josacar at gmail dot com>>  ;)
6

    
7
#%# family=contrib
8
# Cherokee has to have $host/about enabled with server info set to normal
9
# Or you can change the variables below as you wish
10
# Handling arguments
11
#
12
# cherokee_host_type_data Shows data transferred
13
# cherokee_host_type_rate Shows data rate transfer
14
# cherokee_host_type_mod Shows Modules
15
# cherokee_host_type_conn Shows connections
16

    
17
# USAGE:
18
# cp /home/user/cherokee__type_.py /usr/share/munin/plugins/cherokee__type_
19
# chmod +x /usr/share/munin/plugins/cherokee__type_
20
# ln -s /usr/share/munin/plugins/cherokee__type_ cherokee_host_type_data
21
# ln -s /usr/share/munin/plugins/cherokee__type_ cherokee_localhost_type_data
22
# ln -s /usr/share/munin/plugins/cherokee__type_ cherokee_localhost_type_conn
23
# ln -s /usr/share/munin/plugins/cherokee__type_ cherokee_localhost_type_mod
24
# ln -s /usr/share/munin/plugins/cherokee__type_ cherokee_localhost_type_rate
25

    
26

    
27
import urllib2
28
import base64
29
import os
30
import sys
31
import re
32
import json
33

    
34
#py returns invalid json
35
about_location = "/about/info/js"
36

    
37
#Leave empty if not required
38
http_user = "admin"
39
http_pass = "pass"
40

    
41
host = os.getenv('host');
42
type = 'data';
43

    
44
host = "localhost"
45

    
46
match = re.match('^(?:|.*\/)cherokee_([^_]+)_type_(.+)$', sys.argv[0])
47
if match:
48
	host=match.group(1)
49
	type=match.group(2)
50
	if re.match('^([^:]+):(\d+)$', host):
51
		host=match.group(1)
52
		type=match.group(2)
53

    
54
#print "HOST: " + host + " TYPE: " + type
55

    
56
def output_values(response):
57
	if type == "mod" :
58
		section = response["modules"]
59
		for key in section:
60
			print key + '.value ' + str(section[key])
61
	elif type == "data" :
62
		section = response["traffic"]
63
		print "sent.value " + str(section["tx"])
64
		print "received.value " + str(section["rx"])
65
	elif type == "rate" :
66
		section = response["traffic"]
67
		print "sent.value " + str(section["tx"])
68
		print "received.value " + str(section["rx"])
69
	elif type == "conn" :
70
		section = response["connections"]
71
		for key in section:
72
			print key + '.value ' + str(section[key])
73

    
74

    
75
def get_data():
76
	global host
77
	global about_location
78
	global http_user
79
	global http_pass
80
	url = "http://%s%s" % ( host , about_location )
81
	request = urllib2.Request(url)
82
	base64string = base64.standard_b64encode('%s:%s' % (http_user, http_pass))
83
	if len(http_user) > 0 and len(http_pass) > 0:
84
		#print "DEBUG: AUTH: " + base64string
85
		request.add_header("Authorization", "Basic %s" % base64string)   
86
	#print "DEBUG: GET: " + url
87
	raw_data = urllib2.urlopen(request).read()
88
	#print "DEBUG: " + raw_data
89
	return json.loads(raw_data)
90

    
91
def munin_values(res):
92
	output_values(res)
93

    
94
def munin_config(response):
95
	global type
96
	print "graph_category webserver"
97
	if type == "rate":
98
		print "graph_title Cherokee Data Transfer Rate"
99
		print "graph_vlabel Bits sent(+) / received(-) per ${graph_period}"
100
		print "graph_args --base 1000"
101
		print "graph_info"
102
		print "received.label Received"
103
		print "received.graph no"
104
		print "received.type COUNTER"
105
		print "received.cdef received,8,*"
106
		print "sent.label bps"
107
		print "sent.type COUNTER"
108
		print "sent.negative received"
109
		print "sent.cdef sent,8,*"
110
	elif type == "conn":
111
		print "graph_title Cherokee Connections"
112
		print "graph_vlabel Connections"
113
		#print "graph_args --base 1000 --logarithmic"
114
		print "graph_args --base 1000"
115
		section = response["connections"]
116
		for key in section:
117
			print key.strip() + '.label ' + key.capitalize()
118
			print key + '.draw AREASTACK'
119
	elif type == "data":
120
		print "graph_title Cherokee Data Transferred"
121
		print "graph_vlabel Bytes"
122
		print "sent.label Sent"
123
		print "received.label Received"
124
	elif type == "mod":
125
		print "graph_title Cherokee Loaded Modules"
126
		print "graph_vlabel Modules"
127
		print "graph_args --base 1000 --lower-limit 0"
128
		section = response["modules"]
129
		for key in section:
130
			print key + '.label ' + key.capitalize()
131
			print key + '.draw AREASTACK'
132
	
133
if len(sys.argv) > 1 and sys.argv[1] == "config":
134
	munin_config(get_data())
135
else:
136
	munin_values(get_data())