check_digitemp.pl 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. #!/usr/bin/perl -w
  2. # check_digitemp.pl Copyright (C) 2002 by Brian C. Lane <bcl@brianlane.com>
  3. #
  4. # This is a NetSaint plugin script to check the temperature on a local
  5. # machine. Remote usage may be possible with SSH
  6. #
  7. # Permission is hereby granted, free of charge, to any person obtaining a copy
  8. # of this software and associated documentation files (the "Software"), to
  9. # deal in the Software without restriction, including without limitation the
  10. # rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  11. # sell copies of the Software, and to permit persons to whom the Software is
  12. # furnished to do so, subject to the following conditions:
  13. #
  14. # The above copyright notice and this permission notice shall be included in
  15. # all copies or substantial portions of the Software.
  16. #
  17. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18. # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19. # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  20. # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  21. # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  22. # FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  23. # IN THE SOFTWARE.
  24. #
  25. # ===========================================================================
  26. # Howto Install in NetSaint (tested with v0.0.7)
  27. #
  28. # 1. Copy this script to /usr/local/netsaint/libexec/ or wherever you have
  29. # placed your NetSaint plugins
  30. #
  31. # 2. Create a digitemp config file in /usr/local/netsaint/etc/
  32. # eg. digitemp -i -s/dev/ttyS0 -c /usr/local/netsaint/etc/digitemp.conf
  33. #
  34. # 3. Make sure that the webserver user has permission to access the serial
  35. # port being used.
  36. #
  37. # 4. Add a command to /usr/local/netsaint/etc/commands.cfg like this:
  38. # command[check-temp]=$USER1$/check_digitemp.pl -w $ARG1$ -c $ARG2$ \
  39. # -t $ARG3$ -f $ARG4$
  40. # (fold into one line)
  41. #
  42. # 5. Tell NetSaint to monitor the temperature by adding a service line like
  43. # this to your hosts.cfg file:
  44. # service[kermit]=Temperature;0;24x7;3;5;1;home-admins;120;24x7;1;1;1;; \
  45. # check-temp!65!75!1!/usr/local/netsaint/etc/digitemp.conf
  46. # (fold into one line)
  47. # 65 is the warning temperature
  48. # 75 is the critical temperature
  49. # 1 is the sensor # (as reported by digitemp -a) to monitor
  50. # digitemp.conf is the path to the config file
  51. #
  52. # 6. If you use Centigrade instead of Fahrenheit, change the commands.cfg
  53. # line to include the -C argument. You can then pass temperature limits in
  54. # Centigrade in the service line.
  55. #
  56. # ===========================================================================
  57. # Howto Install in Nagios (tested with v1.0b4)
  58. #
  59. # 1. Copy this script to /usr/local/nagios/libexec/ or wherever you have
  60. # placed your Nagios plugins
  61. #
  62. # 2. Create a digitemp config file in /usr/local/nagios/etc/
  63. # eg. digitemp -i -s/dev/ttyS0 -c /usr/local/nagios/etc/digitemp.conf
  64. #
  65. # 3. Make sure that the webserver user has permission to access the serial
  66. # port being used.
  67. #
  68. # 4. Add a command to /usr/local/nagios/etc/checkcommands.cfg like this:
  69. #
  70. # #DigiTemp temperature check command
  71. # define command{
  72. # command_name check_temperature
  73. # command_line $USER1$/check_digitemp.pl -w $ARG1$ -c $ARG2$ \
  74. # -t $ARG3$ -f $ARG4$
  75. # (fold above into one line)
  76. # }
  77. #
  78. # 5. Tell NetSaint to monitor the temperature by adding a service line like
  79. # this to your service.cfg file:
  80. #
  81. # #DigiTemp Temperature check Service definition
  82. # define service{
  83. # use generic-service
  84. # host_name kermit
  85. # service_description Temperature
  86. # is_volatile 0
  87. # check_period 24x7
  88. # max_check_attempts 3
  89. # normal_check_interval 5
  90. # retry_check_interval 2
  91. # contact_groups home-admins
  92. # notification_interval 240
  93. # notification_period 24x7
  94. # notification_options w,u,c,r
  95. # check_command check_temperature!65!75!1! \
  96. # /usr/local/nagios/etc/digitemp.conf
  97. # (fold into one line)
  98. # }
  99. #
  100. # 65 is the warning temperature
  101. # 75 is the critical temperature
  102. # 1 is the sensor # (as reported by digitemp -a) to monitor
  103. # digitemp.conf is the path to the config file
  104. #
  105. # 6. If you use Centigrade instead of Fahrenheit, change the checkcommands.cfg
  106. # line to include the -C argument. You can then pass temperature limits in
  107. # Centigrade in the service line.
  108. #
  109. # ===========================================================================
  110. # Modules to use
  111. use strict;
  112. use Getopt::Std;
  113. # Define all our variable usage
  114. use vars qw($opt_c $opt_f $opt_t $opt_w $opt_F $opt_C
  115. $temperature $conf_file $sensor $temp_fmt
  116. $crit_level $warn_level $null
  117. %exit_codes
  118. $percent $fmt_pct
  119. $verb_err $command_line);
  120. # Predefined exit codes for NetSaint
  121. %exit_codes = ('UNKNOWN' ,-1,
  122. 'OK' , 0,
  123. 'WARNING' , 1,
  124. 'CRITICAL', 2,);
  125. # Default to Fahrenheit input and result (use -C to change this)
  126. $temp_fmt = 3;
  127. # Get the options
  128. if ($#ARGV le 0)
  129. {
  130. &usage;
  131. } else {
  132. getopts('f:t:FCc:w:');
  133. }
  134. # Shortcircuit the switches
  135. if (!$opt_w or $opt_w == 0 or !$opt_c or $opt_c == 0)
  136. {
  137. print "*** You must define WARN and CRITICAL levels!";
  138. &usage;
  139. }
  140. # Check if levels are sane
  141. if ($opt_w >= $opt_c)
  142. {
  143. print "*** WARN level must not be greater than CRITICAL when checking temperature!";
  144. &usage;
  145. }
  146. $warn_level = $opt_w;
  147. $crit_level = $opt_c;
  148. # Default sensor to read is #0
  149. if(!$opt_t)
  150. {
  151. $sensor = 0;
  152. } else {
  153. $sensor = $opt_t;
  154. }
  155. # Default config file is /etc/digitemp.conf
  156. if(!$opt_f)
  157. {
  158. $conf_file = "/etc/digitemp.conf";
  159. } else {
  160. $conf_file = $opt_f;
  161. }
  162. # Check for config file
  163. if( !-f $conf_file ) {
  164. print "*** You must have a digitemp.conf file\n";
  165. &usage;
  166. }
  167. if($opt_C)
  168. {
  169. $temp_fmt = 2;
  170. }
  171. # Read the output from digitemp
  172. # Output in form 0\troom\tattic\tdrink
  173. open( DIGITEMP, "/usr/local/bin/digitemp -c $conf_file -t $sensor -q -o $temp_fmt |" );
  174. # Process the output from the command
  175. while( <DIGITEMP> )
  176. {
  177. # print "$_\n";
  178. chomp;
  179. if( $_ =~ /^nanosleep/i )
  180. {
  181. print "Error reading sensor #$sensor\n";
  182. close(DIGITEMP);
  183. exit $exit_codes{'UNKNOWN'};
  184. } else {
  185. # Check for an error from digitemp, and report it instead
  186. if( $_ =~ /^Error.*/i ) {
  187. print $_;
  188. close(DIGITEMP);
  189. exit $exit_codes{'UNKNOWN'};
  190. } else {
  191. ($null,$temperature) = split(/\t/);
  192. }
  193. }
  194. }
  195. close( DIGITEMP );
  196. if( $temperature and $temperature >= $crit_level )
  197. {
  198. print "Temperature CRITICAL - Sensor #$sensor = $temperature ";
  199. if( $temp_fmt == 3 ) { print "F\n"; } else { print "C\n"; }
  200. exit $exit_codes{'CRITICAL'};
  201. } elsif ($temperature and $temperature >= $warn_level ) {
  202. print "Temperature WARNING - Sensor #$sensor = $temperature ";
  203. if( $temp_fmt == 3 ) { print "F\n"; } else { print "C\n"; }
  204. exit $exit_codes{'WARNING'};
  205. } elsif( $temperature ) {
  206. print "Temperature OK - Sensor #$sensor = $temperature ";
  207. if( $temp_fmt == 3 ) { print "F\n"; } else { print "C\n"; }
  208. exit $exit_codes{'OK'};
  209. } else {
  210. print "Error parsing result for sensor #$sensor\n";
  211. exit $exit_codes{'UNKNOWN'};
  212. }
  213. # Show usage
  214. sub usage()
  215. {
  216. print "\ncheck_digitemp.pl v1.0 - NetSaint Plugin\n";
  217. print "Copyright 2002 by Brian C. Lane <bcl\@brianlane.com>\n";
  218. print "See source for License\n";
  219. print "usage:\n";
  220. print " check_digitemp.pl -t <sensor> -f <config file> -w <warnlevel> -c <critlevel>\n\n";
  221. print "options:\n";
  222. print " -f DigiTemp Config File\n";
  223. print " -t DigiTemp Sensor #\n";
  224. print " -F Temperature in Fahrenheit\n";
  225. print " -C Temperature in Centigrade\n";
  226. print " -w temperature temperature >= to warn\n";
  227. print " -c temperature temperature >= when critical\n";
  228. exit $exit_codes{'UNKNOWN'};
  229. }