Back to index

nagios-plugins  1.4.16
Functions | Variables
check_nagios.c File Reference
#include "common.h"
#include "runcmd.h"
#include "utils.h"

Go to the source code of this file.

Functions

int process_arguments (int, char **)
void print_help (void)
void print_usage (void)
int main (int argc, char **argv)

Variables

const char * progname = "check_nagios"
const char * copyright = "1999-2007"
const char * email = "nagiosplug-devel@lists.sourceforge.net"
char * status_log = NULL
char * process_string = NULL
int expire_minutes = 0
int verbose = 0

Function Documentation

int main ( int  argc,
char **  argv 
)

Definition at line 54 of file check_nagios.c.

{
       int result = STATE_UNKNOWN;
       char input_buffer[MAX_INPUT_BUFFER];
       unsigned long latest_entry_time = 0L;
       unsigned long temp_entry_time = 0L;
       int proc_entries = 0;
       time_t current_time;
       char *temp_ptr;
       FILE *fp;
       int procuid = 0;
       int procpid = 0;
       int procppid = 0;
       int procvsz = 0;
       int procrss = 0;
       float procpcpu = 0;
       char procstat[8];
#ifdef PS_USES_PROCETIME
       char procetime[MAX_INPUT_BUFFER];
#endif /* PS_USES_PROCETIME */
       char procprog[MAX_INPUT_BUFFER];
       char *procargs;
       int pos, cols;
       int expected_cols = PS_COLS - 1;
       const char *zombie = "Z";
       char *temp_string;
       output chld_out, chld_err;
       size_t i;

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

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

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

       /* Set signal handling and alarm timeout */
       if (signal (SIGALRM, timeout_alarm_handler) == SIG_ERR) {
              usage_va(_("Cannot catch SIGALRM"));
       }

       /* handle timeouts gracefully... */
       alarm (timeout_interval);

       /* open the status log */
       fp = fopen (status_log, "r");
       if (fp == NULL) {
              die (STATE_CRITICAL, "NAGIOS %s: %s\n", _("CRITICAL"), _("Cannot open status log for reading!"));
       }

       /* get the date/time of the last item updated in the log */
       while (fgets (input_buffer, MAX_INPUT_BUFFER - 1, fp)) {
              if ((temp_ptr = strstr (input_buffer, "created=")) != NULL) {
                     temp_entry_time = strtoul (temp_ptr + 8, NULL, 10);
                     latest_entry_time = temp_entry_time;
                     break;
              } else if ((temp_ptr = strtok (input_buffer, "]")) != NULL) {
                     temp_entry_time = strtoul (temp_ptr + 1, NULL, 10);
                     if (temp_entry_time > latest_entry_time)
                            latest_entry_time = temp_entry_time;
              }
       }
       fclose (fp);

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

       /* run the command to check for the Nagios process.. */
       if((result = np_runcmd(PS_COMMAND, &chld_out, &chld_err, 0)) != 0)
              result = STATE_WARNING;

       /* count the number of matching Nagios processes... */
       for(i = 0; i < chld_out.lines; i++) {
              cols = sscanf (chld_out.line[i], PS_FORMAT, PS_VARLIST);
              /* Zombie processes do not give a procprog command */
              if ( cols == (expected_cols - 1) && strstr(procstat, zombie) ) {
                     cols = expected_cols;
                     /* Set some value for procargs for the strip command further below
                      * Seen to be a problem on some Solaris 7 and 8 systems */
                     chld_out.line[i][pos] = '\n';
                     chld_out.line[i][pos+1] = 0x0;
              }
              if ( cols >= expected_cols ) {
                     asprintf (&procargs, "%s", chld_out.line[i] + pos);
                     strip (procargs);

                     /* Some ps return full pathname for command. This removes path */
                     temp_string = strtok ((char *)procprog, "/");
                     while (temp_string) {
                            strcpy(procprog, temp_string);
                            temp_string = strtok (NULL, "/");
                     }

                     /* May get empty procargs */
                     if (!strstr(procargs, argv[0]) && strstr(procargs, process_string) && strcmp(procargs,"")) {
                            proc_entries++;
                            if (verbose >= 2) {
                                   printf (_("Found process: %s %s\n"), procprog, procargs);
                            }
                     }
              }
       }

       /* If we get anything on stderr, at least set warning */
       if(chld_err.buflen)
              result = max_state (result, STATE_WARNING);

       /* reset the alarm handler */
       alarm (0);

       if (proc_entries == 0) {
              die (STATE_CRITICAL, "NAGIOS %s: %s\n", _("CRITICAL"), _("Could not locate a running Nagios process!"));
       }

       if (latest_entry_time == 0L) {
              die (STATE_CRITICAL, "NAGIOS %s: %s\n", _("CRITICAL"), _("Cannot parse Nagios log file for valid time"));
       }

       time (&current_time);
       if ((int)(current_time - latest_entry_time) > (expire_minutes * 60)) {
              result = STATE_WARNING;
       } else {
              result = STATE_OK;
       }

       printf ("NAGIOS %s: ", (result == STATE_OK) ? _("OK") : _("WARNING"));
       printf (ngettext ("%d process", "%d processes", proc_entries), proc_entries);
       printf (", ");
       printf (
         ngettext ("status log updated %d second ago",
           "status log updated %d seconds ago",
           (int) (current_time - latest_entry_time) ),
           (int) (current_time - latest_entry_time) );
       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 
)

Variable Documentation

const char* copyright = "1999-2007"

Definition at line 36 of file check_nagios.c.

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

Definition at line 37 of file check_nagios.c.

int expire_minutes = 0

Definition at line 49 of file check_nagios.c.

Definition at line 48 of file check_nagios.c.

const char* progname = "check_nagios"

Definition at line 35 of file check_nagios.c.

char* status_log = NULL

Definition at line 47 of file check_nagios.c.

int verbose = 0

Definition at line 51 of file check_nagios.c.