Code

Fix for regex input of '|', being output causing problems with Nagios' parsing of
[nagiosplug.git] / contrib / check_hltherm.c
1 /******************************************************************************************
2  *
3  * CHECK_HLTHERM.C
4  *
5  * Program: Hot Little Therm temperature plugin for Nagios
6  * License: GPL
7  * Copyright (c) 1999-2002 Ethan Galstad (nagios@nagios.org)
8  *
9  * Last Modified: 02-28-2002
10  *
11  * Command line: check_hltherm <probe> <wtemp> <ctemp> [-l label] [-s scale] [-lower]
12  *
13  * Description:
14  *
15  * This plugin checks the temperature of a given temperature probe on a
16  * Hot Little Therm digital thermometer.  The plugin uses the 'therm' utility
17  * that is included with the HLT software to check the probe temperature.  Both
18  * the HLT digital thermometer and software are produced by Spiderplant. See
19  * their website at http://www.spiderplant.com/hlt for more information.
20  *
21  *****************************************************************************************/
23 #include "config.h"
24 #include "common.h"
25 #include "popen.h"
27 #define DEFAULT_TIMEOUT         10      /* default timeout in seconds */
29 #define HLTHERM_COMMAND          "/usr/local/bin/therm"     /* this should be moved out to the configure script */
32 static void timeout_alarm_handler(int); /* author must provide */
33 int process_arguments(int, char **);
35 int timeout_interval=DEFAULT_TIMEOUT;
37 double wtemp=0.0L;
38 double ctemp=0.0L;
40 int check_lower_temps=FALSE;
42 char probe[MAX_INPUT_BUFFER]="";
43 char label[MAX_INPUT_BUFFER]="Temperature";
44 char scale[MAX_INPUT_BUFFER]="Degrees";
46 FILE *fp;
49 int main(int argc, char **argv){
50         int result=STATE_OK;
51         char command[MAX_INPUT_BUFFER];
52         double temp=0.0L;
53         char input_buffer[MAX_INPUT_BUFFER];
54         int found=0;
56         /* process command line arguments */
57         result=process_arguments(argc,argv);
59         /* display usage if there was a problem */
60         if(result==ERROR){
61                 printf("Incorrect arguments supplied\n");
62                 printf("\n");
63                 printf("Hot Little Therm temperature plugin for Nagios\n");
64                 printf("Copyright (c) 1999-2002 Ethan Galstad (nagios@nagios.org)\n");
65                 printf("Last Modified: 02-28-2002\n");
66                 printf("License: GPL\n");
67                 printf("\n");
68                 printf("Usage: %s <probe> <wtemp> <ctemp> [-l label] [-s scale] [-lower]\n",argv[0]);
69                 printf("\n");
70                 printf("Options:\n");
71                 printf(" <wtemp>  = Temperature necessary to result in a WARNING state\n");
72                 printf(" <ctemp>  = Temperature necessary to result in a CRITICAL state\n");
73                 printf(" [label]  = A descriptive label for the probe.  Example: \"Outside Temp\"\n");
74                 printf(" [scale]  = A descriptive label for the temperature scale.  Example: \"Celsius\"\n");
75                 printf(" [-lower] = Evaluate temperatures with lower values being more critical\n");
76                 printf("\n");
77                 printf("This plugin checks the temperature of a given temperature probe on a\n");
78                 printf("Hot Little Therm digital thermometer.  The plugin uses the 'therm' utility\n");
79                 printf("included with the HLT software to check the probe temperature.  Both the\n");
80                 printf("HLT digital thermometer and software are produced by Spiderplant. See\n");
81                 printf("their website at http://www.spiderplant.com/hlt for more information.\n");
82                 printf("\n");
83                 return STATE_UNKNOWN;
84                 }
87         result=STATE_OK;
89         /* Set signal handling and alarm */
90         if(signal(SIGALRM,timeout_alarm_handler)==SIG_ERR){
91                 printf("Cannot catch SIGALRM");
92                 return STATE_UNKNOWN;
93                 }
95         /* handle timeouts gracefully */
96         alarm(timeout_interval);
98         /* create the command line we're going to use */
99         snprintf(command,sizeof(command),"%s %s",HLTHERM_COMMAND,probe);
100         command[sizeof(command)-1]='\x0';
102         /* run the command to check the temperature on the probe */
103         fp=spopen(command);
104         if(fp==NULL){
105                 printf("Could not open pipe: %s\n",command);
106                 return STATE_UNKNOWN;
107                 }
109         if(fgets(input_buffer,MAX_INPUT_BUFFER-1,fp)){
110                 found=1;
111                 temp=(double)atof(input_buffer);
112                 }
114         /* close the pipe */
115         spclose(fp);
117         if(result==STATE_OK){
119                 if(found==0){
120                         printf("Therm problem - Could not read program output\n");
121                         result=STATE_CRITICAL;
122                         }
123                 else{
124                         if(check_lower_temps==TRUE){
125                                 if(temp<=ctemp)
126                                         result=STATE_CRITICAL;
127                                 else if(temp<=wtemp)
128                                        result=STATE_WARNING;
129                                 }
130                         else{
131                                 if(temp>=ctemp)
132                                         result=STATE_CRITICAL;
133                                 else if(temp>=wtemp)
134                                         result=STATE_WARNING;
135                                 }
137                         printf("Therm %s: %s = %2.1f %s\n",(result==STATE_OK)?"ok":"problem",label,temp,scale);
138                         }
139                 }
141         return result;
142         }
145 /* process command-line arguments */
146 int process_arguments(int argc, char **argv){
147         int x;
149         /* not enough options were supplied */
150         if(argc<4)
151                 return ERROR;
153         /* first option is always the probe name */
154         strncpy(probe,argv[1],sizeof(probe)-1);
155         probe[sizeof(probe)-1]='\x0';
157         /* 2nd and 3rd options are temperature thresholds */
158         wtemp=(double)atof(argv[2]);
159         ctemp=(double)atof(argv[3]);
161         /* process all remaining arguments */
162         for(x=5;x<=argc;x++){
164                 /* we got the lower temperature option */
165                 if(!strcmp(argv[x-1],"-lower"))
166                         check_lower_temps=TRUE;
168                 /* we got the label */
169                 else if(!strcmp(argv[x-1],"-l")){
170                         if(x<argc){
171                                 strncpy(label,argv[x],sizeof(label));
172                                 label[sizeof(label)-1]='\x0';
173                                 x++;
174                                 }
175                         else
176                                 return ERROR;
177                         }
179                 /* we got the scale */
180                 else if(!strcmp(argv[x-1],"-s")){
181                         if(x<argc){
182                                 strncpy(scale,argv[x],sizeof(scale));
183                                 scale[sizeof(scale)-1]='\x0';
184                                 x++;
185                                 }
186                         else
187                                 return ERROR;
188                         }
190                 /* else we got something else... */
191                 else
192                         return ERROR;
193                 }
195         return OK;
196         }
200 /* handle timeouts gracefully... */
201 static void timeout_alarm_handler(int signo){
203         if(signo==SIGALRM){
204     
205                 kill(childpid[fileno(fp)],SIGKILL);
206                 printf("Therm problem - Check timed out after %d seconds\n",timeout_interval);
207                 exit(STATE_CRITICAL);
208                 }
209         }