Back to index

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