added ability to supply an alias for a key re: #10

This commit is contained in:
drewkerrigan 2015-11-15 23:50:12 -05:00
parent 2644151b5f
commit 32a8884881
2 changed files with 47 additions and 30 deletions

View File

@ -68,30 +68,30 @@ optional arguments:
-B AUTH, --basic-auth AUTH -B AUTH, --basic-auth AUTH
Basic auth string "username:password" Basic auth string "username:password"
-p PATH, --path PATH Path. -p PATH, --path PATH Path.
-D DATA, --data DATA The http payload to send as an POST. -D DATA, --data DATA The http payload to send as a POST
-e [KEY_LIST [KEY_LIST ...]], --key_exists [KEY_LIST [KEY_LIST ...]] -e [KEY_LIST [KEY_LIST ...]], --key_exists [KEY_LIST [KEY_LIST ...]]
Checks existence of these keys to determine status. Multiple key values can Checks existence of these keys to determine status.
be delimited with colon (key,value1:value2).
-q [KEY_VALUE_LIST [KEY_VALUE_LIST ...]], --key_equals [KEY_VALUE_LIST [KEY_VALUE_LIST ...]] -q [KEY_VALUE_LIST [KEY_VALUE_LIST ...]], --key_equals [KEY_VALUE_LIST [KEY_VALUE_LIST ...]]
Checks equality of these keys and values (key,value Checks equality of these keys and values
key2,value2) to determine status. (key[>alias],value key2,value2) to determine status.
Multiple key values can be delimited with colon
(key,value1:value2)
-l [KEY_LTE_LIST [KEY_LTE_LIST ...]], --key_lte [KEY_LTE_LIST [KEY_LTE_LIST ...]] -l [KEY_LTE_LIST [KEY_LTE_LIST ...]], --key_lte [KEY_LTE_LIST [KEY_LTE_LIST ...]]
Checks that these keys and values (key,value Checks that these keys and values (key[>alias],value
key2,value2) are less than or equal to the returned key2,value2) are less than or equal to the returned
json value to determine status. json value to determine status.
-g [KEY_GTE_LIST [KEY_GTE_LIST ...]], --key_gte [KEY_GTE_LIST [KEY_GTE_LIST ...]] -g [KEY_GTE_LIST [KEY_GTE_LIST ...]], --key_gte [KEY_GTE_LIST [KEY_GTE_LIST ...]]
Checks that these keys and values (key,value Checks that these keys and values (key[>alias],value
key2,value2) are greater than or equal to the returned key2,value2) are greater than or equal to the returned
json value to determine status. json value to determine status.
-m [METRIC_LIST [METRIC_LIST ...]], --key_metric [METRIC_LIST [METRIC_LIST ...]] -m [METRIC_LIST [METRIC_LIST ...]], --key_metric [METRIC_LIST [METRIC_LIST ...]]
Gathers the values of these keys Gathers the values of these keys (key[>alias],UnitOfMe
(key,UnitOfMeasure,Min,Max,WarnRange,CriticalRange) asure,Min,Max,WarnRange,CriticalRange) for Nagios
for Nagios performance data. More information about performance data. More information about Range format
Range format and units of measure for nagios can be and units of measure for nagios can be found at
found at https://nagios- nagios-plugins.org/doc/guidelines.html Additional
plugins.org/doc/guidelines.html Additional formats for formats for this parameter are: (key),
this parameter are: (key), (key,UnitOfMeasure), (key,UnitOfMeasure), (key,UnitOfMeasure,Min,Max).
(key,UnitOfMeasure,Min,Max).
-s, --ssl HTTPS mode. -s, --ssl HTTPS mode.
-t TIMEOUT, --timeout TIMEOUT -t TIMEOUT, --timeout TIMEOUT
Connection timeout (seconds) Connection timeout (seconds)

View File

@ -106,6 +106,17 @@ class JsonHelper:
else: else:
return (None, 'not_found') return (None, 'not_found')
def _getKeyAlias(original_key):
key = original_key
alias = original_key
if '>' in original_key:
keys = original_key.split('>')
if len(keys) == 2:
key, alias = keys
return key, alias
class JsonRuleProcessor: class JsonRuleProcessor:
"""Perform checks and gather values from a JSON dict given rules and metrics definitions""" """Perform checks and gather values from a JSON dict given rules and metrics definitions"""
def __init__(self, json_data, rules_args): def __init__(self, json_data, rules_args):
@ -123,26 +134,30 @@ class JsonRuleProcessor:
if self.rules.key_list != None: if self.rules.key_list != None:
for k in self.rules.key_list: for k in self.rules.key_list:
if (self.helper.exists(k) == False): key, alias = _getKeyAlias(k)
reason += " Key %s did not exist." % k if (self.helper.exists(key) == False):
reason += " Key %s did not exist." % alias
if self.rules.key_value_list != None: if self.rules.key_value_list != None:
for kv in self.rules.key_value_list: for kv in self.rules.key_value_list:
k, v = kv.split(',') k, v = kv.split(',')
if (self.helper.equals(k, v) == False): key, alias = _getKeyAlias(k)
reason += " Value %s for key %s did not match." % (v, k) if (self.helper.equals(key, v) == False):
reason += " Value for key %s did not match %s." % (alias, v)
if self.rules.key_lte_list != None: if self.rules.key_lte_list != None:
for kv in self.rules.key_lte_list: for kv in self.rules.key_lte_list:
k, v = kv.split(',') k, v = kv.split(',')
if (self.helper.lte(k, v) == False): key, alias = _getKeyAlias(k)
reason += " Value %s was not less than or equal to value for key %s." % (v, k) if (self.helper.lte(key, v) == False):
reason += " Value for key %s was not less than or equal to %s." % (alias, v)
if self.rules.key_gte_list != None: if self.rules.key_gte_list != None:
for kv in self.rules.key_gte_list: for kv in self.rules.key_gte_list:
k, v = kv.split(',') k, v = kv.split(',')
if (self.helper.gte(k, v) == False): key, alias = _getKeyAlias(k)
reason += " Value %s was not greater than or equal to value for key %s." % (v, k) if (self.helper.gte(key, v) == False):
reason += " Value for key %s was not greater than or equal to %s." % (v, alias)
is_alive = (reason == '') is_alive = (reason == '')
@ -168,8 +183,10 @@ class JsonRuleProcessor:
if len(vals) == 6: if len(vals) == 6:
key,uom,minimum,maximum,warn_range,crit_range = vals key,uom,minimum,maximum,warn_range,crit_range = vals
key, alias = _getKeyAlias(key)
if self.helper.exists(key): if self.helper.exists(key):
metrics += "'%s'=%s" % (key, self.helper.get(key)) metrics += "'%s'=%s" % (alias, self.helper.get(key))
if uom: metrics += uom if uom: metrics += uom
metrics += ";%s" % minimum metrics += ";%s" % minimum
metrics += ";%s" % maximum metrics += ";%s" % maximum
@ -194,17 +211,17 @@ def parseArgs():
parser.add_argument('-e', '--key_exists', dest='key_list', nargs='*', parser.add_argument('-e', '--key_exists', dest='key_list', nargs='*',
help='Checks existence of these keys to determine status.') help='Checks existence of these keys to determine status.')
parser.add_argument('-q', '--key_equals', dest='key_value_list', nargs='*', parser.add_argument('-q', '--key_equals', dest='key_value_list', nargs='*',
help='Checks equality of these keys and values (key,value key2,value2) to determine status.\ help='Checks equality of these keys and values (key[>alias],value key2,value2) to determine status.\
Multiple key values can be delimited with colon (key,value1:value2)') Multiple key values can be delimited with colon (key,value1:value2)')
parser.add_argument('-l', '--key_lte', dest='key_lte_list', nargs='*', parser.add_argument('-l', '--key_lte', dest='key_lte_list', nargs='*',
help='Checks that these keys and values (key,value key2,value2) are less than or equal to\ help='Checks that these keys and values (key[>alias],value key2,value2) are less than or equal to\
the returned json value to determine status.') the returned json value to determine status.')
parser.add_argument('-g', '--key_gte', dest='key_gte_list', nargs='*', parser.add_argument('-g', '--key_gte', dest='key_gte_list', nargs='*',
help='Checks that these keys and values (key,value key2,value2) are greater than or equal to\ help='Checks that these keys and values (key[>alias],value key2,value2) are greater than or equal to\
the returned json value to determine status.') the returned json value to determine status.')
parser.add_argument('-m', '--key_metric', dest='metric_list', nargs='*', parser.add_argument('-m', '--key_metric', dest='metric_list', nargs='*',
help='Gathers the values of these keys (key,UnitOfMeasure,Min,Max,WarnRange,CriticalRange) for Nagios performance data.\ help='Gathers the values of these keys (key[>alias],UnitOfMeasure,Min,Max,WarnRange,CriticalRange) for Nagios performance data.\
More information about Range format and units of measure for nagios can be found at https://nagios-plugins.org/doc/guidelines.html\ More information about Range format and units of measure for nagios can be found at nagios-plugins.org/doc/guidelines.html\
Additional formats for this parameter are: (key), (key,UnitOfMeasure), (key,UnitOfMeasure,Min,Max).') Additional formats for this parameter are: (key), (key,UnitOfMeasure), (key,UnitOfMeasure,Min,Max).')
parser.add_argument('-s', '--ssl', action='store_true', help='HTTPS mode.') parser.add_argument('-s', '--ssl', action='store_true', help='HTTPS mode.')
parser.add_argument('-t', '--timeout', type=int, help='Connection timeout (seconds)') parser.add_argument('-t', '--timeout', type=int, help='Connection timeout (seconds)')