Back to index

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