Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100755 91 lines (65 sloc) 2.348 kB
0fbefb7 @onyxfish Add Varnish monitoring plugin.
onyxfish authored
1 #!/usr/bin/env python
706d725 @onyxfish Added varnish plugin header (credits, license, etc.)
onyxfish authored
2 """
22f58ea @Kami CloudKick -> Cloudkick
Kami authored
3 Cloudkick Varnish plugin
706d725 @onyxfish Added varnish plugin header (credits, license, etc.)
onyxfish authored
4 Developed by Christopher Groskopf for The Chicago Tribune News Applications Team
5 http://apps.chicagotribune.com
f5834a8 @onyxfish Added link to newsapps github repo.
onyxfish authored
6 https://github.com/newsapps
706d725 @onyxfish Added varnish plugin header (credits, license, etc.)
onyxfish authored
7
8 Source released under the MIT license.
9
10 Description:
11
12 This plugin will pipe all output from the command "varnishstat -1"
22f58ea @Kami CloudKick -> Cloudkick
Kami authored
13 up to Cloudkick. In addition, it will store the cache_hit and cache_miss stats
706d725 @onyxfish Added varnish plugin header (credits, license, etc.)
onyxfish authored
14 in a tmp file on each execution so that it can compute a hit_rate stat, which will
22f58ea @Kami CloudKick -> Cloudkick
Kami authored
15 also be reported to Cloudkick.
706d725 @onyxfish Added varnish plugin header (credits, license, etc.)
onyxfish authored
16
17 Error reporting:
18
19 This plugin will report an error only if varnishstat fails to
20 execute (generally this would only be the case if Varnish is not running).
21
22 Warn reporting:
23
24 This plugin will report a warning anytime that it fails to parse
25 varnishstat's output.
26
27 TODO:
28
29 Support custom warnings and errors when the hit_rate stat falls below a
30 specified threshold.
31 """
0fbefb7 @onyxfish Add Varnish monitoring plugin.
onyxfish authored
32
33 import os
34 import re
35 import subprocess
36 import sys
37
38 HIT_RATE_FILE = '/tmp/cloudkick-agent-varnish.tmp'
39
40 result = subprocess.Popen(['varnishstat', '-1'], stdout=subprocess.PIPE).communicate()[0]
41
42 if not result:
ee5c1a3 Fixes error status output and removes extra space.
Daniel Benamy authored
43 print 'status err Varnish is not running.'
2754d38 @Kami PEP8-ify (tabs to spaces)
Kami authored
44 sys.exit()
0fbefb7 @onyxfish Add Varnish monitoring plugin.
onyxfish authored
45
46 data = []
47 hits = None
48 misses = None
49
50 try:
2754d38 @Kami PEP8-ify (tabs to spaces)
Kami authored
51 for r in result.split('\n'):
52 if not r:
53 continue
54
55 parts = re.split('\s+', r.strip())
56
57 data.append('metric %s int %s' % (parts[0], parts[1]))
58
59 if parts[0] == 'cache_hit':
60 hits = int(parts[1])
61 elif parts[0] == 'cache_miss':
62 misses = int(parts[1])
63
64 if hits and misses:
65 if os.path.exists(HIT_RATE_FILE):
66 with open(HIT_RATE_FILE, 'r') as f:
67 parts = f.read().split(',')
68 last_hits = int(parts[0])
69 last_misses = int(parts[1])
70
71 delta_hits = hits - last_hits
72 delta_misses = misses - last_misses
73
74 if delta_misses == 0:
75 hit_rate = float(1.0)
76 else:
77 hit_rate = float(delta_hits) / (delta_hits + delta_misses)
78
79 data.append('metric hit_rate float %1.3f' % hit_rate)
80
81 with open(HIT_RATE_FILE, 'w') as f:
82 f.write('%i,%i' % (hits, misses))
0fbefb7 @onyxfish Add Varnish monitoring plugin.
onyxfish authored
83 except Exception, e:
2754d38 @Kami PEP8-ify (tabs to spaces)
Kami authored
84 print 'status warn Error parsing varnishstat output (%s)' % (str(e))
85 sys.exit()
0fbefb7 @onyxfish Add Varnish monitoring plugin.
onyxfish authored
86
87 print 'status ok Varnish is running.'
88
ee5c1a3 Fixes error status output and removes extra space.
Daniel Benamy authored
89 for d in data:
2754d38 @Kami PEP8-ify (tabs to spaces)
Kami authored
90 print d
Something went wrong with that request. Please try again.