1
|
|
|
""" |
2
|
|
|
Enarksh |
3
|
|
|
|
4
|
|
|
Copyright 2013-2016 Set Based IT Consultancy |
5
|
|
|
|
6
|
|
|
Licence MIT |
7
|
|
|
""" |
8
|
|
|
import sys |
9
|
|
|
import traceback |
10
|
|
|
|
11
|
|
|
import enarksh |
12
|
|
|
from enarksh.DataLayer import DataLayer |
13
|
|
|
from enarksh.controller.event_handler.NodeActionMessageBaseEventHandler import NodeActionMessageBaseEventHandler |
14
|
|
|
|
15
|
|
|
|
16
|
|
|
class NagiosMessageEventHandler(NodeActionMessageBaseEventHandler): |
17
|
|
|
""" |
18
|
|
|
An event handler for a NagiosMessage received events. |
19
|
|
|
""" |
20
|
|
|
|
21
|
|
|
# ------------------------------------------------------------------------------------------------------------------ |
22
|
|
|
@staticmethod |
23
|
|
|
def handle(_event, _message, controller): |
24
|
|
|
""" |
25
|
|
|
Handles a NagiosMessage received event. |
26
|
|
|
|
27
|
|
|
:param * _event: Not used. |
28
|
|
|
:param * _message: Not used. |
29
|
|
|
:param enarksh.controller.Controller.Controller controller: The controller. |
30
|
|
|
""" |
31
|
|
|
del _event, _message |
32
|
|
|
|
33
|
|
|
rst_count = {enarksh.ENK_RST_ID_COMPLETED: 0, |
34
|
|
|
enarksh.ENK_RST_ID_ERROR: 0, |
35
|
|
|
enarksh.ENK_RST_ID_QUEUED: 0, |
36
|
|
|
enarksh.ENK_RST_ID_RUNNING: 0, |
37
|
|
|
enarksh.ENK_RST_ID_WAITING: 0} |
38
|
|
|
|
39
|
|
|
response = {'ret': 0, |
40
|
|
|
'rst_count': rst_count, |
41
|
|
|
'sch_count': len(controller.schedules)} |
42
|
|
|
|
43
|
|
|
try: |
44
|
|
|
for schedule in controller.schedules: |
45
|
|
|
schedule.nagios_performance_data(rst_count) |
46
|
|
|
|
47
|
|
|
except Exception as exception: |
|
|
|
|
48
|
|
|
print(exception, file=sys.stderr) |
49
|
|
|
traceback.print_exc(file=sys.stderr) |
50
|
|
|
|
51
|
|
|
response['ret'] = -1 |
52
|
|
|
response['message'] = 'Internal error' |
53
|
|
|
|
54
|
|
|
DataLayer.rollback() |
55
|
|
|
|
56
|
|
|
# Send response message to the CLI client. |
57
|
|
|
controller.message_controller.send_message('lockstep', response) |
58
|
|
|
|
59
|
|
|
# ---------------------------------------------------------------------------------------------------------------------- |
60
|
|
|
|
Generally, you would want to handle very specific errors in the exception handler. This ensure that you do not hide other types of errors which should be fixed.
So, unless you specifically plan to handle any error, consider adding a more specific exception.