END_TEST // mp_human_size START_TEST (test_human_size) { char *ret = NULL; ret = mp_human_size(1); fail_unless (strcmp(ret, "1.00 ") == 0, "mp_human_size failed: 1 => %s", ret); free(ret); ret = mp_human_size(2000); fail_unless (strcmp(ret, "1.95 KiB") == 0, "mp_human_size failed: 2000 => %s", ret); free(ret); ret = mp_human_size(2000000); fail_unless (strcmp(ret, "1.91 MiB") == 0, "mp_human_size failed: 2000000 => %s", ret); free(ret); ret = mp_human_size(2000000000); fail_unless (strcmp(ret, "1.86 GiB") == 0, "mp_human_size failed: 2000000000 => %s", ret); free(ret); ret = mp_human_size(2000000000000); fail_unless (strcmp(ret, "1.82 TiB") == 0, "mp_human_size failed: 2000000000000 => %s", ret); free(ret); }
int main (int argc, char **argv) { /* Local Vars */ FILE *meminfo; char line[64]; char *line_ptr; char *key; float mem_total = 0, mem_free = 0; float slab = 0; float swap_cached = 0, swap_total = 0, swap_free = 0; float page_tables = 0; float buffers = 0; float cached = 0; float apps, swap, used; float usedp; /* Set signal handling and alarm */ if (signal(SIGALRM, timeout_alarm_handler) == SIG_ERR) critical("Setup SIGALRM trap failed!"); /* Process check arguments */ if (process_arguments(argc, argv) != OK) unknown("Parsing arguments failed!"); /* Start plugin timeout */ alarm(mp_timeout); // Read /proc/meminfo meminfo = fopen("/proc/meminfo", "r"); if (meminfo == NULL) unknown("Can't read /proc/meminfo"); while (fgets(line, 64, meminfo) != NULL) { line_ptr = line; key = strsep(&line_ptr, " "); if (strcmp("MemTotal:", key) == 0) { mem_total = readValue(line_ptr); } else if (strcmp("MemFree:", key) == 0) { mem_free = readValue(line_ptr); } else if (strcmp("Slab:", key) == 0) { slab = readValue(line_ptr); } else if (strcmp("SwapCached:", key) == 0) { swap_cached = readValue(line_ptr); } else if (strcmp("SwapTotal:", key) == 0) { swap_total = readValue(line_ptr); } else if (strcmp("SwapFree:", key) == 0) { swap_free= readValue(line_ptr); } else if (strcmp("PageTables:", key) == 0) { page_tables = readValue(line_ptr); } else if (strcmp("Buffers:", key) == 0) { buffers = readValue(line_ptr); } else if (strcmp("Cached:", key) == 0) { cached = readValue(line_ptr); } } // Calculations apps = mem_total - mem_free - buffers - cached - slab - page_tables - swap_cached; swap = swap_total - swap_free; used = mem_total - mem_free - buffers - cached; usedp = (float)(used*100)/(float)mem_total; mp_perfdata_float("memtotal", mem_total, "", NULL); mp_perfdata_float2("slab", slab, "", NULL, 1, 0, 1, mem_total); mp_perfdata_float2("swapcached", swap_cached, "", NULL, 1, 0, 1, mem_total); mp_perfdata_float2("pagetables", page_tables, "", NULL, 1, 0, 1, mem_total); mp_perfdata_float2("apps", apps, "", NULL, 1, 0, 1, mem_total); mp_perfdata_float2("memfree", mem_free, "", NULL, 1, 0, 1, mem_total); mp_perfdata_float2("buffers", buffers, "", NULL, 1, 0, 1, mem_total); mp_perfdata_float2("cached", cached, "", NULL, 1, 0, 1, mem_total); mp_perfdata_float2("swap", swap, "", NULL, 1, 0, 1, mem_total); switch (get_status(usedp, usage_thresholds)) { case STATE_OK: ok("Memory - %2.2f%% (%s of %s) used", usedp, mp_human_size(used), mp_human_size(mem_total)); break; case STATE_WARNING: warning("Memory - %2.2f%% (%s of %s) used", usedp, mp_human_size(used), mp_human_size(mem_total)); break; case STATE_CRITICAL: critical("Memory - %2.2f%% (%s of %s) used", usedp, mp_human_size(used), mp_human_size(mem_total)); break; } unknown("Memory - %2.2f%% (%s of %s) used", usedp, mp_human_size(used), mp_human_size(mem_total)); }