Back to index

nagios-plugins  1.4.16
check_users.c
Go to the documentation of this file.
00001 /*                                                                          
00002         Source: check_users.c
00003         Author: Adam G. Bowen (agbowen@bealenet.com)
00004         $Revision: 1.2 $
00005         $Date: 1999/05/17 14:35:26 $
00006  
00007  * Program: System user count plugin for NetSaint
00008  * License: GPL
00009  * Copyright (c) 1999 Adam G. Bowen (agbowen@bealenet.com)
00010  *
00011  * Description:
00012  *
00013  * This plugin will check the number of users logged on the remote host at
00014  * ipaddress and generate an alert if number is above one of the threshold
00015  * values.
00016  *
00017  * Other errors result in a STATE_UNKNOWN return.
00018  *
00019  * Command line:
00020  *
00021  * check_users ipaddress sys_type 
00022  * check_users ipaddress sys_type <warn_#> <crit_#>
00023  *
00024  * Required input:
00025  *
00026  * ipaddress = The ipaddress of the remote system to run the check on.
00027  * sys_type = The remote system type.
00028  *
00029  * Optional input:
00030  *
00031  * <warn_#> = Number of users necessary to result in a WARNING state.
00032  * <crit_#> = Number of users necessary to result in a CRITICAL state.
00033  *
00034  * Notes:
00035  *
00036  * If <warn_#> and <crit_#> are not passed on the command line, they will be set
00037  * to the default values in the check_users config file.
00038  *
00039  * sys_type is used to determine which config file to use to generate the
00040  * remote command.
00041  *
00042  * The configuration file /usr/local/netsaint/config/check_users/local
00043  * contains the following values:
00044  *
00045  * RSH_COMMAND|<location of rsh command on netsaint system>|
00046  * AWK_COMMAND|<location of awk command on netsaint system>|
00047  * TAIL_COMMAND|<location of tail command on netsaint system>|
00048  * WARN_USERS|<number of users required to generate a warning>|
00049  * CRIT_USERS|<number of users required to generate a critical>|
00050  *
00051  * The configuration file /usr/local/netsaint/config/check_users/<sys_type>
00052  * contains the following values:
00053  *
00054  * WHO_COMMAND|<location of who command on system sys_type>|
00055  * WHO_OPTIONS|<options for who command to retreive required info>|
00056  * AWK_OPTIONS|<options for awk command to retrieve required info>|
00057  * TAIL_OPTIONS|<options for tail command to retrieve required info>|
00058  *
00059  * $Log: check_users.c,v $
00060  * Revision 1.2  1999/05/17 14:35:26  netsaint
00061  * Changed the plugin to use a seperate config directory.  This directory is the
00062  * CONFIG_DIR/command_name directory.
00063  *
00064  * Revision 1.1  1999/05/14 02:50:40  netsaint
00065  * Initial revision
00066  *
00067  *
00068 */
00069 
00070 #include "/usr/local/src/netsaint/include/plugins.h"
00071 
00072 int main(int argc, char *argv[])
00073 {
00074   char expected[MAX_CHARS]="";
00075 
00076   char awk_command[MAX_CHARS];
00077   char awk_options[MAX_CHARS];
00078   char command_line[MAX_CHARS];
00079   char command_name[MAX_CHARS];
00080   char config_file_local[MAX_CHARS];
00081   char config_file_net[MAX_CHARS];
00082   char config_file_remote[MAX_CHARS];
00083   char error_file[MAX_CHARS];
00084   char input_buffer[MAX_CHARS];
00085   char ip_address[MAX_CHARS];
00086   char out_put_file[MAX_CHARS];
00087   char port_telnet[MAX_CHARS];
00088   char protocol[MAX_CHARS];
00089   char rsh_command[MAX_CHARS];
00090   char *sub_string;
00091   char system_name[MAX_CHARS];
00092   char tail_command[MAX_CHARS];
00093   char tail_options[MAX_CHARS];
00094   char users_crit[MAX_CHARS];
00095   char users_warn[MAX_CHARS];
00096   char who_command[MAX_CHARS];
00097   char who_options[MAX_CHARS];
00098 
00099   FILE *out_put_fp;
00100 
00101   int crit_users;
00102   int get_defaults;
00103   int num_users;
00104   int result;
00105   int return_value;
00106   int socket_name;
00107   int telnet_port;
00108   int warn_users;
00109 
00110   /* Initialize alarm signal handling */
00111 
00112   signal(SIGALRM,alarm_signal);
00113 
00114   strcpy(command_name,get_command_name(argv[0]));
00115   if(!((argc==3) || (argc==5)))
00116   {
00117     printf("\n");
00118     printf(" Incorrect number of arguments supplied\n");
00119     printf("\n");
00120     printf(" System user count plugin for NetSaint\n");
00121     printf(" Copyright (c) 1999 Adam G. Bowen (agbowen@bealenet.com)\n");
00122     printf(" $Revision: 1.2 $\n");
00123     printf(" Last Modified $Date: 1999/05/17 14:35:26 $\n");
00124     printf(" License: GPL\n");
00125     printf("\n");
00126     printf(" Description:\n");
00127     printf("\n");
00128     printf(" This plugin will check the number of users logged on the remote host at\n");
00129     printf(" ipaddress and generate an alert if number is above one of the threshold\n");
00130     printf(" values.\n");
00131     printf("\n");
00132     printf(" Usage: %s ipaddress sys_type\n",command_name);
00133     printf(" Usage: %s ipaddress sys_type <warn_#> <crit_#>\n",command_name);
00134     printf("\n");
00135     printf(" Required input:\n");
00136     printf("\n");
00137     printf(" ipaddress = The ipaddress of the remote system to run the check on.\n");
00138     printf(" sys_type = The remote system type.\n");
00139     printf("\n");
00140     printf(" Optional input:\n");
00141     printf("\n");
00142     printf(" <warn_#> = Number of users necessary to result in a WARNING state.\n");
00143     printf(" <crit_#> = Number of users necessary to result in a CRITICAL state.\n");
00144     printf("\n");
00145     printf(" If <warn_#> and <crit_#> are not passed on the command line, they will be set\n");
00146     printf(" to the default values in the %s config file.\n", command_name);
00147     printf("\n");
00148     printf(" sys_type is used to determine which config file to use to generate the\n");
00149     printf(" remote command.\n");
00150     printf("\n");
00151     return_value = STATE_UNKNOWN;
00152   }
00153   else
00154   {
00155     /* Set up config files and get the command line information */
00156 
00157     strcpy(ip_address,argv[1]);
00158     strcpy(system_name,argv[2]);
00159 
00160     strcpy(config_file_local,CONFIG_DIR);
00161     strcpy(config_file_remote,CONFIG_DIR);
00162     strcpy(config_file_net,CONFIG_DIR);
00163     strcat(config_file_local,command_name);
00164     strcat(config_file_remote,command_name);
00165     strcat(config_file_net,CHECK_TELNET);
00166     strcat(config_file_local,"/local");
00167     strcat(config_file_remote,"/");
00168     strcat(config_file_net,"/");
00169     strcat(config_file_remote,system_name);
00170     strcat(config_file_net,system_name);
00171 
00172     if(argc == 3)
00173     {
00174       get_defaults = TRUE;
00175     }
00176     else
00177     {
00178       get_defaults = FALSE;
00179       strcpy(users_warn,argv[3]);
00180       strcpy(users_crit,argv[4]);
00181     }
00182 
00183     /* Check if config files exist */
00184 
00185     if (access(config_file_local, EXISTS) != 0 )
00186     {
00187       printf("Config file %s does not exist!\n",config_file_local);
00188       return_value = STATE_UNKNOWN;
00189     }
00190     else if (access(config_file_remote, EXISTS) != 0 )
00191     {
00192       printf("Config file %s does not exist!\n",config_file_remote);
00193       return_value = STATE_UNKNOWN;
00194     }
00195     else if (access(config_file_net, EXISTS) != 0 )
00196     {
00197       printf("Config file %s does not exist!\n",config_file_net);
00198       return_value = STATE_UNKNOWN;
00199     }
00200     else
00201     {
00202 
00203       /* Local config file variables */
00204 
00205       if((get_defaults == TRUE) && ((return_value=get_var("WARN_USERS", config_file_local, users_warn)) != STATE_OK))
00206       {
00207         printf("WARN_USERS entry not found in config file %s!\n",config_file_local);
00208       }
00209       else if((get_defaults == TRUE) && ((return_value=get_var("CRIT_USERS", config_file_local, users_crit)) != STATE_OK))
00210       {
00211         printf("CRIT_USERS entry not found in config file %s!\n",config_file_local);
00212       }
00213       else if((return_value=get_var("RSH_COMMAND", config_file_local, rsh_command)) != STATE_OK)
00214       {
00215         printf("RSH_COMMAND entry not found in config file %s!\n", config_file_local);
00216       }
00217       else if((return_value=get_var("AWK_COMMAND", config_file_local, awk_command)) != STATE_OK)
00218       {
00219         printf("AWK_COMMAND entry not found in config file %s!\n", config_file_local);
00220       }
00221       else if((return_value=get_var("TAIL_COMMAND", config_file_local, tail_command)) != STATE_OK)
00222       {
00223         printf("TAIL_COMMAND entry not found in config file %s!\n", config_file_local);
00224       }
00225 
00226       /* Remote config file variables */
00227 
00228       else if((return_value=get_var("WHO_COMMAND", config_file_remote, who_command)) != STATE_OK)
00229       {
00230         printf("WHO_COMMAND entry not found in config file %s!\n", config_file_remote);
00231       }
00232       else if((return_value=get_var("WHO_OPTIONS", config_file_remote, who_options)) != STATE_OK)
00233       {
00234         printf("WHO_OPTIONS entry not found in config file %s!\n",config_file_remote);
00235       }
00236       else if((return_value=get_var("AWK_OPTIONS", config_file_remote, awk_options)) != STATE_OK)
00237       {
00238         printf("AWK_OPTIONS entry not found in config file %s!\n",config_file_remote);
00239       }
00240       else if((return_value=get_var("TAIL_OPTIONS", config_file_remote, tail_options)) != STATE_OK)
00241       {
00242         printf("TAIL_OPTIONS entry not found in config file %s!\n", config_file_remote);
00243       }
00244 
00245       /* Network config file variables */
00246 
00247       else if((return_value=get_var("TELNET_PORT", config_file_net, port_telnet)) != STATE_OK)
00248       {
00249         printf("TELNET_PORT entry not found in config file %s!\n",config_file_net);
00250       }
00251       else if((return_value=get_var("TELNET_PROTO", config_file_net, protocol)) != STATE_OK)
00252       {
00253         printf("TELNET_PROTO entry not found in config file %s!\n",config_file_net);
00254       }
00255       else
00256       {
00257 
00258         /* Check alert level consistency */
00259 
00260         warn_users=atoi(users_warn);
00261         crit_users=atoi(users_crit);
00262         return_value = check_consistency(warn_users, crit_users);
00263         if(return_value == STATE_OK)
00264         {
00265 
00266           /* Check the network */
00267 
00268           telnet_port=atoi(port_telnet);
00269           alarm(TIME_OUT);
00270           if((result=open_socket(&socket_name, ip_address, telnet_port, protocol)) != STATE_OK)
00271           {
00272             return_value=exit_error(result,ip_address,protocol,telnet_port);
00273           }
00274           else if((result=recv_socket(&socket_name, expected)) != STATE_OK)
00275           {
00276             return_value=exit_error(result,ip_address,protocol,telnet_port);
00277           }
00278           else if((result=close_socket(&socket_name)) != STATE_OK)
00279           {
00280             return_value=exit_error(result,ip_address,protocol,telnet_port);
00281           }
00282           else
00283           {
00284             alarm(0);
00285 
00286             /* Generate a out_put and error file names */
00287 
00288             strcpy(out_put_file, tmpnam(NULL));
00289             strcpy(error_file, tmpnam(NULL));
00290  
00291             /* Set the command line and arguments to use for the check */
00292   
00293             sprintf(command_line,"%s %s %s %s </dev/null 2>%s|%s %s 2>>%s|%s %s >%s 2>>%s",rsh_command, ip_address, who_command, who_options, error_file, tail_command, tail_options, error_file, awk_command, awk_options, out_put_file, error_file);
00294   
00295             /* Run the command */
00296   
00297             system(command_line);
00298 
00299             return_value=check_output_file(out_put_file);
00300             if (return_value != STATE_OK)
00301             {
00302               print_error(error_file);
00303               return return_value;
00304             }
00305             else
00306             {
00307               out_put_fp=fopen(out_put_file,"r");
00308   
00309               /* Retrive single line from output file */
00310 
00311               fgets(input_buffer,MAX_CHARS-1,out_put_fp);
00312 
00313               /* Populate variables */
00314   
00315               if(strstr(input_buffer,"=") != NULL)
00316               {
00317                 sub_string=strrchr(input_buffer,'=');
00318                 num_users=atoi(++sub_string);
00319               }
00320               else
00321               {
00322                 num_users=atoi(input_buffer);
00323               }
00324 
00325               /* Close output file */
00326   
00327               fclose(out_put_fp);
00328             }
00329 
00330             /* Check the system load against warning and critical levels */
00331 
00332             if(num_users >= crit_users)
00333             {
00334               return_value=STATE_CRITICAL;
00335             }
00336             else if(num_users >= warn_users)
00337             {
00338               return_value=STATE_WARNING;
00339             }
00340         
00341             if(return_value==STATE_OK)
00342             {
00343               printf("User count ok - total users: %d\n", num_users);
00344             }
00345             else
00346             {
00347               printf("User count error - total users: %d\n", num_users);
00348             }
00349   
00350             /* Remove the output and error files */
00351 
00352             remove(out_put_file);
00353             remove(error_file);
00354 
00355           }
00356         }
00357       }
00358     }
00359   } 
00360   return return_value;
00361 }