check_collectd.py 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. #!/usr/bin/env python
  2. # -*- encoding: utf-8 -*-
  3. #####################################################################
  4. # (c) 2016 by Sven Velt, Germany #
  5. # sven-mymon-plugins@velt.biz #
  6. # #
  7. # This file is part of "velt.biz - My Monitoring Plugins" #
  8. # a fork of "team(ix) Monitoring Plugins" in 2015 #
  9. # URL: https://github.com/veltbiz/mymonplugins/ #
  10. # #
  11. # This file is free software: you can redistribute it and/or modify #
  12. # it under the terms of the GNU General Public License as published #
  13. # by the Free Software Foundation, either version 2 of the License, #
  14. # or (at your option) any later version. #
  15. # #
  16. # This file is distributed in the hope that it will be useful, but #
  17. # WITHOUT ANY WARRANTY; without even the implied warranty of #
  18. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
  19. # GNU General Public License for more details. #
  20. # #
  21. # You should have received a copy of the GNU General Public License #
  22. # along with this file. If not, see <http://www.gnu.org/licenses/>. #
  23. #####################################################################
  24. import os
  25. import re
  26. import socket
  27. import sys
  28. from collections import OrderedDict
  29. try:
  30. from monitoringplugin import MonitoringPlugin
  31. except ImportError:
  32. print '=========================='
  33. print 'AIKS! Python import error!'
  34. print '==========================\n'
  35. print 'Could not find "monitoringplugin.py"!\n'
  36. print 'Did you download "%s"' % os.path.basename(sys.argv[0])
  37. print 'without "monitoringplugin.py"?\n'
  38. print 'Please go back to'
  39. print 'https://github.com/veltbiz/mymonplugins and download it,'
  40. print 'or even better:'
  41. print 'get a full archive at http://github.com/veltbiz/mymonplugins/releases\n'
  42. sys.exit(127)
  43. plugin = MonitoringPlugin(
  44. pluginname='check_collectd',
  45. tagforstatusline='COLLECTD',
  46. description='Check values of collectd server',
  47. version='0.1',
  48. )
  49. SOCKPATHs= [
  50. '/var/run/collectd-unixsock',
  51. ]
  52. plugin.add_cmdlineoption('-S', '--socket', 'socket', 'path to socket of collectd', default=None)
  53. plugin.add_cmdlineoption('-H', '--host', 'host', 'Hostname (in collectd) to check', default=None)
  54. plugin.add_cmdlineoption('-V', '--value_spec', 'var', 'value to from collectd', default=None)
  55. plugin.add_cmdlineoption('-w', '', 'warn', 'warning thresold', default=None)
  56. plugin.add_cmdlineoption('-c', '', 'crit', 'warning thresold', default=None)
  57. plugin.parse_cmdlineoptions()
  58. if not plugin.options.host:
  59. plugin.back2nagios(3, 'Need a hostname (-H/--hostname) to check!')
  60. if not plugin.options.var:
  61. plugin.back2nagios(3, 'Need a value_spec (-V/--value_spec) to check!')
  62. # FIXME: New method: find path (file or dir) and test we can read/write from/to it
  63. if not plugin.options.socket:
  64. plugin.verbose(2, "Auto-detecting path to collectd's unixsock...")
  65. for sockpath in SOCKPATHs:
  66. if os.path.exists(sockpath):
  67. plugin.options.socket = sockpath
  68. plugin.verbose(2, 'Found it at "%s"' % sockpath)
  69. break
  70. if not plugin.options.socket:
  71. plugin.back2nagios(3, 'Need a socket path (-S/--socket) to connecto to')
  72. if not os.access(plugin.options.socket, os.W_OK):
  73. plugin.back2nagios(3, 'Could not read from socket "%s"' % plugin.options.socket)
  74. # FIXME: End
  75. s = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
  76. s.connect(plugin.options.socket)
  77. command = 'GETVAL "%s/%s"\n' % (plugin.options.host, plugin.options.var)
  78. plugin.verbose(3, 'Socket command: %s' % command.rstrip())
  79. s.send(command)
  80. s.shutdown(socket.SHUT_WR)
  81. answer = ''
  82. try:
  83. while True:
  84. s.settimeout(10)
  85. data = s.recv(32768)
  86. if data:
  87. answer += data
  88. else:
  89. break
  90. except socket.timeout:
  91. plugin.back2nagios(3, 'Timeout while reading from socket')
  92. answer = answer.split('\n')
  93. plugin.verbose(3, 'Socket answer: %s' % answer)
  94. (status, text) = answer.pop(0).split(' ', 1)
  95. try:
  96. status = long(status)
  97. except ValueError:
  98. plugin.back2nagios(3, 'Unknown answer from socket: "%s"' % answer[0])
  99. plugin.verbose(3, 'Socket status code: %s' % status)
  100. if status < 0:
  101. plugin.back2nagios(3, 'Collectd error: "%s"' % text)
  102. answer = answer[:status]
  103. answer = OrderedDict( [ (p[0], float(p[1])) for p in [ p.split('=') for p in answer ] ] )
  104. for (key,value) in answer.iteritems():
  105. returncode = plugin.value_wc_to_returncode(value, plugin.options.warn, plugin.options.crit)
  106. longoutput = '%s: %s' % (key, value)
  107. perfdata={
  108. 'label': key,
  109. 'value': value,
  110. 'unit': '',
  111. 'warn': plugin.options.warn,
  112. 'crit': plugin.options.crit,
  113. }
  114. plugin.remember_check(key, returncode, longoutput, perfdata=[perfdata,])
  115. plugin.brain2output()
  116. plugin.exit()
  117. sys.exit(0)
  118. if plugin.options.proto not in ['http', 'https']:
  119. plugin.back2nagios(3, 'Unknown protocol "' + plugin.options.proto + '"')
  120. if not plugin.options.port:
  121. if plugin.options.proto == 'https':
  122. plugin.options.port = '443'
  123. else:
  124. plugin.options.port = '80'
  125. url = plugin.options.proto + '://' + plugin.options.host + ':' + plugin.options.port + '/' + plugin.options.url + '?auto'
  126. plugin.verbose(1, 'Status URL: ' + url)
  127. if plugin.options.httpauth:
  128. httpauth = plugin.options.httpauth.split(':')
  129. if len(httpauth) != 2:
  130. plugin.back2nagios(3, 'Wrong format of authentication data! Need "USERNAME:PASSWORD", got "' + plugin.options.httpauth + '"')
  131. passman = urllib2.HTTPPasswordMgrWithDefaultRealm()
  132. passman.add_password(None, url, httpauth[0], httpauth[1])
  133. authhandler = urllib2.HTTPBasicAuthHandler(passman)
  134. opener = urllib2.build_opener(authhandler)
  135. urllib2.install_opener(opener)
  136. try:
  137. data = urllib2.urlopen(url).read()
  138. except urllib2.HTTPError, e:
  139. plugin.back2nagios(2, 'Could not read data! ' + str(e))
  140. except urllib2.URLError, e:
  141. plugin.back2nagios(2, 'Could not connect to server!')
  142. plugin.verbose(2, 'Got data:\n' + data)
  143. try:
  144. idle = int(re.search('Idle(?:Workers|Servers): (\d+)\n', data).group(1))
  145. busy = int(re.search('Busy(?:Workers|Servers): (\d+)\n', data).group(1))
  146. except:
  147. plugin.back2nagios(2, 'Could not analyze data!')
  148. description = OrderedDict()
  149. description['_'] = 'waiting for connection'
  150. description['S'] = 'starting up'
  151. description['R'] = 'reading request'
  152. description['W'] = 'writing/sending reply'
  153. description['K'] = 'keepalive'
  154. description['D'] = 'looking up in DNS'
  155. description['C'] = 'closing connection'
  156. description['L'] = 'logging'
  157. description['G'] = 'gracefully finishing'
  158. description['I'] = 'idle cleanup of worker'
  159. description['.'] = 'open slots(up to ServerLimit)'
  160. states = {'_':0, 'S':0, 'R':0, 'W':0, 'K':0, 'D':0, 'C':0, 'L':0, 'G':0, 'I':0, '.':0,}
  161. scoreboard = re.search('Scoreboard: (.*)\n', data)
  162. if scoreboard:
  163. for worker in scoreboard.group(1):
  164. states[worker] += 1
  165. if plugin.options.statistics:
  166. scoreboard = re.search('Scoreboard: (.*)\n', data)
  167. if scoreboard:
  168. states = {'_':0, 'S':0, 'R':0, 'W':0, 'K':0, 'D':0, 'C':0, 'L':0, 'G':0, 'I':0, '.':0,}
  169. for worker in scoreboard.group(1):
  170. states[worker] += 1
  171. plugin.add_multilineoutput(str(states['_']) + ' waiting for connection')
  172. plugin.add_multilineoutput(str(states['S']) + ' starting up')
  173. plugin.add_multilineoutput(str(states['R']) + ' reading request')
  174. plugin.add_multilineoutput(str(states['W']) + ' writing/sending reply')
  175. plugin.add_multilineoutput(str(states['K']) + ' keepalive')
  176. plugin.add_multilineoutput(str(states['D']) + ' looking up in DNS')
  177. plugin.add_multilineoutput(str(states['C']) + ' closing connection')
  178. plugin.add_multilineoutput(str(states['L']) + ' logging')
  179. plugin.add_multilineoutput(str(states['G']) + ' gracefully finishing')
  180. plugin.add_multilineoutput(str(states['I']) + ' idle cleanup of worker')
  181. plugin.add_multilineoutput(str(states['.']) + ' open slots(up to ServerLimit)')
  182. returncode = plugin.value_wc_to_returncode(busy, plugin.options.warn, plugin.options.crit)
  183. plugin.add_output(str(busy) + ' busy workers, ' + str(idle) + ' idle')
  184. plugin.add_returncode(returncode)
  185. plugin.format_add_performancedata('busy', busy, '', warn=plugin.options.warn, crit=plugin.options.crit, min=0.0)
  186. plugin.format_add_performancedata('idle', idle, '')
  187. plugin.exit()