Back to index

nagios-plugins  1.4.16
Functions
check_load.c File Reference
#include "/usr/local/src/netsaint/include/plugins.h"

Go to the source code of this file.

Functions

int main (int argc, char *argv[])

Function Documentation

int main ( int  argc,
char *  argv[] 
)

Definition at line 130 of file check_load.c.

{
  char expected[MAX_CHARS]="";
  char token_sep[] = ":,";

  char command_line[MAX_CHARS];
  char command_name[MAX_CHARS];
  char config_file_local[MAX_CHARS];
  char config_file_remote[MAX_CHARS];
  char config_file_net[MAX_CHARS];
  char error_file[MAX_CHARS];
  char input_buffer[MAX_CHARS];
  char ip_address[MAX_CHARS];
  char load_crit_1[MAX_CHARS];
  char load_crit_5[MAX_CHARS];
  char load_crit_15[MAX_CHARS];
  char load_warn_1[MAX_CHARS];
  char load_warn_5[MAX_CHARS];
  char load_warn_15[MAX_CHARS];
  char out_put_file[MAX_CHARS];
  char protocol[MAX_CHARS];
  char port_telnet[MAX_CHARS];
  char rsh_command[MAX_CHARS];
  char sub_string[MAX_CHARS];
  char system_name[MAX_CHARS];
  char temp_value[MAX_CHARS];
  char uptime_command[MAX_CHARS];

  FILE *out_put_fp;

  float crit_load_1;
  float crit_load_5;
  float crit_load_15;
  float load_avrg_1;
  float load_avrg_5;
  float load_avrg_15;
  float warn_load_1;
  float warn_load_5;
  float warn_load_15;

  int get_defaults;
  int result;
  int return_value;
  int socket_name;
  int telnet_port;

  /* Initialize alarm signal handling */

  signal(SIGALRM,alarm_signal);

  strcpy(command_name,get_command_name(argv[0]));
  if(!((argc==3) || (argc==9)))
  {
    printf("\n");
    printf(" Incorrect number of arguments supplied\n");
    printf("\n");
    printf(" System load plugin for NetSaint\n");
    printf(" Copyright (c) 1999 Adam G. Bowen (agbowen@bealenet.com)\n");
    printf(" $Revision: 1.7 $\n");
    printf(" Last Modified $Date: 1999/05/17 14:35:26 $\n");
    printf(" License: GPL\n");
    printf("\n");
    printf(" Description:\n");
    printf("\n");
    printf(" This plugin will check the system load on the remote host at ipaddress and\n");
    printf(" generate an alert if average is above one of the threshold values.\n");
    printf("\n");
    printf(" Usage: %s ipaddress sys_type\n",command_name);
    printf(" Usage: %s ipaddress sys_type <w1> <c1> <w5> <c5> <w15> <c15>\n",command_name);
    printf("\n");
    printf(" Required input:\n");
    printf("\n");
    printf(" ipaddress = The ipaddress of the remote system to run the check on.\n");
    printf(" sys_type = The remote system type.\n");
    printf("\n");
    printf(" Optional input:\n");
    printf("\n");
    printf(" <w1> = 1 min load average necessary to result in a WARNING state.\n");
    printf(" <c1> = 1 min load average necessary to result in a CRITICAL state.\n");
    printf(" <w5> = 5 min load average necessary to result in a WARNING state.\n");
    printf(" <c5> = 5 min load average necessary to result in a CRITICAL state.\n");
    printf(" <w15> = 15 min load average necessary to result in a WARNING state.\n");
    printf(" <c15> = 15 min load average necessary to result in a CRITICAL state.\n");
    printf("\n");
    printf(" If <w1>, <c1>, <w5>, <c5>, <w15> and <c15> are not passed on the command line,\n");
    printf(" they will be set to the default values in the %s config file.\n", command_name);
    printf("\n");
    printf(" sys_type is used to determine which config file to use to generate the\n");
    printf(" remote command.\n");
    printf("\n");
    return_value = STATE_UNKNOWN;
  }
  else
  {
    /* Set up config files and get the command line information */

    strcpy(ip_address,argv[1]);
    strcpy(system_name,argv[2]);

    strcpy(config_file_local,CONFIG_DIR);
    strcpy(config_file_remote,CONFIG_DIR);
    strcpy(config_file_net,CONFIG_DIR);
    strcat(config_file_local,command_name);
    strcat(config_file_remote,command_name);
    strcat(config_file_net,CHECK_TELNET);
    strcat(config_file_local,"/local");
    strcat(config_file_remote,"/");
    strcat(config_file_net,"/");
    strcat(config_file_remote,system_name);
    strcat(config_file_net,system_name);

    if(argc == 3)
    {
      get_defaults = TRUE;
    }
    else
    {
      get_defaults = FALSE;
      strcpy(load_warn_1,argv[3]);
      strcpy(load_crit_1,argv[4]);
      strcpy(load_warn_5,argv[5]);
      strcpy(load_crit_5,argv[6]);
      strcpy(load_warn_15,argv[7]);
      strcpy(load_crit_15,argv[8]);
    }

    /* Check if config files exist */

    if (access(config_file_local, EXISTS) != 0 )
    {
      printf("Config file %s does not exist!\n",config_file_local);
      return_value = STATE_UNKNOWN;
    }
    else if (access(config_file_remote, EXISTS) != 0 )
    {
      printf("Config file %s does not exist!\n",config_file_remote);
      return_value = STATE_UNKNOWN;
    }
    else if (access(config_file_net, EXISTS) != 0 )
    {
      printf("Config file %s does not exist!\n",config_file_net);
      return_value = STATE_UNKNOWN;
    }
    else
    {
      /* Local config file variables */

      if((get_defaults == TRUE) && ((return_value=get_var("WARN_LOAD_1", config_file_local, load_warn_1)) != STATE_OK))
      {
        printf("WARN_LOAD_1 entry not found in config file %s!\n",config_file_local);
      }
      else if((get_defaults == TRUE) && ((return_value=get_var("CRIT_LOAD_1", config_file_local, load_crit_1)) != STATE_OK))
      {
        printf("CRIT_LOAD_1 entry not found in config file %s!\n",config_file_local);
      }
      else if((get_defaults == TRUE) && ((return_value=get_var("WARN_LOAD_5", config_file_local, load_warn_5)) != STATE_OK))
      {
        printf("WARN_LOAD_5 entry not found in config file %s!\n",config_file_local);
      }
      else if((get_defaults == TRUE) && ((return_value=get_var("CRIT_LOAD_5", config_file_local, load_crit_5)) != STATE_OK))
      {
        printf("CRIT_LOAD_5 entry not found in config file %s!\n",config_file_local);
      }
      else if((get_defaults == TRUE) && ((return_value=get_var("WARN_LOAD_15", config_file_local, load_warn_15)) != STATE_OK))
      {
        printf("WARN_LOAD_15 entry not found in config file %s!\n",config_file_local);
      }
      else if((get_defaults == TRUE) && ((return_value=get_var("CRIT_LOAD_15", config_file_local, load_crit_15)) != STATE_OK))
      {
        printf("CRIT_LOAD_15 entry not found in config file %s!\n",config_file_local);
      }
      else if((return_value=get_var("RSH_COMMAND", config_file_local, rsh_command)) != STATE_OK)
      {
        printf("RSH_COMMAND entry not found in config file %s!\n", config_file_local);
      }

      /* Remote config file variables */

      else if((return_value=get_var("UPTIME_COMMAND", config_file_remote, uptime_command)) != STATE_OK)
      {
        printf("UPTIME_COMMAND entry not found in config file %s!\n", config_file_remote);
      }

      /* Network config file variables */

      else if((return_value=get_var("TELNET_PORT", config_file_net, port_telnet)) != STATE_OK)
      {
        printf("TELNET_PORT entry not found in config file %s!\n",config_file_net);
      }
      else if((return_value=get_var("TELNET_PROTO", config_file_net, protocol)) != STATE_OK)
      {
        printf("TELNET_PROTO entry not found in config file %s!\n",config_file_net);
      }
      else
      {

        /* Check alert level consistency */

        warn_load_1=atof(load_warn_1);
        crit_load_1=atof(load_crit_1);
        warn_load_5=atof(load_warn_5);
        crit_load_5=atof(load_crit_5);
        warn_load_15=atof(load_warn_15);
        crit_load_15=atof(load_crit_15);
        if((result=check_consistency(warn_load_1, crit_load_1)) != STATE_OK)
        {
          return_value = result;
        }
        else if((result=check_consistency(warn_load_5, crit_load_5)) != STATE_OK)
        {
          return_value = result;
        }
        else if((result=check_consistency(warn_load_15, crit_load_15)) != STATE_OK)
        {
          return_value = result;
        }
        else
        {

          /* Check the network */

          telnet_port=atoi(port_telnet);
          alarm(TIME_OUT);
          if((result=open_socket(&socket_name, ip_address, telnet_port, protocol)) != STATE_OK)
          {
            return_value=exit_error(result,ip_address,protocol,telnet_port);
          }
          else if((result=recv_socket(&socket_name, expected)) != STATE_OK)
          {
            return_value=exit_error(result,ip_address,protocol,telnet_port);
          }
          else if((result=close_socket(&socket_name)) != STATE_OK)
          {
            return_value=exit_error(result,ip_address,protocol,telnet_port);
          }
          else
          {
            alarm(0);

            /* Generate out_put and error file names */

            strcpy(out_put_file, tmpnam(NULL));
            strcpy(error_file, tmpnam(NULL));
 
            /* Set the command line and arguments to use for the check */

            sprintf(command_line,"%s %s %s </dev/null >%s 2>%s",rsh_command, ip_address, uptime_command, out_put_file, error_file);

            /* Run the command */
  
            system(command_line);

            return_value=check_output_file(out_put_file);
            if (return_value != STATE_OK)
            {
              print_error(error_file);
            }
            else
            {
              out_put_fp=fopen(out_put_file,"r");

              /* Retrive single line from output file */

              fgets(input_buffer,MAX_CHARS-1,out_put_fp);

              /* Populate variables */

              strcpy(sub_string,strstr(input_buffer,"load average:"));
              strcpy(temp_value,strtok(sub_string,token_sep));
              load_avrg_1 = atof(strcpy(temp_value,strtok(NULL,token_sep)));
              load_avrg_5 = atof(strcpy(temp_value,strtok(NULL,token_sep)));
              load_avrg_15 = atof(strcpy(temp_value,strtok(NULL,token_sep)));

              /* Close output file */

              fclose(out_put_fp);
      
              /* Check the system load against warning and critical levels */

              if((load_avrg_1 >= crit_load_1)||(load_avrg_5 >= crit_load_5)||(load_avrg_15 >= crit_load_15))
              {
                return_value=STATE_CRITICAL;
              }
              else if((load_avrg_1 >= warn_load_1)||(load_avrg_5 >= warn_load_5)||(load_avrg_15 >= warn_load_15))
              {
                return_value=STATE_WARNING;
              }
  
              if(return_value==STATE_OK)
              {
                printf("System load ok - load average: %.2f, %.2f, %.2f\n", load_avrg_1,load_avrg_5,load_avrg_15);
              }
              else
              {
                printf("System load error - load average: %.2f, %.2f, %.2f\n", load_avrg_1,load_avrg_5,load_avrg_15);
              }
            }

            /* Remove the output and error files */
      
            remove(out_put_file);
            remove(error_file);
          }
        }
      } 
    }
  }

  return return_value;
}

Here is the call graph for this function: