Back to index

nagios-plugins  1.4.16
plugins.h
Go to the documentation of this file.
00001 /*
00002         Source: plugins.h 
00003         Author: Adam G. Bowen (agbowen@bealenet.com) 
00004         $Revision: 1.9 $
00005         $Date: 1999/05/17 14:26:35 $
00006 
00007 
00008  * Header file for plugins for Netsaint program.
00009  *
00010  * $Log: plugins.h,v $
00011  * Revision 1.9  1999/05/17 14:26:35  netsaint
00012  * Added #define CHECK_TELNET.
00013  *
00014  * Revision 1.8  1999/05/14 02:24:04  netsaint
00015  * Changed the get_var subroutine to:
00016  *     int get_var(char *LINE_NAME, char *file_name, char *var_name)
00017  * LINE_NAME = values to look for in the file.
00018  * file_name = the file to search.
00019  * var_name = pointer to variable in calling program to copy data.
00020  * Changed the check_network subroutine into 4 new subroutines:
00021  *     open_socket
00022  *     send_socket
00023  *     recv_socket
00024  *     close_socket
00025  * This will allow more felxibility of the socket functions.
00026  *
00027  * Revision 1.7  1999/05/07 15:21:52  netsaint
00028  * Changed the exit_error subroutine from type void to type int.  This
00029  * subroutine will now return an int value.
00030  * Added the check_output_file subroutine.  This subroutine takes a pointer to
00031  * a file name as an argument and checks if the file exists, if the file is
00032  * readable, if the file is not zero size, and if the file can be opened.  If
00033  * any of these test fail, the subrotuine returns with a STATE_UNKNOWN error.
00034  * Added subroutine get_command_name.  This subroutine takes the command_name
00035  * (argv[0]) as an argument and strips off all leading directories and returns
00036  * a pointer to the command_name.
00037  * Added subroutine check_consistency.  This subroutine takes 2 integers as
00038  * arguments.  The first is warn_value, the second is critical_value.  The
00039  * subroutine checks that warn_value < critical_value.  If this test fails,
00040  * the subroutine returns a STATE_UNKNOWN error.
00041  * Added the following includes:
00042  *      <ctype.h>
00043  *      <time.h>
00044  *
00045  * Revision 1.6  1999/05/06 15:13:34  netsaint
00046  * Corrected typos in error messages.
00047  *
00048  * Revision 1.5  1999/05/03 14:56:49  netsaint
00049  * Added the #define TIME_OUT statement.  TIME_OUT is used when setting the
00050  * alarm prior to attempting a network connection.  Also added the exit_error
00051  * subroutine.  This is used to print error messages recived while attempting
00052  * network connections.
00053  *
00054  * Revision 1.4  1999/04/29 18:19:25  netsaint
00055  * Added subroutine alarm_signal, this is ued to trap the SIGALRM signal.
00056  * Added check_net subroutine, this is used by plugins to make a socket
00057  * connection to a remote host before running the rsh command.  This should
00058  * prevent problems with plugins hanging on down remote hosts.  Added the
00059  * print_error subroutine.  This will print the contents of the error file
00060  * if the file exists and is larger than zero.
00061  *
00062  * Revision 1.3  1999/04/27 17:24:00  netsaint
00063  * Added #define SUNDAY 0 and subroutine strupr to convert string to upper case.
00064  *
00065  * Revision 1.2  1999/04/23 16:05:01  netsaint
00066  * Added #define statement for CONFIG_DIR.
00067  *
00068  * Revision 1.1  1999/04/23 13:17:54  netsaint
00069  * Initial revision
00070  *
00071 */
00072 
00073 #include <ctype.h>
00074 #include <errno.h>
00075 #include <netdb.h>
00076 #include <signal.h>
00077 #include <stdio.h>
00078 #include <stdlib.h>
00079 #include <string.h>
00080 #include <time.h>
00081 #include <unistd.h>
00082 #include <arpa/inet.h>
00083 #include <netinet/in.h>
00084 #include <sys/socket.h>
00085 #include <sys/stat.h>
00086 #include <sys/time.h>
00087 #include <sys/timeb.h>
00088 #include <sys/types.h>
00089 #include <sys/wait.h>
00090 
00091 /* service state return codes */
00092 
00093 #define STATE_CRITICAL 2
00094 #define STATE_WARNING  1
00095 #define STATE_OK       0
00096 #define STATE_UNKNOWN  -1
00097 
00098 #define TRUE           1
00099 #define FALSE          0
00100 #define EXISTS         4
00101 #define READABLE       6
00102 
00103 #define SATURDAY       6
00104 #define SUNDAY         0
00105 
00106 #define TIME_OUT       5
00107 
00108 #define MAX_CHARS      256
00109 
00110 #define CONFIG_DIR     "/usr/local/netsaint/config/"
00111 #define CHECK_TELNET   "check_telnet"
00112 
00113 int get_var(char *LINE_NAME, char *file_name, char *var_name)
00114 {
00115   char temp_input_buffer[MAX_CHARS];
00116   char tok_sep[] = "|";
00117   char temp_var_name[MAX_CHARS];
00118   char *temp_var_value;
00119 
00120   FILE *temp_fp;
00121 
00122   int return_value = STATE_UNKNOWN;
00123 
00124   strcpy(var_name,"NOT_FOUND");
00125 
00126   temp_fp=fopen(file_name,"r");
00127   while(fgets(temp_input_buffer,MAX_CHARS-1,temp_fp) != NULL)
00128   {
00129          strcpy(temp_var_name,strtok(temp_input_buffer,tok_sep));
00130          temp_var_value=strtok(NULL,tok_sep);
00131          if(strcmp(LINE_NAME,temp_var_name) == 0)
00132          {
00133                 strcpy(var_name,temp_var_value);
00134                 return_value = STATE_OK;
00135          }
00136   }
00137   return return_value;
00138 }
00139 void strupr (char *string_name)
00140 {
00141   do
00142   {
00143          *string_name=toupper(*string_name);
00144   }
00145   while(*string_name++);
00146   return;
00147 }
00148 void alarm_signal(int sig_num)
00149 {
00150   printf("No response received from host in %d seconds, possible host down or network error.\n",TIME_OUT);
00151   exit(STATE_CRITICAL);
00152 }
00153 int open_socket(int * socket_name, char *host, int port_num, char *protocol)
00154 {
00155   int return_value = STATE_OK;
00156 
00157   struct protoent *proto_ptr;
00158   struct sockaddr_in remote_addr;
00159   struct hostent *host_addr;
00160 
00161   if(isdigit(host[0]))
00162   {
00163     inet_aton(host, &remote_addr.sin_addr);
00164   }
00165   else
00166   {
00167     if((host_addr = gethostbyname(host)) == NULL)
00168     {
00169       switch(errno)
00170       {
00171         case HOST_NOT_FOUND:
00172           return_value = 1;
00173           break;
00174         case TRY_AGAIN:
00175           return_value = 2;
00176           break;
00177         case NO_RECOVERY:
00178           return_value = 3;
00179           break;
00180         case NO_ADDRESS:
00181           return_value = 4;
00182           break;
00183         default:
00184           return_value = 5;
00185       }
00186     }
00187     else
00188     {
00189       memcpy(&remote_addr.sin_addr, host_addr->h_addr, host_addr->h_length);
00190     }
00191   }
00192 
00193   remote_addr.sin_family=AF_INET;
00194   remote_addr.sin_port=htons(port_num);
00195 
00196   if((proto_ptr = getprotobyname(protocol)) == NULL)
00197   {
00198     return_value = 6;
00199   }
00200   if((*socket_name=socket(AF_INET,SOCK_STREAM,proto_ptr->p_proto)) < 0)
00201   {
00202     return_value = 7;
00203   }
00204   if((connect(*socket_name,(struct sockaddr *)&remote_addr,sizeof(remote_addr))) < 0)
00205   {
00206     switch(errno)
00207     {
00208       case ECONNREFUSED:
00209         return_value = 8;
00210         break;
00211       case ETIMEDOUT:
00212         return_value = 9;
00213         break;
00214       case ENETUNREACH:
00215         return_value = 10;
00216         break;
00217       default:
00218         return_value = 11;
00219         break;
00220     }
00221     close(*socket_name);
00222   }
00223   return return_value;
00224 }
00225 int recv_socket(int *socket_name, char *expected)
00226 {
00227   char input_buffer[MAX_CHARS];
00228 
00229   int return_value = STATE_OK;
00230   if((recv(*socket_name,&input_buffer,MAX_CHARS-1,0)) < 0)
00231   {
00232     return_value = 12;
00233   }
00234   if(strstr(input_buffer,expected) == NULL)
00235   {
00236     return_value = 13;
00237   }
00238   return return_value;
00239 }
00240 int send_socket(int *socket_name, char *send_buffer)
00241 {
00242   int return_value = STATE_OK;
00243   if (strcmp(send_buffer,"\n") == 0)
00244   {
00245     if((send(*socket_name,"\n",strlen("\n"),0)) <0)
00246     {
00247       return_value = 14;
00248     }
00249   }
00250   else
00251   {
00252     if((send(*socket_name,&send_buffer,strlen(send_buffer),0)) <0)
00253     {
00254       return_value = 14;
00255     }
00256   }
00257   return return_value;
00258 }
00259 int close_socket(int *socket_name)
00260 {
00261   close(*socket_name);
00262   return STATE_OK;
00263 }
00264 void print_error(char *file_name)
00265 {
00266   char input_buffer[MAX_CHARS];
00267 
00268   FILE *temp_fp;
00269   
00270   struct stat file_stat;
00271 
00272   if (access(file_name, READABLE) == 0 )
00273   {
00274     if(stat(file_name, &file_stat) == 0)
00275     {
00276       if(file_stat.st_size != 0)
00277       {
00278         if((temp_fp=fopen(file_name,"r")) != NULL)
00279         {
00280           printf("The following errors were produced:  ");
00281           while(fgets(input_buffer,MAX_CHARS-1,temp_fp) != NULL)
00282           {
00283             printf("%s",input_buffer);
00284           }
00285           fclose(temp_fp);
00286         }
00287       }
00288     }
00289   }
00290   printf("\n");
00291   return;
00292 }
00293 int exit_error(int error_code, char *ip_address, char *protocol, int port)
00294 {
00295 
00296   int return_value;
00297 
00298   switch(error_code)
00299   {
00300     case 1:
00301       printf("Hostname %s not found\n",ip_address);
00302       return_value=STATE_WARNING;
00303       break;
00304     case 2:
00305       printf("Hostname %s not found, but try again\n",ip_address);
00306       return_value=STATE_WARNING;
00307       break;
00308     case 3:
00309       printf("DNS error looking up hostname %s\n",ip_address);
00310       return_value=STATE_WARNING;
00311       break;
00312     case 4:
00313       printf("No ip address for host %s\n",ip_address);
00314       return_value=STATE_WARNING;
00315       break;
00316     case 5:
00317       printf("Unknow host name lookup error\n");
00318       return_value=STATE_WARNING;
00319       break;
00320     case 6:
00321       printf("Unable to map protocol %s\n",protocol);
00322       return_value=STATE_WARNING;
00323       break;
00324     case 7:
00325       printf("Unable to create socket to host %s on port %d\n",ip_address,port);
00326       return_value=STATE_CRITICAL;
00327       break;
00328     case 8:
00329       printf("Connection refused by %s on port %d\n",ip_address,port);
00330       return_value=STATE_CRITICAL;
00331       break;
00332     case 9:
00333       printf("Timed out while creating socket to host %s on port %d\n",ip_address,port);
00334       return_value=STATE_CRITICAL;
00335       break;
00336     case 10:
00337       printf("Network unreachable for host %s\n",ip_address);
00338       return_value=STATE_CRITICAL;
00339       break;
00340     case 11:
00341       printf("Unknown connect error to %s on port %d\n",ip_address,port);
00342       return_value=STATE_CRITICAL;
00343       break;
00344     case 12:
00345       printf("Could not read form port %s on host %d\n",ip_address,port);
00346       return_value=STATE_CRITICAL;
00347       break;
00348     case 13:
00349       printf("Invalid response string received from host %s on port %d\n",ip_address,port);
00350       return_value=STATE_WARNING;
00351       break;
00352     case 14:
00353       printf("Error occured while sending data to host %s on port %d\n",ip_address,port);
00354       return_value=STATE_WARNING;
00355       break;
00356     default:
00357       printf("Unknown error received from host %s on port %d\n",ip_address,port);
00358       return_value=STATE_UNKNOWN;
00359   }
00360   return return_value;
00361 }
00362 int check_output_file(char *file_name)
00363 {
00364 
00365   FILE *temp_fp;
00366 
00367   int return_value;
00368 
00369   struct stat file_stat;
00370 
00371   if (access(file_name, READABLE) != 0 )
00372   {
00373     printf("Unable to read output file for system.  ");
00374     return_value = STATE_UNKNOWN;
00375   }
00376   else if(stat(file_name, &file_stat) != 0)
00377   {
00378     printf("Unable to retrive status of output file %s.  ",file_name);
00379     return_value = STATE_UNKNOWN;
00380   }
00381   else if(file_stat.st_size == 0)
00382   {
00383     printf("Output file %s has zero size.  ",file_name);
00384     return_value = STATE_UNKNOWN;
00385   }
00386   else if((temp_fp=fopen(file_name,"r")) == NULL)
00387   {
00388     printf("Unable to open output file for system.  ");
00389     return_value = STATE_UNKNOWN;
00390   }
00391   else
00392   {
00393     fclose(temp_fp);
00394     return_value = STATE_OK;
00395   }
00396   return return_value;
00397 }
00398 char *get_command_name(char *command_line)
00399 {
00400   char *return_string;
00401   char *sub_string;
00402   if((sub_string=strrchr(command_line,'/')) != NULL)
00403   {
00404     return_string=++sub_string;
00405   }
00406   else
00407   {
00408     return_string=command_line;
00409   }
00410   return return_string;
00411 }
00412 int check_consistency(float warn_value, float critical_value)
00413 {
00414 
00415   int return_value;
00416 
00417   if(warn_value >= critical_value)
00418   {
00419     printf("Inconsistency in parameters: warning value %.2f >= critical value %.2f.\n", warn_value, critical_value);
00420     return_value = STATE_UNKNOWN;
00421   }
00422   else
00423   {
00424     return_value = STATE_OK;
00425   }
00426   return return_value;
00427 }