Exemple #1
0
def snmp_oid_service_avg(ctxt):
    """
    REtrieves service infrastructure from ctxt

    data = {
       "snmp_read_comunity": "....",
       "snmp_variable": "...."
    }
    """
    import sys
#    logging.basicConfig(level=logging.DEBUG, stream=sys.stdout)
    try:
        data = _load_data(ctxt["service"].data)

        monitoredservice = ctxt["service"].infrastructure.name

        machines = get_vms_by_service(monitoredservice)
#        print("Returned machines: %s" % machines)
        values = []

        for machine in machines:
            logging.debug("[metric: snmp_oid_service_avg] quering variable %s in machine: %s " % (data["snmp_variable"], machine.name))
            try:
                name = machine.name
                community = data.get("snmp_read_comunity", "public")
                variable = data["snmp_variable"]
                logging.debug("Machine: %s, Community: %s, variable: %s" % (name, community, variable))      
                ret = get_variable(name, 514, community, ".1.3.6.1.4.1.2021.11.9.0")
                logging.debug("[metric: snmp_oid_service_avg] retrieved: %s " % ret)
                values.append(ret)
            except Exception as err:
                logging.error("[metric: snmp_oid_service_avg] reading snmp variable from machine %s got error: %s" % (machine.name, err))

        if not len(values):
            logging.debug("[metric: snmp_oid_service_avg] no values, returning 0")
            return 0

        tot = 0
        for val in values:             
            tot += float(val)
        tot /= len(values)

        logging.debug("[metric: snmp_oid_service_avg] snmp query returned: %s" % tot)
        logging.debug("[metric: snmp_oid_service_avg] metric ended")
        return tot
    except Exception as er:
        logging.error("[metric: snmp_oid_service_avg] Exception: %s" % er)
        return 0
Exemple #2
0
def snmp_oid(ctxt):
    """
    ctxt["service"].data["host"]                  #default: localhost
    ctxt["service"].data["port"]                  #default: 514
    ctxt["service"].data["snmp_read_comunity"]    #default: public
    ctxt["service"].data["snmp_variable"]
    """
    logging.debug("[metric: snmp_oid] Entering  ...")   
    data = _load_data(ctxt["service"].data)
    try:
        ret = get_variable(
            data.get("host", "localhost"), 
            data.get("port", 514),
            data.get("snmp_read_comunity", "public"),
            data["snmp_variable"])
        logging.debug("[metric: snmp_oid] snmp query for machine: %s returned: %s" % (date.get("host", "localhost"), ret))
        logging.debug("[metric: snmp_oid] metric ended")
        return ret
    except Exception as er:
        logging.debug("[metric: snmp_oid] snmp query ERROR: %s" % er)
        return None