Back to index

nagios-plugins  1.4.16
Defines | Enumerations | Functions | Variables
check_procs.c File Reference
#include "common.h"
#include "utils.h"
#include "utils_cmd.h"
#include "regex.h"
#include <pwd.h>

Go to the source code of this file.

Defines

#define ALL   1
#define STAT   2
#define PPID   4
#define USER   8
#define PROG   16
#define ARGS   32
#define VSZ   64
#define RSS   128
#define PCPU   256
#define ELAPSED   512
#define EREG_ARGS   1024

Enumerations

enum  metric {
  METRIC_PROCS, METRIC_VSZ, METRIC_RSS, METRIC_CPU,
  METRIC_ELAPSED
}

Functions

int process_arguments (int, char **)
int validate_arguments (void)
int check_thresholds (int)
int convert_to_seconds (char *)
void print_help (void)
void print_usage (void)
enum metric int verbose int
uid pid_t ppid int vsz int rss
float pcpu char *statopts char
*prog char *args char
*input_filename regex_t
re_args char *fmt char *fails
char tmp[MAX_INPUT_BUFFER]
FILE *ps_input int 
main (int argc, char **argv)

Variables

const char * progname = "check_procs"
const char * program_name = "check_procs"
const char * copyright = "2000-2008"
const char * email = "nagiosplug-devel@lists.sourceforge.net"
int wmax = -1
int cmax = -1
int wmin = -1
int cmin = -1
int options = 0
char * metric_name

Define Documentation

#define ALL   1

Definition at line 59 of file check_procs.c.

#define ARGS   32

Definition at line 64 of file check_procs.c.

#define ELAPSED   512

Definition at line 68 of file check_procs.c.

#define EREG_ARGS   1024

Definition at line 69 of file check_procs.c.

#define PCPU   256

Definition at line 67 of file check_procs.c.

#define PPID   4

Definition at line 61 of file check_procs.c.

#define PROG   16

Definition at line 63 of file check_procs.c.

#define RSS   128

Definition at line 66 of file check_procs.c.

#define STAT   2

Definition at line 60 of file check_procs.c.

#define USER   8

Definition at line 62 of file check_procs.c.

#define VSZ   64

Definition at line 65 of file check_procs.c.


Enumeration Type Documentation

enum metric
Enumerator:
METRIC_PROCS 
METRIC_VSZ 
METRIC_RSS 
METRIC_CPU 
METRIC_ELAPSED 

Definition at line 72 of file check_procs.c.


Function Documentation

int check_thresholds ( int  value)

Definition at line 585 of file check_procs.c.

{
       if (wmax == -1 && cmax == -1 && wmin == -1 && cmin == -1) {
              return OK;
       }
       else if (cmax >= 0 && cmin >= 0 && cmax < cmin) {
              if (value > cmax && value < cmin)
                     return STATE_CRITICAL;
       }
       else if (cmax >= 0 && value > cmax) {
              return STATE_CRITICAL;
       }
       else if (cmin >= 0 && value < cmin) {
              return STATE_CRITICAL;
       }

       if (wmax >= 0 && wmin >= 0 && wmax < wmin) {
              if (value > wmax && value < wmin) {
                     return STATE_WARNING;
              }
       }
       else if (wmax >= 0 && value > wmax) {
              return STATE_WARNING;
       }
       else if (wmin >= 0 && value < wmin) {
              return STATE_WARNING;
       }
       return STATE_OK;
}

Here is the caller graph for this function:

int convert_to_seconds ( char *  etime)

Definition at line 618 of file check_procs.c.

                                {

       char *ptr;
       int total;

       int hyphcnt;
       int coloncnt;
       int days;
       int hours;
       int minutes;
       int seconds;

       hyphcnt = 0;
       coloncnt = 0;
       days = 0;
       hours = 0;
       minutes = 0;
       seconds = 0;

       for (ptr = etime; *ptr != '\0'; ptr++) {
       
              if (*ptr == '-') {
                     hyphcnt++;
                     continue;
              }
              if (*ptr == ':') {
                     coloncnt++;
                     continue;
              }
       }

       if (hyphcnt > 0) {
              sscanf(etime, "%d-%d:%d:%d",
                            &days, &hours, &minutes, &seconds);
              /* linux 2.6.5/2.6.6 reporting some processes with infinite
               * elapsed times for some reason */
              if (days == 49710) {
                     return 0;
              }
       } else {
              if (coloncnt == 2) {
                     sscanf(etime, "%d:%d:%d",
                            &hours, &minutes, &seconds);
              } else if (coloncnt == 1) {
                     sscanf(etime, "%d:%d",
                            &minutes, &seconds);
              }
       }

       total = (days * 86400) +
              (hours * 3600) +
              (minutes * 60) +
              seconds;

       if (verbose >= 3 && metric == METRIC_ELAPSED) {
                     printf("seconds: %d\n", total);
       }
       return total;
}

Here is the caller graph for this function:

enum metric int verbose int uid pid_t ppid int vsz int rss float pcpu char* statopts char* prog char* args char* input_filename regex_t re_args char* fmt char* fails char tmp [MAX_INPUT_BUFFER] FILE* ps_input int main ( int  argc,
char **  argv 
) [abstract]

Definition at line 100 of file check_procs.c.

{
       char *input_buffer;
       char *input_line;
       char *procprog;

       pid_t mypid = 0;
       int procuid = 0;
       pid_t procpid = 0;
       pid_t procppid = 0;
       int procvsz = 0;
       int procrss = 0;
       int procseconds = 0;
       float procpcpu = 0;
       char procstat[8];
       char procetime[MAX_INPUT_BUFFER] = { '\0' };
       char *procargs;

       const char *zombie = "Z";

       int resultsum = 0; /* bitmask of the filter criteria met by a process */
       int found = 0; /* counter for number of lines returned in `ps` output */
       int procs = 0; /* counter for number of processes meeting filter criteria */
       int pos; /* number of spaces before 'args' in `ps` output */
       int cols; /* number of columns in ps output */
       int expected_cols = PS_COLS - 1;
       int warn = 0; /* number of processes in warn state */
       int crit = 0; /* number of processes in crit state */
       int i = 0, j = 0;
       int result = STATE_UNKNOWN;
       output chld_out, chld_err;

       setlocale (LC_ALL, "");
       bindtextdomain (PACKAGE, LOCALEDIR);
       textdomain (PACKAGE);
       setlocale(LC_NUMERIC, "POSIX");

       input_buffer = malloc (MAX_INPUT_BUFFER);
       procprog = malloc (MAX_INPUT_BUFFER);

       asprintf (&metric_name, "PROCS");
       metric = METRIC_PROCS;

       /* Parse extra opts if any */
       argv=np_extra_opts (&argc, argv, progname);

       if (process_arguments (argc, argv) == ERROR)
              usage4 (_("Could not parse arguments"));

       /* get our pid */
       mypid = getpid();

       /* Set signal handling and alarm timeout */
       if (signal (SIGALRM, timeout_alarm_handler) == SIG_ERR) {
              die (STATE_UNKNOWN, _("Cannot catch SIGALRM"));
       }
       (void) alarm ((unsigned) timeout_interval);

       if (verbose >= 2)
              printf (_("CMD: %s\n"), PS_COMMAND);

       if (input_filename == NULL) {
              result = cmd_run( PS_COMMAND, &chld_out, &chld_err, 0);
              if (chld_err.lines > 0) {
                     printf ("%s: %s", _("System call sent warnings to stderr"), chld_err.line[0]);
                     exit(STATE_WARNING);
              }
       } else {
              result = cmd_file_read( input_filename, &chld_out, 0);
       }

       /* flush first line: j starts at 1 */
       for (j = 1; j < chld_out.lines; j++) {
              input_line = chld_out.line[j];

              if (verbose >= 3)
                     printf ("%s", input_line);

              strcpy (procprog, "");
              asprintf (&procargs, "%s", "");

              cols = sscanf (input_line, PS_FORMAT, PS_VARLIST);

              /* Zombie processes do not give a procprog command */
              if ( cols < expected_cols && strstr(procstat, zombie) ) {
                     cols = expected_cols;
              }
              if ( cols >= expected_cols ) {
                     resultsum = 0;
                     asprintf (&procargs, "%s", input_line + pos);
                     strip (procargs);

                     /* Some ps return full pathname for command. This removes path */
                     strcpy(procprog, base_name(procprog));

                     /* we need to convert the elapsed time to seconds */
                     procseconds = convert_to_seconds(procetime);

                     if (verbose >= 3)
                            printf ("proc#=%d uid=%d vsz=%d rss=%d pid=%d ppid=%d pcpu=%.2f stat=%s etime=%s prog=%s args=%s\n", 
                                   procs, procuid, procvsz, procrss,
                                   procpid, procppid, procpcpu, procstat, 
                                   procetime, procprog, procargs);

                     /* Ignore self */
                     if (mypid == procpid) continue;

                     if ((options & STAT) && (strstr (statopts, procstat)))
                            resultsum |= STAT;
                     if ((options & ARGS) && procargs && (strstr (procargs, args) != NULL))
                            resultsum |= ARGS;
                     if ((options & EREG_ARGS) && procargs && (regexec(&re_args, procargs, (size_t) 0, NULL, 0) == 0))
                            resultsum |= EREG_ARGS;
                     if ((options & PROG) && procprog && (strcmp (prog, procprog) == 0))
                            resultsum |= PROG;
                     if ((options & PPID) && (procppid == ppid))
                            resultsum |= PPID;
                     if ((options & USER) && (procuid == uid))
                            resultsum |= USER;
                     if ((options & VSZ)  && (procvsz >= vsz))
                            resultsum |= VSZ;
                     if ((options & RSS)  && (procrss >= rss))
                            resultsum |= RSS;
                     if ((options & PCPU)  && (procpcpu >= pcpu))
                            resultsum |= PCPU;

                     found++;

                     /* Next line if filters not matched */
                     if (!(options == resultsum || options == ALL))
                            continue;

                     procs++;
                     if (verbose >= 2) {
                            printf ("Matched: uid=%d vsz=%d rss=%d pid=%d ppid=%d pcpu=%.2f stat=%s etime=%s prog=%s args=%s\n", 
                                   procuid, procvsz, procrss,
                                   procpid, procppid, procpcpu, procstat, 
                                   procetime, procprog, procargs);
                     }

                     if (metric == METRIC_VSZ)
                            i = check_thresholds (procvsz);
                     else if (metric == METRIC_RSS)
                            i = check_thresholds (procrss);
                     /* TODO? float thresholds for --metric=CPU */
                     else if (metric == METRIC_CPU)
                            i = check_thresholds ((int)procpcpu); 
                     else if (metric == METRIC_ELAPSED)
                            i = check_thresholds (procseconds);

                     if (metric != METRIC_PROCS) {
                            if (i == STATE_WARNING) {
                                   warn++;
                                   asprintf (&fails, "%s%s%s", fails, (strcmp(fails,"") ? ", " : ""), procprog);
                                   result = max_state (result, i);
                            }
                            if (i == STATE_CRITICAL) {
                                   crit++;
                                   asprintf (&fails, "%s%s%s", fails, (strcmp(fails,"") ? ", " : ""), procprog);
                                   result = max_state (result, i);
                            }
                     }
              } 
              /* This should not happen */
              else if (verbose) {
                     printf(_("Not parseable: %s"), input_buffer);
              }
       }

       if (found == 0) {                                              /* no process lines parsed so return STATE_UNKNOWN */
              printf (_("Unable to read output\n"));
              return STATE_UNKNOWN;
       }

       if ( result == STATE_UNKNOWN ) 
              result = STATE_OK;

       /* Needed if procs found, but none match filter */
       if ( metric == METRIC_PROCS ) {
              result = max_state (result, check_thresholds (procs) );
       }

       if ( result == STATE_OK ) {
              printf ("%s %s: ", metric_name, _("OK"));
       } else if (result == STATE_WARNING) {
              printf ("%s %s: ", metric_name, _("WARNING"));
              if ( metric != METRIC_PROCS ) {
                     printf (_("%d warn out of "), warn);
              }
       } else if (result == STATE_CRITICAL) {
              printf ("%s %s: ", metric_name, _("CRITICAL"));
              if (metric != METRIC_PROCS) {
                     printf (_("%d crit, %d warn out of "), crit, warn);
              }
       } 
       printf (ngettext ("%d process", "%d processes", (unsigned long) procs), procs);
       
       if (strcmp(fmt,"") != 0) {
              printf (_(" with %s"), fmt);
       }

       if ( verbose >= 1 && strcmp(fails,"") )
              printf (" [%s]", fails);

       printf ("\n");
       return result;
}

Here is the call graph for this function:

void print_help ( void  )
void print_usage ( void  )

Definition at line 389 of file check_cpqarray.c.

{
  printf("cpqarrayd [options]\n");
  printf("   -h         prints this text\n");
  printf("   -d         enables debugging\n");
}
int process_arguments ( int  argc,
char **  argv 
)
int validate_arguments ( void  )

Variable Documentation

int cmax = -1

Definition at line 54 of file check_procs.c.

int cmin = -1

Definition at line 56 of file check_procs.c.

const char* copyright = "2000-2008"

Definition at line 36 of file check_procs.c.

const char* email = "nagiosplug-devel@lists.sourceforge.net"

Definition at line 37 of file check_procs.c.

char* metric_name

Definition at line 71 of file check_procs.c.

int options = 0

Definition at line 58 of file check_procs.c.

const char* progname = "check_procs"

Definition at line 34 of file check_procs.c.

const char* program_name = "check_procs"

Definition at line 35 of file check_procs.c.

int wmax = -1

Definition at line 53 of file check_procs.c.

int wmin = -1

Definition at line 55 of file check_procs.c.