Back to index

nagios-plugins  1.4.16
check_hltherm.c
Go to the documentation of this file.
00001 /******************************************************************************************
00002  *
00003  * CHECK_HLTHERM.C
00004  *
00005  * Program: Hot Little Therm temperature plugin for Nagios
00006  * License: GPL
00007  * Copyright (c) 1999-2002 Ethan Galstad (nagios@nagios.org)
00008  *
00009  * Last Modified: 02-28-2002
00010  *
00011  * Command line: check_hltherm <probe> <wtemp> <ctemp> [-l label] [-s scale] [-lower]
00012  *
00013  * Description:
00014  *
00015  * This plugin checks the temperature of a given temperature probe on a
00016  * Hot Little Therm digital thermometer.  The plugin uses the 'therm' utility
00017  * that is included with the HLT software to check the probe temperature.  Both
00018  * the HLT digital thermometer and software are produced by Spiderplant. See
00019  * their website at http://www.spiderplant.com/hlt for more information.
00020  *
00021  *****************************************************************************************/
00022 
00023 #include "config.h"
00024 #include "common.h"
00025 #include "popen.h"
00026 
00027 #define DEFAULT_TIMEOUT         10 /* default timeout in seconds */
00028 
00029 #define HLTHERM_COMMAND          "/usr/local/bin/therm"     /* this should be moved out to the configure script */
00030 
00031 
00032 static void timeout_alarm_handler(int); /* author must provide */
00033 int process_arguments(int, char **);
00034 
00035 int timeout_interval=DEFAULT_TIMEOUT;
00036 
00037 double wtemp=0.0L;
00038 double ctemp=0.0L;
00039 
00040 int check_lower_temps=FALSE;
00041 
00042 char probe[MAX_INPUT_BUFFER]="";
00043 char label[MAX_INPUT_BUFFER]="Temperature";
00044 char scale[MAX_INPUT_BUFFER]="Degrees";
00045 
00046 FILE *fp;
00047 
00048 
00049 int main(int argc, char **argv){
00050        int result=STATE_OK;
00051        char command[MAX_INPUT_BUFFER];
00052        double temp=0.0L;
00053        char input_buffer[MAX_INPUT_BUFFER];
00054        int found=0;
00055 
00056        /* process command line arguments */
00057        result=process_arguments(argc,argv);
00058 
00059        /* display usage if there was a problem */
00060        if(result==ERROR){
00061               printf("Incorrect arguments supplied\n");
00062               printf("\n");
00063               printf("Hot Little Therm temperature plugin for Nagios\n");
00064               printf("Copyright (c) 1999-2002 Ethan Galstad (nagios@nagios.org)\n");
00065               printf("Last Modified: 02-28-2002\n");
00066               printf("License: GPL\n");
00067               printf("\n");
00068               printf("Usage: %s <probe> <wtemp> <ctemp> [-l label] [-s scale] [-lower]\n",argv[0]);
00069               printf("\n");
00070               printf("Options:\n");
00071               printf(" <wtemp>  = Temperature necessary to result in a WARNING state\n");
00072               printf(" <ctemp>  = Temperature necessary to result in a CRITICAL state\n");
00073               printf(" [label]  = A descriptive label for the probe.  Example: \"Outside Temp\"\n");
00074               printf(" [scale]  = A descriptive label for the temperature scale.  Example: \"Celsius\"\n");
00075               printf(" [-lower] = Evaluate temperatures with lower values being more critical\n");
00076               printf("\n");
00077               printf("This plugin checks the temperature of a given temperature probe on a\n");
00078               printf("Hot Little Therm digital thermometer.  The plugin uses the 'therm' utility\n");
00079               printf("included with the HLT software to check the probe temperature.  Both the\n");
00080               printf("HLT digital thermometer and software are produced by Spiderplant. See\n");
00081               printf("their website at http://www.spiderplant.com/hlt for more information.\n");
00082               printf("\n");
00083               return STATE_UNKNOWN;
00084                }
00085 
00086 
00087        result=STATE_OK;
00088 
00089        /* Set signal handling and alarm */
00090        if(signal(SIGALRM,timeout_alarm_handler)==SIG_ERR){
00091               printf("Cannot catch SIGALRM");
00092               return STATE_UNKNOWN;
00093                }
00094 
00095        /* handle timeouts gracefully */
00096        alarm(timeout_interval);
00097 
00098        /* create the command line we're going to use */
00099        snprintf(command,sizeof(command),"%s %s",HLTHERM_COMMAND,probe);
00100        command[sizeof(command)-1]='\x0';
00101 
00102        /* run the command to check the temperature on the probe */
00103        fp=spopen(command);
00104        if(fp==NULL){
00105               printf("Could not open pipe: %s\n",command);
00106               return STATE_UNKNOWN;
00107                }
00108 
00109        if(fgets(input_buffer,MAX_INPUT_BUFFER-1,fp)){
00110               found=1;
00111               temp=(double)atof(input_buffer);
00112                }
00113 
00114        /* close the pipe */
00115        spclose(fp);
00116 
00117        if(result==STATE_OK){
00118 
00119               if(found==0){
00120                      printf("Therm problem - Could not read program output\n");
00121                      result=STATE_CRITICAL;
00122                       }
00123               else{
00124                      if(check_lower_temps==TRUE){
00125                             if(temp<=ctemp)
00126                                    result=STATE_CRITICAL;
00127                             else if(temp<=wtemp)
00128                                    result=STATE_WARNING;
00129                              }
00130                      else{
00131                             if(temp>=ctemp)
00132                                    result=STATE_CRITICAL;
00133                             else if(temp>=wtemp)
00134                                    result=STATE_WARNING;
00135                              }
00136 
00137                      printf("Therm %s: %s = %2.1f %s\n",(result==STATE_OK)?"ok":"problem",label,temp,scale);
00138                       }
00139                }
00140 
00141        return result;
00142         }
00143 
00144 
00145 /* process command-line arguments */
00146 int process_arguments(int argc, char **argv){
00147        int x;
00148 
00149        /* not enough options were supplied */
00150        if(argc<4)
00151               return ERROR;
00152 
00153        /* first option is always the probe name */
00154        strncpy(probe,argv[1],sizeof(probe)-1);
00155        probe[sizeof(probe)-1]='\x0';
00156 
00157        /* 2nd and 3rd options are temperature thresholds */
00158        wtemp=(double)atof(argv[2]);
00159        ctemp=(double)atof(argv[3]);
00160 
00161        /* process all remaining arguments */
00162        for(x=5;x<=argc;x++){
00163 
00164               /* we got the lower temperature option */
00165               if(!strcmp(argv[x-1],"-lower"))
00166                      check_lower_temps=TRUE;
00167 
00168               /* we got the label */
00169               else if(!strcmp(argv[x-1],"-l")){
00170                      if(x<argc){
00171                             strncpy(label,argv[x],sizeof(label));
00172                             label[sizeof(label)-1]='\x0';
00173                             x++;
00174                              }
00175                      else
00176                             return ERROR;
00177                       }
00178 
00179               /* we got the scale */
00180               else if(!strcmp(argv[x-1],"-s")){
00181                      if(x<argc){
00182                             strncpy(scale,argv[x],sizeof(scale));
00183                             scale[sizeof(scale)-1]='\x0';
00184                             x++;
00185                              }
00186                      else
00187                             return ERROR;
00188                       }
00189 
00190               /* else we got something else... */
00191               else
00192                      return ERROR;
00193                }
00194 
00195        return OK;
00196         }
00197 
00198 
00199 
00200 /* handle timeouts gracefully... */
00201 static void timeout_alarm_handler(int signo){
00202 
00203        if(signo==SIGALRM){
00204     
00205               kill(childpid[fileno(fp)],SIGKILL);
00206               printf("Therm problem - Check timed out after %d seconds\n",timeout_interval);
00207               exit(STATE_CRITICAL);
00208                }
00209         }