From: octo Date: Tue, 6 Dec 2005 12:24:51 +0000 (+0000) Subject: Fixes for the load module (did not work correctly under Solaris) X-Git-Tag: collectd-3.5.0~26 X-Git-Url: https://git.verplant.org/?a=commitdiff_plain;h=15996f86d77adf5c05f8b172b2256572ec90f812;p=collectd.git Fixes for the load module (did not work correctly under Solaris) --- diff --git a/configure.in b/configure.in index aeda992c..f297bdad 100644 --- a/configure.in +++ b/configure.in @@ -6,7 +6,9 @@ AC_LANG(C) AC_PREFIX_DEFAULT("/opt/collectd") -dnl Checks for programs. +# +# Checks for programs. +# AC_PROG_CC AC_PROG_CPP AC_PROG_INSTALL @@ -19,7 +21,9 @@ AC_LIBTOOL_DLOPEN AC_PROG_LIBTOOL #AC_PROG_RANLIB -dnl Checks for header files. +# +# Checks for header files. +# AC_HEADER_STDC AC_HEADER_SYS_WAIT AC_CHECK_HEADERS(fcntl.h unistd.h) @@ -31,30 +35,46 @@ AC_CHECK_HEADERS(sys/time.h sys/times.h) AC_CHECK_HEADERS(sys/types.h) AC_CHECK_HEADERS(sys/resource.h) AC_CHECK_HEADERS(errno.h) +AC_CHECK_HEADERS(syslog.h) +AC_CHECK_HEADERS(dlfcn.h) + +# For load module +AC_CHECK_HEADERS(sys/loadavg.h) + +# For ping module AC_CHECK_HEADERS(arpa/inet.h) AC_CHECK_HEADERS(netinet/in.h) AC_CHECK_HEADERS(netdb.h) -AC_CHECK_HEADERS(syslog.h) -AC_CHECK_HEADERS(dlfcn.h) + +# For users module AC_CHECK_HEADERS(utmp.h) AC_CHECK_HEADERS(utmpx.h) dnl Checking for libraries AC_CHECK_LIB(m, ext) -dnl Checks for typedefs, structures, and compiler characteristics. +# +# Checks for typedefs, structures, and compiler characteristics. +# AC_C_CONST AC_TYPE_PID_T AC_TYPE_SIZE_T AC_HEADER_TIME -dnl Checks for library functions. +# +# Checks for library functions. +# AC_PROG_GCC_TRADITIONAL AC_CHECK_FUNCS(gettimeofday select socket strdup strstr strtol) AC_CHECK_FUNCS(socket, , AC_CHECK_LIB(socket, socket)) AC_CHECK_FUNCS(gethostbyname, , AC_CHECK_LIB(nsl, gethostbyname)) AC_CHECK_FUNCS(strchr memcpy strstr strcmp strncmp strncpy strlen) AC_CHECK_FUNCS(strncasecmp strcasecmp strncmp) + +# For load module +AC_CHECK_FUNCS(getloadavg, [have_getloadavg="yes"], [have_getloadavg="no"]) + +# For users module AC_CHECK_FUNCS(getutent getutxent) AC_MSG_CHECKING([for kernel type ($host_os)]) @@ -235,7 +255,7 @@ AM_CONDITIONAL(BUILD_MODULE_HDDTEMP, test "x$enable_hddtemp" = "xyes") AC_ARG_ENABLE(load, AC_HELP_STRING([--disable-load], [Disable system load statistics]),, [enable_load="yes"]) if test "x$enable_load" != "xno" then - if test "x$ac_system" = "xLinux" -o "x$with_kstat" = "xyes" -o "x$with_libstatgrab" = "xyes" + if test "x$have_getloadavg" = "xyes" -o "x$ac_system" = "xLinux" -o "x$with_libstatgrab" = "xyes" then enable_load="yes" else diff --git a/src/config.h.in b/src/config.h.in index 975f16e1..1c63a1fb 100644 --- a/src/config.h.in +++ b/src/config.h.in @@ -60,6 +60,9 @@ /* Define to 1 if you have the `gethostbyname' function. */ #undef HAVE_GETHOSTBYNAME +/* Define to 1 if you have the `getloadavg' function. */ +#undef HAVE_GETLOADAVG + /* Define to 1 if you have the `gettimeofday' function. */ #undef HAVE_GETTIMEOFDAY @@ -180,6 +183,9 @@ /* Define to 1 if you have the header file. */ #undef HAVE_SYSLOG_H +/* Define to 1 if you have the header file. */ +#undef HAVE_SYS_LOADAVG_H + /* Define to 1 if you have the header file. */ #undef HAVE_SYS_RESOURCE_H diff --git a/src/load.c b/src/load.c new file mode 100644 index 00000000..2a14edd2 --- /dev/null +++ b/src/load.c @@ -0,0 +1,124 @@ +#include "load.h" + +#if COLLECT_LOAD +#define MODULE_NAME "load" + +#include "plugin.h" +#include "common.h" + +#ifdef HAVE_SYS_LOADAVG_H +#include +#endif + +#ifdef HAVE_GETLOADAVG +#if !defined(LOADAVG_1MIN) || !defined(LOADAVG_5MIN) || !defined(LOADAVG_15MIN) +#define LOADAVG_1MIN 0 +#define LOADAVG_5MIN 1 +#define LOADAVG_15MIN 2 +#endif +#endif /* defined(HAVE_GETLOADAVG) */ + +static char *load_file = "load.rrd"; + +static char *ds_def[] = +{ + "DS:shortterm:GAUGE:25:0:100", + "DS:midterm:GAUGE:25:0:100", + "DS:longterm:GAUGE:25:0:100", + NULL +}; +static int ds_num = 3; + +extern time_t curtime; + +void load_init (void) +{ + return; +} + +void load_write (char *host, char *inst, char *val) +{ + rrd_update_file (host, load_file, val, ds_def, ds_num); +} + +#define BUFSIZE 256 +void load_submit (double snum, double mnum, double lnum) +{ + char buf[BUFSIZE]; + + if (snprintf (buf, BUFSIZE, "%u:%.2f:%.2f:%.2f", (unsigned int) curtime, + snum, mnum, lnum) >= BUFSIZE) + return; + + plugin_submit (MODULE_NAME, "-", buf); +} +#undef BUFSIZE + +void load_read (void) +{ +#if defined(HAVE_GETLOADAVG) + double load[3]; + + if (getloadavg (load, 3) == 3) + load_submit (load[LOADAVG_1MIN], load[LOADAVG_5MIN], load[LOADAVG_15MIN]); + else + syslog (LOG_WARNING, "load: getloadavg failed: %s", strerror (errno)); +/* #endif HAVE_GETLOADAVG */ + +#elif defined(KERNEL_LINUX) + double snum, mnum, lnum; + FILE *loadavg; + char buffer[16]; + + char *fields[8]; + int numfields; + + if ((loadavg = fopen ("/proc/loadavg", "r")) == NULL) + { + syslog (LOG_WARNING, "load: fopen: %s", strerror (errno)); + return; + } + + if (fgets (buffer, 16, loadavg) == NULL) + { + syslog (LOG_WARNING, "load: fgets: %s", strerror (errno)); + return; + } + + if (fclose (loadavg)) + syslog (LOG_WARNING, "load: fclose: %s", strerror (errno)); + + numfields = strsplit (buffer, fields, 8); + + if (numfields < 3) + return; + + snum = atof (fields[0]); + mnum = atof (fields[1]); + lnum = atof (fields[2]); + + load_submit (snum, mnum, lnum); +/* #endif KERNEL_LINUX */ + +#elif defined(HAVE_LIBSTATGRAB) + double snum, mnum, lnum; + sg_load_stats *ls; + + if ((ls = sg_get_load_stats ()) == NULL) + return; + + snum = ls->min1; + mnum = ls->min5; + lnum = ls->min15; + + load_submit (snum, mnum, lnum); +#endif /* HAVE_LIBSTATGRAB */ +} + +void module_register (void) +{ + plugin_register (MODULE_NAME, load_init, load_read, load_write); +} + +#undef MODULE_NAME +#endif /* COLLECT_LOAD */ diff --git a/src/load.h b/src/load.h new file mode 100644 index 00000000..ce437eb0 --- /dev/null +++ b/src/load.h @@ -0,0 +1,15 @@ +#ifndef LOAD_H +#define LOAD_H + +#include "collectd.h" +#include "common.h" + +#ifndef COLLECT_LOAD +#if defined(HAVE_GETLOADAVG) || defined(KERNEL_LINUX) || defined(HAVE_LIBSTATGRAB) +#define COLLECT_LOAD 1 +#else +#define COLLECT_LOAD 0 +#endif +#endif /* !defined(COLLECT_LOAD) */ + +#endif /* LOAD_H */