예제 #1
0
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;
}
예제 #2
0
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;
}