-
Notifications
You must be signed in to change notification settings - Fork 0
/
actions.pas
374 lines (336 loc) · 11.5 KB
/
actions.pas
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
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
{ --------------------------------------------------------------------------
godaemon
Actions unit
Copyright (c) Michael Nixon 2015.
Please see the LICENSE file for licensing information.
-------------------------------------------------------------------------- }
{ --------------------------------------------------------------------------
-------------------------------------------------------------------------- }
unit actions;
interface
uses baseunix, unix, unixutil, sysutils, classes, logger;
function DoActionStart: longint;
function DoActionStatus: longint;
function DoActionStop: longint;
function DoActionNagiosStatus: longint;
function DoActionInfo: longint;
function DoActionReload: longint;
function DoActionForceStop: longint;
{ --------------------------------------------------------------------------
-------------------------------------------------------------------------- }
implementation
uses settings, mainapp, fork, commandline, nrpe, ipcpipe;
{ --------------------------------------------------------------------------
Handle the "force stop" action - send SIGKILL to the task
Returns 0 if the task is running, or 1 if it is not running
-------------------------------------------------------------------------- }
function DoActionForceStop: longint;
const
waittimeout = 3000;
var
pid: longint;
killError: longint;
begin
if PIDFileExists then begin
pid := PIDReadFromFile;
if PIDIsAlive(pid) then begin
if PIDIsGoDaemon(pid) then begin
{ Try to stop this process }
FGLog(_settings.daemonName + ' is alive with pid = ' + inttostr(pid));
fpkill(pid, SIGUSR1);
killError := fpgeterrno;
if (killError = ESysEINVAL) or (killError = ESysESRCH) or (killError = ESysEPERM) then begin
writeln('Could not signal the daemon. Do you have enough permissions? (sudo)');
result := 1;
exit;
end;
writeln('Requested a forceful stop.');
writeln('Waiting...');
sleep(waittimeout);
if PIDFileExists then begin
pid := PIDReadFromFile;
if PIDIsAlive(pid) then begin
if PIDIsGoDaemon(pid) then begin
writeln('FATAL: Task is still running! (Try using sudo)');
result := 1;
exit;
end;
end;
end;
writeln('Successfully stopped the task.');
result := 0;
exit;
end;
end;
end;
FGLog('Task was not running');
FGLog(_settings.daemonName + ' status: Stopped');
result := 1;
end;
{ --------------------------------------------------------------------------
Handle the "reload" action - send SIGHUP to the task
Returns 0 if the task is running, or 1 if it is not running
-------------------------------------------------------------------------- }
function DoActionReload: longint;
var
pid: longint;
killError: longint;
begin
if PIDFileExists then begin
pid := PIDReadFromFile;
if PIDIsAlive(pid) then begin
if PIDIsGoDaemon(pid) then begin
{ Try to stop this process }
FGLog(_settings.daemonName + ' is alive with pid = ' + inttostr(pid));
fpkill(pid, SIGHUP);
killError := fpgeterrno;
if (killError = ESysEINVAL) or (killError = ESysESRCH) or (killError = ESysEPERM) then begin
writeln('Could not signal the daemon. Do you have enough permissions? (sudo)');
result := 1;
exit;
end;
writeln('SIGHUP sent to daemon.');
result := 0;
exit;
end;
end;
end;
FGLog('Task was not running');
FGLog(_settings.daemonName + ' status: Stopped');
result := 1;
end;
{ --------------------------------------------------------------------------
Handle the "info" action
Returns the process exit code
-------------------------------------------------------------------------- }
function DoActionInfo: longint;
const
funcname = 'DoActionInfo(): ';
begin
PrintInfo;
result := 0;
end;
{ --------------------------------------------------------------------------
Act as a nagios plugin and check on the status of the task.
Prints the nagios status to stdout in the standard nagios format and
returns a process exit code of:
0: Status OK (task is running and no problems)
1: Status WARNING (task is running, but some problems - failures)
2: Status CRITICAL (task is not running)
3: Status UNKNOWN (could not determine task status)
i -------------------------------------------------------------------------- }
function DoActionNagiosStatus: longint;
const
funcname = 'DoActionNagiosStatus(): ';
MAX_CHECK_ATTEMPTS = 3;
CHECK_ATTEMPT_DELAY = 300; { Must be different to the usual tick rate }
{ MAX_CHECK_ATTEMPTS * CHECK_ATTEMPT_DELAY must be lower than icinga's
maximum waiting time for a check }
var
status: longint;
statusMessage: ansistring;
timestamp: longint;
running: boolean;
pid: longint;
checkAttempt: longint;
begin
running := false;
if PIDFileExists then begin
pid := PIDReadFromFile;
if PIDIsAlive(pid) then begin
if PIDIsGoDaemon(pid) then begin
running := true;
end;
end;
end;
if running then begin
{ Task is running so proceed with other checks }
if _settings.daemonNRPEStatusMonitoring then begin
{ Advanced monitoring }
{ There is a small chance that the status file is being updated at the
exact time we read it. We will try several times to mitigate this.
Due to the godaemon design, this will not fail on successive attempts
unless there is another cause }
checkAttempt := 0;
while not GetNagiosStatus(status, statusMessage, timestamp) do begin
inc(checkAttempt);
if checkAttempt >= MAX_CHECK_ATTEMPTS then begin
writeln('godaemon (advanced monitoring): Daemon is running, but could not get status');
result := _nagios_status_unknown;
exit;
end;
sleep(CHECK_ATTEMPT_DELAY);
end;
writeln(statusMessage);
result := status;
end else begin
{ Simple monitoring }
writeln('godaemon (simple monitoring): Daemon is running');
result := _nagios_status_ok;
end;
end else begin
{ Task is not running - critical status }
writeln('godaemon: Daemon is not running');
result := _nagios_status_critical;
end;
end;
{ --------------------------------------------------------------------------
Handle the "stop" action - get information about a task
Returns 0 if the task is running, or 1 if it is not running
-------------------------------------------------------------------------- }
function DoActionStop: longint;
const
tasknotrunning = 'Task was not running';
var
pid: longint;
timeout: longint;
killError: longint;
stoptimeout: longint;
begin
if PIDFileExists then begin
pid := PIDReadFromFile;
if PIDIsAlive(pid) then begin
if PIDIsGoDaemon(pid) then begin
{ Try to stop this process }
FGLog(_settings.daemonName + ' is alive with pid = ' + inttostr(pid));
write('Waiting for the task to stop [');
fpkill(pid, SIGTERM);
killError := fpgeterrno;
if (killError = ESysEINVAL) or (killError = ESysESRCH) or (killError = ESysEPERM) then begin
writeln('.] - FAILED');
writeln('Could not signal the daemon. Do you have enough permissions? (sudo)');
result := 0;
exit;
end;
timeout := 0;
stoptimeout := _settings.daemonStopTimeout * 2;
while PIDIsAlive(pid) do begin
sleep(500);
write('. ');
inc(timeout);
{ We need to time out incase this is called by a script }
if timeout > stoptimeout then begin
if _settings.daemonForceStop then begin
writeln('.] - TIMED OUT');
result := DoActionForceStop;
exit;
end else begin
writeln('.] - FAILED');
FGLog('Timed out while stopping the task. Try the ''force-stop'' action.');
result := 0;
exit;
end;
end;
end;
writeln('.] - OK');
end else begin
FGLog(tasknotrunning);
end;
end else begin
FGLog(tasknotrunning);
end;
end else begin
FGLog(tasknotrunning);
end;
FGLog(_settings.daemonName + ' status: Stopped');
result := 1;
end;
{ --------------------------------------------------------------------------
Handle the "status" action - get information about a task
Returns 0 if the task is running, or 1 if it is not running
-------------------------------------------------------------------------- }
function DoActionStatus: longint;
const
funcname = 'DoActionStatus(): ';
var
pid: longint;
status: longint;
begin
{ default = not running }
status := 1;
if PIDFileExists then begin
pid := PIDReadFromFile;
if PIDIsAlive(pid) then begin
if PIDIsGoDaemon(pid) then begin
status := 0;
end;
end;
end;
if status = 0 then begin
FGLog(_settings.daemonName + ' status: Running');
end else begin
FGLog(_settings.daemonName + ' status: Stopped');
end;
result := status;
end;
{ --------------------------------------------------------------------------
Handle the "start" action
Returns the process exit code
-------------------------------------------------------------------------- }
function DoActionStart: longint;
const
funcname = 'DoActionStart(): ';
var
endpoint: tipcPipeEndpoint;
begin
{ We use a pipe to communicate a successful start to the parent. }
_pipes.taskStartupPipe := tipcPipe.Create;
if not _pipes.taskStartupPipe.CreateEndpoints then begin
FGLog(funcname + 'Cannot create taskStartupPipe');
result := 1;
exit;
end;
FGLog('Starting job: ' + _settings.daemonName);
{ If we need to change user id / group id, validate these now }
if _settings.daemonChangeUser then begin
if not GetUserAndGroupIDs then begin
FGLog(funcname + 'Cannot determine user/group IDs - stopping');
result := 1;
exit;
end;
end else begin
FGLog(funcname + 'Running the task as the current user which might be unsafe.');
end;
if not _settings.foregroundMode then begin
{ Daemonise }
if not Daemonise(true, '[godaemon] ' + _settings.daemonName) then begin
FGLog(funcname + 'Daemonise() failed - out of resources?');
result := 1;
exit;
end;
{ We are a daemon! Only the daemon child carries on executing here. The
parent branches off inside Daemonise to parentstart.pas }
end else begin
{ Foreground mode }
FGLog(funcname + 'Running in the foreground.');
end;
{ Prepare to communicate with the parent }
endpoint := _pipes.taskStartupPipe.GetChildEndpoint;
{ Initialise the logger }
StartLogger;
{ Only allow one copy of the task }
if _settings.daemonUsePIDFile then begin
if not StartAllowOneProcess then begin
_logger.Log(funcname + 'This task is already running (PID file exists with valid PID) - stopping');
if not _settings.foregroundMode then begin
endpoint.SendString('This task is already running');
end;
StopLogger;
result := 1;
exit;
end;
end;
{ Start handling unix signals }
StartSignalHandler;
{ Hand off to the task process (mainapp.pas). More communication with the
parent process occurs there }
result := StartTaskProcess;
_logger.Log(funcname + 'Shutting down with ExitCode ' + inttostr(ExitCode));
{ Cleanup }
if _settings.daemonUsePIDFile then begin
FinishAllowOneProcess;
end;
StopSignalHandler;
StopLogger;
end;
end.