Back to index

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