Exemple #1
0
def check_memory_element(
    label,
    used,
    total,
    levels,
    label_total="",
    show_free=False,
    metric_name=None,
    create_percent_metric=False,
):
    """Return a check result for one memory element"""
    if show_free:
        show_value = total - used
        show_text = " free"
    else:
        show_value = used
        show_text = ""

    infotext = "%s: %s%s - %s of %s%s" % (
        label,
        get_percent_human_readable(100.0 * show_value / total),
        show_text,
        get_bytes_human_readable(show_value, base=1024),
        get_bytes_human_readable(total, base=1024),
        (" %s" % label_total).rstrip(),
    )

    try:
        mode, (warn, crit) = levels
    except (ValueError, TypeError):  # handle None, "ignore"
        mode, (warn, crit) = "ignore", (None, None)

    warn, crit, levels_text = normalize_mem_levels(mode, warn, crit, total)
    state = _compute_state(used, warn, crit)
    if state and levels_text:
        infotext = "%s (%s)" % (infotext, levels_text)

    perf = []
    if metric_name:
        perf.append((metric_name, used, warn, crit, 0, total))
    if create_percent_metric:
        scale_to_perc = 100.0 / total
        perf.append((
            "mem_used_percent",
            used * scale_to_perc,
            warn * scale_to_perc if warn is not None else None,
            crit * scale_to_perc if crit is not None else None,
            0,
            None,  # some times over 100%!
        ))

    return state, infotext, perf
Exemple #2
0
def check_memory(params, meminfo):
    if isinstance(params, tuple):
        params = {"levels": params}

    memtotal = MemBytes(meminfo['MemTotal'])
    memused = MemBytes(memtotal.kb - meminfo['MemFree'])

    if "SwapFree" in meminfo:
        swaptotal = MemBytes(meminfo['SwapTotal'])
        swapused = MemBytes(swaptotal.kb - meminfo['SwapFree'])
        perfdata = [('swap_used', swapused.bytes, None, None, 0,
                     swaptotal.bytes)]
    else:
        swaptotal = None
        swapused = None
        perfdata = []

    # Size of Pagetable on Linux can be relevant e.g. on ORACLE
    # servers with much memory, that do not use HugeTables. We account
    # that for used
    if 'PageTables' in meminfo:
        pagetables = MemBytes(meminfo['PageTables'])
        perfdata.append(('mem_lnx_page_tables', pagetables.bytes))
    else:
        pagetables = None

    # Buffers and Cached are optional. On Linux both mean basically the same.
    caches = MemBytes(meminfo.get('Buffers', 0) + meminfo.get('Cached', 0))

    ramused = MemBytes(memused.kb - caches.kb)
    perfdata.append(('mem_used', ramused.bytes, None, None, 0, memtotal.bytes))
    perfdata.append(('mem_used_percent', 100. * ramused.bytes / memtotal.bytes,
                     None, None, 0, 100.))

    totalused, totalused_descr = _get_total_usage(ramused, swapused,
                                                  pagetables)

    infotext = check_memory_element(
        totalused_descr,
        totalused.bytes,
        memtotal.bytes,
        None,
        label_total="RAM" if totalused_descr != "RAM" else "",
    )[1]

    # Take into account averaging
    average_min = params.get("average")
    if average_min:
        totalused_mb_avg = get_average("mem.used.total",
                                       time.time(),
                                       totalused.mb,
                                       average_min,
                                       initialize_zero=False)
        totalused_perc_avg = totalused_mb_avg / memtotal.mb * 100
        infotext += ", %d min average %.1f%%" % (average_min,
                                                 totalused_perc_avg)
        perfdata.append(('memusedavg', totalused_mb_avg))
        comp_mb = totalused_mb_avg
    else:
        comp_mb = totalused.mb

    # Normalize levels and check them
    totalvirt = MemBytes((swaptotal.kb if swaptotal is not None else 0) +
                         memtotal.kb)
    warn, crit = params.get("levels", (None, None))
    mode = get_levels_mode_from_value(warn)
    warn_mb, crit_mb, levels_text = normalize_mem_levels(
        mode,
        abs(warn),
        abs(crit),
        totalvirt.mb,
        _perc_total=memtotal.mb,
        render_unit=1024**2,
    )
    perfdata.append(('mem_lnx_total_used', totalused.bytes, warn_mb * 1024**2,
                     crit_mb * 1024**2, 0, totalvirt.bytes))

    # Check levels
    state = _compute_state(comp_mb, warn_mb, crit_mb)
    if state and levels_text:
        infotext = "%s (%s)" % (infotext, levels_text)

    yield state, infotext, perfdata

    if totalused_descr != "RAM":
        yield check_memory_element(
            "RAM",
            ramused.bytes,  # <- caches subtracted
            memtotal.bytes,
            None,
        )
        if swaptotal and swaptotal.bytes:
            yield check_memory_element(
                "Swap",
                swapused.bytes,
                swaptotal.bytes,
                None,
            )
        if pagetables:
            yield 0, "Pagetables: %s" % pagetables.render(), []

    # Add additional metrics, provided by Linux.
    if meminfo.get('Mapped'):
        for key, label, metric in (
            ('Mapped', 'Mapped', 'mem_lnx_mapped'),
            ('Committed_AS', 'Committed', 'mem_lnx_committed_as'),
            ('Shmem', 'Shared', 'mem_lnx_shmem'),
        ):
            value = MemBytes(meminfo.get(key, 0))
            yield 0, "%s: %s" % (label, value.render()), [(metric, value.bytes)
                                                          ]