check_mk通用应用检测插件

客户端mk_tvmapp

import json

filename = '/tmp/tvmapp.json'

print '<<<tvmapp>>>'
for a in json.load(open(filename)):
    print 'item_name %s' % a['item_name']
    print 'timestamp %s' % a['timestamp']
    print 'check_type %s' % a['check_type']

    if a['check_type'] == '0':
        print 'status %s' % a['status'] 
        print 'message %s' % a['message']
    else:
        print 'unit %s' % a['unit']
        print 'current_value %s' % a['current_value']
        print 'threhold_value %s' % a['threhold_value']

#output
<<<tvmapp>>>
item_name mtq_unique_visitor
timestamp 1499320801
check_type 0
status 1
message this is a status message - current status is 1
item_name mtq_page_view
timestamp 1499320801
check_type 1
unit %
current_value 92
threhold_value >90|<60

/tmp/tvmapp.json

[
    {
        "check_type": "0", 
        "item_name": "mtq_unique_visitor", 
        "message": "this is a status message - current status is 1", 
        "status": "1", 
        "timestamp": "1499320801"
    }, 
    {
        "check_type": "1", 
        "item_name": "mtq_page_view", 
        "current_value": "92", 
        "threhold_value": ">90|<60", 
        "unit": "%", 
        "timestamp": "1499320801"
    }
]

服务端tvmapp

import time, re

def inventory_app(info):
    inventory = []
    for line in info:
        if line[0] == 'item_name':
            inventory.append((' '.join(line[1:]), None))
    return inventory

def parse_app(info):
    '''
    data:
    {
        'mtq_unique_visitor': { 'timestamp': '', 'check_type': '', 'status': '', 'message': '' },
        'mtq_page_view': { 'timestamp': '', 'check_type': '', 'unit': '', 'current_value': '', 'threhold_value': ''}
    }
    '''
    data = {}
    tmp = ''

    for line in info:
        if line[0] == 'item_name':
            tmp = ' '.join(line[1:])
            data[tmp] = {}
        else:
            data[tmp][line[0]] = ' '.join(line[1:])

    return data

def check_app(item, params, info):
    data = parse_app(info)

    if data[item] is None: 
        return 2, 'The data is not exist.', [(item, 1)]
    elif int(time.time()) - int(data[item]['timestamp']) >= 300:
        return 2, 'The data is not update.', [(item, 1)]
    elif data[item]['check_type'] == '0':
        if data[item]['status'] == '0':
            return 0, data[item]['message'], [(item, 0)]
        else:
            return 2, data[item]['message'], [(item, 1)]
    elif data[item]['check_type'] == '1': 
        exp = re.sub('([!<>]=?|=)', 
            lambda x: data[item]['current_value'] + '=' + x.group(0) if x.group(0) == '=' else data[item]['current_value'] + x.group(0), data[item]['threhold_value']) 
            .replace('|', ' or ').replace('&', ' and ')
        if eval(exp):
            return 0, 'threhold is %s, current value is %s%s' % (data[item]['threhold_value'], data[item]['current_value'], data[item]['unit']), [(item, int(data[item]['current_value']))]
        else:
            return 2, 'threhold is %s, current value is %s%s' % (data[item]['threhold_value'], data[item]['current_value'], data[item]['unit']), [(item, int(data[item]['current_value']))]

check_info['tvmapp'] = {
    'inventory_function'  : inventory_app,
    'check_function'      : check_app,
    'has_perfdata'        : True,
    'service_description' : 'tvmapp %s',
}
原文地址:https://www.cnblogs.com/liujitao79/p/7204630.html