在 nagiosplugin 中返回除了指标之外的信息

Returning information in addition to metric in nagiosplugin

我有一个 nagiosplugin 检查,它使用 salt(由于网络架构)获取包的版本,然后使用 salt.pkg_version_cmp 方法将它与参数中给定的版本进行比较。

我能够 return 在指标中显示此检查 (-1|0|1) 的结果并显示状态栏。

但我想在状态行中添加包的实际版本,我不知道如何 return 使用 nagiosplugin class| 函数,而不是使用全局变量

这是我的 nagiosplugin.Resource class:

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.Summary(基本)

class VersionSummary(nagiosplugin.Summary):
def ok(self, results):
    return ("ok")

def problem(self, results):
    return("warning")

我想要这样的摘要(忽略关于 Unknown/warning/critical 的条件检查):

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 个检查(第一个在主要部分,然后第二个在摘要中以获取信息)

度量标准 class (https://pythonhosted.org/nagiosplugin/api/intermediate.html#module-nagiosplugin.metric) 似乎不能携带值以外的东西。

除了编写新的 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))

它采用return第一个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()

规则是从main发送到summary,结果是version。上下文评估它是否为 critical/warning/ok 并将结果发送到摘要。