forked from cfengine/core
/
cf-monitord.c
318 lines (261 loc) · 9.96 KB
/
cf-monitord.c
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
/*
Copyright 2019 Northern.tech AS
This file is part of CFEngine 3 - written and maintained by Northern.tech AS.
This program is free software; you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by the
Free Software Foundation; version 3.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
To the extent this program is licensed as part of the Enterprise
versions of CFEngine, the applicable Commercial Open Source License
(COSL) may apply to this file if you as a licensee so wish it. See
included file COSL.txt.
*/
#include <generic_agent.h>
#include <mon.h>
#include <eval_context.h>
#include <env_monitor.h>
#include <conversion.h>
#include <vars.h>
#include <signals.h>
#include <scope.h>
#include <known_dirs.h>
#include <man.h>
#include <bootstrap.h>
#include <timeout.h>
#include <time_classes.h>
#include <loading.h>
#include <cleanup.h>
typedef enum
{
MONITOR_CONTROL_FORGET_RATE,
MONITOR_CONTROL_MONITOR_FACILITY,
MONITOR_CONTROL_HISTOGRAMS,
MONITOR_CONTROL_TCP_DUMP,
MONITOR_CONTROL_NONE
} MonitorControl;
static void ThisAgentInit(EvalContext *ctx);
static GenericAgentConfig *CheckOpts(int argc, char **argv);
static void KeepPromises(EvalContext *ctx, const Policy *policy);
/*****************************************************************************/
/* Globals */
/*****************************************************************************/
extern int NO_FORK;
extern const ConstraintSyntax CFM_CONTROLBODY[];
/*******************************************************************/
/* Command line options */
/*******************************************************************/
static const char *const CF_MONITORD_SHORT_DESCRIPTION =
"monitoring daemon for CFEngine";
static const char *const CF_MONITORD_MANPAGE_LONG_DESCRIPTION =
"cf-monitord is the monitoring daemon for CFEngine. It samples probes defined in policy code and attempts to learn the "
"normal system state based on current and past observations. Current estimates are made available as "
"special variables (e.g. $(mon.av_cpu)) to cf-agent, which may use them to inform policy decisions.";
static const struct option OPTIONS[] =
{
{"help", no_argument, 0, 'h'},
{"debug", no_argument, 0, 'd'},
{"verbose", no_argument, 0, 'v'},
{"dry-run", no_argument, 0, 'n'},
{"version", no_argument, 0, 'V'},
{"no-lock", no_argument, 0, 'K'},
{"file", required_argument, 0, 'f'},
{"log-level", required_argument, 0, 'g'},
{"inform", no_argument, 0, 'I'},
{"diagnostic", no_argument, 0, 'x'},
{"no-fork", no_argument, 0, 'F'},
{"histograms", no_argument, 0, 'H'},
{"tcpdump", no_argument, 0, 'T'},
{"color", optional_argument, 0, 'C'},
{"timestamp", no_argument, 0, 'l'},
{NULL, 0, 0, '\0'}
};
static const char *const HINTS[] =
{
"Print the help message",
"Enable debugging output",
"Output verbose information about the behaviour of cf-monitord",
"All talk and no action mode - make no changes, only inform of promises not kept",
"Output the version of the software",
"Ignore system lock",
"Specify an alternative input file than the default. This option is overridden by FILE if supplied as argument.",
"Specify how detailed logs should be. Possible values: 'error', 'warning', 'notice', 'info', 'verbose', 'debug'",
"Print basic information about changes made to the system, i.e. promises repaired",
"Activate internal diagnostics (developers only)",
"Run process in foreground, not as a daemon",
"Ignored for backward compatibility",
"Interface with tcpdump if available to collect data about network",
"Enable colorized output. Possible values: 'always', 'auto', 'never'. If option is used, the default value is 'auto'",
"Log timestamps on each line of log output",
NULL
};
/*****************************************************************************/
int main(int argc, char *argv[])
{
GenericAgentConfig *config = CheckOpts(argc, argv);
EvalContext *ctx = EvalContextNew();
GenericAgentConfigApply(ctx, config);
GenericAgentDiscoverContext(ctx, config);
Policy *policy = LoadPolicy(ctx, config);
GenericAgentPostLoadInit(ctx);
ThisAgentInit(ctx);
KeepPromises(ctx, policy);
MonitorStartServer(ctx, policy);
PolicyDestroy(policy);
GenericAgentFinalize(ctx, config);
return 0;
}
/*******************************************************************/
static GenericAgentConfig *CheckOpts(int argc, char **argv)
{
extern char *optarg;
int c;
GenericAgentConfig *config = GenericAgentConfigNewDefault(AGENT_TYPE_MONITOR, GetTTYInteractive());
while ((c = getopt_long(argc, argv, "dvnIf:g:VSxHTKMFhC::l",
OPTIONS, NULL)) != -1)
{
switch (c)
{
case 'f':
GenericAgentConfigSetInputFile(config, GetInputDir(), optarg);
MINUSF = true;
break;
case 'd':
LogSetGlobalLevel(LOG_LEVEL_DEBUG);
NO_FORK = true;
break;
case 'K':
config->ignore_locks = true;
break;
case 'I':
LogSetGlobalLevel(LOG_LEVEL_INFO);
break;
case 'v':
LogSetGlobalLevel(LOG_LEVEL_VERBOSE);
NO_FORK = true;
break;
case 'g':
LogSetGlobalLevelArgOrExit(optarg);
break;
case 'F':
NO_FORK = true;
break;
case 'H': /* Keep accepting this option for compatibility -- no longer used */
break;
case 'T':
MonNetworkSnifferEnable(true);
break;
case 'V':
{
Writer *w = FileWriter(stdout);
GenericAgentWriteVersion(w);
FileWriterDetach(w);
}
DoCleanupAndExit(EXIT_SUCCESS);
case 'h':
{
Writer *w = FileWriter(stdout);
WriterWriteHelp(w, "cf-monitord", OPTIONS, HINTS, true, NULL);
FileWriterDetach(w);
}
DoCleanupAndExit(EXIT_SUCCESS);
case 'M':
{
Writer *out = FileWriter(stdout);
ManPageWrite(out, "cf-monitord", time(NULL),
CF_MONITORD_SHORT_DESCRIPTION,
CF_MONITORD_MANPAGE_LONG_DESCRIPTION,
OPTIONS, HINTS,
true);
FileWriterDetach(out);
DoCleanupAndExit(EXIT_SUCCESS);
}
case 'x':
Log(LOG_LEVEL_ERR, "Self-diagnostic functionality is retired.");
DoCleanupAndExit(EXIT_SUCCESS);
case 'C':
if (!GenericAgentConfigParseColor(config, optarg))
{
DoCleanupAndExit(EXIT_FAILURE);
}
break;
case 'l':
LoggingEnableTimestamps(true);
break;
default:
{
Writer *w = FileWriter(stdout);
WriterWriteHelp(w, "cf-monitord", OPTIONS, HINTS, true, NULL);
FileWriterDetach(w);
}
DoCleanupAndExit(EXIT_FAILURE);
}
}
if (!GenericAgentConfigParseArguments(config, argc - optind, argv + optind))
{
Log(LOG_LEVEL_ERR, "Too many arguments");
DoCleanupAndExit(EXIT_FAILURE);
}
return config;
}
/*****************************************************************************/
static void KeepPromises(EvalContext *ctx, const Policy *policy)
{
Seq *constraints = ControlBodyConstraints(policy, AGENT_TYPE_MONITOR);
if (constraints)
{
for (size_t i = 0; i < SeqLength(constraints); i++)
{
Constraint *cp = SeqAt(constraints, i);
if (!IsDefinedClass(ctx, cp->classes))
{
continue;
}
VarRef *ref = VarRefParseFromScope(cp->lval, "control_monitor");
const void *value = EvalContextVariableGet(ctx, ref, NULL);
VarRefDestroy(ref);
if (!value)
{
Log(LOG_LEVEL_ERR, "Unknown lval '%s' in monitor control body", cp->lval);
continue;
}
if (strcmp(cp->lval, CFM_CONTROLBODY[MONITOR_CONTROL_HISTOGRAMS].lval) == 0)
{
/* Keep accepting this option for backward compatibility. */
}
if (strcmp(cp->lval, CFM_CONTROLBODY[MONITOR_CONTROL_TCP_DUMP].lval) == 0)
{
MonNetworkSnifferEnable(BooleanFromString(value));
}
if (strcmp(cp->lval, CFM_CONTROLBODY[MONITOR_CONTROL_FORGET_RATE].lval) == 0)
{
sscanf(value, "%lf", &FORGETRATE);
Log(LOG_LEVEL_DEBUG, "forget rate %f", FORGETRATE);
}
}
}
}
/*****************************************************************************/
/* Level 1 */
/*****************************************************************************/
static void ThisAgentInit(EvalContext *ctx)
{
umask(077);
strcpy(VPREFIX, "cf-monitord");
time_t t = SetReferenceTime();
UpdateTimeClasses(ctx, t);
signal(SIGINT, HandleSignalsForDaemon);
signal(SIGTERM, HandleSignalsForDaemon);
signal(SIGBUS, HandleSignalsForDaemon);
signal(SIGHUP, SIG_IGN);
signal(SIGPIPE, SIG_IGN);
signal(SIGUSR1, HandleSignalsForDaemon);
signal(SIGUSR2, HandleSignalsForDaemon);
FORGETRATE = 0.6;
MonitorInitialize();
}