check_sensors.py 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. #!/usr/bin/env python
  2. # -*- encoding: utf-8 -*-
  3. #####################################################################
  4. # (c) 2010-2011 by Sven Velt and team(ix) GmbH, Nuernberg, Germany #
  5. # sv@teamix.net #
  6. # #
  7. # This file is part of "team(ix) Monitoring Plugins" #
  8. # URL: http://oss.teamix.org/projects/monitoringplugins/ #
  9. # #
  10. # This file is free software: you can redistribute it and/or modify #
  11. # it under the terms of the GNU General Public License as published #
  12. # by the Free Software Foundation, either version 2 of the License, #
  13. # or (at your option) any later version. #
  14. # #
  15. # This file is distributed in the hope that it will be useful, but #
  16. # WITHOUT ANY WARRANTY; without even the implied warranty of #
  17. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
  18. # GNU General Public License for more details. #
  19. # #
  20. # You should have received a copy of the GNU General Public License #
  21. # along with this file. If not, see <http://www.gnu.org/licenses/>. #
  22. #####################################################################
  23. import datetime
  24. import time
  25. import os
  26. import re
  27. import sys
  28. try:
  29. from monitoringplugin import MonitoringPlugin
  30. except ImportError:
  31. print '=========================='
  32. print 'AIKS! Python import error!'
  33. print '==========================\n'
  34. print 'Could not find "monitoringplugin.py"!\n'
  35. print 'Did you download "%s"' % os.path.basename(sys.argv[0])
  36. print 'without "monitoringplugin.py"?\n'
  37. print 'Please go back to'
  38. print 'http://oss.teamix.org/projects/monitoringplugins/ and download it,'
  39. print 'or even better:'
  40. print 'get a hole archive at http://oss.teamix.org/projects/monitoringplugins/files\n'
  41. sys.exit(127)
  42. plugin = MonitoringPlugin(pluginname='check_sensors', tagforstatusline='Sensors', description='Check environment sensors', version='0.2')
  43. plugin.add_cmdlineoption('-s', '', 'sensorid', '(comma separated list of) sensor id(s), no spaces', default=None)
  44. plugin.add_cmdlineoption('-m', '', 'maxage', 'maximum age of data files (default: 600 seconds/10 minutes)', type="int", default=600)
  45. plugin.add_cmdlineoption('-p', '', 'path', 'path to data files', default='/tmp')
  46. plugin.add_cmdlineoption('-b', '', 'basefilename', 'base of sensor file name', default='sensor_')
  47. plugin.add_cmdlineoption('-w', '', 'tempwarn', 'warning thresold for temperature sensors', default=None)
  48. plugin.add_cmdlineoption('-c', '', 'tempcrit', 'critical thresold for temperature sensors', default=None)
  49. plugin.add_cmdlineoption('-W', '', 'humwarn', 'warning thresold for humidity sensors', default=None)
  50. plugin.add_cmdlineoption('-C', '', 'humcrit', 'critical thresold for humidity sensors', default=None)
  51. plugin.parse_cmdlineoptions()
  52. # No sensor id
  53. if not plugin.options.sensorid:
  54. plugin.back2nagios(3, 'Need at least one sensor id!')
  55. # Make list of sensor ids
  56. if ',' in plugin.options.sensorid:
  57. plugin.options.sensorid = plugin.options.sensorid.split(',')
  58. else:
  59. plugin.options.sensorid = [plugin.options.sensorid,]
  60. # Check all sensor ids are hex
  61. re_hex = re.compile('^[0-9A-Fa-f]+$')
  62. for sid in plugin.options.sensorid:
  63. if not re_hex.search(sid):
  64. plugin.back2nagios(3, 'Sensor id "%s" must be integer or hex!' % sid)
  65. plugin.verbose(1, 'Sensor id(s): ' + ' - '.join([str(s) for s in plugin.options.sensorid]))
  66. searchpattern = re.compile(r'(\d+)\s+Sensor:\s*([0-9A-Za-z]+)\s+Raw:\s*(-?[0-9\.]+)?\s+Value:\s*(-?[0-9\.]+)\s+Unit:\s*(\S+)\b')
  67. for sensorid in plugin.options.sensorid:
  68. filename = os.path.join(plugin.options.path, '%s%s' % (plugin.options.basefilename, sensorid))
  69. try:
  70. plugin.verbose(2, 'Reading sensor %s' % sensorid)
  71. data = file(filename).readlines()
  72. except IOError:
  73. plugin.back2nagios(3, 'Could not read file "%s"' % filename)
  74. if plugin.options.verbose >= 3:
  75. plugin.verbose(3, 'Read line(s):' % data)
  76. for line in data:
  77. plugin.verbose(3, '--> ' + line.lstrip().rstrip())
  78. plugin.verbose(2, 'Checking age of file')
  79. fileage = time.time() - os.path.getmtime(filename)
  80. if fileage > plugin.options.maxage:
  81. plugin.add_output('Data of sensor "%s" to old' % sensorid)
  82. plugin.add_returncode(3)
  83. plugin.verbose(2, 'File to old, age: %s but only %s seconds allowed'% (long(fileage), plugin.options.maxage))
  84. else:
  85. plugin.verbose(2, 'File age OK, age: %s and %s seconds are allowed'% (long(fileage), plugin.options.maxage))
  86. valuesinfile = 0
  87. for line in data:
  88. result = searchpattern.search(line)
  89. if result:
  90. sensor_type = None
  91. (readtime, sid, raw, value, unit) = result.groups()
  92. readtime = datetime.datetime.fromtimestamp(long(readtime))
  93. readtime = readtime.isoformat(' ')
  94. if unit in ['degC', '\xc2\xb0C']:
  95. sensor_type = 'temp'
  96. sensor_name = 'temp_' + str(sensorid)
  97. warn = plugin.options.tempwarn
  98. crit = plugin.options.tempcrit
  99. unit = 'C'
  100. pdunit = ''
  101. elif unit == '%RH':
  102. sensor_type = 'hum'
  103. sensor_name = 'hum_' + str(sensorid)
  104. warn = plugin.options.humwarn
  105. crit = plugin.options.humcrit
  106. pdunit = '%'
  107. if sensor_type:
  108. valuesinfile += 1
  109. returncode = plugin.value_wc_to_returncode(float(value), warn, crit)
  110. if returncode == 0:
  111. plugin.add_output('%s: %s%s' % (sensor_name, value, unit))
  112. else:
  113. plugin.add_output('%s: %s %s%s' % (sensor_name, plugin.RETURNSTRINGS[returncode], value, unit))
  114. plugin.add_returncode(returncode)
  115. plugin.add_multilineoutput('%s %s: %s%s (%s)' % (sensor_name, plugin.RETURNSTRINGS[returncode], value, unit, readtime))
  116. plugin.format_add_performancedata(sensor_name, value, pdunit, warn=warn, crit=crit)
  117. else:
  118. plugin.verbose(1, 'Unknown sensor type "%s" on %s' % (unit, sensorid))
  119. if valuesinfile == 0:
  120. plugin.verbose(2, 'No data found for sensor %s' % sensorid)
  121. plugin.exit()