- cpufreq
CPU frequency (For laptops with speed step or a similar technology)
+ - cpusleep
+ CPU sleep: Time spent in suspend (For mobile devices which enter suspend automatically)
+
- curl
Parse statistics from websites using regular expressions.
have_termios_h="no"
AC_CHECK_HEADERS(termios.h, [have_termios_h="yes"])
+# For cpusleep plugin
+AC_CACHE_CHECK([whether clock_boottime and clock_monotonic are supported],
+ [c_cv_have_clock_boottime_monotonic],
+ AC_COMPILE_IFELSE([AC_LANG_PROGRAM(
+[[
+#include <time.h>
+]],
+[[
+ struct timespec b, m;
+ clock_gettime(CLOCK_BOOTTIME, &b );
+ clock_gettime(CLOCK_MONOTONIC, &m );
+]]
+ )],
+ [c_cv_have_clock_boottime_monotonic="yes"],
+ [c_cv_have_clock_boottime_monotonic="no"]))
+
+
# For the turbostat plugin
have_asm_msrindex_h="no"
AC_CHECK_HEADERS(asm/msr-index.h, [have_asm_msrindex_h="yes"])
fi
if test "x$have_getmntent" = "xsun"; then
AC_DEFINE(HAVE_SUN_GETMNTENT, 1,
- [Define if the function getmntent exists. It's the version from libsun.])
+ [Define if the function getmntent exists. It is the version from libsun.])
fi
if test "x$have_getmntent" = "xseq"; then
AC_DEFINE(HAVE_SEQ_GETMNTENT, 1,
- [Define if the function getmntent exists. It's the version from libseq.])
+ [Define if the function getmntent exists. It is the version from libseq.])
fi
if test "x$have_getmntent" = "xgen"; then
AC_DEFINE(HAVE_GEN_GETMNTENT, 1,
- [Define if the function getmntent exists. It's the version from libgen.])
+ [Define if the function getmntent exists. It is the version from libgen.])
fi
# Check for htonll
fi
])
-if test "x$with_libiptc" = "xpkgconfig" && test "x$PKG_CONFIG" = "x"
-then
- with_libiptc="no (Don't have pkg-config)"
-fi
-
if test "x$with_libiptc" = "xpkgconfig"
then
$PKG_CONFIG --exists 'libiptc' 2>/dev/null
# configure using pkg-config
if test "x$with_libmodbus" = "xuse_pkgconfig"
then
- if test "x$PKG_CONFIG" = "x"
- then
- with_libmodbus="no (Don't have pkg-config)"
- fi
-fi
-if test "x$with_libmodbus" = "xuse_pkgconfig"
-then
AC_MSG_NOTICE([Checking for libmodbus using $PKG_CONFIG])
$PKG_CONFIG --exists 'libmodbus' 2>/dev/null
if test $? -ne 0
with_libmnl="no (Linux only library)"
fi
])
-if test "x$PKG_CONFIG" = "x"
-then
- with_libmnl="no (Don't have pkg-config)"
-fi
if test "x$with_libmnl" = "xyes"
then
if $PKG_CONFIG --exists libmnl 2>/dev/null; then
SAVE_CFLAGS="$CFLAGS"
SAVE_LIBS="$LIBS"
dnl ARCHFLAGS="" -> disable multi -arch on OSX (see Config_heavy.pl:fetch_string)
- PERL_CFLAGS=`ARCHFLAGS="" $perl_interpreter -MExtUtils::Embed -e ccopts`
+ PERL_CFLAGS=`ARCHFLAGS="" $perl_interpreter -MExtUtils::Embed -e perl_inc`
PERL_LIBS=`ARCHFLAGS="" $perl_interpreter -MExtUtils::Embed -e ldopts`
CFLAGS="$CFLAGS $PERL_CFLAGS"
LIBS="$LIBS $PERL_LIBS"
AM_CONDITIONAL(BUILD_WITH_LM_SENSORS, test "x$with_libsensors" = "xyes")
# }}}
-# --with-libsigrok {{{
-with_libsigrok_cflags=""
-with_libsigrok_ldflags=""
-AC_ARG_WITH(libsigrok, [AS_HELP_STRING([--with-libsigrok@<:@=PREFIX@:>@], [Path to libsigrok.])],
-[
- if test "x$withval" = "xno"
- then
- with_libsigrok="no"
- else
- with_libsigrok="yes"
- if test "x$withval" != "xyes"
- then
- with_libsigrok_cflags="-I$withval/include"
- with_libsigrok_ldflags="-L$withval/lib"
- fi
- fi
-],[with_libsigrok="yes"])
-
-# libsigrok has a glib dependency
-if test "x$with_libsigrok" = "xyes"
-then
-m4_ifdef([AM_PATH_GLIB_2_0],
- [
- AM_PATH_GLIB_2_0([2.28.0],
- [with_libsigrok_cflags="$with_libsigrok_cflags $GLIB_CFLAGS"; with_libsigrok_ldflags="$with_libsigrok_ldflags $GLIB_LIBS"])
- ],
- [
- with_libsigrok="no (glib not available)"
- ]
+# libsigrok {{{
+AC_SUBST([LIBSIGROK_CFLAGS])
+AC_SUBST([LIBSIGROK_LIBS])
+PKG_CHECK_MODULES([LIBSIGROK], [libsigrok < 0.4],
+ [with_libsigrok="yes"],
+ [with_libsigrok="no (pkg-config could not find libsigrok)"]
)
-fi
-
-# libsigrok headers
-if test "x$with_libsigrok" = "xyes"
-then
- SAVE_CPPFLAGS="$CPPFLAGS"
- CPPFLAGS="$CPPFLAGS $with_libsigrok_cflags"
-
- AC_CHECK_HEADERS(libsigrok/libsigrok.h, [], [with_libsigrok="no (libsigrok/libsigrok.h not found)"])
-
- CPPFLAGS="$SAVE_CPPFLAGS"
-fi
-
-# libsigrok library
-if test "x$with_libsigrok" = "xyes"
-then
- SAVE_CPPFLAGS="$CPPFLAGS"
- SAVE_LDFLAGS="$LDFLAGS"
- CPPFLAGS="$CPPFLAGS $with_libsigrok_cflags"
- LDFLAGS="$LDFLAGS $with_libsigrok_ldflags"
-
- AC_CHECK_LIB(sigrok, sr_init,
- [
- AC_DEFINE(HAVE_LIBSIGROK, 1, [Define to 1 if you have the sigrok library (-lsigrok).])
- ],
- [with_libsigrok="no (libsigrok not found)"])
-
- CPPFLAGS="$SAVE_CPPFLAGS"
- LDFLAGS="$SAVE_LDFLAGS"
-fi
-if test "x$with_libsigrok" = "xyes"
-then
- BUILD_WITH_LIBSIGROK_CFLAGS="$with_libsigrok_cflags"
- BUILD_WITH_LIBSIGROK_LDFLAGS="$with_libsigrok_ldflags"
- AC_SUBST(BUILD_WITH_LIBSIGROK_CFLAGS)
- AC_SUBST(BUILD_WITH_LIBSIGROK_LDFLAGS)
-fi
-AM_CONDITIONAL(BUILD_WITH_LIBSIGROK, test "x$with_libsigrok" = "xyes")
# }}}
# --with-libstatgrab {{{
if test "x$with_libstatgrab" = "xyes" \
&& test "x$with_libstatgrab_pkg_config" = "xyes"
then
- if test "x$PKG_CONFIG" != "x"
+ AC_MSG_CHECKING([pkg-config for libstatgrab])
+ temp_result="found"
+ $PKG_CONFIG --exists libstatgrab 2>/dev/null
+ if test "$?" != "0"
then
- AC_MSG_CHECKING([pkg-config for libstatgrab])
- temp_result="found"
- $PKG_CONFIG --exists libstatgrab 2>/dev/null
- if test "$?" != "0"
- then
- with_libstatgrab_pkg_config="no"
- with_libstatgrab="no (pkg-config doesn't know libstatgrab)"
- temp_result="not found"
- fi
- AC_MSG_RESULT([$temp_result])
- else
- AC_MSG_NOTICE([pkg-config not available, trying to guess flags for the statgrab library.])
with_libstatgrab_pkg_config="no"
- with_libstatgrab_ldflags="$with_libstatgrab_ldflags -lstatgrab"
+ with_libstatgrab="no (pkg-config doesn't know libstatgrab)"
+ temp_result="not found"
fi
+ AC_MSG_RESULT([$temp_result])
fi
if test "x$with_libstatgrab" = "xyes" \
# configure using pkg-config
if test "x$with_libupsclient" = "xuse_pkgconfig"
then
- if test "x$PKG_CONFIG" = "x"
- then
- with_libupsclient="no (Don't have pkg-config)"
- fi
-fi
-if test "x$with_libupsclient" = "xuse_pkgconfig"
-then
AC_MSG_NOTICE([Checking for libupsclient using $PKG_CONFIG])
$PKG_CONFIG --exists 'libupsclient' 2>/dev/null
if test $? -ne 0
# configure using pkg-config
if test "x$with_libvarnish" = "xuse_pkgconfig"
then
- if test "x$PKG_CONFIG" = "x"
- then
- with_libvarnish="no (Don't have pkg-config)"
- fi
-fi
-if test "x$with_libvarnish" = "xuse_pkgconfig"
-then
AC_MSG_NOTICE([Checking for varnishapi using $PKG_CONFIG])
$PKG_CONFIG --exists 'varnishapi' 2>/dev/null
if test $? -ne 0
with_libvirt="no (pkg-config isn't available)"
with_libvirt_cflags=""
with_libvirt_ldflags=""
-if test "x$PKG_CONFIG" != "x"
+$PKG_CONFIG --exists 'libxml-2.0' 2>/dev/null
+if test "$?" = "0"
then
- $PKG_CONFIG --exists 'libxml-2.0' 2>/dev/null
- if test "$?" = "0"
- then
- with_libxml2="yes"
- else
- with_libxml2="no (pkg-config doesn't know libxml-2.0)"
- fi
+ with_libxml2="yes"
+else
+ with_libxml2="no (pkg-config doesn't know libxml-2.0)"
+fi
- $PKG_CONFIG --exists libvirt 2>/dev/null
- if test "$?" = "0"
- then
- with_libvirt="yes"
- else
- with_libvirt="no (pkg-config doesn't know libvirt)"
- fi
+$PKG_CONFIG --exists libvirt 2>/dev/null
+if test "$?" = "0"
+then
+ with_libvirt="yes"
+else
+ with_libvirt="no (pkg-config doesn't know libvirt)"
fi
if test "x$with_libxml2" = "xyes"
then
with_libopenipmipthread_cflags=""
with_libopenipmipthread_libs=""
-AC_MSG_CHECKING([for pkg-config])
-temp_result="no"
-if test "x$PKG_CONFIG" = "x"
-then
- with_libopenipmipthread="no"
- temp_result="no"
-else
- temp_result="$PKG_CONFIG"
-fi
-AC_MSG_RESULT([$temp_result])
-
if test "x$with_libopenipmipthread" = "xyes"
then
AC_MSG_CHECKING([for libOpenIPMIpthread])
plugin_contextswitch="no"
plugin_cpu="no"
plugin_cpufreq="no"
+plugin_cpusleep="no"
plugin_curl_json="no"
plugin_curl_xml="no"
plugin_df="no"
then
plugin_turbostat="yes"
fi
+
+ if test "x$c_cv_have_clock_boottime_monotonic" = "xyes"
+ then
+ plugin_cpusleep="yes"
+ fi
fi
if test "x$ac_system" = "xOpenBSD"
AC_PLUGIN([contextswitch], [$plugin_contextswitch], [context switch statistics])
AC_PLUGIN([cpu], [$plugin_cpu], [CPU usage statistics])
AC_PLUGIN([cpufreq], [$plugin_cpufreq], [CPU frequency statistics])
+AC_PLUGIN([cpusleep], [$plugin_cpusleep], [CPU sleep statistics])
AC_PLUGIN([csv], [yes], [CSV output plugin])
AC_PLUGIN([curl], [$with_libcurl], [CURL generic web statistics])
AC_PLUGIN([curl_json], [$plugin_curl_json], [CouchDB statistics])
AC_MSG_RESULT([ contextswitch . . . . $enable_contextswitch])
AC_MSG_RESULT([ cpu . . . . . . . . . $enable_cpu])
AC_MSG_RESULT([ cpufreq . . . . . . . $enable_cpufreq])
+AC_MSG_RESULT([ cpusleep . . . . . . $enable_cpusleep])
AC_MSG_RESULT([ csv . . . . . . . . . $enable_csv])
AC_MSG_RESULT([ curl . . . . . . . . $enable_curl])
AC_MSG_RESULT([ curl_json . . . . . . $enable_curl_json])
check_PROGRAMS =
TESTS =
+noinst_LTLIBRARIES += libformat_json.la
+libformat_json_la_SOURCES = utils_format_json.c utils_format_json.h
+libformat_json_la_CPPFLAGS = $(AM_CPPFLAGS)
+libformat_json_la_LDFLAGS = $(AM_LDFLAGS)
+libformat_json_la_LIBADD =
+if BUILD_WITH_LIBYAJL
+libformat_json_la_CPPFLAGS += $(BUILD_WITH_LIBYAJL_CPPFLAGS)
+libformat_json_la_LDFLAGS += $(BUILD_WITH_LIBYAJL_LDFLAGS)
+libformat_json_la_LIBADD += $(BUILD_WITH_LIBYAJL_LIBS)
+check_PROGRAMS += test_format_json
+TESTS += test_format_json
+test_format_json_SOURCES = utils_format_json_test.c testing.h
+test_format_json_LDADD = libformat_json.la daemon/libmetadata.la daemon/libplugin_mock.la -lm
+endif
+
noinst_LTLIBRARIES += liblatency.la
liblatency_la_SOURCES = utils_latency.c utils_latency.h
check_PROGRAMS += test_utils_latency
amqp_la_SOURCES = amqp.c \
utils_cmd_putval.c utils_cmd_putval.h \
utils_parse_option.c utils_parse_option.h \
- utils_format_graphite.c utils_format_graphite.h \
- utils_format_json.c utils_format_json.h
+ utils_format_graphite.c utils_format_graphite.h
amqp_la_LDFLAGS = $(PLUGIN_LDFLAGS) $(BUILD_WITH_LIBRABBITMQ_LDFLAGS)
amqp_la_CPPFLAGS = $(AM_CPPFLAGS) $(BUILD_WITH_LIBRABBITMQ_CPPFLAGS)
-amqp_la_LIBADD = $(BUILD_WITH_LIBRABBITMQ_LIBS)
+amqp_la_LIBADD = $(BUILD_WITH_LIBRABBITMQ_LIBS) libformat_json.la
endif
if BUILD_PLUGIN_APACHE
cpufreq_la_LDFLAGS = $(PLUGIN_LDFLAGS)
endif
+if BUILD_PLUGIN_CPUSLEEP
+pkglib_LTLIBRARIES += cpusleep.la
+cpusleep_la_SOURCES = cpusleep.c
+cpusleep_la_LDFLAGS = $(PLUGIN_LDFLAGS)
+endif
+
if BUILD_PLUGIN_CSV
pkglib_LTLIBRARIES += csv.la
csv_la_SOURCES = csv.c
if BUILD_PLUGIN_SIGROK
pkglib_LTLIBRARIES += sigrok.la
sigrok_la_SOURCES = sigrok.c
-sigrok_la_CFLAGS = $(AM_CFLAGS) $(BUILD_WITH_LIBSIGROK_CFLAGS)
-sigrok_la_LDFLAGS = $(PLUGIN_LDFLAGS) $(BUILD_WITH_LIBSIGROK_LDFLAGS)
-sigrok_la_LIBADD = -lsigrok
+sigrok_la_CFLAGS = $(AM_CFLAGS) $(LIBSIGROK_CFLAGS)
+sigrok_la_LDFLAGS = $(PLUGIN_LDFLAGS)
+sigrok_la_LIBADD = $(LIBSIGROK_LIBS)
endif
if BUILD_PLUGIN_SMART
if BUILD_PLUGIN_WRITE_GRAPHITE
pkglib_LTLIBRARIES += write_graphite.la
write_graphite_la_SOURCES = write_graphite.c \
- utils_format_graphite.c utils_format_graphite.h \
- utils_format_json.c utils_format_json.h
+ utils_format_graphite.c utils_format_graphite.h
write_graphite_la_LDFLAGS = $(PLUGIN_LDFLAGS)
+write_graphite_la_LIBADD = libformat_json.la
endif
if BUILD_PLUGIN_WRITE_HTTP
pkglib_LTLIBRARIES += write_http.la
write_http_la_SOURCES = write_http.c \
- utils_format_json.c utils_format_json.h \
utils_format_kairosdb.c utils_format_kairosdb.h
-write_http_la_CFLAGS = $(AM_CFLAGS) $(BUILD_WITH_LIBCURL_CFLAGS)
write_http_la_LDFLAGS = $(PLUGIN_LDFLAGS)
-write_http_la_LIBADD = $(BUILD_WITH_LIBCURL_LIBS)
+write_http_la_CFLAGS = $(AM_CFLAGS) $(BUILD_WITH_LIBCURL_CFLAGS)
+write_http_la_LIBADD = $(BUILD_WITH_LIBCURL_LIBS) libformat_json.la
endif
if BUILD_PLUGIN_WRITE_KAFKA
pkglib_LTLIBRARIES += write_kafka.la
write_kafka_la_SOURCES = write_kafka.c \
utils_format_graphite.c utils_format_graphite.h \
- utils_format_json.c utils_format_json.h \
utils_cmd_putval.c utils_cmd_putval.h \
utils_crc32.c utils_crc32.h
write_kafka_la_CPPFLAGS = $(AM_CPPFLAGS) $(BUILD_WITH_LIBRDKAFKA_CPPFLAGS)
write_kafka_la_LDFLAGS = $(PLUGIN_LDFLAGS) $(BUILD_WITH_LIBRDKAFKA_LDFLAGS)
-write_kafka_la_LIBADD = $(BUILD_WITH_LIBRDKAFKA_LIBS)
+write_kafka_la_LIBADD = $(BUILD_WITH_LIBRDKAFKA_LIBS) libformat_json.la
endif
if BUILD_PLUGIN_WRITE_LOG
=item *
-B<2.> collectd will block I<SIGINT>. Pressing I<Ctrl+C> will usually cause
+B<2.> Python will be handling I<SIGINT>. Pressing I<Ctrl+C> will usually cause
collectd to shut down. This would be problematic in an interactive session,
-therefore this signal will be blocked. You can still use it to interrupt
-syscalls like sleep and pause but it won't generate a I<KeyboardInterrupt>
-exception either.
+therefore Python will be handling it in interactive sessions. This allows you
+to use I<Ctrl+C> to interrupt Python code without killing collectd. This also
+means you can catch I<KeyboardInterrupt> exceptions which does not work during
+normal operation.
To quit collectd send I<EOF> (press I<Ctrl+D> at the beginning of a new line).
#@BUILD_PLUGIN_CONTEXTSWITCH_TRUE@LoadPlugin contextswitch
@BUILD_PLUGIN_CPU_TRUE@@BUILD_PLUGIN_CPU_TRUE@LoadPlugin cpu
#@BUILD_PLUGIN_CPUFREQ_TRUE@LoadPlugin cpufreq
+#@BUILD_PLUGIN_CPUSLEEP_TRUE@LoadPlugin cpusleep
@LOAD_PLUGIN_CSV@LoadPlugin csv
#@BUILD_PLUGIN_CURL_TRUE@LoadPlugin curl
#@BUILD_PLUGIN_CURL_JSON_TRUE@LoadPlugin curl_json
# Header "X-Custom-Header: custom_value"
# SSLVersion "TLSv1"
# Format "Command"
+# Metrics true
+# Notifications false
# StoreRates false
# BufferSize 4096
# LowSpeedLimit 0
sure B<cpufreqd> (L<http://cpufreqd.sourceforge.net/>) or a similar tool is
installed and an "cpu governor" (that's a kernel module) is loaded.
+=head2 Plugin C<cpusleep>
+
+This plugin doesn't have any options. It reads CLOCK_BOOTTIME and
+CLOCK_MONOTONIC and reports the difference between these clocks. Since
+BOOTTIME clock increments while device is suspended and MONOTONIC
+clock does not, the derivative of the difference between these clocks
+gives the relative amount of time the device has spent in suspend
+state. The recorded value is in milliseconds of sleep per seconds of
+wall clock.
+
=head2 Plugin C<csv>
=over 4
=item B<MappedOnly> B<true>|B<false>
-When set to B<true>, only metrics that can be mapped to to a I<type> will be
+When set to B<true>, only metrics that can be mapped to a I<type> will be
collected, all other metrics will be ignored. Defaults to B<false>.
=back
=item B<AllPortsSummary> I<true>|I<false>
If this option is set to I<true> a summary of statistics from all connections
-are collectd. This option defaults to I<false>.
+are collected. This option defaults to I<false>.
=back
Defaults to B<Command>.
+=item B<Metrics> B<true>|B<false>
+
+Controls whether I<metrics> are POSTed to this location. Defaults to B<true>.
+
+=item B<Notifications> B<false>|B<true>
+
+Controls whether I<notifications> are POSTed to this location. Defaults to B<false>.
+
=item B<StoreRates> B<true|false>
If set to B<true>, convert counter values to rates. If set to B<false> (the
--- /dev/null
+/**
+ * collectd - src/cpusleep.c
+ * Copyright (C) 2016 rinigus
+ *
+ * The MIT License (MIT)
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ *
+ * Authors:
+ * rinigus <http://github.com/rinigus>
+ *
+ * CPU sleep is reported in milliseconds of sleep per second of wall
+ * time. For that, the time difference between BOOT and MONOTONIC clocks
+ * is reported using derive type.
+**/
+
+#include "collectd.h"
+
+#include "common.h"
+#include "plugin.h"
+#include <time.h>
+
+static void cpusleep_submit(derive_t cpu_sleep) {
+ value_t values[1];
+ value_list_t vl = VALUE_LIST_INIT;
+
+ values[0].derive = cpu_sleep;
+
+ vl.values = values;
+ vl.values_len = 1;
+ sstrncpy(vl.host, hostname_g, sizeof(vl.host));
+ sstrncpy(vl.plugin, "cpusleep", sizeof(vl.plugin));
+ sstrncpy(vl.type, "total_time_in_ms", sizeof(vl.type));
+
+ plugin_dispatch_values(&vl);
+}
+
+static int cpusleep_read(void) {
+ struct timespec b, m;
+ if (clock_gettime(CLOCK_BOOTTIME, &b) < 0) {
+ ERROR("cpusleep plugin: clock_boottime failed");
+ return (-1);
+ }
+
+ if (clock_gettime(CLOCK_MONOTONIC, &m) < 0) {
+ ERROR("cpusleep plugin: clock_monotonic failed");
+ return (-1);
+ }
+
+ // to avoid false positives in counter overflow due to reboot,
+ // derive is used. Sleep is calculated in milliseconds
+ derive_t diffsec = b.tv_sec - m.tv_sec;
+ derive_t diffnsec = b.tv_nsec - m.tv_nsec;
+ derive_t sleep = diffsec * 1000 + diffnsec / 1000000;
+
+ cpusleep_submit(sleep);
+
+ return (0);
+}
+
+void module_register(void) {
+ plugin_register_read("cpusleep", cpusleep_read);
+} /* void module_register */
*
* There are two versions of this function: If `wordexp' exists shell wildcards
* will be expanded and the function will include all matches found. If
- * `wordexp' (or, more precisely, it's header file) is not available the
+ * `wordexp' (or, more precisely, its header file) is not available the
* simpler function is used which does not do any such expansion.
*/
#if HAVE_WORDEXP_H
*
* PARAMETERS
* `compare' The function-pointer `compare' is used to compare two keys. It
- * has to return less than zero if it's first argument is smaller
+ * has to return less than zero if its first argument is smaller
* then the second argument, more than zero if the first argument
* is bigger than the second argument and zero if they are equal.
* If your keys are char-pointers, you can use the `strcmp'
* c_avl_pick
*
* DESCRIPTION
- * Remove a (pseudo-)random element from the tree and return it's `key' and
+ * Remove a (pseudo-)random element from the tree and return its `key' and
* `value'. Entries are not returned in any particular order. This function
* is intended for cache-flushes that don't care about the order but simply
* want to remove all elements, one at a time.
*
* PARAMETERS
* `compare' The function-pointer `compare' is used to compare two keys. It
- * has to return less than zero if it's first argument is smaller
+ * has to return less than zero if its first argument is smaller
* then the second argument, more than zero if the first argument
* is bigger than the second argument and zero if they are equal.
* If your keys are char-pointers, you can use the `strcmp'
#include <stdlib.h>
#include <stdio.h>
+#include <stdarg.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
(c)->errbuf[sizeof ((c)->errbuf) - 1] = 0; \
} while (0)
-#if COLLECT_DEBUG
-# define LCC_DEBUG(...) printf (__VA_ARGS__)
-#else
-# define LCC_DEBUG(...) /**/
-#endif
-
/*
* Types
*/
/*
* Private functions
*/
+static int lcc_tracef(char const *format, ...)
+{
+ va_list ap;
+ int status;
+
+ char const *trace = getenv (LCC_TRACE_ENV);
+ if (!trace || (strcmp ("", trace) == 0) || (strcmp ("0", trace) == 0))
+ return 0;
+
+ va_start (ap, format);
+ status = vprintf (format, ap);
+ va_end (ap);
+
+ return status;
+}
+
/* Even though Posix requires "strerror_r" to return an "int",
* some systems (e.g. the GNU libc) return a "char *" _and_
* ignore the second argument ... -tokkee */
{
int status;
- LCC_DEBUG ("send: --> %s\n", command);
+ lcc_tracef ("send: --> %s\n", command);
status = fprintf (c->fh, "%s\r\n", command);
if (status < 0)
return (-1);
}
lcc_chomp (buffer);
- LCC_DEBUG ("receive: <-- %s\n", buffer);
+ lcc_tracef ("receive: <-- %s\n", buffer);
/* Convert the leading status to an integer and make `ptr' to point to the
* beginning of the message. */
break;
}
lcc_chomp (buffer);
- LCC_DEBUG ("receive: <-- %s\n", buffer);
+ lcc_tracef ("receive: <-- %s\n", buffer);
res.lines[i] = strdup (buffer);
if (res.lines[i] == NULL)
#include "lcc_features.h"
+/* COLLECTD_TRACE is the environment variable used to control trace output. When
+ * set to something non-zero, all lines sent to / received from the daemon are
+ * printed to STDOUT. */
+#ifndef LCC_TRACE_ENV
+# define LCC_TRACE_ENV "COLLECTD_TRACE"
+#endif
+
/*
* Includes (for data types)
*/
if (memcmp (pss.hash, hash, sizeof (pss.hash)) != 0)
{
WARNING ("network plugin: Verifying HMAC-SHA-256 signature failed: "
- "Hash mismatch.");
+ "Hash mismatch. Username: %s", pss.username);
}
else
{
pea.username);
if (cypher == NULL)
{
+ ERROR ("network plugin: Failed to get cypher. Username: %s", pea.username);
sfree (pea.username);
return (-1);
}
if (err != 0)
{
sfree (pea.username);
- ERROR ("network plugin: gcry_cipher_decrypt returned: %s",
- gcry_strerror (err));
+ ERROR ("network plugin: gcry_cipher_decrypt returned: %s. Username: %s",
+ gcry_strerror (err), pea.username);
return (-1);
}
buffer + buffer_offset, payload_len);
if (memcmp (hash, pea.hash, sizeof (hash)) != 0)
{
+ ERROR ("network plugin: Checksum mismatch. Username: %s", pea.username);
sfree (pea.username);
- ERROR ("network plugin: Decryption failed: Checksum mismatch.");
return (-1);
}
#if HAVE_LIBGCRYPT
int packet_was_signed = (flags & PP_SIGNED);
- int packet_was_encrypted = (flags & PP_ENCRYPTED);
+ int packet_was_encrypted = (flags & PP_ENCRYPTED);
int printed_ignore_warning = 0;
#endif /* HAVE_LIBGCRYPT */
if (ping_thread_loop != 0)
{
pthread_mutex_unlock (&ping_lock);
- return (-1);
+ return (0);
}
ping_thread_loop = 1;
"Will use a timeout of %gs.", ping_timeout);
}
- if (start_thread () != 0)
- return (-1);
-
- return (0);
+ return (start_thread ());
} /* }}} int ping_init */
static int config_set_string (const char *name, /* {{{ */
" data if it was supplied.";
+static pthread_t main_thread;
+static PyOS_sighandler_t python_sigint_handler;
static _Bool do_interactive = 0;
/* This is our global thread state. Python saves some stuff in thread-local
return 0;
}
-static void cpy_int_handler(int sig) {
- return;
-}
-
static void *cpy_interactive(void *data) {
- sigset_t sigset;
- struct sigaction old;
+ PyOS_sighandler_t cur_sig;
/* Signal handler in a plugin? Bad stuff, but the best way to
* handle it I guess. In an interactive session people will
* mess. Chances are, this isn't what the user wanted to do.
*
* So this is the plan:
- * 1. Block SIGINT in the main thread.
- * 2. Install our own signal handler that does nothing.
- * 3. Unblock SIGINT in the interactive thread.
+ * 1. Restore Python's own signal handler
+ * 2. Tell Python we just forked so it will accept this thread
+ * as the main one. No version of Python will ever handle
+ * interrupts anywhere but in the main thread.
+ * 3. After the interactive loop is done, restore collectd's
+ * SIGINT handler.
+ * 4. Raise SIGINT for a clean shutdown. The signal is sent to
+ * the main thread to ensure it wakes up the main interval
+ * sleep so that collectd shuts down immediately not in 10
+ * seconds.
*
* This will make sure that SIGINT won't kill collectd but
- * still interrupt syscalls like sleep and pause.
- * It does not raise a KeyboardInterrupt exception because so
- * far nobody managed to figure out how to do that. */
- struct sigaction sig_int_action = {
- .sa_handler = cpy_int_handler
- };
- sigaction (SIGINT, &sig_int_action, &old);
-
- sigemptyset(&sigset);
- sigaddset(&sigset, SIGINT);
- pthread_sigmask(SIG_UNBLOCK, &sigset, NULL);
+ * still interrupt syscalls like sleep and pause. */
+
PyEval_AcquireThread(state);
if (PyImport_ImportModule("readline") == NULL) {
/* This interactive session will suck. */
cpy_log_exception("interactive session init");
- }
+ }
+ cur_sig = PyOS_setsig(SIGINT, python_sigint_handler);
+ /* We totally forked just now. Everyone saw that, right? */
+ PyOS_AfterFork();
PyRun_InteractiveLoop(stdin, "<stdin>");
+ PyOS_setsig(SIGINT, cur_sig);
PyErr_Print();
PyEval_ReleaseThread(state);
NOTICE("python: Interactive interpreter exited, stopping collectd ...");
- /* Restore the original collectd SIGINT handler and raise SIGINT.
- * The main thread still has SIGINT blocked and there's nothing we
- * can do about that so this thread will handle it. But that's not
- * important, except that it won't interrupt the main loop and so
- * it might take a few seconds before collectd really shuts down. */
- sigaction (SIGINT, &old, NULL);
- raise(SIGINT);
- pause();
+ pthread_kill(main_thread, SIGINT);
return NULL;
}
static int cpy_init(void) {
PyObject *ret;
static pthread_t thread;
- sigset_t sigset;
if (!Py_IsInitialized()) {
WARNING("python: Plugin loaded but not configured.");
else
Py_DECREF(ret);
}
- sigemptyset(&sigset);
- sigaddset(&sigset, SIGINT);
- pthread_sigmask(SIG_BLOCK, &sigset, NULL);
state = PyEval_SaveThread();
+ main_thread = pthread_self();
if (do_interactive) {
if (plugin_thread_create(&thread, NULL, cpy_interactive, NULL)) {
ERROR("python: Error creating thread for interactive interpreter.");
#endif
static int cpy_init_python(void) {
+ PyOS_sighandler_t cur_sig;
PyObject *sys;
PyObject *module;
char *argv = "";
#endif
+ /* Chances are the current signal handler is already SIG_DFL, but let's make sure. */
+ cur_sig = PyOS_setsig(SIGINT, SIG_DFL);
Py_Initialize();
+ python_sigint_handler = PyOS_setsig(SIGINT, cur_sig);
PyType_Ready(&ConfigType);
PyType_Ready(&PluginDataType);
pthread_mutex_unlock (&queue_lock);
/* We now need the cache lock so the entry isn't updated while
- * we make a copy of it's values */
+ * we make a copy of its values */
pthread_mutex_lock (&cache_lock);
status = c_avl_get (cache, queue_entry->filename,
{
if (r->instance_prefix == NULL)
{
- strjoin (vl.type_instance, sizeof (vl.type_instance),
+ int status = strjoin (vl.type_instance, sizeof (vl.type_instance),
r_area->instances_buffer, r->instances_num, "-");
+ if (status != 0)
+ {
+ ERROR ("udb_result_submit: creating type_instance failed with status %d.",
+ status);
+ return (status);
+ }
}
else
{
char tmp[DATA_MAX_NAME_LEN];
- strjoin (tmp, sizeof (tmp), r_area->instances_buffer,
+ int status = strjoin (tmp, sizeof (tmp), r_area->instances_buffer,
r->instances_num, "-");
+ if (status != 0)
+ {
+ ERROR ("udb_result_submit: creating type_instance failed with status %d.",
+ status);
+ return (status);
+ }
tmp[sizeof (tmp) - 1] = 0;
snprintf (vl.type_instance, sizeof (vl.type_instance), "%s-%s",
case ns_t_srv: return ("SRV");
case ns_t_atma: return ("ATMA");
case ns_t_naptr: return ("NAPTR");
+ case ns_t_opt: return ("OPT");
+# if __NAMESER >= 19991006
case ns_t_kx: return ("KX");
case ns_t_cert: return ("CERT");
case ns_t_a6: return ("A6");
case ns_t_dname: return ("DNAME");
case ns_t_sink: return ("SINK");
- case ns_t_opt: return ("OPT");
-# if __NAMESER >= 19991006
+ case ns_t_apl: return ("APL");
case ns_t_tsig: return ("TSIG");
# endif
+# if __NAMESER >= 20090302
+ case ns_t_ds: return ("DS");
+ case ns_t_sshfp: return ("SSHFP");
+ case ns_t_ipseckey: return ("IPSECKEY");
+ case ns_t_rrsig: return ("RRSIG");
+ case ns_t_nsec: return ("NSEC");
+ case ns_t_dnskey: return ("DNSKEY");
+ case ns_t_dhcid: return ("DHCID");
+ case ns_t_nsec3: return ("NSEC3");
+ case ns_t_nsec3param: return ("NSEC3PARAM");
+ case ns_t_hip: return ("HIP");
+ case ns_t_spf: return ("SPF");
case ns_t_ixfr: return ("IXFR");
+# endif
case ns_t_axfr: return ("AXFR");
case ns_t_mailb: return ("MAILB");
case ns_t_maila: return ("MAILA");
case ns_t_any: return ("ANY");
+# if __NAMESER >= 19991006
case ns_t_zxfr: return ("ZXFR");
-/* #endif __NAMESER >= 19991006 */
+# endif
+# if __NAMESER >= 20090302
+ case ns_t_dlv: return ("DLV");
+# endif
+/* #endif __NAMESER >= 19991001 */
#elif (defined (__BIND)) && (__BIND >= 19950621)
case T_A: return ("A"); /* 1 ... */
case T_NS: return ("NS");
/**
* collectd - src/utils_format_json.c
- * Copyright (C) 2009 Florian octo Forster
+ * Copyright (C) 2009-2015 Florian octo Forster
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
#include "collectd.h"
+#include "utils_format_json.h"
+
#include "plugin.h"
#include "common.h"
-
#include "utils_cache.h"
-#include "utils_format_json.h"
+
+#if HAVE_LIBYAJL
+# include <yajl/yajl_common.h>
+# include <yajl/yajl_gen.h>
+# if HAVE_YAJL_YAJL_VERSION_H
+# include <yajl/yajl_version.h>
+# endif
+# if defined(YAJL_MAJOR) && (YAJL_MAJOR > 1)
+# define HAVE_YAJL_V2 1
+# endif
+#endif
static int json_escape_string (char *buffer, size_t buffer_size, /* {{{ */
const char *string)
store_rates, (*ret_buffer_free) - 2));
} /* }}} int format_json_value_list */
+#if HAVE_LIBYAJL
+static int json_add_string (yajl_gen g, char const *str) /* {{{ */
+{
+ if (str == NULL)
+ return (int) yajl_gen_null (g);
+
+ return (int) yajl_gen_string (g, (unsigned char const *) str, (unsigned int) strlen (str));
+} /* }}} int json_add_string */
+
+#define JSON_ADD(g, str) do { \
+ yajl_gen_status status = json_add_string (g, str); \
+ if (status != yajl_gen_status_ok) { return -1; } \
+} while (0)
+
+#define JSON_ADDF(g, format, ...) do { \
+ char *str = ssnprintf_alloc (format, __VA_ARGS__); \
+ yajl_gen_status status = json_add_string (g, str); \
+ free (str); \
+ if (status != yajl_gen_status_ok) { return -1; } \
+} while (0)
+
+static int format_json_meta (yajl_gen g, notification_meta_t *meta) /* {{{ */
+{
+ if (meta == NULL)
+ return 0;
+
+ JSON_ADD (g, meta->name);
+ switch (meta->type)
+ {
+ case NM_TYPE_STRING:
+ JSON_ADD (g, meta->nm_value.nm_string);
+ break;
+ case NM_TYPE_SIGNED_INT:
+ JSON_ADDF (g, "%"PRIi64, meta->nm_value.nm_signed_int);
+ break;
+ case NM_TYPE_UNSIGNED_INT:
+ JSON_ADDF (g, "%"PRIu64, meta->nm_value.nm_unsigned_int);
+ break;
+ case NM_TYPE_DOUBLE:
+ JSON_ADDF (g, JSON_GAUGE_FORMAT, meta->nm_value.nm_double);
+ break;
+ case NM_TYPE_BOOLEAN:
+ JSON_ADD (g, meta->nm_value.nm_boolean ? "true" : "false");
+ break;
+ default:
+ ERROR ("format_json_meta: unknown meta data type %d (name \"%s\")", meta->type, meta->name);
+ yajl_gen_null (g);
+ }
+
+ return format_json_meta (g, meta->next);
+} /* }}} int format_json_meta */
+
+static int format_time (yajl_gen g, cdtime_t t) /* {{{ */
+{
+ char buffer[RFC3339NANO_SIZE] = "";
+
+ if (rfc3339nano (buffer, sizeof (buffer), t) != 0)
+ return -1;
+
+ JSON_ADD (g, buffer);
+ return 0;
+} /* }}} int format_time */
+
+static int format_alert (yajl_gen g, notification_t const *n) /* {{{ */
+{
+ yajl_gen_array_open (g);
+ yajl_gen_map_open (g); /* BEGIN alert */
+
+ /*
+ * labels
+ */
+ JSON_ADD (g, "labels");
+ yajl_gen_map_open (g); /* BEGIN labels */
+
+ JSON_ADD (g, "alertname");
+ if (strncmp (n->plugin, n->type, strlen (n->plugin)) == 0)
+ JSON_ADDF (g, "collectd_%s", n->type);
+ else
+ JSON_ADDF (g, "collectd_%s_%s", n->plugin, n->type);
+
+ JSON_ADD (g, "instance");
+ JSON_ADD (g, n->host);
+
+ /* mangling of plugin instance and type instance into labels is copied from
+ * the Prometheus collectd exporter. */
+ if (strlen (n->plugin_instance) > 0)
+ {
+ JSON_ADD (g, n->plugin);
+ JSON_ADD (g, n->plugin_instance);
+ }
+ if (strlen (n->type_instance) > 0)
+ {
+ if (strlen (n->plugin_instance) > 0)
+ JSON_ADD (g, "type");
+ else
+ JSON_ADD (g, n->plugin);
+ JSON_ADD (g, n->type_instance);
+ }
+
+ JSON_ADD (g, "severity");
+ JSON_ADD (g, (n->severity == NOTIF_FAILURE) ? "FAILURE"
+ : (n->severity == NOTIF_WARNING) ? "WARNING"
+ : (n->severity == NOTIF_OKAY) ? "OKAY"
+ : "UNKNOWN");
+
+ JSON_ADD (g, "service");
+ JSON_ADD (g, "collectd");
+
+ yajl_gen_map_close (g); /* END labels */
+
+ /*
+ * annotations
+ */
+ JSON_ADD (g, "annotations");
+ yajl_gen_map_open (g); /* BEGIN annotations */
+
+ JSON_ADD (g, "summary");
+ JSON_ADD (g, n->message);
+
+ if (format_json_meta (g, n->meta) != 0)
+ return -1;
+
+ yajl_gen_map_close (g); /* END annotations */
+
+ JSON_ADD (g, "startsAt");
+ format_time (g, n->time);
+
+ yajl_gen_map_close (g); /* END alert */
+ yajl_gen_array_close (g);
+
+ return 0;
+} /* }}} format_alert */
+
+/*
+ * Format (prometheus/alertmanager v1):
+ *
+ * [{
+ * "labels": {
+ * "alertname": "collectd_cpu",
+ * "instance": "host.example.com",
+ * "severity": "FAILURE",
+ * "service": "collectd",
+ * "cpu": "0",
+ * "type": "wait"
+ * },
+ * "annotations": {
+ * "summary": "...",
+ * // meta
+ * },
+ * "startsAt": <rfc3339 time>,
+ * "endsAt": <rfc3339 time>, // not used
+ * }]
+ */
+int format_json_notification (char *buffer, size_t buffer_size, /* {{{ */
+ notification_t const *n)
+{
+ yajl_gen g;
+ unsigned char const *out;
+#if HAVE_YAJL_V2
+ size_t unused_out_len;
+#else
+ unsigned int unused_out_len;
+#endif
+
+ if ((buffer == NULL) || (n == NULL))
+ return EINVAL;
+
+#if HAVE_YAJL_V2
+ g = yajl_gen_alloc (NULL);
+ if (g == NULL)
+ return -1;
+# if COLLECT_DEBUG
+ yajl_gen_config (g, yajl_gen_beautify);
+ yajl_gen_config (g, yajl_gen_validate_utf8);
+# endif
+
+#else /* !HAVE_YAJL_V2 */
+ yajl_gen_config conf = { 0 };
+# if COLLECT_DEBUG
+ conf.beautify = 1;
+ conf.indentString = " ";
+# endif
+ g = yajl_gen_alloc (&conf, NULL);
+ if (g == NULL)
+ return -1;
+#endif
+
+ if (format_alert (g, n) != 0)
+ {
+ yajl_gen_clear (g);
+ yajl_gen_free (g);
+ return -1;
+ }
+
+ /* copy to output buffer */
+ yajl_gen_get_buf (g, &out, &unused_out_len);
+ sstrncpy (buffer, (void *) out, buffer_size);
+
+ yajl_gen_clear (g);
+ yajl_gen_free (g);
+ return 0;
+} /* }}} format_json_notification */
+#else
+int format_json_notification (char *buffer, size_t buffer_size, /* {{{ */
+ notification_t const *n)
+{
+ ERROR ("format_json_notification: Not available (requires libyajl).");
+ return ENOTSUP;
+} /* }}} int format_json_notification */
+#endif
+
/* vim: set sw=2 sts=2 et fdm=marker : */
const data_set_t *ds, const value_list_t *vl, int store_rates);
int format_json_finalize (char *buffer,
size_t *ret_buffer_fill, size_t *ret_buffer_free);
+int format_json_notification (char *buffer, size_t buffer_size,
+ notification_t const *n);
#endif /* UTILS_FORMAT_JSON_H */
--- /dev/null
+/**
+ * collectd - src/utils_format_json_test.c
+ * Copyright (C) 2015 Florian octo Forster
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+ * DEALINGS IN THE SOFTWARE.
+ *
+ * Authors:
+ * Florian octo Forster <octo at collectd.org>
+ */
+
+#include "testing.h"
+#include "collectd.h"
+#include "utils_format_json.h"
+#include "common.h" /* for STATIC_ARRAY_SIZE */
+
+#include <yajl/yajl_common.h>
+#include <yajl/yajl_parse.h>
+#if HAVE_YAJL_YAJL_VERSION_H
+# include <yajl/yajl_version.h>
+#endif
+#if YAJL_MAJOR > 1
+# define HAVE_YAJL_V2 1
+#endif
+
+typedef struct
+{
+ char const *key;
+ char const *value;
+} label_t;
+
+typedef struct
+{
+ label_t *expected_labels;
+ size_t expected_labels_num;
+
+ label_t *current_label;
+} test_case_t;
+
+#if HAVE_YAJL_V2
+static int test_map_key (void *ctx, unsigned char const *key, size_t key_len)
+#else
+static int test_map_key (void *ctx, unsigned char const *key, unsigned int key_len)
+#endif
+{
+ test_case_t *c = ctx;
+ size_t i;
+
+ c->current_label = NULL;
+ for (i = 0; i < c->expected_labels_num; i++)
+ {
+ label_t *l = c->expected_labels + i;
+ if ((strlen (l->key) == key_len)
+ && (strncmp (l->key, (char const *) key, key_len) == 0))
+ {
+ c->current_label = l;
+ break;
+ }
+ }
+
+ return 1; /* continue */
+}
+
+static int expect_label (char const *name, char const *got, char const *want)
+{
+ _Bool ok = (strcmp (got, want) == 0);
+ char msg[1024];
+
+ if (ok)
+ snprintf (msg, sizeof (msg), "label[\"%s\"] = \"%s\"", name, got);
+ else
+ snprintf (msg, sizeof (msg), "label[\"%s\"] = \"%s\", want \"%s\"", name, got, want);
+
+ OK1 (ok, msg);
+ return 0;
+}
+
+#if HAVE_YAJL_V2
+static int test_string (void *ctx, unsigned char const *value, size_t value_len)
+#else
+static int test_string (void *ctx, unsigned char const *value, unsigned int value_len)
+#endif
+{
+ test_case_t *c = ctx;
+
+ if (c->current_label != NULL)
+ {
+ label_t *l = c->current_label;
+ char *got;
+ int status;
+
+ got = malloc (value_len + 1);
+ memmove (got, value, value_len);
+ got[value_len] = 0;
+
+ status = expect_label (l->key, got, l->value);
+
+ free (got);
+
+ if (status != 0)
+ return 0; /* abort */
+ }
+
+ return 1; /* continue */
+}
+
+static int expect_json_labels (char *json, label_t *labels, size_t labels_num)
+{
+ yajl_callbacks funcs = {
+ .yajl_string = test_string,
+ .yajl_map_key = test_map_key,
+ };
+
+ test_case_t c = { labels, labels_num, NULL };
+
+ yajl_handle hndl;
+#if HAVE_YAJL_V2
+ CHECK_NOT_NULL (hndl = yajl_alloc (&funcs, /* alloc = */ NULL, &c));
+#else
+ CHECK_NOT_NULL (hndl = yajl_alloc (&funcs, /* config = */ NULL, /* alloc = */ NULL, &c));
+#endif
+ OK (yajl_parse (hndl, (unsigned char *) json, strlen (json)) == yajl_status_ok);
+
+ yajl_free (hndl);
+ return 0;
+}
+
+DEF_TEST(notification)
+{
+ label_t labels[] = {
+ {"summary", "this is a message"},
+ {"alertname", "collectd_unit_test"},
+ {"instance", "example.com"},
+ {"service", "collectd"},
+ {"unit", "case"},
+ };
+
+ /* 1448284606.125 ^= 1555083754651779072 */
+ notification_t n = { NOTIF_WARNING, 1555083754651779072ULL, "this is a message",
+ "example.com", "unit", "", "test", "case", NULL };
+
+ char got[1024];
+ CHECK_ZERO (format_json_notification (got, sizeof (got), &n));
+ // printf ("got = \"%s\";\n", got);
+
+ return expect_json_labels (got, labels, STATIC_ARRAY_SIZE (labels));
+}
+
+int main (void)
+{
+ RUN_TEST(notification);
+
+ END_TEST;
+}
#define WH_FORMAT_JSON 1
#define WH_FORMAT_KAIROSDB 2
int format;
+ _Bool send_metrics;
+ _Bool send_notifications;
CURL *curl;
struct curl_slist *headers;
}
} /* }}} wh_reset_buffer */
-static int wh_send_buffer (wh_callback_t *cb) /* {{{ */
+/* must hold cb->send_lock when calling */
+static int wh_post_nolock (wh_callback_t *cb, char const *data) /* {{{ */
{
int status = 0;
- curl_easy_setopt (cb->curl, CURLOPT_POSTFIELDS, cb->send_buffer);
+ curl_easy_setopt (cb->curl, CURLOPT_POSTFIELDS, data);
status = curl_easy_perform (cb->curl);
wh_log_http_error (cb);
status, cb->curl_errbuf);
}
return (status);
-} /* }}} wh_send_buffer */
+} /* }}} wh_post_nolock */
static int wh_callback_init (wh_callback_t *cb) /* {{{ */
{
return (0);
}
- status = wh_send_buffer (cb);
+ status = wh_post_nolock (cb, cb->send_buffer);
wh_reset_buffer (cb);
}
else if (cb->format == WH_FORMAT_JSON || cb->format == WH_FORMAT_KAIROSDB)
return (status);
}
- status = wh_send_buffer (cb);
+ status = wh_post_nolock (cb, cb->send_buffer);
wh_reset_buffer (cb);
}
else
pthread_mutex_lock (&cb->send_lock);
- if (cb->curl == NULL)
+ if (wh_callback_init (cb) != 0)
{
- status = wh_callback_init (cb);
- if (status != 0)
- {
- ERROR ("write_http plugin: wh_callback_init failed.");
- pthread_mutex_unlock (&cb->send_lock);
- return (-1);
- }
+ ERROR ("write_http plugin: wh_callback_init failed.");
+ pthread_mutex_unlock (&cb->send_lock);
+ return (-1);
}
status = wh_flush_nolock (timeout, cb);
}
pthread_mutex_lock (&cb->send_lock);
-
- if (cb->curl == NULL)
+ if (wh_callback_init (cb) != 0)
{
- status = wh_callback_init (cb);
- if (status != 0)
- {
- ERROR ("write_http plugin: wh_callback_init failed.");
- pthread_mutex_unlock (&cb->send_lock);
- return (-1);
- }
+ ERROR ("write_http plugin: wh_callback_init failed.");
+ pthread_mutex_unlock (&cb->send_lock);
+ return (-1);
}
if (command_len >= cb->send_buffer_free)
int status;
pthread_mutex_lock (&cb->send_lock);
-
- if (cb->curl == NULL)
+ if (wh_callback_init (cb) != 0)
{
- status = wh_callback_init (cb);
- if (status != 0)
- {
- ERROR ("write_http plugin: wh_callback_init failed.");
- pthread_mutex_unlock (&cb->send_lock);
- return (-1);
- }
+ ERROR ("write_http plugin: wh_callback_init failed.");
+ pthread_mutex_unlock (&cb->send_lock);
+ return (-1);
}
status = format_json_value_list (cb->send_buffer,
return (-EINVAL);
cb = user_data->data;
+ assert (cb->send_metrics);
switch(cb->format) {
case WH_FORMAT_JSON:
return (status);
} /* }}} int wh_write */
+static int wh_notify (notification_t const *n, user_data_t *ud) /* {{{ */
+{
+ wh_callback_t *cb;
+ char alert[4096];
+ int status;
+
+ if ((ud == NULL) || (ud->data == NULL))
+ return (EINVAL);
+
+ cb = ud->data;
+ assert (cb->send_notifications);
+
+ status = format_json_notification (alert, sizeof (alert), n);
+ if (status != 0)
+ {
+ ERROR ("write_http plugin: formatting notification failed");
+ return status;
+ }
+
+ pthread_mutex_lock (&cb->send_lock);
+ if (wh_callback_init (cb) != 0)
+ {
+ ERROR ("write_http plugin: wh_callback_init failed.");
+ pthread_mutex_unlock (&cb->send_lock);
+ return (-1);
+ }
+
+ status = wh_post_nolock (cb, alert);
+ pthread_mutex_unlock (&cb->send_lock);
+
+ return (status);
+} /* }}} int wh_notify */
+
static int config_set_format (wh_callback_t *cb, /* {{{ */
oconfig_item_t *ci)
{
cb->timeout = 0;
cb->log_http_error = 0;
cb->headers = NULL;
-
+ cb->send_metrics = 1;
+ cb->send_notifications = 0;
pthread_mutex_init (&cb->send_lock, /* attr = */ NULL);
}
else if (strcasecmp ("Format", child->key) == 0)
status = config_set_format (cb, child);
+ else if (strcasecmp ("Metrics", child->key) == 0)
+ cf_util_get_boolean (child, &cb->send_metrics);
+ else if (strcasecmp ("Notifications", child->key) == 0)
+ cf_util_get_boolean (child, &cb->send_notifications);
else if (strcasecmp ("StoreRates", child->key) == 0)
status = cf_util_get_boolean (child, &cb->store_rates);
else if (strcasecmp ("BufferSize", child->key) == 0)
return (-1);
}
+ if (!cb->send_metrics && !cb->send_notifications)
+ {
+ ERROR ("write_http plugin: Neither metrics nor notifications "
+ "are enabled for \"%s\".", cb->name);
+ wh_callback_free (cb);
+ return (-1);
+ }
+
if (cb->low_speed_limit > 0)
cb->low_speed_time = CDTIME_T_TO_TIME_T(plugin_get_interval());
plugin_register_flush (callback_name, wh_flush, &user_data);
user_data.free_func = wh_callback_free;
- plugin_register_write (callback_name, wh_write, &user_data);
+
+ if (cb->send_metrics)
+ {
+ plugin_register_write (callback_name, wh_write, &user_data);
+ user_data.free_func = NULL;
+
+ plugin_register_flush (callback_name, wh_flush, &user_data);
+ }
+
+ if (cb->send_notifications)
+ {
+ plugin_register_notification (callback_name, wh_notify, &user_data);
+ user_data.free_func = NULL;
+ }
return (0);
} /* }}} int wh_config_node */
za_read_derive (ksp, "deleted", "cache_operation", "deleted");
#if defined(KERNEL_FREEBSD)
za_read_derive (ksp, "allocated","cache_operation", "allocated");
-#if __FreeBSD_version < 1002501
- /* stolen removed from sysctl kstat.zfs.misc.arcstats on FreeBSD 10.2+ */
- za_read_derive (ksp, "stolen", "cache_operation", "stolen");
-#endif
#endif
/* Issue indicators */