Back to index

nagios-plugins  1.4.16
check_http.c
Go to the documentation of this file.
00001 /*                                                                          
00002         Source: check_http.c
00003         Author: Adam G. Bowen (agbowen@bealenet.com)
00004         $Revision: 1.2 $
00005         $Date: 1999/05/17 14:35:26 $
00006  
00007  * Program: HTTP socket plugin for NetSaint
00008  * License: GPL
00009  * Copyright (c) 1999 Adam G. Bowen (agbowen@bealenet.com)
00010  *
00011  * Description:
00012  *
00013  * This plugin will establish a socket on the http port of the host specified by
00014  * ipaddress and receives a responce from the host.  If any error occures while
00015  * creating the socket, receiving data from the socket or closing the socket,
00016  * this error will result in an alert.  The incorrect responce or responce time
00017  * above the threshold vaules will also cause an alert. 
00018  *
00019  * Other errors result in a STATE_UNKNOWN return.
00020  *
00021  * Command line:
00022  *
00023  * check_http ipaddress sys_type
00024  * check_http ipaddress sys_type <warn_ms> <crit_ms>
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  * <warn_ms> = Number of miliseconds necessary to result in a WARNING state.
00034  * <crit_ms> = Number of miliseconds necessary to result in a CRITICAL state.
00035  *
00036  * Notes:
00037  *
00038  * If <warn_ms> and <crit_ms> are not passed on the command line, they will be 
00039  * set to the default values in the check_fs config file.
00040  *
00041  * sys_type is used to determine which config file to use to generate the
00042  * remote command.
00043  *
00044  * The configuration file /usr/local/netsaint/config/check_http/<sys_type>
00045  * contains the following values:
00046  *
00047  * HTTP_PORT|<http port on system sys_type>|
00048  * HTTP_PROTO|<protocol to use for socket>|
00049  * HTTP_EXPECT|<string to receive from remote system>|
00050  * WARN_MS|<default number of miliseconds needed to generate a warning>|
00051  * CRIT_MS|<default number of miliseconds needed to generate a critical>|
00052  *
00053  * $Log: check_http.c,v $
00054  * Revision 1.2  1999/05/17 14:35:26  netsaint
00055  * Changed the plugin to use a seperate config directory.  This directory is the
00056  * CONFIG_DIR/command_name directory.
00057  *
00058  * Revision 1.1  1999/05/14 02:49:59  netsaint
00059  * Initial revision
00060  *
00061  *
00062 */
00063 
00064 #include "/usr/local/src/netsaint/include/plugins.h"
00065 
00066 int main(int argc, char *argv[])
00067 {
00068   char command_name[MAX_CHARS];
00069   char config_file_net[MAX_CHARS];
00070   char expected[MAX_CHARS];
00071   char ip_address[MAX_CHARS];
00072   char ms_crit[MAX_CHARS];
00073   char ms_warn[MAX_CHARS];
00074   char port_http[MAX_CHARS];
00075   char protocol[MAX_CHARS];
00076   char send_buffer[MAX_CHARS];
00077   char system_name[MAX_CHARS];
00078 
00079   int crit_ms;
00080   int end_time;
00081   int http_port;
00082   int get_defaults;
00083   int micro_seconds;
00084   int result;
00085   int return_value;
00086   int seconds;
00087   int socket_name;
00088   int start_time;
00089   int total_time;
00090   int warn_ms;
00091 
00092   struct timeval end_buff;
00093   struct timeval start_buff;
00094   struct timezone end_tz;
00095   struct timezone start_tz;
00096 
00097   /* Initialize alarm signal handling */
00098 
00099   signal(SIGALRM,alarm_signal);
00100 
00101   strcpy(command_name,get_command_name(argv[0]));
00102   if(!((argc==3) || (argc==5)))
00103   {
00104     printf("\n");
00105     printf(" Incorrect number of arguments supplied\n");
00106     printf("\n");
00107     printf(" HTTP socket plugin for NetSaint\n");
00108     printf(" Copyright (c) 1999 Adam G. Bowen (agbowen@bealenet.com)\n");
00109     printf(" $Revision: 1.2 $\n");
00110     printf(" Last Modified $Date: 1999/05/17 14:35:26 $\n");
00111     printf(" License: GPL\n");
00112     printf("\n");
00113     printf(" Description:\n");
00114     printf("\n");
00115     printf(" This plugin will establish a socket on the http port of the host specified by\n");
00116     printf(" ipaddress and receives a responce from the host.  If any error occures while\n");
00117     printf(" creating the socket, receiving data from the socket or closing the socket, this\n");
00118     printf(" error will result in an alert.  The incorrect responce or responce time above\n");
00119     printf(" the threshold vaules will also cause an alert. \n");
00120     printf("\n");
00121     printf(" Usage: %s ipaddress sys_type\n",command_name);
00122     printf(" Usage: %s ipaddress sys_type <warn_ms> <crit_ms>\n",command_name);
00123     printf("\n");
00124     printf(" Required input:\n");
00125     printf("\n");
00126     printf(" ipaddress = The ipaddress of the remote system to run the check on.\n");
00127     printf(" sys_type = The remote system type.\n");
00128     printf("\n");
00129     printf(" Optional input:\n");
00130     printf("\n");
00131     printf(" <warn_ms> = Number of miliseconds necessary to result in a WARNING state.\n");
00132     printf(" <crit_ms> = Number of miliseconds necessary to result in a CRITICAL state.\n");
00133     printf("\n");
00134     printf(" If <warn_ms> and <crit_ms> are not passed on the command line, they will be\n");
00135     printf(" set to the default values in the %s config file.\n", command_name);
00136     printf("\n");
00137     printf(" sys_type is used to determine which config file to use to generate the\n");
00138     printf(" remote command.\n");
00139     printf("\n");
00140     return_value = STATE_UNKNOWN;
00141   }
00142   else
00143   {
00144     /* Set up config files and get the command line information */
00145 
00146     strcpy(ip_address,argv[1]);
00147     strcpy(system_name,argv[2]);
00148 
00149     strcpy(config_file_net,CONFIG_DIR);
00150     strcat(config_file_net,command_name);
00151     strcat(config_file_net,"/");
00152     strcat(config_file_net,system_name);
00153 
00154     if(argc == 3)
00155     {
00156       get_defaults = TRUE;
00157     }
00158     else
00159     {
00160       get_defaults = FALSE;
00161       strcpy(ms_warn,argv[3]);
00162       strcpy(ms_crit,argv[4]);
00163     }
00164 
00165     /* Check if config files exist */
00166 
00167     if (access(config_file_net, EXISTS) != 0 )
00168     {
00169       printf("Config file %s does not exist!\n",config_file_net);
00170       return_value = STATE_UNKNOWN;
00171     }
00172     else
00173     {
00174 
00175       /* Network config file variables */
00176 
00177       if((get_defaults == TRUE) && ((return_value=get_var("WARN_MS", config_file_net, ms_warn)) != STATE_OK))
00178       {
00179         printf("WARN_MS entry not found in config file %s!\n",config_file_net);
00180       }
00181       else if((get_defaults == TRUE) && ((return_value=get_var("CRIT_MS", config_file_net, ms_crit)) != STATE_OK))
00182       {
00183         printf("CRIT_MS entry not found in config file %s!\n",config_file_net);
00184       }
00185       else if((return_value=get_var("HTTP_PORT", config_file_net, port_http)) != STATE_OK)
00186       {
00187         printf("HTTP_PORT entry not found in config file %s!\n",config_file_net);
00188       }
00189       else if((return_value=get_var("HTTP_PROTO", config_file_net, protocol)) != STATE_OK)
00190       {
00191         printf("HTTP_PROTO entry not found in config file %s!\n",config_file_net);
00192       }
00193       else if((return_value=get_var("HTTP_EXPECT", config_file_net, expected)) != STATE_OK)
00194       {
00195         printf("HTTP_EXPECT entry not found in config file %s!\n",config_file_net);
00196       }
00197       else
00198       {
00199 
00200         /* Check alert level consistency */
00201 
00202         warn_ms=atoi(ms_warn);
00203         crit_ms=atoi(ms_crit);
00204         return_value = check_consistency(warn_ms, crit_ms);
00205         if(return_value == STATE_OK)
00206         {
00207 
00208           /* Check the network */
00209 
00210           http_port=atoi(port_http);
00211           gettimeofday(&start_buff, &start_tz);
00212 
00213           /* Set alarm */
00214 
00215           alarm(TIME_OUT); 
00216           if((result=open_socket(&socket_name, ip_address, http_port, protocol)) != STATE_OK)
00217           {
00218             return_value=exit_error(result,ip_address,protocol,http_port);
00219           } 
00220           else
00221           {
00222             sprintf(send_buffer,"GET / HTTP/1.0\n");
00223             if((result=send_socket(&socket_name, send_buffer)) != STATE_OK)
00224             {
00225               return_value=exit_error(result,ip_address,protocol,http_port);
00226             }
00227             else
00228             {
00229               sprintf(send_buffer,"\n");
00230               if((result=send_socket(&socket_name, send_buffer)) != STATE_OK)
00231               {
00232                 return_value=exit_error(result,ip_address,protocol,http_port);
00233               }
00234               else if((result=recv_socket(&socket_name, expected)) != STATE_OK)
00235               {
00236                 return_value=exit_error(result,ip_address,protocol,http_port);
00237               }
00238               else if((result=close_socket(&socket_name)) != STATE_OK)
00239               {
00240                 return_value=exit_error(result,ip_address,protocol,http_port);
00241               }
00242               else
00243               {
00244 
00245                 /* Nothing hungup reset alarm */
00246 
00247                 alarm(0); 
00248                 gettimeofday(&end_buff, &end_tz);
00249                 seconds=(start_buff.tv_sec%1000);
00250                 micro_seconds=start_buff.tv_usec;
00251                 start_time=((seconds * 1000) + (micro_seconds/1000));
00252                 seconds=(end_buff.tv_sec%1000);
00253                 micro_seconds=end_buff.tv_usec;
00254                 end_time=((seconds * 1000) + (micro_seconds/1000));
00255                 total_time = end_time - start_time;
00256                 if(total_time > crit_ms)
00257                 {
00258                   return_value = STATE_CRITICAL;
00259                 }
00260                 else if(total_time > warn_ms)
00261                 {
00262                   return_value = STATE_WARNING;
00263                 }
00264                 else
00265                 {
00266                   return_value = STATE_OK;
00267                 }
00268                 if(return_value == STATE_OK)
00269                 {
00270                   printf("HTTP ok - %d ms responce time.\n",total_time);
00271                 }
00272                 else
00273                 {
00274                   printf("HTTP error slow responce time - %d ms.\n",total_time);
00275                 }
00276               }
00277             }
00278           }
00279         }
00280       }
00281     }
00282   }
00283   return return_value;
00284 }