1
|
|
|
""" |
2
|
|
|
Enarksh |
3
|
|
|
|
4
|
|
|
Copyright 2013-2016 Set Based IT Consultancy |
5
|
|
|
|
6
|
|
|
Licence MIT |
7
|
|
|
""" |
8
|
|
|
import os |
9
|
|
|
|
10
|
|
|
import zmq |
11
|
|
|
|
12
|
|
|
import enarksh |
|
|
|
|
13
|
|
|
from enarksh.C import C |
14
|
|
|
from enarksh.Config import Config |
15
|
|
|
from enarksh.controller.message.NagiosMessage import NagiosMessage |
16
|
|
|
|
17
|
|
|
|
18
|
|
|
class NagiosClient: |
19
|
|
|
""" |
20
|
|
|
A client for Nagios. |
21
|
|
|
""" |
22
|
|
|
|
23
|
|
|
# ------------------------------------------------------------------------------------------------------------------ |
24
|
|
|
def __init__(self, io): |
25
|
|
|
""" |
26
|
|
|
Object constructor. |
27
|
|
|
|
28
|
|
|
:param enarksh.style.EnarkshStyle.EnarkshStyle io: The output decorator. |
29
|
|
|
""" |
30
|
|
|
self.__zmq_context = None |
31
|
|
|
""" |
32
|
|
|
The ZMQ context. |
33
|
|
|
|
34
|
|
|
:type: Context |
35
|
|
|
""" |
36
|
|
|
|
37
|
|
|
self.__zmq_controller = None |
38
|
|
|
""" |
39
|
|
|
The socket for communicating with the controller. |
40
|
|
|
|
41
|
|
|
:type: zmq.sugar.socket.Socket |
42
|
|
|
""" |
43
|
|
|
|
44
|
|
|
self._io = io |
45
|
|
|
""" |
46
|
|
|
The output decorator. |
47
|
|
|
|
48
|
|
|
:type: enarksh.style.EnarkshStyle.EnarkshStyle |
49
|
|
|
""" |
50
|
|
|
|
51
|
|
|
self.__state = '' |
52
|
|
|
""" |
53
|
|
|
The state for Nagios. |
54
|
|
|
|
55
|
|
|
:type: str |
56
|
|
|
""" |
57
|
|
|
|
58
|
|
|
self.__message = '' |
59
|
|
|
""" |
60
|
|
|
The message for Nagios. |
61
|
|
|
|
62
|
|
|
:type: str |
63
|
|
|
""" |
64
|
|
|
|
65
|
|
|
self.__performance_data = '' |
66
|
|
|
""" |
67
|
|
|
The performance data for Nagios. |
68
|
|
|
|
69
|
|
|
:type: str |
70
|
|
|
""" |
71
|
|
|
|
72
|
|
|
# ------------------------------------------------------------------------------------------------------------------ |
73
|
|
|
@staticmethod |
74
|
|
|
def __get_pid(daemon): |
75
|
|
|
""" |
76
|
|
|
Returns the PID of a daemon of Enarksh. |
77
|
|
|
|
78
|
|
|
:param str daemon: The name of the daemon. |
79
|
|
|
|
80
|
|
|
:rtype: str|None |
81
|
|
|
""" |
82
|
|
|
try: |
83
|
|
|
config = Config.get() |
84
|
|
|
|
85
|
|
|
pid_file = os.path.join(C.HOME, config.get_enarksh_lock_dir(), daemon + '.pid') |
86
|
|
|
with open(pid_file) as handle: |
87
|
|
|
pid = handle.read(1000) |
88
|
|
|
|
89
|
|
|
return int(pid) |
90
|
|
|
|
91
|
|
|
except Exception: |
92
|
|
|
return None |
93
|
|
|
|
94
|
|
|
# ------------------------------------------------------------------------------------------------------------------ |
95
|
|
|
def __test_daemon_is_running(self, daemon): |
96
|
|
|
""" |
97
|
|
|
Test whether a daemon is running. |
98
|
|
|
|
99
|
|
|
:param str daemon: The name of the daemon. |
100
|
|
|
|
101
|
|
|
:rtype: bool |
102
|
|
|
""" |
103
|
|
|
pid = self.__get_pid(daemon) |
104
|
|
|
|
105
|
|
|
if pid is None: |
106
|
|
|
return False |
107
|
|
|
|
108
|
|
|
try: |
109
|
|
|
proc_file = os.path.join('/proc', str(pid), 'comm') |
110
|
|
|
name = open(proc_file).read(1000).strip() |
111
|
|
|
|
112
|
|
|
return name == daemon |
113
|
|
|
|
114
|
|
|
except Exception: |
115
|
|
|
return False |
116
|
|
|
|
117
|
|
|
# ------------------------------------------------------------------------------------------------------------------ |
118
|
|
|
def __test_daemons_are_running(self): |
119
|
|
|
""" |
120
|
|
|
Tests all 3 daemons are running. |
121
|
|
|
""" |
122
|
|
|
# Test controller is running |
123
|
|
|
for daemon in ['controller', 'logger', 'spawner']: |
124
|
|
|
if not self.__test_daemon_is_running(daemon): |
125
|
|
|
if self.__message: |
126
|
|
|
self.__message += ' ' |
127
|
|
|
self.__message = '{} is not running'.format(daemon) |
128
|
|
|
|
129
|
|
|
if self.__message: |
130
|
|
|
self.__message = 'Enarksh CRITICAL - ' + self.__message |
131
|
|
|
self.__state = 2 |
132
|
|
|
else: |
133
|
|
|
self.__message = 'Enarksh OK - ' |
134
|
|
|
self.__state = 0 |
135
|
|
|
|
136
|
|
|
# ------------------------------------------------------------------------------------------------------------------ |
137
|
|
|
def __get_performance_data(self): |
138
|
|
|
""" |
139
|
|
|
Retrieves performance data from the controller. |
140
|
|
|
""" |
141
|
|
|
# Compose the message for the controller. |
142
|
|
|
message = NagiosMessage() |
143
|
|
|
|
144
|
|
|
# Send the message to the controller. |
145
|
|
|
self.__zmq_controller.send_pyobj(message) |
146
|
|
|
|
147
|
|
|
# Await the response from the controller. |
148
|
|
|
response = self.__zmq_controller.recv_pyobj() |
149
|
|
|
|
150
|
|
|
self.__performance_data += 'schedules={}, waiting={}, queued={}, running={}, completed={}, error={}'. \ |
151
|
|
|
format(response['sch_count'], |
152
|
|
|
response['rst_count'][C.ENK_RST_ID_WAITING], |
153
|
|
|
response['rst_count'][C.ENK_RST_ID_QUEUED], |
154
|
|
|
response['rst_count'][C.ENK_RST_ID_RUNNING], |
155
|
|
|
response['rst_count'][C.ENK_RST_ID_COMPLETED], |
156
|
|
|
response['rst_count'][C.ENK_RST_ID_ERROR]) |
157
|
|
|
|
158
|
|
|
self.__message += 'Schedules = {}, Waiting = {}, Queued = {}, Running = {}, Completed = {}, Error = {}'. \ |
159
|
|
|
format(response['sch_count'], |
160
|
|
|
response['rst_count'][C.ENK_RST_ID_WAITING], |
161
|
|
|
response['rst_count'][C.ENK_RST_ID_QUEUED], |
162
|
|
|
response['rst_count'][C.ENK_RST_ID_RUNNING], |
163
|
|
|
response['rst_count'][C.ENK_RST_ID_COMPLETED], |
164
|
|
|
response['rst_count'][C.ENK_RST_ID_ERROR]) |
165
|
|
|
|
166
|
|
|
# ------------------------------------------------------------------------------------------------------------------ |
167
|
|
|
def main(self): |
168
|
|
|
""" |
169
|
|
|
The main function of Nagios. |
170
|
|
|
""" |
171
|
|
|
# Initialize ZMQ. |
172
|
|
|
self.__zmq_init() |
173
|
|
|
|
174
|
|
|
self.__test_daemons_are_running() |
175
|
|
|
|
176
|
|
|
if self.__state == 0: |
177
|
|
|
self.__get_performance_data() |
178
|
|
|
|
179
|
|
|
print(self.__message + '|' + self.__performance_data) |
180
|
|
|
|
181
|
|
|
return self.__state |
182
|
|
|
|
183
|
|
|
# ------------------------------------------------------------------------------------------------------------------ |
184
|
|
|
def __zmq_init(self): |
185
|
|
|
""" |
186
|
|
|
Initializes ZMQ. |
187
|
|
|
""" |
188
|
|
|
config = Config.get() |
189
|
|
|
|
190
|
|
|
self.__zmq_context = zmq.Context() |
191
|
|
|
|
192
|
|
|
# Create socket for communicating with the controller. |
193
|
|
|
self.__zmq_controller = self.__zmq_context.socket(zmq.REQ) |
194
|
|
|
self.__zmq_controller.connect(config.get_controller_lockstep_end_point()) |
195
|
|
|
|
196
|
|
|
# ---------------------------------------------------------------------------------------------------------------------- |
197
|
|
|
|