/usr/lib/python2.7/dist-packages/carbon/rewrite.py is in graphite-carbon 0.9.12-3.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 | import time
import re
from os.path import exists, getmtime
from twisted.internet.task import LoopingCall
from carbon import log
class RewriteRuleManager:
def __init__(self):
self.preRules = []
self.postRules = []
self.read_task = LoopingCall(self.read_rules)
self.rules_last_read = 0.0
def clear(self):
self.preRules = []
self.postRules = []
def read_from(self, rules_file):
self.rules_file = rules_file
self.read_rules()
self.read_task.start(10, now=False)
def read_rules(self):
if not exists(self.rules_file):
self.clear()
return
# Only read if the rules file has been modified
try:
mtime = getmtime(self.rules_file)
except:
log.err("Failed to get mtime of %s" % self.rules_file)
return
if mtime <= self.rules_last_read:
return
pre = []
post = []
section = None
for line in open(self.rules_file):
line = line.strip()
if line.startswith('#') or not line:
continue
if line.startswith('[') and line.endswith(']'):
section = line[1:-1].lower()
else:
pattern, replacement = line.split('=', 1)
pattern, replacement = pattern.strip(), replacement.strip()
rule = RewriteRule(pattern, replacement)
if section == 'pre':
pre.append(rule)
elif section == 'post':
post.append(rule)
self.preRules = pre
self.postRules = post
self.rules_last_read = mtime
class RewriteRule:
def __init__(self, pattern, replacement):
self.pattern = pattern
self.replacement = replacement
self.regex = re.compile(pattern)
def apply(self, metric):
return self.regex.sub(self.replacement, metric)
# Ghetto singleton
RewriteRuleManager = RewriteRuleManager()
|