我有一个nagiosplugin检查,使用salt(由于网络体系结构)获取软件包的版本,然后使用salt.pkg_version_cmp方法将其与参数中给出的版本进行比较。
我能够以度量标准返回此检查的结果(-1 | 0 | 1)并显示状态行。
但是我想在状态行中添加该软件包的实际版本,我不知道如何使用nagiosplugin class | function而不是使用全局变量来返回它。
这是我的nagiosplugin.Resource类:
class CheckSaltPkgVersion(nagiosplugin.Resource):
def __init__(self, args):
self.package = args.package
self.rule = args.rule
self.target = args.vm
def salt_pkg_version(self):
sa = getSaltAdapter()
version = sa.exec_function([{
'client': 'local',
'tgt': self.target,
'fun': 'pkg.version',
'arg': [self.package]
}])
compare = sa.exec_function([{
'client': 'local',
'tgt': self.target,
'fun': 'pkg.version_cmp',
'arg': [version['return'][0][self.target], self.rule]
}])
# depending of compare and version, mist return smth between 0 and 4
return compare['return'][0][self.target]
def probe(self):
"""Runs"""
yield nagiosplugin.Metric(self.package,
self.salt_pkg_version(),
context='salt_pkg_version_compare')
这是我的nagiosplugin。摘要(基本)
class VersionSummary(nagiosplugin.Summary):
def ok(self, results):
return ("ok")
def problem(self, results):
return("warning")
我想要一个这样的摘要(忽略关于未知/警告/严重的条件检查):
class VersionSummary(nagiosplugin.Summary):
def ok(self, results):
return 'the package %s is in version %s' % (results.first_significant.metric.name, version)
def problem(self, results):
return 'the package %s is in version %s which is less that %s' % (results.first_significant.metric.name, version, rule)
我进行了搜索,但发现的唯一内容是:nagiosplugin: how to show different fmt_metric based on the value?,他正在进行2次检查(主要是第一个检查,然后是摘要中的第二个检查以获取信息)
Metric类(https://pythonhosted.org/nagiosplugin/api/intermediate.html#module-nagiosplugin.metric)似乎不能携带值以外的其他东西。
答案 0 :(得分:0)
除了写一个新的Context,我找不到其他解决方案:
class CheckContext(nagiosplugin.Context):
def __init__(self, name, rule, critical, fmt_metric=None, result_cls=nagiosplugin.Result):
super(CheckContext, self).__init__(name, fmt_metric, result_cls)
self.rule = rule
self.critical = critical
def performance(self, metric, ressource):
return None
def evaluate(self, metric, resource):
need_update = self.compare(metric.value)
if (need_update):
if (self.critical == 0):
return self.result_cls(nagiosplugin.Warn, None, metric)
else:
return self.result_cls(nagiosplugin.Critical, None, metric)
else:
return self.result_cls(nagiosplugin.Ok, None, metric)
def compare(self,metric):
metric = re.sub("[^0-9]", '.', metric).split('.')
rule = re.sub("[^0-9]", '.', self.rule).split('.')
i = 0
while (i < len(rule) and int(rule[i]) <= int(metric[i])):
i+=1
if (len(metric) <= i):
metric.append(0)
return (i < len(rule))
采用返回第一个salt函数的版本。现在,在“比较”中进行两个字符串之间的比较。
这是摘要和主要内容
class VersionSummary(nagiosplugin.Summary):
def __init__(self, rule):
self.rule = rule;
def ok(self, results):
return ("Package = %s, version = %s, rule = %s" % (results.__getitem__(0).metric.name, results.__getitem__(0).metric.value, self.rule))
def problem(self, results):
return("Package %s must be in version %s but is in version %s" % (results.__getitem__(0).metric.name, self.rule, results.__getitem__(0).metric.value))
@nagiosplugin.guarded
def main():
args = parse_args()
check = nagiosplugin.Check(CheckSaltPkgVersion(args.package, args.vm),
CheckContext('salt_pkg_version_compare',
args.rule,
args.critical),
VersionSummary(args.rule))
check.main()
规则从主要发送到摘要,结果在版本中发送。上下文评估它是否为关键/警告/确定,并将结果发送到摘要。