-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathjissue.py
executable file
·127 lines (111 loc) · 4.27 KB
/
jissue.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#!/usr/bin/python
from jira.client import JIRA
import os
import sys
import logging
import ConfigParser
USER = 'nagios'
PASS = 'nagiospw'
API_URL = "https://jira.domain.com"
PROJECT = 'DEMO'
ISSUETYPE = 'Bug'
logging.basicConfig(filename='/var/log/nagios3/jira-handler.log',
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
datefmt='%Y-%m-%d %H:%M:%S',
level=logging.DEBUG)
def load_config ():
Config = ConfigParser.ConfigParser()
try:
Config.read(os.path.expanduser("~/.jira4nagios.config"))
# Assuming it finds a section called 'jira4nagios' with
# options: ['user', 'pass', 'api_url', 'project', 'issuetype']
USER = Config.get('jira4nagios', 'user')
PASS = Config.get('jira4nagios', 'pass')
API_URL = Config.get('jira4nagios', 'api_url')
PROJECT = Config.get('jira4nagios', 'project')
ISSUETYPE = Config.get('jira4nagios', 'issuetype')
except:
logging.error("Could not read options[s] from config file: ~/.jira4nagios.config")
def jconnect ():
try:
jira = JIRA(options={'server': API_URL}, basic_auth=(USER, PASS))
return jira
except Exception as err:
err = "Couldn't create connection to jira: %s" % err
sys.stderr.write(err)
logging.error(err)
def search_similar_issues (jc, servicedesc, servicestate, hostname):
query = "labels=%s and labels=%s and labels=%s" % (servicedesc, servicestate, hostname)
similar_issues = jc.search_issues(query)
keys = [ "%s" % (issue.key) for issue in similar_issues ]
return keys
def create_issue (jc, summary, description, alert_id, servicedesc, servicestate, hostname):
try:
new_issue = jc.create_issue(project={'key': PROJECT},
summary=summary,
description=description,
labels=["alertid_"+alert_id, servicedesc, servicestate, hostname],
issuetype={'name': ISSUETYPE})
logging.info("Created issue with alert_id %s\n" % alert_id)
keys = search_similar_issues(jc, servicedesc, servicestate, hostname)
if len(keys) != 0:
for key in keys:
if key != new_issue.key:
jc.create_issue_link(type="Relates",
inwardIssue=key,
outwardIssue=new_issue.key)
except Exception as err:
err = "Couldn't create issue: %s" % (err)
sys.stderr.write(err)
logging.error(err)
return False
def close_issue (jc, alert_id):
try:
nag = jc.search_issues('labels=alertid_'+sys.argv[5])
jc.transition_issue(nag[0].id, 5)
logging.info("Closed issue with alert_id %s" % alert_id)
except Exception as err:
err = "Couldn't close issue: %s" % (err)
sys.stderr.write(err)
logging.error(err)
return False
def print_usage():
#print "Usage: %s SOFT|HARD <SUMMARY> <DESCRIPTION> CRITICAL|OK ALERT_ID SERVICEDESC HOSTNAME" % sys.argv[0]
print "Usage: %s STATE SUMMARY DESCRIPTION STATUS ALERTID SERVICE_DESCRIPTION SERVICE_STATE HOSTNAME" % sys.argv[0]
print """
STATE = SOFT or HARD
SUMMARY
DESCRIPTION
STATUS = CRITICAL or OK
ALERTID
SERVICE_DESCRIPTION
SERVICE_STATE
HOSTNAME
"""
def main():
try:
nag_state = sys.argv[1] # SOFT/HARD
summary = sys.argv[2]
description = sys.argv[3]
nag_status = sys.argv[4] # CRITICAL/OK
alert_id = sys.argv[5] # Nagios alert ID
servicedesc = sys.argv[6]
servicestate = sys.argv[7]
hostname = sys.argv[8]
except IndexError:
print_usage()
return False
load_config()
jc = jconnect()
if jc:
if nag_state == "HARD":
if nag_status == "CRITICAL":
create_issue(jc, summary, description, alert_id, servicedesc, servicestate, hostname)
elif nag_status == "OK":
close_issue(jc, alert_id)
elif nag_stage == "SOFT" and nag_status == "UNKNOWN":
create_issue(jc, summary, description, alert_id)
else:
return False
if __name__ == "__main__":
main()