Advertisement
Guest User

Untitled

a guest
Jul 18th, 2019
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.94 KB | None | 0 0
  1. import sys
  2. import re
  3. import json
  4. # import requests
  5.  
  6. # ROUTING_KEY = "" # ENTER EVENTS V2 API INTEGRATION KEY HERE
  7.  
  8. # def trigger_incident():
  9. # # Triggers a PagerDuty incident without a previously generated incident key
  10. # # Uses Events V2 API - documentation: https://v2.developer.pagerduty.com/docs/send-an-event-events-api-v2
  11.  
  12. # header = {
  13. # "Content-Type": "application/json"
  14. # }
  15.  
  16. # payload = { # Payload is built with the least amount of fields required to trigger an incident
  17. # "routing_key": ROUTING_KEY,
  18. # "event_action": "trigger",
  19. # "payload": {
  20. # "summary": "Example alert on host1.example.com",
  21. # "source": "monitoringtool:cloudvendor:central-region-dc-01:852559987:cluster/api-stats-prod-003",
  22. # "severity": "critical",
  23. # "custom details": {
  24. # "username":,
  25. # "jobname":
  26. # }
  27. # }
  28. # }
  29.  
  30. # response = requests.post('https://events.pagerduty.com/v2/enqueue',
  31. # data=json.dumps(payload),
  32. # headers=header)
  33.  
  34. # if response.json()["status"] == "success":
  35. # print('Incident created with with dedup key (also known as incident / alert key) of ' + '"' + response.json()['dedup_key'] + '"')
  36. # else:
  37. # print(response.text) # print error message if not successful
  38.  
  39.  
  40. def check_job_start(line):
  41. matcher = re.match(
  42. r"(?P<time>.*M).*job\/(?P<jobname>[\w-]+)\/ #[\d]+.*Started by user (?P<username>[\w-]+)", line)
  43. if (matcher != None):
  44. time = matcher.group("time")
  45. job = matcher.group("jobname")
  46. user = matcher.group("username")
  47. # trigger_incident()
  48. print("Hi " + user + ", did start the Job " + job + " at time " + time)
  49.  
  50.  
  51. def check_config_submit(line):
  52. matcher = re.match(
  53. r"(?P<time>.*M).*job\/(?P<jobname>[\w_]+)\/configSubmit by (?!.*timer)(?P<username>[\w]+)", line)
  54. if (matcher != None):
  55. time = matcher.group("time")
  56. job = matcher.group("jobname")
  57. user = matcher.group("username")
  58. # trigger_incident()
  59. print("Hi " + user + ", did submit config for Job " + job + " at time " + time)
  60.  
  61.  
  62. with open("./timestamp.txt", "r") as timestamp_file:
  63. last_timestamp = timestamp_file.read()
  64. print(last_timestamp)
  65. read_flag = False
  66. latest_timestamp = ""
  67. with open("./logfile.log", "r") as logfile:
  68. for line in logfile:
  69. line = str(line.split("\n")[0])
  70. if re.match(last_timestamp, line):
  71. print("date matched")
  72. read_flag = True
  73. continue
  74. if read_flag:
  75. print(line)
  76. check_job_start(line)
  77. check_config_submit(line)
  78.  
  79. latest_timestamp = re.match("(?P<time>.*M).*", line)
  80. with open("./timestamp.txt", "w+") as timestamp_file:
  81. timestamp_file.write(str(latest_timestamp.group("time")))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement