Back to index

nagios-plugins  1.4.16
check_boot.c
Go to the documentation of this file.
00001 /*                                                                          
00002         Source: check_boot.c
00003         Author: Adam G. Bowen (agbowen@bealenet.com)
00004         $Revision: 1.5 $
00005         $Date: 1999/05/17 14:35:26 $
00006  
00007  * Program: System uptime 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 system uptime on the remote host at ipaddress and
00014  * generate an alert if the uptime is above the <max_up> value or if
00015  * (<weekend_boot> = Y) and ((max_up - uptime) < 7) and ((today = Saturday) or
00016  * (today = Sunday)).
00017  *
00018  * Other errors result in a STATE_UNKNOWN return.
00019  *
00020  * Command line:
00021  *
00022  * check_boot ipaddress sys_type
00023  * check_boot ipaddress sys_type <weekend_boot>
00024  * check_boot ipaddress sys_type <weekend_boot> <max_up>
00025  *
00026  * Required input:
00027  *
00028  * ipaddress = The ipaddress of the remote system to run the check on.
00029  * sys_type = The remote system type.
00030  *
00031  * Optional input:
00032  *
00033  * <weekend_boot> = Should this system only be rebooted on the weekend.
00034  * <max_up> = Number of days up required to return a CRITICAL state.
00035  *
00036  * Notes:
00037  *
00038  * If <weekend_boot> and <max_up> are not passed on the command line, they will
00039  * be set to the default values in the check_boot config file.
00040  *
00041  * If <weekend_boot> is passed on the command line, and <max_up> is not,
00042  * <max_up> will be set to the default value in the check_boot config file.
00043  *
00044  * sys_type is used to determine which config file to use to generate the
00045  * remote command.
00046  *
00047  * The configuration file /usr/local/netsaint/config/check_boot/local
00048  * contains the following values:
00049  *
00050  * RSH_COMMAND|<location of rsh command on netsaint system>|
00051  * CUT_COMMAND|<location of cut command on netsaint system>|
00052  * WEEKEND_BOOT|<should check system only be booted on the weekend>|
00053  * MAX_UP|<max days between reboot>|
00054  *
00055  * The configuration file /usr/local/netsaint/config/check_boot/<sys_type>
00056  * contains the following values:
00057  *
00058  * UPTIME_COMMAND|<location of uptime command on system sys_type>|
00059  * CUT_OPTIONS|<options for cut command to retrieve required info>|
00060  *
00061  * $Log: check_boot.c,v $
00062  * Revision 1.5  1999/05/17 14:35:26  netsaint
00063  * Changed the plugin to use a seperate config directory.  This directory is the
00064  * CONFIG_DIR/command_name directory.
00065  *
00066  * Revision 1.4  1999/05/14 03:01:17  netsaint
00067  * Added the following integer variable:
00068  *     socket_name
00069  * Changed the call check_net to open_socket.  Added a call to recv_socket and
00070  * close_socket.  The check_net subroutine was changed to provide more
00071  * flexibility.
00072  * Added a call to the subroutine get_command_name.
00073  * Changed the error checking routines to ensure that any error cause the
00074  * program to terminate.
00075  *
00076  * Revision 1.3  1999/05/07 15:34:59  netsaint
00077  * Removed the char variable error_buffer and the FILE *error_fp variable.
00078  * These variables are no longer needed since the printing of the error file is
00079  * handled in a subroutine.
00080  * Added a call to the check_output_file subroutine.  This routine checks the
00081  * status of the output file.  Also removed the struct stat file_stat variable.
00082  *
00083  * Revision 1.2  1999/05/03 14:48:17  netsaint
00084  * Changed the config_file_prefix to config_file_fs_prefix.  Added the following
00085  * character variables:
00086  *     config_file_net_prefix
00087  *     config_file_net
00088  *     expected
00089  *     protocol
00090  * Added the following integer variables:
00091  *     result
00092  *     telnet_port
00093  * Added signal handler for the SIGALRM signal.  All these changes were to
00094  * prevent the plugin from hanging when attempting to perform check on a remote
00095  * system that is down or not working properly.  Prior to issuing the rsh
00096  * command, the plugin will attempt to establish a telnet session to the
00097  * plugin will not issue the rsh command.  Prior to establishing the telnet
00098  * connection, an alarm is set.  If the telnet connections does not return
00099  * control to the plugin before the timer expires, a SIGALRM signal will be
00100  * sent to the process which will caues the plugin to exit with a
00101  * STATE_CRITICAL error.
00102  * Added struct stat file_stat.
00103  * This is used to get the size of the out_put_file.  If the size is zero,
00104  * the plugin exits with a STATE_UNKNOWN.  Also change the error_file printing
00105  * to a subroutine in the plugins.h file.
00106  *
00107  * Revision 1.1  1999/04/28 15:18:06  netsaint
00108  * Initial revision
00109  *
00110 */
00111 
00112 #include "/usr/local/src/netsaint/include/plugins.h"
00113 
00114 int main(int argc, char *argv[])
00115 {
00116   char expected[MAX_CHARS]="";
00117   char token_sep[] = " ,";
00118   char weekend_boot[MAX_CHARS]="";
00119 
00120   char command_line[MAX_CHARS];
00121   char command_name[MAX_CHARS];
00122   char config_file_local[MAX_CHARS];
00123   char config_file_net[MAX_CHARS];
00124   char config_file_remote[MAX_CHARS];
00125   char cut_command[MAX_CHARS];
00126   char cut_options[MAX_CHARS];
00127   char day_of_week[MAX_CHARS];
00128   char error_file[MAX_CHARS];
00129   char input_buffer[MAX_CHARS];
00130   char ip_address[MAX_CHARS];
00131   char out_put_file[MAX_CHARS];
00132   char port_telnet[MAX_CHARS];
00133   char protocol[MAX_CHARS];
00134   char rsh_command[MAX_CHARS];
00135   char system_name[MAX_CHARS];
00136   char temp_value[MAX_CHARS];
00137   char uptime_command[MAX_CHARS];
00138   char uptime_return[MAX_CHARS];
00139   char up_days[MAX_CHARS];
00140   char up_max[MAX_CHARS];
00141   char up_units[MAX_CHARS];
00142 
00143   FILE *out_put_fp;
00144 
00145   int boot_weekend;
00146   int days_up;
00147   int get_max_up;
00148   int get_weekend;
00149   int is_weekend;
00150   int max_up;
00151   int result;
00152   int return_value;
00153   int socket_name;
00154   int telnet_port;
00155   int today;
00156 
00157   struct tm *curtime;
00158   
00159   time_t bintime;
00160 
00161   /* Initialize alarm signal handling */
00162 
00163   signal(SIGALRM,alarm_signal);
00164 
00165   strcpy(command_name,get_command_name(argv[0]));
00166   if(!((argc==3) || (argc==4) || (argc==5)))
00167   {
00168     printf("\n");
00169     printf(" Incorrect number of arguments supplied\n");
00170     printf("\n");
00171     printf(" System uptime plugin for NetSaint\n");
00172     printf(" Copyright (c) 1999 Adam G. Bowen (agbowen@bealenet.com)\n");
00173     printf(" $Revision: 1.5 $\n");
00174     printf(" Last Modified $Date: 1999/05/17 14:35:26 $\n");
00175     printf(" License: GPL\n");
00176     printf("\n");
00177     printf(" Description:\n");
00178     printf("\n");
00179     printf("This plugin will check the system uptime on the remote host at ipaddress and\n");
00180     printf("generate an alert if the uptime is above the <max_up> value or if\n");
00181     printf("(<weekend_boot> = Y) and ((max_up - uptime) < 7) and ((today = Saturday) or\n");
00182     printf("(today = Sunday)).\n");
00183     printf("\n");
00184     printf(" Usage: %s ipaddress sys_type\n",command_name);
00185     printf(" Usage: %s ipaddress sys_type <weekend_boot>\n",command_name);
00186     printf(" Usage: %s ipaddress sys_type <weekend_boot> <max_up>\n",command_name);
00187     printf("\n");
00188     printf(" Required input:\n");
00189     printf("\n");
00190     printf(" ipaddress = The ipaddress of the remote system to run the check on.\n");
00191     printf(" sys_type = The remote system type.\n");
00192     printf("\n");
00193     printf(" Optional input:\n");
00194     printf("\n");
00195     printf(" <weekend_boot> = Should this system only be rebooted on the weekend?\n");
00196     printf(" <max_up> = Number of days up required to result in a CRITICAL state.\n");
00197     printf("\n");
00198     printf(" If <weekend_boot> and <max_up> are not passed on the command line, they will\n");
00199     printf(" be set to the default values in the %s config file.\n", command_name);
00200     printf("\n");
00201     printf(" If <weekend_boot> is passed on the command line, and <max_up> is not,\n");
00202     printf(" <max_up> will be set to the default value in the %s config file.\n",command_name);
00203     printf("\n");
00204     printf(" sys_type is used to determine which config file to use to generate the\n");
00205     printf(" remote command.\n");
00206     printf("\n");
00207     return_value = STATE_UNKNOWN;
00208   }
00209   else
00210   {
00211     /* Set up config files and get the command line information */
00212 
00213     strcpy(ip_address,argv[1]);
00214     strcpy(system_name,argv[2]);
00215 
00216     strcpy(config_file_local,CONFIG_DIR);
00217     strcpy(config_file_remote,CONFIG_DIR);
00218     strcpy(config_file_net,CONFIG_DIR);
00219     strcat(config_file_local,command_name);
00220     strcat(config_file_remote,command_name);
00221     strcat(config_file_net,CHECK_TELNET);
00222     strcat(config_file_local,"/local");
00223     strcat(config_file_remote,"/");
00224     strcat(config_file_remote,system_name);
00225     strcat(config_file_net,"/");
00226     strcat(config_file_net,system_name);
00227 
00228     if(argc == 3)
00229     {
00230       get_max_up = TRUE;
00231       get_weekend = TRUE;
00232     }
00233     else if(argc == 4)
00234     {
00235       get_max_up = TRUE;
00236       get_weekend = FALSE;
00237       strupr(strncpy(weekend_boot,argv[3],1));
00238     }
00239     else
00240     {
00241       get_max_up = FALSE;
00242       get_weekend = FALSE;
00243       strupr(strncpy(weekend_boot,argv[3],1));
00244       strcpy(up_max,argv[4]);
00245     }
00246 
00247     /* Check if config files exist */
00248 
00249     if (access(config_file_local, EXISTS) != 0 )
00250     {
00251       printf("Config file %s does not exist!\n",config_file_local);
00252       return_value = STATE_UNKNOWN;
00253     }
00254     else if (access(config_file_remote, EXISTS) != 0 )
00255     {
00256       printf("Config file %s does not exist!\n",config_file_remote);
00257       return_value = STATE_UNKNOWN;
00258     }
00259     else if (access(config_file_net, EXISTS) != 0 )
00260     {
00261       printf("Config file %s does not exist!\n",config_file_net);
00262       return_value = STATE_UNKNOWN;
00263     }
00264     else
00265     {
00266 
00267       /* Local config file variables */
00268 
00269       if((get_weekend == TRUE) && ((return_value=get_var("WEEKEND_BOOT", config_file_local, weekend_boot)) != STATE_OK))
00270       {
00271         printf("WEEKEND_BOOT entry not found in config file %s!\n",config_file_local);
00272       }
00273       else if((get_max_up == TRUE) && ((return_value=get_var("MAX_UP", config_file_local, up_max)) != STATE_OK))
00274       {
00275         printf("MAX_UP entry not found in config file %s!\n",config_file_local);
00276       }
00277       else if((return_value=get_var("RSH_COMMAND", config_file_local, rsh_command)) != STATE_OK)
00278       {
00279         printf("RSH_COMMAND entry not found in config file %s!\n", config_file_local);
00280       }
00281       else if((return_value=get_var("CUT_COMMAND", config_file_local, cut_command)) != STATE_OK)
00282       {
00283         printf("CUT_COMMAND entry not found in config file %s!\n", config_file_local);
00284       }
00285 
00286       /* Remote config file variables */
00287 
00288       else if((return_value=get_var("UPTIME_COMMAND", config_file_remote, uptime_command)) != STATE_OK)
00289       {
00290         printf("UPTIME_COMMAND entry not found in config file %s!\n", config_file_remote);
00291       }
00292       else if((return_value=get_var("CUT_OPTIONS", config_file_remote, cut_options)) != STATE_OK)
00293       {
00294         printf("CUT_OPTIONS entry not found in config file %s!\n",config_file_remote);
00295       }
00296 
00297       /* Network config file variables */
00298 
00299       else if((return_value=get_var("TELNET_PORT", config_file_net, port_telnet)) != STATE_OK)
00300       {
00301         printf("TELNET_PORT entry not found in config file %s!\n",config_file_net);
00302       }
00303       else if((return_value=get_var("TELNET_PROTO", config_file_net, protocol)) != STATE_OK)
00304       {
00305         printf("TELNET_PROTO entry not found in config file %s!\n",config_file_net);
00306       }
00307       else
00308       {
00309 
00310         /* Check alert level consistency */
00311 
00312         max_up=atoi(up_max);
00313         if((strcmp(weekend_boot,"Y") == 0) && (max_up < 7))
00314         {
00315           printf("Inconsistency in parameters: weekend_boot set to yes and max_up (%d days) less than 7 days.\n",max_up);
00316           return_value = STATE_UNKNOWN;
00317         }
00318         else if(max_up < 1)
00319         {
00320           printf("Invalid parameter: max_up (%d) less than 1 day.\n",max_up);
00321           return_value = STATE_UNKNOWN;
00322         }
00323         else
00324         {
00325           return_value = STATE_OK;
00326         }
00327         if(return_value == STATE_OK)
00328         {
00329 
00330           /* Check the network */
00331 
00332           telnet_port=atoi(port_telnet);
00333           alarm(TIME_OUT);
00334           if((result=open_socket(&socket_name, ip_address, telnet_port, protocol)) != STATE_OK)
00335           {
00336             return_value=exit_error(result,ip_address,protocol,telnet_port);
00337           }
00338           else if((result=recv_socket(&socket_name, expected)) != STATE_OK)
00339           {
00340             return_value=exit_error(result,ip_address,protocol,telnet_port);
00341           }
00342           else if((result=close_socket(&socket_name)) != STATE_OK)
00343           {
00344             return_value=exit_error(result,ip_address,protocol,telnet_port);
00345           }
00346           else
00347           {
00348             alarm(0);
00349 
00350             /* Generate a out_put and error file names */
00351 
00352             strcpy(out_put_file, tmpnam(NULL));
00353             strcpy(error_file, tmpnam(NULL));
00354 
00355             /* set the command line and arguments to use for the check */
00356   
00357             sprintf(command_line,"%s %s %s </dev/null 2>%s|%s %s >%s 2>>%s",rsh_command, ip_address, uptime_command, error_file, cut_command, cut_options, out_put_file, error_file);
00358 
00359             /* Run the command */
00360   
00361             system(command_line);
00362 
00363             return_value=check_output_file(out_put_file);
00364             if(return_value != STATE_OK)
00365             {
00366               print_error(error_file);
00367             }
00368             else
00369             {
00370 
00371               out_put_fp=fopen(out_put_file,"r");
00372       
00373               /* Retrive single line from output file */
00374 
00375               fgets(input_buffer,MAX_CHARS-1,out_put_fp);
00376 
00377               /* Populate variables */
00378       
00379               strcpy(temp_value,strtok(input_buffer,token_sep));
00380               strcpy(temp_value,strtok(NULL,token_sep));
00381               strcpy(up_days,strtok(NULL,token_sep));
00382               if(strchr(up_days,':') == NULL)
00383               {
00384                 strupr(strcpy(up_units,strtok(NULL,token_sep)));
00385                 if(strstr(up_units,"DAY"))
00386                 {
00387                   days_up = atoi(up_days);
00388                   time(&bintime);
00389                   curtime = localtime(&bintime);
00390                   strftime(day_of_week,MAX_CHARS,"%w",curtime);
00391                   today = atoi(day_of_week);
00392               
00393                   if((today == SATURDAY) || (today == SUNDAY))
00394                   {
00395                     is_weekend = TRUE;
00396                   }
00397                   else
00398                   {
00399                     is_weekend = FALSE;
00400                   }
00401                   if(strcmp(weekend_boot,"Y") == 0)
00402                   {
00403                     boot_weekend = TRUE;
00404                   }
00405                   else
00406                   {
00407                     boot_weekend = FALSE;
00408                   }
00409                   if(days_up > max_up)
00410                   {
00411                     return_value=STATE_CRITICAL;
00412                   }
00413                   else if ((boot_weekend == TRUE) && (is_weekend == TRUE) && ((max_up - days_up) < 7))
00414                   {
00415                     return_value=STATE_WARNING;
00416                   }
00417                   else
00418                   {
00419                     return_value=STATE_OK;
00420                   }
00421                 }
00422                 else
00423                 {
00424                   return_value=STATE_OK;
00425                 }
00426                 strcpy(uptime_return,up_days);
00427                 strcat(uptime_return," ");
00428                 strcat(uptime_return,up_units);
00429               }
00430               else
00431               {
00432                 return_value=STATE_OK;
00433                 sprintf(uptime_return,"%s",up_days);
00434               }
00435 
00436               /* Close output file */
00437 
00438               fclose(out_put_fp);
00439               if(return_value==STATE_OK)
00440               {
00441                 printf("System uptime ok - up %s.\n", uptime_return);
00442               }
00443               else if (return_value==STATE_WARNING)
00444               {
00445                 printf("System uptime waring - system up %s, will exceed max up (%d days) before next week end.\n", uptime_return, max_up);
00446               }
00447               else
00448               {
00449                 printf("System uptime error - system up %s, exceeded max up (%d days).\n", uptime_return, max_up);
00450               }
00451             }
00452 
00453             /* remove the output and error files */
00454   
00455             remove(out_put_file);
00456             remove(error_file);
00457           }
00458         }
00459       }
00460     }
00461   }
00462   return return_value;
00463 }