forked from stfc/telegraf-scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmetrics-influxdb-squid
executable file
·65 lines (49 loc) · 2.39 KB
/
metrics-influxdb-squid
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
#!/usr/bin/python
import datetime
from datetime import datetime, timedelta
import socket
import subprocess, re
import sys
data = ''
# Squid stats
squidStatsCmd = ['/usr/bin/squidclient', '-h', 'localhost', '-p', '3128']
mgrSections = ["5min", "info", "storedir"]
regexps = [
("5min", re.compile("^(?P<stat_name>(?!syscalls|dns|icp|server.other|server.ftp|server.http)[a-zA-Z_.]+(?<!median_svc_time)) = (?P<stat_value>[0-9.]*)(?P<stat_is_rate>[ /])(?P<stat_units>[a-z]*)$")),
("info_5", re.compile("^\s*(?P<stat_name>[a-zA-Z %]*):\s*5min: (?P<stat_value>[0-9.]*)(?P<stat_units>%), 60min: ([0-9.]*)%$")),
("info_60", re.compile("^\s*(?P<stat_name>[a-zA-Z %]*):\s*5min: ([0-9.]*)(?P<stat_units>%), 60min: (?P<stat_value>[0-9.]*)%$")),
("storedir", re.compile("^(?P<stat_name>Percent Used): (?P<stat_value>\d.\d*)(?P<stat_units>%)$")),
("storedir", re.compile("^(?P<stat_name>Filesystem Space in use): \d*/\d* [A-Z]{2} \((?P<stat_value>\d.\d*)(?P<stat_units>%)\)$")),
("info", re.compile("^\s*(?P<stat_name>Number of file desc currently in use):\s*(?P<stat_value>\d*)$")),
]
# Gather raw data from sections
results = ""
for section in mgrSections:
subproc = subprocess.Popen(squidStatsCmd + ["mgr:" + section], stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
output, errors = subproc.communicate()
retcode = subproc.returncode
if retcode == 0:
results += output
# Process raw data
if results <> "":
stats = results.split("\n")
for s in stats:
for n, r in regexps:
m = r.match(s)
if m != None:
m = m.groupdict()
# Fix units for rate based stats
if "stat_is_rate" in m and m["stat_is_rate"] == "/":
m["stat_units"] = "per " + m["stat_units"]
# Storedir stats are not too well named
if n == "storedir":
m["stat_name"] = "store_directory_" + m["stat_name"]
elif n == "info_60":
m["stat_name"] = m["stat_name"] + "_60min"
# Prefix all stat names
m["stat_name"] = m["stat_name"].replace(".", "_").replace(" ","_").lower()
# Name corrections
m["stat_name"] = m["stat_name"].replace("%", "percent")
# Generate line for InfluxDB
data += m["stat_name"] + ' value=' + m["stat_value"] + '\n'
print data