check_hltherm.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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. #include "config.h"
  23. #include "common.h"
  24. #include "popen.h"
  25. #define DEFAULT_TIMEOUT 10 /* default timeout in seconds */
  26. #define HLTHERM_COMMAND "/usr/local/bin/therm" /* this should be moved out to the configure script */
  27. static void timeout_alarm_handler(int); /* author must provide */
  28. int process_arguments(int, char **);
  29. int timeout_interval=DEFAULT_TIMEOUT;
  30. double wtemp=0.0L;
  31. double ctemp=0.0L;
  32. int check_lower_temps=FALSE;
  33. char probe[MAX_INPUT_BUFFER]="";
  34. char label[MAX_INPUT_BUFFER]="Temperature";
  35. char scale[MAX_INPUT_BUFFER]="Degrees";
  36. FILE *fp;
  37. int main(int argc, char **argv){
  38. int result=STATE_OK;
  39. char command[MAX_INPUT_BUFFER];
  40. double temp=0.0L;
  41. char input_buffer[MAX_INPUT_BUFFER];
  42. int found=0;
  43. /* process command line arguments */
  44. result=process_arguments(argc,argv);
  45. /* display usage if there was a problem */
  46. if(result==ERROR){
  47. printf("Incorrect arguments supplied\n");
  48. printf("\n");
  49. printf("Hot Little Therm temperature plugin for Nagios\n");
  50. printf("Copyright (c) 1999-2002 Ethan Galstad (nagios@nagios.org)\n");
  51. printf("Last Modified: 02-28-2002\n");
  52. printf("License: GPL\n");
  53. printf("\n");
  54. printf("Usage: %s <probe> <wtemp> <ctemp> [-l label] [-s scale] [-lower]\n",argv[0]);
  55. printf("\n");
  56. printf("Options:\n");
  57. printf(" <wtemp> = Temperature necessary to result in a WARNING state\n");
  58. printf(" <ctemp> = Temperature necessary to result in a CRITICAL state\n");
  59. printf(" [label] = A descriptive label for the probe. Example: \"Outside Temp\"\n");
  60. printf(" [scale] = A descriptive label for the temperature scale. Example: \"Celsius\"\n");
  61. printf(" [-lower] = Evaluate temperatures with lower values being more critical\n");
  62. printf("\n");
  63. printf("This plugin checks the temperature of a given temperature probe on a\n");
  64. printf("Hot Little Therm digital thermometer. The plugin uses the 'therm' utility\n");
  65. printf("included with the HLT software to check the probe temperature. Both the\n");
  66. printf("HLT digital thermometer and software are produced by Spiderplant. See\n");
  67. printf("their website at http://www.spiderplant.com/hlt for more information.\n");
  68. printf("\n");
  69. return STATE_UNKNOWN;
  70. }
  71. result=STATE_OK;
  72. /* Set signal handling and alarm */
  73. if(signal(SIGALRM,timeout_alarm_handler)==SIG_ERR){
  74. printf("Cannot catch SIGALRM");
  75. return STATE_UNKNOWN;
  76. }
  77. /* handle timeouts gracefully */
  78. alarm(timeout_interval);
  79. /* create the command line we're going to use */
  80. snprintf(command,sizeof(command),"%s %s",HLTHERM_COMMAND,probe);
  81. command[sizeof(command)-1]='\x0';
  82. /* run the command to check the temperature on the probe */
  83. fp=spopen(command);
  84. if(fp==NULL){
  85. printf("Could not open pipe: %s\n",command);
  86. return STATE_UNKNOWN;
  87. }
  88. if(fgets(input_buffer,MAX_INPUT_BUFFER-1,fp)){
  89. found=1;
  90. temp=(double)atof(input_buffer);
  91. }
  92. /* close the pipe */
  93. spclose(fp);
  94. if(result==STATE_OK){
  95. if(found==0){
  96. printf("Therm problem - Could not read program output\n");
  97. result=STATE_CRITICAL;
  98. }
  99. else{
  100. if(check_lower_temps==TRUE){
  101. if(temp<=ctemp)
  102. result=STATE_CRITICAL;
  103. else if(temp<=wtemp)
  104. result=STATE_WARNING;
  105. }
  106. else{
  107. if(temp>=ctemp)
  108. result=STATE_CRITICAL;
  109. else if(temp>=wtemp)
  110. result=STATE_WARNING;
  111. }
  112. printf("Therm %s: %s = %2.1f %s\n",(result==STATE_OK)?"ok":"problem",label,temp,scale);
  113. }
  114. }
  115. return result;
  116. }
  117. /* process command-line arguments */
  118. int process_arguments(int argc, char **argv){
  119. int x;
  120. /* not enough options were supplied */
  121. if(argc<4)
  122. return ERROR;
  123. /* first option is always the probe name */
  124. strncpy(probe,argv[1],sizeof(probe)-1);
  125. probe[sizeof(probe)-1]='\x0';
  126. /* 2nd and 3rd options are temperature thresholds */
  127. wtemp=(double)atof(argv[2]);
  128. ctemp=(double)atof(argv[3]);
  129. /* process all remaining arguments */
  130. for(x=5;x<=argc;x++){
  131. /* we got the lower temperature option */
  132. if(!strcmp(argv[x-1],"-lower"))
  133. check_lower_temps=TRUE;
  134. /* we got the label */
  135. else if(!strcmp(argv[x-1],"-l")){
  136. if(x<argc){
  137. strncpy(label,argv[x],sizeof(label));
  138. label[sizeof(label)-1]='\x0';
  139. x++;
  140. }
  141. else
  142. return ERROR;
  143. }
  144. /* we got the scale */
  145. else if(!strcmp(argv[x-1],"-s")){
  146. if(x<argc){
  147. strncpy(scale,argv[x],sizeof(scale));
  148. scale[sizeof(scale)-1]='\x0';
  149. x++;
  150. }
  151. else
  152. return ERROR;
  153. }
  154. /* else we got something else... */
  155. else
  156. return ERROR;
  157. }
  158. return OK;
  159. }
  160. /* handle timeouts gracefully... */
  161. static void timeout_alarm_handler(int signo){
  162. if(signo==SIGALRM){
  163. kill(childpid[fileno(fp)],SIGKILL);
  164. printf("Therm problem - Check timed out after %d seconds\n",timeout_interval);
  165. exit(STATE_CRITICAL);
  166. }
  167. }