graylog-system-notifications 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. #!/usr/bin/python3
  2. # graylog system notification script
  3. # Copyright (C) 2022 Philipp Fromme
  4. #
  5. # This program is free software: you can redistribute it and/or modify
  6. # it under the terms of the GNU General Public License as published by
  7. # the Free Software Foundation, either version 3 of the License, or
  8. # (at your option) any later version.
  9. #
  10. # This program is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. # GNU General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU General Public License
  16. # along with this program. If not, see <https://www.gnu.org/licenses/>.
  17. import argparse
  18. import configparser
  19. import logging
  20. import json
  21. import requests
  22. api_token = '{{ graylog_config['api_token'] }}'
  23. api_token_password = 'token'
  24. api_url_base = 'http://127.0.0.1:9000/api'
  25. headers = {'Content-Type': 'application/json', 'X-Requested-By': 'cli'}
  26. logging.basicConfig(format='%(message)s', datefmt='%b %d %H:%M:%S',
  27. level='WARNING')
  28. LOGGER = logging.getLogger()
  29. def get_request(url):
  30. api_url = '{}/{}'.format(api_url_base, url)
  31. response = requests.get(api_url, headers=headers, auth=(api_token, api_token_password))
  32. if response.status_code == 200:
  33. return json.loads(response.content.decode('utf-8'))
  34. else:
  35. return None
  36. def get_system_notifications():
  37. return get_request('system/notifications')
  38. def get_system():
  39. return get_request('system')
  40. def main():
  41. parser = argparse.ArgumentParser(description="Get system notifications of a graylog instance")
  42. parser.add_argument("--node", "-n", help="Show the affected node id", action="store_true")
  43. parser.add_argument("--level", "-l", help="Set the log level", default="WARNING")
  44. args = parser.parse_args()
  45. LOGGER.setLevel(args.level)
  46. notifications = get_system_notifications()
  47. LOGGER.debug(notifications)
  48. if notifications['total'] == 0:
  49. LOGGER.info('No messages')
  50. exit(0)
  51. else:
  52. for note in notifications['notifications']:
  53. output = ''
  54. output += 'Severity: {}\n'.format(note['severity'])
  55. output += 'Type: {}\n'.format(note['type'])
  56. if 'details' in note:
  57. output += 'Details: {}\n'.format(note['details'])
  58. output += 'Timestamp: {}\n'.format(note['timestamp'])
  59. if args.node:
  60. output += 'Node ID: {}\n'.format(note['node_id'])
  61. LOGGER.warning(output)
  62. if __name__ == "__main__":
  63. main()