check_livestatus_latency.py 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. #!/usr/bin/env python
  2. # -*- encoding: utf-8 -*-
  3. #####################################################################
  4. # (c) 2016 by Sven Velt, Germany #
  5. # sven-mymonplugins@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://gogs.velt.biz/velt.biz/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. 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 'https://gogs.velt.biz/velt.biz/MyMonPlugins and download it,'
  39. print 'or even better:'
  40. print 'get a full archive at http://gogs.velt.biz/velt.biz/MyMonPlugins/releases\n'
  41. sys.exit(127)
  42. plugin = MonitoringPlugin(
  43. pluginname='check_livestatus_latency',
  44. tagforstatusline='LATENCY',
  45. description='Check latency via Livestatus',
  46. version='0.1',
  47. )
  48. SOCKPATHs= [
  49. '/var/cache/naemon/live',
  50. '/usr/local/nagios/var/rw/live',
  51. '/var/lib/nagios3/rw/live',
  52. '/usr/local/icinga/var/rw/live',
  53. '/var/lib/icinga/rw/live',
  54. ]
  55. plugin.add_cmdlineoption('-S', '--socket', 'socket', 'path to livestatus socket', default=None)
  56. plugin.add_cmdlineoption('-w', '', 'warn', 'warning thresold', default='10')
  57. plugin.add_cmdlineoption('-c', '', 'crit', 'warning thresold', default='20')
  58. plugin.parse_cmdlineoptions()
  59. # FIXME: New method: find path (file or dir) and test we can read/write from/to it
  60. if not plugin.options.socket:
  61. plugin.verbose(2, "Auto-detecting path to livestatus unixsock...")
  62. for sockpath in SOCKPATHs:
  63. if os.path.exists(sockpath):
  64. plugin.options.socket = sockpath
  65. plugin.verbose(2, 'Found it at "%s"' % sockpath)
  66. break
  67. if not plugin.options.socket:
  68. plugin.back2nagios(3, 'Need a socket path (-S/--socket) to connecto to')
  69. if not os.access(plugin.options.socket, os.W_OK):
  70. plugin.back2nagios(3, 'Could not read from socket "%s"' % plugin.options.socket)
  71. # FIXME: End
  72. cmd_svc='''GET services
  73. OutputFormat: csv
  74. Stats: min latency
  75. Stats: max latency
  76. Stats: avg latency
  77. Stats: min execution_time
  78. Stats: max execution_time
  79. Stats: avg execution_time
  80. '''
  81. cmd_hst='''GET hosts
  82. OutputFormat: csv
  83. Stats: min latency
  84. Stats: max latency
  85. Stats: avg latency
  86. Stats: min execution_time
  87. Stats: max execution_time
  88. Stats: avg execution_time
  89. '''
  90. # Read service data from socket
  91. s = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
  92. s.connect(plugin.options.socket)
  93. plugin.verbose(3, 'Livestatus: GET services')
  94. s.send(cmd_svc)
  95. s.shutdown(socket.SHUT_WR)
  96. answer = ''
  97. try:
  98. while True:
  99. s.settimeout(10)
  100. data = s.recv(32768)
  101. if data:
  102. answer += data
  103. else:
  104. break
  105. except socket.timeout:
  106. plugin.back2nagios(3, 'Timeout while reading from socket')
  107. svc_answer = answer.split('\n')[0].split(';')
  108. plugin.verbose(3, 'Socket answer: %s' % answer.rstrip())
  109. svc_answer = [ float(fl) for fl in svc_answer ]
  110. plugin.verbose(3, 'Answer: %s' % svc_answer)
  111. # Read host data from socket
  112. s = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
  113. s.connect(plugin.options.socket)
  114. plugin.verbose(3, 'Livestatus: GET hosts')
  115. s.send(cmd_hst)
  116. s.shutdown(socket.SHUT_WR)
  117. answer = ''
  118. try:
  119. while True:
  120. s.settimeout(10)
  121. data = s.recv(32768)
  122. if data:
  123. answer += data
  124. else:
  125. break
  126. except socket.timeout:
  127. plugin.back2nagios(3, 'Timeout while reading from socket')
  128. hst_answer = answer.split('\n')[0].split(';')
  129. plugin.verbose(3, 'Socket answer: %s' % answer.rstrip())
  130. hst_answer = [ float(fl) for fl in hst_answer ]
  131. plugin.verbose(3, 'Answer: %s' % hst_answer)
  132. # Check values against thresholds
  133. for (k, v) in (
  134. ('AvgServiceLatency', svc_answer[2]),
  135. ('AvgServiceExecution', svc_answer[5]),
  136. ('AvgHostLatency', hst_answer[2]),
  137. ('AvgHostExecution', hst_answer[5]),
  138. ):
  139. rc = plugin.value_wc_to_returncode(v, plugin.options.warn, plugin.options.crit)
  140. perfdata = { 'label':k, 'value':'%.3f' % v, 'unit':'s', 'warn':plugin.options.warn, 'crit':plugin.options.crit }
  141. plugin.remember_check(k, rc, '%.2fs' % v, perfdata=[perfdata,])
  142. # Exit
  143. plugin.brain2output()
  144. plugin.exit()