static void get_paging_status (bool show_swapping, bool swapping_only, paging_data_t *paging) { struct proc_vmem *vmem = NULL; unsigned long nr_vmem_pgpgin[2], nr_vmem_pgpgout[2]; unsigned long nr_vmem_pgfault[2]; unsigned long nr_vmem_pgfree[2]; unsigned long nr_vmem_pgmajfault[2]; unsigned long nr_vmem_pgsteal[2]; unsigned long nr_vmem_pgscand[2]; unsigned long nr_vmem_pgscank[2]; unsigned long nr_vmem_dpswpin[2], nr_vmem_dpswpout[2]; unsigned int i, tog = 0, sleep_time = 1; int err; err = proc_vmem_new (&vmem); if (err < 0) plugin_error (STATE_UNKNOWN, err, "memory exhausted"); for (i = 0; i < 2; i++) { proc_vmem_read (vmem); nr_vmem_pgpgin[tog] = proc_vmem_get_pgpgin (vmem); nr_vmem_pgpgout[tog] = proc_vmem_get_pgpgout (vmem); nr_vmem_pgfault[tog] = proc_vmem_get_pgfault (vmem); nr_vmem_pgmajfault[tog] = proc_vmem_get_pgmajfault (vmem); nr_vmem_pgfree[tog] = proc_vmem_get_pgfree (vmem); nr_vmem_pgsteal[tog] = proc_vmem_get_pgsteal (vmem); nr_vmem_pgscand[tog] = proc_vmem_get_pgscand (vmem); nr_vmem_pgscank[tog] = proc_vmem_get_pgscank (vmem); nr_vmem_dpswpin[tog] = proc_vmem_get_pswpin (vmem); nr_vmem_dpswpout[tog] = proc_vmem_get_pswpout (vmem); sleep (sleep_time); tog = !tog; } paging->dpgpgin = nr_vmem_pgpgin[1] - nr_vmem_pgpgin[0]; paging->dpgpgout = nr_vmem_pgpgout[1] - nr_vmem_pgpgout[0]; paging->dpgfault = nr_vmem_pgfault[1] - nr_vmem_pgfault[0]; paging->dpgmajfault = nr_vmem_pgmajfault[1] - nr_vmem_pgmajfault[0]; paging->dpgfree = nr_vmem_pgfree[1] - nr_vmem_pgfree[0]; paging->dpgsteal = nr_vmem_pgsteal[1] - nr_vmem_pgsteal[0]; paging->dpgscand = nr_vmem_pgscand[1] - nr_vmem_pgscand[0]; paging->dpgscank = nr_vmem_pgscank[1] - nr_vmem_pgscank[0]; paging->dpswpin = nr_vmem_dpswpin[1] - nr_vmem_dpswpin[0]; paging->dpswpout = nr_vmem_dpswpout[1] - nr_vmem_dpswpout[0]; paging->summary = swapping_only ? (paging->dpswpin + paging->dpswpout) : paging->dpgmajfault; proc_vmem_unref (vmem); }
int main (int argc, char **argv) { bool show_swapping = false; int c, status, err; char *critical = NULL, *warning = NULL; char *status_msg; char *perfdata_paging_msg, *perfdata_swapping_msg = NULL; unsigned int i, tog = 0, sleep_time = 1; thresholds *my_threshold = NULL; struct proc_vmem *vmem = NULL; unsigned long dpgpgin, dpgpgout, dpgfault, dpgfree, dpgmajfault, dpgscand, dpgscank, dpgsteal; unsigned long nr_vmem_pgpgin[2], nr_vmem_pgpgout[2]; unsigned long nr_vmem_pgfault[2]; unsigned long nr_vmem_pgfree[2]; unsigned long nr_vmem_pgmajfault[2]; unsigned long nr_vmem_pgsteal[2]; unsigned long nr_vmem_pgscand[2]; unsigned long nr_vmem_pgscank[2]; unsigned long dpswpin, dpswpout; unsigned long nr_vmem_dpswpin[2], nr_vmem_dpswpout[2]; unsigned long *tracked_value = &dpgmajfault; set_program_name (argv[0]); while ((c = getopt_long (argc, argv, "psc:w:bkmg" GETOPT_HELP_VERSION_STRING, longopts, NULL)) != -1) { switch (c) { default: usage (stderr); case 'p': /* show_paging = true; */ break; case 's': show_swapping = true; break; case 'c': critical = optarg; break; case 'w': warning = optarg; break; case_GETOPT_HELP_CHAR case_GETOPT_VERSION_CHAR } } status = set_thresholds (&my_threshold, warning, critical); if (status == NP_RANGE_UNPARSEABLE) usage (stderr); err = proc_vmem_new (&vmem); if (err < 0) plugin_error (STATE_UNKNOWN, err, "memory exhausted"); for (i = 0; i < 2; i++) { proc_vmem_read (vmem); nr_vmem_pgpgin[tog] = proc_vmem_get_pgpgin (vmem); nr_vmem_pgpgout[tog] = proc_vmem_get_pgpgout (vmem); nr_vmem_pgfault[tog] = proc_vmem_get_pgfault (vmem); nr_vmem_pgmajfault[tog] = proc_vmem_get_pgmajfault (vmem); nr_vmem_pgfree[tog] = proc_vmem_get_pgfree (vmem); nr_vmem_pgsteal[tog] = proc_vmem_get_pgsteal (vmem); if (show_swapping) { nr_vmem_dpswpin[tog] = proc_vmem_get_pswpin (vmem); nr_vmem_dpswpout[tog] = proc_vmem_get_pswpout (vmem); } nr_vmem_pgscand[tog] = proc_vmem_get_pgscand (vmem); nr_vmem_pgscank[tog] = proc_vmem_get_pgscank (vmem); sleep (sleep_time); tog = !tog; } dpgpgin = nr_vmem_pgpgin[1] - nr_vmem_pgpgin[0]; dpgpgout = nr_vmem_pgpgout[1] - nr_vmem_pgpgout[0]; dpgfault = nr_vmem_pgfault[1] - nr_vmem_pgfault[0]; dpgmajfault = nr_vmem_pgmajfault[1] - nr_vmem_pgmajfault[0]; dpgfree = nr_vmem_pgfree[1] - nr_vmem_pgfree[0]; dpgsteal = nr_vmem_pgsteal[1] - nr_vmem_pgsteal[0]; dpgscand = nr_vmem_pgscand[1] - nr_vmem_pgscand[0]; dpgscank = nr_vmem_pgscank[1] - nr_vmem_pgscank[0]; if (show_swapping) { dpswpin = nr_vmem_dpswpin[1] - nr_vmem_dpswpin[0]; dpswpout = nr_vmem_dpswpout[1] - nr_vmem_dpswpout[0]; perfdata_swapping_msg = xasprintf (" vmem_pswpin/s=%lu vmem_pswpout/s=%lu", dpswpin, dpswpout); } proc_vmem_unref (vmem); status = get_status (*tracked_value, my_threshold); free (my_threshold); status_msg = xasprintf ("%s: %lu majfault/s", state_text (status), *tracked_value); perfdata_paging_msg = xasprintf ("vmem_pgpgin/s=%lu vmem_pgpgout/s=%lu vmem_pgfault/s=%lu " "vmem_pgmajfault/s=%lu vmem_pgfree/s=%lu vmem_pgsteal/s=%lu " "vmem_pgscand/s=%lu vmem_pgscank/s=%lu", dpgpgin, dpgpgout, dpgfault, dpgmajfault, dpgfree, dpgsteal, dpgscand, dpgscank); printf ("%s %s | %s%s\n", "PAGING", status_msg, perfdata_paging_msg, perfdata_swapping_msg ? perfdata_swapping_msg : ""); return status; }
int main (int argc, char **argv) { bool vmem_perfdata = false; int c, status, err; int shift = k_shift; char *critical = NULL, *warning = NULL; char *units = NULL; char *status_msg; char *perfdata_swap_msg, *perfdata_vmem_msg = NULL; float percent_used = 0; thresholds *my_threshold = NULL; struct proc_sysmem *sysmem = NULL; unsigned long kb_swap_cached; unsigned long kb_swap_free; unsigned long kb_swap_total; unsigned long kb_swap_used; struct proc_vmem *vmem = NULL; unsigned long dpswpin, dpswpout; unsigned long kb_swap_pageins[2]; unsigned long kb_swap_pageouts[2]; set_program_name (argv[0]); while ((c = getopt_long (argc, argv, "sc:w:bkmg" GETOPT_HELP_VERSION_STRING, longopts, NULL)) != -1) { switch (c) { default: usage (stderr); case 's': vmem_perfdata = true; break; case 'c': critical = optarg; break; case 'w': warning = optarg; break; case 'b': shift = b_shift; units = xstrdup ("B"); break; case 'k': shift = k_shift; units = xstrdup ("kB"); break; case 'm': shift = m_shift; units = xstrdup ("MB"); break; case 'g': shift = g_shift; units = xstrdup ("GB"); break; case_GETOPT_HELP_CHAR case_GETOPT_VERSION_CHAR } } status = set_thresholds (&my_threshold, warning, critical); if (status == NP_RANGE_UNPARSEABLE) usage (stderr); /* output in kilobytes by default */ if (units == NULL) units = xstrdup ("kB"); err = proc_sysmem_new (&sysmem); if (err < 0) plugin_error (STATE_UNKNOWN, err, "memory exhausted"); proc_sysmem_read (sysmem); kb_swap_cached = proc_sysmem_get_swap_cached (sysmem); kb_swap_free = proc_sysmem_get_swap_free (sysmem); kb_swap_total = proc_sysmem_get_swap_total (sysmem); kb_swap_used = proc_sysmem_get_swap_used (sysmem); if (vmem_perfdata) { err = proc_vmem_new (&vmem); if (err < 0) plugin_error (STATE_UNKNOWN, err, "memory exhausted"); proc_vmem_read (vmem); kb_swap_pageins[0] = proc_vmem_get_pswpin (vmem); kb_swap_pageouts[0] = proc_vmem_get_pswpout (vmem); sleep (1); proc_vmem_read (vmem); kb_swap_pageins[1] = proc_vmem_get_pswpin (vmem); kb_swap_pageouts[1] = proc_vmem_get_pswpout (vmem); dpswpin = kb_swap_pageins[1] - kb_swap_pageins[0]; dpswpout = kb_swap_pageouts[1] - kb_swap_pageouts[0]; perfdata_vmem_msg = xasprintf (" swap_pageins/s=%lu swap_pageouts/s=%lu", dpswpin, dpswpout); } if (kb_swap_total != 0) percent_used = (kb_swap_used * 100.0 / kb_swap_total); status = get_status (percent_used, my_threshold); free (my_threshold); status_msg = xasprintf ("%s: %.2f%% (%Lu %s) used", state_text (status), percent_used, SU (kb_swap_used)); perfdata_swap_msg = xasprintf ("swap_total=%Lu%s swap_used=%Lu%s swap_free=%Lu%s " /* The amount of swap, in kB, used as cache memory */ "swap_cached=%Lu%s", SU (kb_swap_total), SU (kb_swap_used), SU (kb_swap_free), SU (kb_swap_cached)); printf ("%s %s | %s%s\n", "SWAP", status_msg, perfdata_swap_msg, vmem_perfdata ? perfdata_vmem_msg : ""); proc_vmem_unref (vmem); proc_sysmem_unref (sysmem); return status; }
int main (int argc, char **argv) { bool vmem_perfdata = false; int c, status, err; int shift = k_shift; char *critical = NULL, *warning = NULL; char *units = NULL; char *status_msg, *perfdata_mem_msg, *perfdata_vmem_msg = "", *perfdata_memavailable_msg = ""; float mem_percent = 0; thresholds *my_threshold = NULL; struct proc_sysmem *sysmem = NULL; unsigned long kb_mem_main_available; unsigned long kb_mem_main_buffers; unsigned long kb_mem_main_cached; unsigned long kb_mem_main_free; unsigned long kb_mem_main_shared; unsigned long kb_mem_main_total; unsigned long kb_mem_main_used; unsigned long kb_mem_active; unsigned long kb_mem_anon_pages; unsigned long kb_mem_committed_as; unsigned long kb_mem_dirty; unsigned long kb_mem_inactive; struct proc_vmem *vmem = NULL; unsigned long dpgpgin, dpgpgout, dpgmajfault; unsigned long nr_vmem_pgpgin[2]; unsigned long nr_vmem_pgpgout[2]; unsigned long nr_vmem_pgmajfault[2]; /* by default we display the memory used */ unsigned long *kb_mem_monitored = &kb_mem_main_used; set_program_name (argv[0]); while ((c = getopt_long (argc, argv, "aMSCsc:w:bkmg" GETOPT_HELP_VERSION_STRING, longopts, NULL)) != -1) { switch (c) { default: usage (stderr); case 'a': kb_mem_monitored = &kb_mem_main_available; break; case 'C': /* does nothing, exists for compatibility */ break; case 's': vmem_perfdata = true; break; case 'c': critical = optarg; break; case 'w': warning = optarg; break; case 'b': shift = b_shift; units = xstrdup ("B"); break; case 'k': shift = k_shift; units = xstrdup ("kB"); break; case 'm': shift = m_shift; units = xstrdup ("MB"); break; case 'g': shift = g_shift; units = xstrdup ("GB"); break; case_GETOPT_HELP_CHAR case_GETOPT_VERSION_CHAR } } status = set_thresholds (&my_threshold, warning, critical); if (status == NP_RANGE_UNPARSEABLE) usage (stderr); /* output in kilobytes by default */ if (units == NULL) units = xstrdup ("kB"); err = proc_sysmem_new (&sysmem); if (err < 0) plugin_error (STATE_UNKNOWN, err, "memory exhausted"); proc_sysmem_read (sysmem); kb_mem_active = proc_sysmem_get_active (sysmem); kb_mem_anon_pages = proc_sysmem_get_anon_pages (sysmem); kb_mem_committed_as = proc_sysmem_get_committed_as (sysmem); kb_mem_dirty = proc_sysmem_get_dirty (sysmem); kb_mem_inactive = proc_sysmem_get_inactive (sysmem); kb_mem_main_available = proc_sysmem_get_main_available (sysmem); kb_mem_main_buffers = proc_sysmem_get_main_buffers (sysmem); kb_mem_main_cached = proc_sysmem_get_main_cached (sysmem); kb_mem_main_free = proc_sysmem_get_main_free (sysmem); kb_mem_main_shared = proc_sysmem_get_main_shared (sysmem); kb_mem_main_total = proc_sysmem_get_main_total (sysmem); kb_mem_main_used = proc_sysmem_get_main_used (sysmem); if (vmem_perfdata) { err = proc_vmem_new (&vmem); if (err < 0) plugin_error (STATE_UNKNOWN, err, "memory exhausted"); proc_vmem_read (vmem); nr_vmem_pgpgin[0] = proc_vmem_get_pgpgin (vmem); nr_vmem_pgpgout[0] = proc_vmem_get_pgpgout (vmem); nr_vmem_pgmajfault[0] = proc_vmem_get_pgmajfault (vmem); sleep (1); proc_vmem_read (vmem); nr_vmem_pgpgin[1] = proc_vmem_get_pgpgin (vmem); nr_vmem_pgpgout[1] = proc_vmem_get_pgpgout (vmem); nr_vmem_pgmajfault[1] = proc_vmem_get_pgmajfault (vmem); dpgpgin = nr_vmem_pgpgin[1] - nr_vmem_pgpgin[0]; dpgpgout = nr_vmem_pgpgout[1] - nr_vmem_pgpgout[0]; dpgmajfault = nr_vmem_pgmajfault[1] - nr_vmem_pgmajfault[0]; perfdata_vmem_msg = xasprintf (", vmem_pageins/s=%lu, vmem_pageouts/s=%lu, " "vmem_pgmajfault/s=%lu", dpgpgin, dpgpgout, dpgmajfault); } /* Note: we should perhaps implement the following tests instead: * 1. The Main Memory Test * (MemFree + Buffers + Cached) / MemTotal < threshold * 2. The Page Fault Test * Major pagefaults > threshold * * See. http://doc.qt.digia.com/qtextended4.4/syscust-oom.html */ if (kb_mem_main_total != 0) mem_percent = ((*kb_mem_monitored) * 100.0 / kb_mem_main_total); status = get_status (mem_percent, my_threshold); perfdata_memavailable_msg = xasprintf ("mem_available=%Lu%s ", SU (kb_mem_main_available)); status_msg = xasprintf ("%s: %.2f%% (%Lu %s) %s", state_text (status), mem_percent, SU (*kb_mem_monitored), (kb_mem_monitored == &kb_mem_main_available) ? "available" : "used"); free (my_threshold); perfdata_mem_msg = xasprintf ("mem_total=%Lu%s mem_used=%Lu%s mem_free=%Lu%s " "mem_shared=%Lu%s mem_buffers=%Lu%s mem_cached=%Lu%s %s" "mem_active=%Lu%s mem_anonpages=%Lu%s mem_committed=%Lu%s " "mem_dirty=%Lu%s mem_inactive=%Lu%s" , SU (kb_mem_main_total) , SU (kb_mem_main_used) , SU (kb_mem_main_free) , SU (kb_mem_main_shared) , SU (kb_mem_main_buffers) , SU (kb_mem_main_cached) , perfdata_memavailable_msg , SU (kb_mem_active) , SU (kb_mem_anon_pages) , SU (kb_mem_committed_as) , SU (kb_mem_dirty) , SU (kb_mem_inactive)); printf ("%s %s | %s%s\n", program_name_short, status_msg, perfdata_mem_msg, perfdata_vmem_msg); proc_sysmem_unref (sysmem); proc_vmem_unref (vmem); return status; }