-
Notifications
You must be signed in to change notification settings - Fork 0
/
settings.pas
290 lines (246 loc) · 8.93 KB
/
settings.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
{ --------------------------------------------------------------------------
godaemon
Settings unit
Copyright (c) Michael Nixon 2015.
Please see the LICENSE file for licensing information.
-------------------------------------------------------------------------- }
{ --------------------------------------------------------------------------
-------------------------------------------------------------------------- }
unit settings;
interface
uses sysutils, classes, logger, pipes, ipcpipe;
{ --------------------------------------------------------------------------
Globals
-------------------------------------------------------------------------- }
const
{ Program information }
{ Here comes the messy ifdef table from hell. I will abstract this out into
a lib of sorts at some point.... }
_programname = 'godaemon';
_compiledate = '20161001'; { this needs automating }
_version = 'v1.6.1-' +
{$ifdef fpc}
'fpc.' +
{$else}
'unknown_compiler.' +
{$endif}
{$ifdef CPUAMD64}
'amd64.' +
{$else}
{$ifdef CPU386}
'x86.' +
{$else}
{$ifdef CPUARM}
'arm' +
{$ifdef CPU32}
'(32-bit).' +
{$else}
{$ifdef CPU64}
'(64-bit).' +
{$else}
'(unknown).' +
{$endif}
{$endif}
{$else}
'unknown_cpu.' +
{$endif}
{$endif}
{$endif}
{$ifdef LINUX}
'linux';
{$else}
{$ifdef WINDOWS}
'windows';
{$else}
'unknown_os';
{$endif}
{$endif}
{ Default: Time in seconds between resetting the throttle }
_safety_throttle_timespan = 900;
{ Default: Number of failures before stopping }
_safety_throttle_count = 5;
{ Default: Number of milliseconds to wait as a minimum between restarts }
_safety_throttle_delay_ms = 3000;
{ Task actions }
_action_list_count = 7;
_action_list: array[0.._action_list_count - 1] of ansistring = (
'start', 'stop', 'status', 'nagios-status', 'info', 'reload', 'force-stop'
);
{ Blacklisted environment variable keys. Don't allow the user to set these }
_forbidden_env_list_count = 1;
_forbidden_env_list: array[0.._forbidden_env_list_count - 1] of ansistring = (
'GODAEMON'
);
{ Default system paths for PID and log files, suitable for linux }
_system_pid_path = '/var/run';
_system_log_path = '/var/log';
{ Nagios plugin status codes }
_nagios_status_ok = 0;
_nagios_status_warning = 1;
_nagios_status_critical = 2;
_nagios_status_unknown = 3;
type
{ Task action codes }
eAction = (eaStart, eaStop, eaStatus, eaNagiosStatus, eaInfo, eaReload, eaForceStop);
rPipes = record
{ A pair of pipes used to communicate between the godaemon "daemon"
process and the child task (sandbox) godaemon process to transfer log
output from the task to the parent }
sandboxReadPipe: TInputPipeStream;
sandboxWritePipe: TOutputPipeStream;
{ A pipe used to communicate between the godaemon shell process and the
daemon child when starting a new task }
taskStartupPipe: tipcPipe;
{ A pipe used to send control information between the daemon process
and the sandbox }
controlPipe: tipcPipe;
end;
rGlobalSettings = record
{ The number of lines to tail from the log file when sending an email }
logTailCount: longint;
{ Time in seconds between resetting the throttle }
throttleTimespan: longint;
{ Number of failures before stopping }
throttleCount: longint;
{ Number of milliseconds to wait as a minimum between restarts }
throttleDelay: longint;
{ Use exponential retry backoff ? }
throttleExponential: boolean;
{ Maximum seconds to limit exponential backoff delay to }
throttleExponentialLimit: longint;
{ true of we are running in the foreground }
foregroundMode: boolean;
{ The log file we are writing to }
logFile: ansistring;
{ The email address we will send notices to if enabled }
emailAddress: ansistring;
{ The program name we are daemonising }
daemonName: ansistring;
{ The full path and filename of the daemon }
daemonFilename: ansistring;
{ The full path to the daemon }
daemonPath: ansistring;
{ The full path and filename to the PID fike }
daemonPIDFile: ansistring;
{ Use system paths instead of the local directory for state files }
useSystemPaths: boolean;
{ Full path to status filename for enhanced monitoring (nagios) }
daemonStatusFilename: ansistring;
{ Write the daemon output to the log file? }
daemonCaptureOutput: boolean;
{ Restart the program if it fails? }
daemonRestartOnFailure: boolean;
{ Use safe restart throttling? }
daemonSafetyThrottle: boolean;
{ Use a PID file to limit concurrent instances? }
daemonUsePIDFile: boolean;
{ Send an email if the program fails? }
daemonEmailOnFailure: boolean;
{ Timeout when stopping the task (in seconds) }
daemonStopTimeout: longint;
{ Forcefully stop the task if the timeout is exceeded? }
daemonForceStop: boolean;
{ Change to a different user id / group id? }
daemonChangeUser: boolean;
{ User ID to change to }
daemonUserID: longint;
{ Raw field of User ID to change to from task file }
daemonUserIDField: ansistring;
{ Group ID to change to }
daemonGroupID: longint;
{ Raw field of Group ID to change to from task file }
daemonGroupIDField: ansistring;
{ Username to change to (used for display purposes) }
daemonUserName: ansistring;
{ Group name to change to (used for display purposes) }
daemonGroupName: ansistring;
{ True if we send a WARNING for a recently failed task }
daemonNRPEAlertRecentlyFailed: boolean;
{ True if we want to use advanced nagios monitoring }
daemonNRPEStatusMonitoring: boolean;
{ Eyecatch string to look for when using advanced nagios monitoring }
daemonNRPEEyecatch: ansistring;
{ Default status string to use when using advanced nagios monitoring }
daemonNRPEDefaultStatus: ansistring;
{ ID code of default nagios status }
daemonNRPEDefaultStatusID: longint;
{ String for default nagios status }
daemonNRPEDefaultStatusString: ansistring;
{ Timestamp of custom NRPE status update }
daemonNRPEStatusTS: longint;
{ Index of first arg to pass to the daemonised program }
firstParamArgIndex: longint;
{ Parameters for daemonised program }
params: tstringlist;
{ Additional environment variables to pass to the task. Key=value pairs }
environment: tstringlist;
{ Currently a daemon? }
daemonised: boolean;
{ Task recently failed? }
recentlyFailed: boolean;
{ Action to perform }
action: eAction;
end;
var
_pipes: rPipes;
_settings: rGlobalSettings;
_logger: tLogger;
_signalstop: boolean;
procedure InitialiseGlobalSettings;
{ --------------------------------------------------------------------------
-------------------------------------------------------------------------- }
implementation
{ --------------------------------------------------------------------------
Set global settings to sane defaults
Most of these are task file defaults as well (if they are missing from
the task file).
-------------------------------------------------------------------------- }
procedure InitialiseGlobalSettings;
begin
_settings.foregroundMode := false;
_settings.logFile := '';
_settings.emailAddress := '';
_settings.daemonName := '';
_settings.daemonFilename := '';
_settings.daemonPath := '';
_settings.daemonPIDFile := '';
_settings.daemonCaptureOutput := false;
_settings.daemonRestartOnFailure := false;
_settings.daemonSafetyThrottle := true;
_settings.daemonUsePIDFile := true;
_settings.daemonEmailOnFailure := false;
_settings.daemonised := false;
_settings.useSystemPaths := false;
_settings.logTailCount := 50;
_settings.throttleTimespan := _safety_throttle_timespan;
_settings.throttleCount := _safety_throttle_count;
_settings.throttleDelay := _safety_throttle_delay_ms;
_settings.throttleExponential := false;
_settings.throttleExponentialLimit := 30;
_settings.daemonStopTimeout := 10;
_settings.daemonForceStop := false;
_settings.daemonChangeUser := false;
_settings.daemonUserID := -1;
_settings.daemonGroupID := -1;
_settings.daemonUserIDField := '';
_settings.daemonGroupIDField := '';
_settings.daemonUserName := '';
_settings.daemonGroupName := '';
_settings.params := tstringlist.Create;
_settings.environment := tstringlist.Create;
_settings.daemonNRPEAlertRecentlyFailed := true;
_settings.daemonNRPEStatusMonitoring := false;
_settings.daemonNRPEEyecatch := '';
_settings.daemonNRPEDefaultStatus := 'UNKNOWN:No report yet';
_settings.daemonNRPEStatusTS := 0;
end;
initialization
begin
{ Safety }
_logger := nil;
_pipes.sandboxReadPipe := nil;
_pipes.sandboxWritePipe := nil;
_pipes.taskStartupPipe := nil;
_pipes.controlPipe := nil;
end;
end.