fastd.py.j2 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. #!/usr/bin/python
  2. # This file is managed by ansible, don't make changes here - they will be overwritten.
  3. #
  4. # This program is free software: you can redistribute it and/or modify
  5. # it under the terms of the GNU General Public License as published by
  6. # the Free Software Foundation, either version 3 of the License, or
  7. # any later version.
  8. #
  9. # This program is distributed in the hope that it will be useful,
  10. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. # GNU General Public License for more details.
  13. #
  14. # You should have received a copy of the GNU General Public License
  15. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. #
  17. # Author:
  18. # dray <dresen@itsecteam.ms>
  19. #
  20. # Script to count fastd connections adapted from
  21. # https://github.com/FreiFunkMuenster/gateway-ffms/blob/master/nrpe/check_fastd_connections.py
  22. #
  23. # About this plugin:
  24. # This plugin collects the number of fastd connection for FFMS
  25. #
  26. # Configuration
  27. #
  28. # <Module fastd>
  29. # server_address "/tmp/fastd-status"
  30. # </Module>
  31. #
  32. # for multiple fastd instances, add each socket to server_address seperated by :
  33. # Example:
  34. # server_address "/tmp/fastd-a-status:/tmp/fastd-b-status"
  35. #
  36. #
  37. import collectd
  38. import socket
  39. import sys
  40. import json
  41. server_address = ''
  42. def read(data=None):
  43. vl = collectd.Values(type='gauge')
  44. vl.plugin='python.fastd'
  45. try:
  46. connections = 0
  47. servers = server_address.split(":")
  48. for server in servers:
  49. sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
  50. sock.connect(server)
  51. received_count = 1
  52. received_data = ""
  53. while received_count > 0:
  54. data = sock.recv(1024)
  55. received_count = len(data)
  56. received_data += data;
  57. sock.close();
  58. received_json = json.loads(received_data);
  59. for item in received_json.items()[2][1].items():
  60. connection = item[1]
  61. connections += 1
  62. vl.dispatch(values=[connections])
  63. except socket.error, msg:
  64. collectd.error("[FFMS Fastd] Socket read failed: %s" % (msg))
  65. # vl.dispatch(values=[-1])
  66. def write(vl, data=None):
  67. for i in vl.values:
  68. print "%s (%s): %f" % (vl.plugin, vl.type, i)
  69. def config (config):
  70. global server_address
  71. server_address = '' #'/tmp/fastd-status'
  72. for node in config.children:
  73. key = node.key.lower()
  74. val = node.values[0]
  75. if key == 'server_address':
  76. server_address = val
  77. else:
  78. collectd.warning("ffms_fastd plugin: Unknown configuration key: %s." % key )
  79. continue
  80. if server_address == '':
  81. collectd.error('ffms_fastd plugin: No Serveradress configured.' )
  82. else:
  83. collectd.info("ffms_fastd plugin: Successful configured with server_address %s." % server_address)
  84. collectd.register_read(read);
  85. collectd.register_write(write);
  86. collectd.register_config(config);