-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
84 lines (74 loc) · 3.3 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
import sys
import requests
import json
import logging
from muting_rules import GetMutingRules
from incidents import GetOpenIncidents
from issues import GetOpenIssues
from close_issue import CloseOpenIssue
#change level to DEBUG for troubleshooting
logging.basicConfig(filename='muting.log', format='%(asctime)s %(message)s', datefmt='%m/%d/%Y %I:%M:%S %p', level=logging.INFO)
### CONFIG ###
API_KEY = '<user key>'
ACCOUNTID = 1
INGEST_KEY = '<license|insert key>'
### CONFIG ###
def main():
getRules = GetMutingRules(API_KEY, ACCOUNTID)
getIncidents = GetOpenIncidents(API_KEY, ACCOUNTID)
getIssues = GetOpenIssues(API_KEY, ACCOUNTID)
closeIssue = CloseOpenIssue(API_KEY)
results = []
signalLossResults = []
try:
rules = getRules.getMutingRules()
incidents = getIncidents.getOpenIncidents()
issues = getIssues.getOpenIssues()
for rule in rules:
if (rule['enabled'] is True):
if (rule['status'] != 'ACTIVE'):
mutingId = int(rule['id'])
for incident in incidents:
inc_num = round(incident['mutingId'])
if (mutingId == inc_num):
incidentId = incident['incidentId']
evalType = incident['evalType']
if (evalType != 'expiration'): #don't auto-close signal loss incidents
for issue in issues:
if (incidentId in issue['incidentIds']):
issueId = issue['issueId']
closeResult = closeIssue.closeIssue(ACCOUNTID, issueId)
results.append({'eventType': 'CloseMutedIssueResult', 'conditionName': incident['conditionName'], 'policyName': incident['policyName'], 'issueId': issueId, 'closeResult': closeResult})
else:
signalLossResults.append({'eventType': 'SignalLossPersistAfterMutingWindow', 'conditionName': incident['conditionName'], 'policyName': incident['policyName'], 'incidentId': incidentId, 'targetName': incident['target'], 'entity': incident['entity'], 'app_support_team': incident['app_support_team']})
if (len(results) > 0):
postResults(results)
# logging.debug('Results')
# logging.debug(results)
else:
logging.info('No close results to post.')
if (len(signalLossResults) > 0):
postResults(signalLossResults)
# logging.debug('SigLossResults')
# logging.debug(results)
else:
logging.info('No signal loss results to post.')
except Exception as e:
raise
sys.exit(1)
def postResults(res):
uri = 'https://insights-collector.newrelic.com/v1/accounts/' + str(ACCOUNTID) + '/events'
h = {
'X-Insert-Key': INGEST_KEY,
'Content-Type': 'application/json'
}
data = json.dumps(res)
resp = requests.post(uri, headers=h, data=data)
if (resp.status_code == 200):
logging.info('Posted to Results to NRDB successfully')
pass
else:
logging.error('Failed to post results to NRDB: ' + str(resp.status_code))
logging.debug(resp)
if __name__ == '__main__':
main()