Skip to content

Commit

Permalink
Added initial support for a metric base path from which all other met…
Browse files Browse the repository at this point in the history
…rics are derived. Think of this as a root path
  • Loading branch information
standaloneSA committed Sep 23, 2015
1 parent 7931f20 commit 23ef040
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 27 deletions.
40 changes: 22 additions & 18 deletions graphios.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,24 +110,25 @@

class GraphiosMetric(object):
def __init__(self):
self.LABEL = '' # The name in the perfdata from nagios
self.VALUE = '' # The measured value of that metric
self.UOM = '' # The unit of measure for the metric
self.DATATYPE = '' # HOSTPERFDATA|SERVICEPERFDATA
self.METRICTYPE = 'gauge' # gauge|counter|timer etc..
self.TIMET = '' # Epoc time the measurement was taken
self.HOSTNAME = '' # name of th host measured
self.SERVICEDESC = '' # nagios configured service description
self.PERFDATA = '' # the space-delimited raw perfdata
self.SERVICECHECKCOMMAND = '' # literal check command syntax
self.HOSTCHECKCOMMAND = '' # literal check command syntax
self.HOSTSTATE = '' # current state afa nagios is concerned
self.HOSTSTATETYPE = '' # HARD|SOFT
self.SERVICESTATE = '' # current state afa nagios is concerned
self.SERVICESTATETYPE = '' # HARD|SOFT
self.GRAPHITEPREFIX = '' # graphios prefix
self.GRAPHITEPOSTFIX = '' # graphios suffix
self.VALID = False # if this metric is valid
self.LABEL = '' # The name in the perfdata from nagios
self.VALUE = '' # The measured value of that metric
self.UOM = '' # The unit of measure for the metric
self.DATATYPE = '' # HOSTPERFDATA|SERVICEPERFDATA
self.METRICTYPE = 'gauge' # gauge|counter|timer etc..
self.TIMET = '' # Epoc time the measurement was taken
self.HOSTNAME = '' # name of th host measured
self.SERVICEDESC = '' # nagios configured service description
self.PERFDATA = '' # the space-delimited raw perfdata
self.SERVICECHECKCOMMAND = '' # literal check command syntax
self.HOSTCHECKCOMMAND = '' # literal check command syntax
self.HOSTSTATE = '' # current state afa nagios is concerned
self.HOSTSTATETYPE = '' # HARD|SOFT
self.SERVICESTATE = '' # current state afa nagios is concerned
self.SERVICESTATETYPE = '' # HARD|SOFT
self.METRICBASEPATH = cfg["metric_base_path"] # Establishes a root base path
self.GRAPHITEPREFIX = '' # graphios prefix
self.GRAPHITEPOSTFIX = '' # graphios suffix
self.VALID = False # if this metric is valid

def validate(self):
# because we eliminated all whitespace, there shouldn't be any quotes
Expand All @@ -148,6 +149,9 @@ def validate(self):
else:
# not using service descriptions
if (
# We should keep this logic and not check for a
# base path here. Just because there's a base path
# doesn't mean the metric should be considered valid
self.GRAPHITEPREFIX == "" and
self.GRAPHITEPOSTFIX == ""
):
Expand Down
28 changes: 19 additions & 9 deletions graphios_backends.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ def __init__(self, cfg):
try:
cfg['librato_namevals']
except:
self.namevals = ['GRAPHITEPREFIX', 'SERVICEDESC',
self.namevals = ['METRICBASEPATH', 'GRAPHITEPREFIX', 'SERVICEDESC',
'GRAPHITEPOSTFIX', 'LABEL']
else:
self.namevals = cfg['librato_namevals'].split(",")
Expand Down Expand Up @@ -257,6 +257,12 @@ def __init__(self, cfg):
except:
self.use_service_desc = False

try:
cfg['metric_base_path']
self.metric_base_path = cfg['metric_base_path']
except:
self.metric_base_path = ''

try:
cfg['test_mode']
self.test_mode = cfg['test_mode']
Expand Down Expand Up @@ -313,10 +319,11 @@ def build_path(self, m):
"""
Builds a carbon metric
"""
pre = ""
if m.METRICBASEPATH != "":
pre = "%s." % m.METRICBASEPATH
if m.GRAPHITEPREFIX != "":
pre = "%s." % m.GRAPHITEPREFIX
else:
pre = ""
pre += "%s." % m.GRAPHITEPREFIX
if m.GRAPHITEPOSTFIX != "":
post = ".%s" % m.GRAPHITEPOSTFIX
else:
Expand Down Expand Up @@ -419,7 +426,7 @@ def convert(self, metrics):
# Converts the metric object list into a list of statsd tuples
out_list = []
for m in metrics:
path = '%s.%s.%s.%s' % (m.GRAPHITEPREFIX, m.HOSTNAME,
path = '%s.%s.%s.%s.%s' % (m.METRICBASEPATH, m.GRAPHITEPREFIX, m.HOSTNAME,
m.GRAPHITEPOSTFIX, m.LABEL)
path = re.sub(r'\.$', '', path) # fix paths that end in dot
path = re.sub(r'\.\.', '.', path) # fix paths with empty values
Expand Down Expand Up @@ -521,14 +528,16 @@ def build_url(self, server):
def build_path(self, m):
""" Returns a path """
path = ""
if m.METRICBASEPATH != "":
path += "%s." % m.METRICBASEPATH

if m.GRAPHITEPREFIX != "":
path = "%s.%s." % (m.GRAPHITEPREFIX, m.HOSTNAME)
else:
path = "%s." % m.HOSTNAME
path += "%s.%s." % m.GRAPHITEPREFIX

path += "%s." % m.HOSTNAME

if m.SERVICEDESC != "":
path = "%s%s." % (path, m.SERVICEDESC)
path += "%s." % m.SERVICEDESC

path = "%s%s" % (path, m.LABEL)

Expand Down Expand Up @@ -696,6 +705,7 @@ def send(self, metrics):
print("%s:%s" % ('HOSTSTATETYPE ', metric.HOSTSTATETYPE))
print("%s:%s" % ('SERVICESTATE ', metric.SERVICESTATE))
print("%s:%s" % ('SERVICESTATETYPE ', metric.SERVICESTATETYPE))
print("%s:%s" % ('METRICBASEPATH ', metric.METRICBASEPATH))
print("%s:%s" % ('GRAPHITEPREFIX ', metric.GRAPHITEPREFIX))
print("%s:%s" % ('GRAPHITEPOSTFIX ', metric.GRAPHITEPOSTFIX))
print("-------")
Expand Down

0 comments on commit 23ef040

Please sign in to comment.