Exemplo n.º 1
0
def update_def():
    # Update processes states
    file_path = os.path.join(DATA_DIR, 'ggprocess')

    if not os.path.exists(file_path):
        snmp.add_int('0.0', 1)
        snmp.add_str('0.1', "Error: missing `ggprocess' file.")
    else:
        entries = [x.strip() for x in open(file_path, 'r').readlines()]
        entries_set = set(entries)

        if len(entries_set) > 1 or 'RUNNING' not in entries_set:
            snmp.add_int('0.0', 1)
        else:
            snmp.add_int('0.0', 0)

        snmp.add_str('0.1', ', '.join(entries))

    # Update lags states
    file_path = os.path.join(DATA_DIR, 'gglag')

    if not os.path.exists(file_path):
        snmp.add_int('1.0', 1)
        snmp.add_str('1.1', "Error: missing `gglag' file.")
    else:
        entries = [x.strip() for x in open(file_path, 'r').readlines()
            if x.strip().isdigit()]

        if len([x for x in entries if int(x) >= 30]) > 0:
            snmp.add_int('1.0', 1)
        else:
            snmp.add_int('1.0', 0)

        snmp.add_str('1.1', ', '.join(entries))
Exemplo n.º 2
0
def poll():
    instances = get_instances()

    snmp.add_int("1", len(instances))

    for index, instance in enumerate(instances):
        # Define base instance information
        snmp.add_int("2.%d" % index, instance[0])
        snmp.add_str("3.%d" % index, instance[1])

        # Request statistics data
        request = urllib2.urlopen(
            "http://localhost:8080/%03d-%s/api/check/stats.php?instance=%s" % (instance + ("search",))
        )

        total = 0.0
        data = json.load(request)

        for entry in data[0]["stats"]["load"]["request_per_10_sec"]:
            total += float(entry["nb_requests_per_sec"])

        snmp.add_str("4.%d" % index, total)
Exemplo n.º 3
0
def poll():
    base_id = extract_id(opt_base)

    instances = get_instances()

    # Define instances count OID (.1) with value
    oid_chunks = [base_id, 1]
    snmp.add_int('.'.join([str(x) for x in oid_chunks]), len(instances))

    for instance_id, instance_name in enumerate(instances):
        base_dir = os.path.join(DATA_DIR, instance_name, opt_base)

        if not os.path.exists(base_dir):
            current_time = time.strftime('%d/%m/%Y %H:%M:%S', time.localtime())
            log.write("[%s] Warning: can't find `%s' monitoring data directory\n" % (current_time, instance_name))
            continue

        # Define instance entry (.2.1.1."instance")
        oid_chunks = [base_id, 2, 1, 1, len(instance_name)] + [ord(x) for x in instance_name]
        snmp.add_str('.'.join([str(x) for x in oid_chunks]), instance_name)

        if not os.path.exists(base_dir):
            current_time = time.strftime('%d/%m/%Y %H:%M:%S', time.localtime())
            log.write("[%s] Warning: can't find `%s'\n" % (current_time, base_dir))

        for check_name in os.listdir(base_dir):
            check_id = extract_id(check_name)
            out_of_date = None

            custom_freshness = opt_freshness
            try: 
                if CHECK_DATA[opt_base][check_name]['freshness']:
                    custom_freshness = CHECK_DATA[opt_base][check_name]['freshness']
            except:
                pass

            for entry_name in os.listdir(os.path.join(base_dir, check_name)):
                entry_id = extract_id(entry_name)

                # Get check data
                try:
                    data = CHECK_DATA[opt_base][check_name][entry_name]
                except:
                    current_time = time.strftime('%d/%m/%Y %H:%M:%S', time.localtime())
                    log.write("[%s] Warning: can't find `%s:%s:%s' definition\n" % (current_time, opt_base, check_name, entry_name))
                    continue

                file_path = os.path.join(base_dir, check_name, entry_name)

                # Check for file freshness
                if custom_freshness is not None and time.time() - os.path.getmtime(file_path) > custom_freshness:
                    out_of_date = file_path

                if out_of_date is not None:
                    if entry_name.endswith('.status'):
                        oid_chunks = [base_id, 2, 1, check_id + (entry_id + 1) * 100, len(instance_name)] + \
                            [ord(x) for x in instance_name]

                        getattr(snmp, 'add_int')('.'.join([str(x) for x in oid_chunks + [1]]), 1)

                        getattr(snmp, 'add_str')('.'.join([str(x) for x in oid_chunks + [2]]),
                            "`%s' file is out of date (%d sec) %s - %s" % (out_of_date[len(DATA_DIR)+1:], custom_freshness, opt_base, check_name))

                    continue

                fd = codecs.open(file_path, 'r', 'utf-8')

                count = 0

                for line in fd:
                    line = line.strip().encode('ascii', 'replace')

                    if line.startswith('#'):
                        continue

                    count += 1

                    for chunk_id, chunk in enumerate(line.split(None, len(data[1]) - 1)):
                        oid_chunks = [base_id, 2, 1, check_id + (entry_id + 1) * 100, len(instance_name)] + \
                            [ord(x) for x in instance_name] + [chunk_id + 1]

                        if data[0]:
                            oid_chunks.append(count)

                        # Define check entry according to its type (stored in data[1])
                        getattr(snmp, 'add_' + data[1][chunk_id])('.'.join([str(x) for x in oid_chunks]), chunk)

                    # Stop if non-table
                    if not data[0]:
                        break

                # Define check count entry
                if data[0]:
                    oid_chunks = [base_id, 2, 1, check_id + (entry_id + 1) * 100, len(instance_name)] + \
                        [ord(x) for x in instance_name] + [0]

                    getattr(snmp, 'add_int')('.'.join([str(x) for x in oid_chunks]), count)

                fd.close()