graylog-system-notifications 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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. general_config_path = "/etc/graylog-api-scripts.conf"
  23. general_config = configparser.ConfigParser()
  24. general_config.read(general_config_path)
  25. api_token = general_config['DEFAULTS']['token']
  26. api_token_password = 'token'
  27. api_url_base = 'http://127.0.0.1:9000/api'
  28. headers = {'Content-Type': 'application/json', 'X-Requested-By': 'cli'}
  29. logging.basicConfig(format='%(message)s', datefmt='%b %d %H:%M:%S',
  30. level='WARNING')
  31. LOGGER = logging.getLogger()
  32. def get_request(url):
  33. api_url = '{}/{}'.format(api_url_base, url)
  34. response = requests.get(api_url, headers=headers, auth=(api_token, api_token_password))
  35. if response.status_code == 200:
  36. return json.loads(response.content.decode('utf-8'))
  37. else:
  38. return None
  39. def get_system_notifications():
  40. return get_request('system/notifications')
  41. def get_system():
  42. return get_request('system')
  43. def main():
  44. parser = argparse.ArgumentParser(description="Get system notifications of a graylog instance")
  45. parser.add_argument("--node", "-n", help="Show the affected node id", action="store_true")
  46. parser.add_argument("--level", "-l", help="Set the log level", default="WARNING")
  47. args = parser.parse_args()
  48. LOGGER.setLevel(args.level)
  49. notifications = get_system_notifications()
  50. LOGGER.debug(notifications)
  51. if notifications['total'] == 0:
  52. LOGGER.info('No messages')
  53. exit(0)
  54. else:
  55. for note in notifications['notifications']:
  56. output = ''
  57. output += 'Severity: {}\n'.format(note['severity'])
  58. output += 'Type: {}\n'.format(note['type'])
  59. if 'details' in note:
  60. output += 'Details: {}\n'.format(note['details'])
  61. output += 'Timestamp: {}\n'.format(note['timestamp'])
  62. if args.node:
  63. output += 'Node ID: {}\n'.format(note['node_id'])
  64. LOGGER.warning(output)
  65. if __name__ == "__main__":
  66. main()