-
Notifications
You must be signed in to change notification settings - Fork 0
/
autotask_create_ticket_host_services.py
352 lines (306 loc) · 11.1 KB
/
autotask_create_ticket_host_services.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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
#!/usr/bin/env python
import argparse
import os
import time
import json
import sys
from mk_livestatus import Socket
#from CheckMK import SingleSiteConnection
from CheckMK.LiveStatus import *
#import CheckMK
from OpinKerfiAutoTask.OkAutoTask import OkAutoTask
from jinja2 import Environment, FileSystemLoader
def parse_input_arguments():
""" Parses the input arguments of this script """
parser = argparse.ArgumentParser()
parser.add_argument("host_backend_address")
parser.add_argument("host_backend_id")
parser.add_argument("host_backend_name")
parser.add_argument("host_name")
parser.add_argument("host_address")
parser.add_argument("acknowledged_by")
args = parser.parse_args()
return args
def get_template_environment():
""" Sets up where to look for jinja templates """
THIS_DIR = os.path.dirname(os.path.abspath(__file__))
j2_env = Environment(loader=FileSystemLoader(THIS_DIR), trim_blocks=True)
return j2_env
def get_livestatus_connection(host_backend_address):
""" Returns splitted host port data """
backend = host_backend_address.split(':')
backend_host = backend[0]
backend_port = backend[1]
return {'backend_host': backend_host, "backend_port": int(backend_port)}
def get_states_mapping(state):
""" TODO: map numeric stats to warning, critical """
pass
def get_autotask_mapping(name):
""" Maps backend_id in thruk to autotask customer id and queue"""
backends = [
{
'name': "manage.ruv.is",
'backend_id': "d07cd",
'autotask_id': 667,
'queue': "NOC"
}, {
'name': 'nagios.ksgatt.is',
'backend_id': "9e0c6",
'autotask_id': 4475,
'queue': "NOC"
}, {
'name': 'hortense.skattur.is',
'backend_id': "1f017",
'autotask_id': 385,
'queue': "NOC"
}, {
'name': 'admin.okhysing.is',
'backend_id': "0dc8e",
'autotask_id': 34019,
'queue': "NOC"
}, {
'name': 'netvik.is1net.net',
'backend_id': "dbc17",
'autotask_id': 3128,
'queue': "NOC"
}, {
'name': 'nagios.okhysing.is',
'backend_id': "20498",
'autotask_id': 34019,
'queue': "NOC"
}, {
'name': 'netvik.netrekstur.okh.is',
'backend_id': "82eea",
'autotask_id': 34019,
'queue': "NOC"
}, {
'name': 'ver-monitor-01.okh.is',
'backend_id': "01d46",
'autotask_id': 34019,
'queue': "NOC"
}, {
'name': 'netvik.icehotels.is',
'backend_id': "9e0f4",
'autotask_id': 5041,
'queue': "NOC"
}, {
'name': 'netvik.nordural.is',
'backend_id': "e20a9",
'autotask_id': 560,
'queue': "NOC"
}
]
backend_list = [
backend for backend in backends if backend['name'] == name]
return backend_list[0]
def get_autotask_domain_to_customer_mapping(hostname):
domains = [
{
'name': 'matis.local',
'autotask_id': 996,
'queue': "NOC"
}, {
'name': 'landmotun.local',
'autotask_id': 4527,
'queue': "NOC"
}, {
'name': 'okh.is',
'autotask_id': 34019,
'queue': "NOC"
}, {
'name': 'okhysing.is',
'autotask_id': 34019,
'queue': "NOC"
}, {
'name': 'ok.is',
'autotask_id': 0,
'queue': "NOC"
}, {
'name': 'ksgatt.is',
'autotask_id': 4475,
'queue': "NOC"
}, {
'name': 'ks.is',
'autotask_id': 4475,
'queue': "NOC"
}, {
'name': 'lifland.is',
'autotask_id': 3463,
'queue': "NOC"
}, {
'name': 'vogar.is',
'autotask_id': 3339,
'queue': "NOC"
}, {
'name': 'gjtravel.is',
'autotask_id': 7789,
'queue': "NOC"
}, {
'name': 'askja.is',
'autotask_id': 4535,
'queue': "NOC"
}, {
'name': 'askja.local',
'autotask_id': 4535,
'queue': "NOC"
}, {
'name': 'mainmanager.internal',
'autotask_id': 676,
'queue': "NOC"
}, {
'name': 'iceconsult.is',
'autotask_id': 676,
'queue': "NOC"
}, {
'name': 'skattur.is',
'autotask_id': 385,
'queue': "NOC"
}, {
'name': 'ruv.is',
'autotask_id': 667,
'queue': "NOC"
}, {
'name': 'stef.local',
'autotask_id': 481,
'queue': "NOC"
}
]
# Lookup domain from domains dictinoary and return autotask_id for matching item
domain_result = [
domain for domain in domains if domain['name'] in hostname.lower()]
if domain_result:
return domain_result[0]
else:
return {
'name': 'okhysing.is',
'autotask_id': 34019,
'queue': "NOC"
} # Opin Kerfi Hysing
def query_livestatus_host_status(input_hostname, livestatus_socket):
query = livestatus_socket.hosts.columns(
'name',
'address',
'plugin_output',
'last_state_change',
'contacts'
).filter('name = ' + input_hostname).filter('state != 0').filter('acknowledged != 1')
host_acknowledged = query.call()
return host_acknowledged
def query_livestatus_host_services(input_hostname, livestatus_socket):
query = livestatus_socket.services.columns(
'host_name',
'service_description',
'state',
'host_address',
'plugin_output',
'last_state_change'
).filter('host_name = ' + input_hostname).filter('state != 0').filter('acknowledged != 1')
services_acknowledged = query.call()
return services_acknowledged
class LiveStatusAction:
def __init__(self, host_backend_url):
self.livestatus_socket = 'tcp:' + host_backend_url
def command(self, command):
""" Post a command over single connection. """
command = "[%s] %s" % (int(time.time()), command)
# Create the connection
#conn = lstatus.SingleSiteConnection(self.livestatus_socket)
conn = SingleSiteConnection(self.livestatus_socket)
# Run the command the connection
# TODO: remove results since the command does not return value.
results = conn.command(command)
# Return the results
return results
def ack_host(self, host_name, acknowledged_by, message):
""" Helper Function: Acknowledge a host problem with a comment.
This should be able to to pickup the user name from the session.
"""
cmd = "ACKNOWLEDGE_HOST_PROBLEM;%(host_name)s;1;1;1;%(acknowledged_by)s;%(message)s\n" % locals(
)
self.command(cmd)
def ack_hosts(self, host_list, acknowledged_by, message):
""" Helper Function: Acknowledge a list of host problems with a comment.
This should be able to pickup the user name from the session in a
later version. """
cmd_t = "ACKNOWLEDGE_HOST_PROBLEM;%(host_name)s;1;1;1;%(acknowledged_by)s;%(message)s\n"
for host_name in host_list:
self.command(cmd_t % locals())
def ack_service(self, service, acknowledged_by, message):
""" Helper Function: Acknowledge a service problem with a comment. """
self.command(
"ACKNOWLEDGE_SVC_PROBLEM;%(service)s;1;1;1;%(acknowledged_by)s;%(message)s\n" % locals())
def ack_services(self, service_list, acknowledged_by, message):
""" Helper Function: Acknowledge a list service problems with a comment. """
for service in service_list:
self.command(
"ACKNOWLEDGE_SVC_PROBLEM;" +
service['host_name'] + ";" +
service['description'] + ";" +
"1;1;1;" +
acknowledged_by + ";" +
message + "\n"
)
if __name__ == "__main__":
args = parse_input_arguments()
templates = get_template_environment()
autotask = OkAutoTask()
backend = get_autotask_mapping(args.host_backend_name)
socket_info = get_livestatus_connection(args.host_backend_address)
livestatus_action = LiveStatusAction(args.host_backend_address)
# Create live status socket from input data
livestatus_socket = Socket((
socket_info['backend_host'],
socket_info['backend_port']
))
# Query for non acknowlegded host issues
host_statuses = query_livestatus_host_status(
args.host_name,
livestatus_socket
)
# Query for non acknowlegded service issues
host_service_statuses = query_livestatus_host_services(
args.host_name,
livestatus_socket
)
# Convert epoch time to date
for hs in host_statuses:
last_state_change_epoch=hs['last_state_change']
last_state_change_date=time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(last_state_change_epoch))
# Update field with datetime instead of epoch time
hs['last_state_change']=last_state_change_date
# Convert epoch time to date
for hss in host_service_statuses:
last_state_change_epoch=hss['last_state_change']
last_state_change_date=time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(last_state_change_epoch))
# Update field with datetime instead of epoch time
hss['last_state_change']=last_state_change_date
# Generate autotask description text from template
autotask_rendered_description = templates.get_template('autotaskTemplate.j2').render(
args=args,
host_statuses=host_statuses,
host_service_statuses=host_service_statuses
)
# Create ticket if there are host_statues or host_service_statuses
if len(host_statuses) >= 1 or len(host_service_statuses) >= 1:
customer = get_autotask_domain_to_customer_mapping(args.host_name)
ticket = autotask.create_ticket(
title="Nagios eftirlit - " + args.host_name,
description=autotask_rendered_description,
queue=customer['queue'],
accountID=customer['autotask_id'],
ticketSource='Monitoring Alert',
ticketType='Alert',
ticketCategory='AEM Alert'
)
print("Ticket created: " + ticket.TicketNumber)
# Generate comment from template
comment = templates.get_template(
'thrukcommentTemplate.j2').render(ticket=ticket)
# If there are host statuses we acknowledge and add comment
if len(host_statuses) >= 1:
livestatus_action.ack_host(
args.host_name, args.acknowledged_by, comment)
# If there are service statuses we acknowledge and add comment
if len(host_service_statuses) >= 1:
livestatus_action.ack_services(
host_service_statuses, args.acknowledged_by, comment)