configure_zabbix_alerts.py 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. import json
  2. import urllib.request
  3. ZABBIX_URL = 'http://192.168.130.170:8081/api_jsonrpc.php'
  4. ZABBIX_USER = 'Admin'
  5. ZABBIX_PASS = 'zabbix'
  6. DISCORD_WEBHOOK = 'https://discord.com/api/webhooks/1504740323576774739/2-MNclIGcYSxtLrQ-jzIXWl6miW3dOFTvB6KZsTQIX1FFis6JFoszATegAJoosJD7CMT'
  7. EMAIL_USER = 'lanfr144@gmail.com'
  8. EMAIL_PASS = '321iaSTB'
  9. def zabbix_request(method, params, auth=None):
  10. payload = {
  11. 'jsonrpc': '2.0',
  12. 'method': method,
  13. 'params': params,
  14. 'id': 1
  15. }
  16. if auth:
  17. payload['auth'] = auth
  18. req = urllib.request.Request(ZABBIX_URL, data=json.dumps(payload).encode('utf-8'), headers={'Content-Type': 'application/json-rpc'})
  19. with urllib.request.urlopen(req) as response:
  20. res = json.loads(response.read().decode('utf-8'))
  21. if 'error' in res:
  22. print(f"Error in {method}: {res['error']}")
  23. return None
  24. return res['result']
  25. def main():
  26. # 1. Login
  27. auth_token = zabbix_request('user.login', {'username': ZABBIX_USER, 'password': ZABBIX_PASS})
  28. if not auth_token:
  29. print("Login failed")
  30. return
  31. print("Zabbix Auth Token:", auth_token)
  32. # 2. Configure Email Media Type
  33. # Find existing Email media type (type 0 = Email)
  34. email_mt = zabbix_request('mediatype.get', {'filter': {'type': '0'}}, auth_token)
  35. email_params = {
  36. 'type': '0',
  37. 'name': 'Email',
  38. 'smtp_server': 'smtp.gmail.com',
  39. 'smtp_port': '587',
  40. 'smtp_helo': 'gmail.com',
  41. 'smtp_email': EMAIL_USER,
  42. 'smtp_security': '1', # STARTTLS
  43. 'smtp_verify_peer': '0',
  44. 'smtp_verify_host': '0',
  45. 'smtp_authentication': '1',
  46. 'username': EMAIL_USER,
  47. 'passwd': EMAIL_PASS,
  48. 'content_type': '1' # HTML
  49. }
  50. if email_mt:
  51. email_params['mediatypeid'] = email_mt[0]['mediatypeid']
  52. zabbix_request('mediatype.update', email_params, auth_token)
  53. print("Updated Email Media Type")
  54. email_id = email_mt[0]['mediatypeid']
  55. else:
  56. res = zabbix_request('mediatype.create', email_params, auth_token)
  57. email_id = res['mediatypeids'][0]
  58. print("Created Email Media Type")
  59. # 3. Configure Discord Media Type (type 4 = Webhook)
  60. discord_script = """
  61. var req = new HttpRequest();
  62. req.addHeader('Content-Type: application/json');
  63. var webhook = params.URL;
  64. var payload = JSON.stringify({
  65. "content": params.Subject + "\\n" + params.Message
  66. });
  67. var resp = req.post(webhook, payload);
  68. if (req.getStatus() != 204 && req.getStatus() != 200) {
  69. throw 'Failed with status ' + req.getStatus();
  70. }
  71. return 'OK';
  72. """
  73. discord_mt = zabbix_request('mediatype.get', {'filter': {'name': 'Discord Webhook'}}, auth_token)
  74. discord_params = {
  75. 'name': 'Discord Webhook',
  76. 'type': '4',
  77. 'parameters': [
  78. {'name': 'URL', 'value': DISCORD_WEBHOOK},
  79. {'name': 'Subject', 'value': '{ALERT.SUBJECT}'},
  80. {'name': 'Message', 'value': '{ALERT.MESSAGE}'}
  81. ],
  82. 'script': discord_script,
  83. 'process_tags': '0'
  84. }
  85. if discord_mt:
  86. discord_params['mediatypeid'] = discord_mt[0]['mediatypeid']
  87. zabbix_request('mediatype.update', discord_params, auth_token)
  88. print("Updated Discord Media Type")
  89. discord_id = discord_mt[0]['mediatypeid']
  90. else:
  91. res = zabbix_request('mediatype.create', discord_params, auth_token)
  92. discord_id = res['mediatypeids'][0]
  93. print("Created Discord Media Type")
  94. # 4. Update Admin User Media
  95. users = zabbix_request('user.get', {'filter': {'username': 'Admin'}, 'selectMedias': 'extend'}, auth_token)
  96. if users:
  97. admin_id = users[0]['userid']
  98. medias = [
  99. {'mediatypeid': email_id, 'sendto': [EMAIL_USER], 'active': 0, 'severity': 63, 'period': '1-7,00:00-24:00'},
  100. {'mediatypeid': discord_id, 'sendto': ['Discord'], 'active': 0, 'severity': 63, 'period': '1-7,00:00-24:00'}
  101. ]
  102. zabbix_request('user.update', {'userid': admin_id, 'medias': medias}, auth_token)
  103. print("Updated Admin user media")
  104. # 5. Create Web Scenario & Trigger for "> 5 seconds"
  105. hosts = zabbix_request('host.get', {'filter': {'host': 'Zabbix server'}}, auth_token)
  106. if hosts:
  107. host_id = hosts[0]['hostid']
  108. # Create web scenario
  109. httptests = zabbix_request('httptest.get', {'filter': {'name': 'Food App Performance'}}, auth_token)
  110. if not httptests:
  111. zabbix_request('httptest.create', {
  112. 'name': 'Food App Performance',
  113. 'hostid': host_id,
  114. 'delay': '30s',
  115. 'steps': [
  116. {
  117. 'name': 'Homepage',
  118. 'url': 'http://172.18.0.3:8501', # Using docker internal IP or host IP
  119. 'status_codes': '200',
  120. 'no': 1
  121. }
  122. ]
  123. }, auth_token)
  124. print("Created HTTP Test for Food App")
  125. # Create Trigger > 5s
  126. triggers = zabbix_request('trigger.get', {'filter': {'description': 'Food App is too slow (> 5s)'}}, auth_token)
  127. if not triggers:
  128. zabbix_request('trigger.create', {
  129. 'description': 'Food App is too slow (> 5s)',
  130. 'expression': f'last(/Zabbix server/web.test.time[Food App Performance,Homepage,resp])>5',
  131. 'priority': 4 # High
  132. }, auth_token)
  133. print("Created Trigger for slow performance")
  134. triggers_down = zabbix_request('trigger.get', {'filter': {'description': 'Food App is DOWN'}}, auth_token)
  135. if not triggers_down:
  136. zabbix_request('trigger.create', {
  137. 'description': 'Food App is DOWN',
  138. 'expression': f'last(/Zabbix server/web.test.fail[Food App Performance])<>0',
  139. 'priority': 5 # Disaster
  140. }, auth_token)
  141. print("Created Trigger for downtime")
  142. # 6. Action to send message
  143. actions = zabbix_request('action.get', {'filter': {'name': 'Alert Discord and Email'}}, auth_token)
  144. if not actions:
  145. zabbix_request('action.create', {
  146. 'name': 'Alert Discord and Email',
  147. 'eventsource': 0,
  148. 'status': 0,
  149. 'esc_period': '1m',
  150. 'filter': {
  151. 'evaltype': 0,
  152. 'conditions': [
  153. {'conditiontype': 3, 'operator': 2, 'value': 'Food App'} # Trigger name contains "Food App"
  154. ]
  155. },
  156. 'operations': [
  157. {
  158. 'operationtype': 0,
  159. 'opmessage': {
  160. 'default_msg': 1,
  161. 'mediatypeid': 0 # all
  162. },
  163. 'opmessage_usr': [
  164. {'userid': admin_id}
  165. ]
  166. }
  167. ]
  168. }, auth_token)
  169. print("Created Action for Alerts")
  170. print("Zabbix Configuration Complete.")
  171. if __name__ == '__main__':
  172. main()