Back to index

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