Skip to content

Commit efb0789

Browse files
committed
Add and enable EventLog plugin.
1 parent 9594c33 commit efb0789

File tree

5 files changed

+245
-0
lines changed

5 files changed

+245
-0
lines changed

Makefile.am

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1679,6 +1679,24 @@ syslog_la_LDFLAGS = $(PLUGIN_LDFLAGS)
16791679
syslog_la_LIBADD = libcollectd.la
16801680
endif
16811681

1682+
if BUILD_PLUGIN_EVENTLOG
1683+
RCFLAGS = --preprocessor="${CC} -E -xc-header -DRC_INVOKED"
1684+
1685+
%.lo: %.mc
1686+
windmc $*.mc --headerdir=src --rcdir=src
1687+
$(LIBTOOL) $(AM_LIBTOOLFLAGS) $(LIBTOOLFLAGS) --tag=RC --mode=compile $(RC) $(RCFLAGS) $*.rc -o $*.lo
1688+
1689+
pkglib_LTLIBRARIES += eventlogres.la
1690+
eventlogres_la_SOURCES = src/eventlogres.mc src/eventlogres.c
1691+
eventlogres_la_LDFLAGS = $(PLUGIN_LDFLAGS)
1692+
1693+
pkglib_LTLIBRARIES += eventlog.la
1694+
eventlog_la_SOURCES = src/eventlog.c
1695+
eventlog_la_LDFLAGS = $(PLUGIN_LDFLAGS)
1696+
eventlog_la_LIBADD = libcollectd.la
1697+
eventlog_la_DEPENDENCIES = eventlogres.la
1698+
endif
1699+
16821700
if BUILD_PLUGIN_TABLE
16831701
pkglib_LTLIBRARIES += table.la
16841702
table_la_SOURCES = src/table.c

build.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,7 @@ build_windows ()
151151
--disable-all-plugins \
152152
--host="mingw32" \
153153
--enable-logfile \
154+
--enable-eventlog \
154155
--enable-network \
155156
--enable-wmi
156157

configure.ac

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ AC_PROG_CC_C99([],
3030

3131
AX_COMPILER_VENDOR
3232

33+
LT_PROG_RC
3334
AC_PROG_CXX
3435
AC_PROG_CPP
3536
AC_PROG_EGREP
@@ -6142,6 +6143,7 @@ plugin_dpdkevents="no"
61426143
plugin_dpdkstat="no"
61436144
plugin_entropy="no"
61446145
plugin_ethstat="no"
6146+
plugin_eventlog="no"
61456147
plugin_fhcount="no"
61466148
plugin_fscache="no"
61476149
plugin_gps="no"
@@ -6183,6 +6185,7 @@ plugin_virt="no"
61836185
plugin_vmem="no"
61846186
plugin_vserver="no"
61856187
plugin_wireless="no"
6188+
plugin_wmi="no"
61866189
plugin_write_prometheus="no"
61876190
plugin_xencpu="no"
61886191
plugin_zfs_arc="no"
@@ -6244,6 +6247,7 @@ if test "x$ac_system" = "xLinux"; then
62446247
fi
62456248
62466249
if test "x$ac_system" = "xWindows"; then
6250+
plugin_eventlog="yes"
62476251
plugin_wmi="yes"
62486252
fi
62496253
@@ -6562,6 +6566,7 @@ AC_PLUGIN([drbd], [$plugin_drbd], [DRBD statistics])
65626566
AC_PLUGIN([email], [yes], [EMail statistics])
65636567
AC_PLUGIN([entropy], [$plugin_entropy], [Entropy statistics])
65646568
AC_PLUGIN([ethstat], [$plugin_ethstat], [Stats from NIC driver])
6569+
AC_PLUGIN([eventlog], [$plugin_eventlog], [Windows event logging plugin])
65656570
AC_PLUGIN([exec], [yes], [Execution of external programs])
65666571
AC_PLUGIN([fhcount], [$plugin_fhcount], [File handles statistics])
65676572
AC_PLUGIN([filecount], [yes], [Count files in directories])
@@ -6701,6 +6706,12 @@ else
67016706
LOAD_PLUGIN_SYSLOG="##"
67026707
fi
67036708
6709+
if test "x$enable_eventlog" = "xyes"; then
6710+
default_log_plugin="eventlog"
6711+
else
6712+
LOAD_PLUGIN_EVENTLOG="##"
6713+
fi
6714+
67046715
if test "x$enable_logfile" = "xyes"; then
67056716
if test "x$default_log_plugin" = "xnone"; then
67066717
default_log_plugin="logfile"
@@ -6720,6 +6731,7 @@ fi
67206731
AC_MSG_RESULT([$default_log_plugin])
67216732
67226733
AC_SUBST([LOAD_PLUGIN_SYSLOG])
6734+
AC_SUBST([LOAD_PLUGIN_EVENTLOG])
67236735
AC_SUBST([LOAD_PLUGIN_LOGFILE])
67246736
AC_SUBST([LOAD_PLUGIN_LOG_LOGSTASH])
67256737
@@ -6982,6 +6994,7 @@ AC_MSG_RESULT([ drbd . . . . . . . . $enable_drbd])
69826994
AC_MSG_RESULT([ email . . . . . . . . $enable_email])
69836995
AC_MSG_RESULT([ entropy . . . . . . . $enable_entropy])
69846996
AC_MSG_RESULT([ ethstat . . . . . . . $enable_ethstat])
6997+
AC_MSG_RESULT([ eventlog . . . . . . $enable_eventlog])
69856998
AC_MSG_RESULT([ exec . . . . . . . . $enable_exec])
69866999
AC_MSG_RESULT([ fhcount . . . . . . . $enable_fhcount])
69877000
AC_MSG_RESULT([ filecount . . . . . . $enable_filecount])

src/eventlog.c

Lines changed: 208 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,208 @@
1+
/**
2+
* collectd - src/eventlog.c
3+
* Copyright (c) 2018 Google LLC
4+
*
5+
* Permission is hereby granted, free of charge, to any person obtaining a
6+
* copy of this software and associated documentation files (the "Software"),
7+
* to deal in the Software without restriction, including without limitation
8+
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
9+
* and/or sell copies of the Software, and to permit persons to whom the
10+
* Software is furnished to do so, subject to the following conditions:
11+
*
12+
* The above copyright notice and this permission notice shall be included in
13+
* all copies or substantial portions of the Software.
14+
*
15+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20+
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21+
* DEALINGS IN THE SOFTWARE.
22+
**/
23+
24+
#include "collectd.h"
25+
26+
#include "common.h"
27+
#include "plugin.h"
28+
29+
#include "eventlogres.h"
30+
31+
#include <windows.h>
32+
33+
#if COLLECT_DEBUG
34+
static int log_level = LOG_DEBUG;
35+
#else
36+
static int log_level = LOG_INFO;
37+
#endif /* COLLECT_DEBUG */
38+
static int notif_severity = 0;
39+
40+
static const char *config_keys[] = {
41+
"LogLevel", "NotifyLevel",
42+
};
43+
static int config_keys_num = STATIC_ARRAY_SIZE(config_keys);
44+
45+
static HANDLE event_source;
46+
static char log_prefix[512];
47+
48+
int event_type_from_priority(int event_id) {
49+
switch (event_id) {
50+
case LOG_INFO:
51+
return EVENTLOG_INFORMATION_TYPE;
52+
case LOG_NOTICE:
53+
return EVENTLOG_INFORMATION_TYPE;
54+
case LOG_DEBUG:
55+
return EVENTLOG_INFORMATION_TYPE;
56+
case LOG_ERR:
57+
return EVENTLOG_ERROR_TYPE;
58+
default:
59+
return EVENTLOG_WARNING_TYPE;
60+
}
61+
}
62+
63+
static void openlog(const char *ident) {
64+
sprintf(log_prefix, "%s[%d]", ident, getpid());
65+
event_source = RegisterEventSource(NULL, ident);
66+
if (event_source == NULL) {
67+
ERROR("eventlog: failed to register '%s' as an event source", ident);
68+
}
69+
}
70+
71+
static int closelog() {
72+
BOOL success = DeregisterEventSource(event_source);
73+
if (!success) {
74+
ERROR("eventlog: failed to deregister 'collectd' as an event source");
75+
return 1;
76+
}
77+
return 0;
78+
}
79+
80+
static void veventlog(int priority, const char *format, va_list args) {
81+
char msg[2056];
82+
vsprintf(msg, format, args);
83+
const char *messages[] = {log_prefix, msg};
84+
85+
BOOL success = ReportEvent(
86+
event_source,
87+
event_type_from_priority(priority), // wType
88+
0, // wCategory
89+
MSG_LOG, // dwEventID
90+
NULL, // lpUserSid
91+
2, // wNumStrings
92+
0, // dwDataSize
93+
messages, // lpStrings
94+
NULL // lpRawData
95+
);
96+
if (!success) {
97+
ERROR("eventlog: failed to report event to event log");
98+
}
99+
}
100+
101+
static void eventlog(int priority, const char *format, ...) {
102+
va_list args;
103+
va_start(args, format);
104+
veventlog(priority, format, args);
105+
va_end(args);
106+
}
107+
108+
static int el_config(const char *key, const char *value) {
109+
if (strcasecmp(key, "LogLevel") == 0) {
110+
log_level = parse_log_severity(value);
111+
if (log_level < 0) {
112+
log_level = LOG_INFO;
113+
ERROR("eventlog: invalid loglevel [%s] defaulting to 'info'", value);
114+
return 1;
115+
}
116+
} else if (strcasecmp(key, "NotifyLevel") == 0) {
117+
notif_severity = parse_notif_severity(value);
118+
if (notif_severity < 0)
119+
return 1;
120+
}
121+
122+
return 0;
123+
} /* int el_config */
124+
125+
static void el_log(int severity, const char *msg,
126+
user_data_t __attribute__((unused)) * user_data) {
127+
if (severity > log_level)
128+
return;
129+
130+
eventlog(severity, "%s", msg);
131+
} /* void el_log */
132+
133+
static int el_shutdown(void) {
134+
return closelog();
135+
}
136+
137+
static int el_notification(const notification_t *n,
138+
user_data_t __attribute__((unused)) * user_data) {
139+
char buf[1024] = "";
140+
size_t offset = 0;
141+
int log_severity;
142+
const char *severity_string;
143+
int status;
144+
145+
if (n->severity > notif_severity)
146+
return 0;
147+
148+
switch (n->severity) {
149+
case NOTIF_FAILURE:
150+
severity_string = "FAILURE";
151+
log_severity = LOG_ERR;
152+
break;
153+
case NOTIF_WARNING:
154+
severity_string = "WARNING";
155+
log_severity = LOG_WARNING;
156+
break;
157+
case NOTIF_OKAY:
158+
severity_string = "OKAY";
159+
log_severity = LOG_NOTICE;
160+
break;
161+
default:
162+
severity_string = "UNKNOWN";
163+
log_severity = LOG_ERR;
164+
}
165+
166+
#define BUFFER_ADD(...) \
167+
do { \
168+
status = snprintf(&buf[offset], sizeof(buf) - offset, __VA_ARGS__); \
169+
if (status < 1) \
170+
return -1; \
171+
else if (((size_t)status) >= (sizeof(buf) - offset)) \
172+
return -ENOMEM; \
173+
else \
174+
offset += ((size_t)status); \
175+
} while (0)
176+
177+
#define BUFFER_ADD_FIELD(field) \
178+
do { \
179+
if (n->field[0]) \
180+
BUFFER_ADD(", " #field " = %s", n->field); \
181+
} while (0)
182+
183+
BUFFER_ADD("Notification: severity = %s", severity_string);
184+
BUFFER_ADD_FIELD(host);
185+
BUFFER_ADD_FIELD(plugin);
186+
BUFFER_ADD_FIELD(plugin_instance);
187+
BUFFER_ADD_FIELD(type);
188+
BUFFER_ADD_FIELD(type_instance);
189+
BUFFER_ADD_FIELD(message);
190+
191+
#undef BUFFER_ADD_FIELD
192+
#undef BUFFER_ADD
193+
194+
buf[sizeof(buf) - 1] = '\0';
195+
196+
el_log(log_severity, buf, NULL);
197+
198+
return 0;
199+
} /* int el_notification */
200+
201+
void module_register(void) {
202+
openlog("collectd");
203+
204+
plugin_register_config("eventlog", el_config, config_keys, config_keys_num);
205+
plugin_register_log("eventlog", el_log, /* user_data = */ NULL);
206+
plugin_register_notification("eventlog", el_notification, NULL);
207+
plugin_register_shutdown("eventlog", el_shutdown);
208+
} /* void module_register(void) */

src/eventlogres.mc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
MessageId=0x1
2+
SymbolicName=MSG_LOG
3+
Language=English
4+
%1: %2
5+
.

0 commit comments

Comments
 (0)