diff options
Diffstat (limited to 'contrib/check_hltherm.c')
-rw-r--r-- | contrib/check_hltherm.c | 209 |
1 files changed, 209 insertions, 0 deletions
diff --git a/contrib/check_hltherm.c b/contrib/check_hltherm.c new file mode 100644 index 0000000..85c989f --- /dev/null +++ b/contrib/check_hltherm.c | |||
@@ -0,0 +1,209 @@ | |||
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 | *****************************************************************************************/ | ||
22 | |||
23 | #include "config.h" | ||
24 | #include "common.h" | ||
25 | #include "popen.h" | ||
26 | |||
27 | #define DEFAULT_TIMEOUT 10 /* default timeout in seconds */ | ||
28 | |||
29 | #define HLTHERM_COMMAND "/usr/local/bin/therm" /* this should be moved out to the configure script */ | ||
30 | |||
31 | |||
32 | static void timeout_alarm_handler(int); /* author must provide */ | ||
33 | int process_arguments(int, char **); | ||
34 | |||
35 | int timeout_interval=DEFAULT_TIMEOUT; | ||
36 | |||
37 | double wtemp=0.0L; | ||
38 | double ctemp=0.0L; | ||
39 | |||
40 | int check_lower_temps=FALSE; | ||
41 | |||
42 | char probe[MAX_INPUT_BUFFER]=""; | ||
43 | char label[MAX_INPUT_BUFFER]="Temperature"; | ||
44 | char scale[MAX_INPUT_BUFFER]="Degrees"; | ||
45 | |||
46 | FILE *fp; | ||
47 | |||
48 | |||
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; | ||
55 | |||
56 | /* process command line arguments */ | ||
57 | result=process_arguments(argc,argv); | ||
58 | |||
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 | } | ||
85 | |||
86 | |||
87 | result=STATE_OK; | ||
88 | |||
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 | } | ||
94 | |||
95 | /* handle timeouts gracefully */ | ||
96 | alarm(timeout_interval); | ||
97 | |||
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'; | ||
101 | |||
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 | } | ||
108 | |||
109 | if(fgets(input_buffer,MAX_INPUT_BUFFER-1,fp)){ | ||
110 | found=1; | ||
111 | temp=(double)atof(input_buffer); | ||
112 | } | ||
113 | |||
114 | /* close the pipe */ | ||
115 | spclose(fp); | ||
116 | |||
117 | if(result==STATE_OK){ | ||
118 | |||
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 | } | ||
136 | |||
137 | printf("Therm %s: %s = %2.1f %s\n",(result==STATE_OK)?"ok":"problem",label,temp,scale); | ||
138 | } | ||
139 | } | ||
140 | |||
141 | return result; | ||
142 | } | ||
143 | |||
144 | |||
145 | /* process command-line arguments */ | ||
146 | int process_arguments(int argc, char **argv){ | ||
147 | int x; | ||
148 | |||
149 | /* not enough options were supplied */ | ||
150 | if(argc<4) | ||
151 | return ERROR; | ||
152 | |||
153 | /* first option is always the probe name */ | ||
154 | strncpy(probe,argv[1],sizeof(probe)-1); | ||
155 | probe[sizeof(probe)-1]='\x0'; | ||
156 | |||
157 | /* 2nd and 3rd options are temperature thresholds */ | ||
158 | wtemp=(double)atof(argv[2]); | ||
159 | ctemp=(double)atof(argv[3]); | ||
160 | |||
161 | /* process all remaining arguments */ | ||
162 | for(x=5;x<=argc;x++){ | ||
163 | |||
164 | /* we got the lower temperature option */ | ||
165 | if(!strcmp(argv[x-1],"-lower")) | ||
166 | check_lower_temps=TRUE; | ||
167 | |||
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 | } | ||
178 | |||
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 | } | ||
189 | |||
190 | /* else we got something else... */ | ||
191 | else | ||
192 | return ERROR; | ||
193 | } | ||
194 | |||
195 | return OK; | ||
196 | } | ||
197 | |||
198 | |||
199 | |||
200 | /* handle timeouts gracefully... */ | ||
201 | static void timeout_alarm_handler(int signo){ | ||
202 | |||
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 | } | ||