/**
* collectd - src/vserver.c
* Copyright (C) 2006,2007 Sebastian Harl
+ * Copyright (C) 2007-2010 Florian octo Forster
*
- * 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; either version 2 of the License, or (at your
- * option) any later version.
+ * 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:
*
- * 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.
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
*
- * 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.,
- * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
+ * 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:
* Sebastian Harl <sh at tokkee.org>
+ * Florian octo Forster <octo at collectd.org>
**/
#include "collectd.h"
#include "plugin.h"
#include <dirent.h>
-#include <errno.h>
-#include <stdio.h>
-#include <string.h>
-#include <syslog.h>
#include <sys/types.h>
-#include <unistd.h>
#define BUFSIZE 512
-#define MODULE_NAME "vserver"
#define PROCDIR "/proc/virtual"
-#if defined(KERNEL_LINUX)
-# define VSERVER_HAVE_READ 1
-#else
-# define VSERVER_HAVE_READ 0
-#endif /* defined(KERNEL_LINUX) */
-
-static data_source_t octets_dsrc[2] =
-{
- {"rx", DS_TYPE_COUNTER, 0, 4294967295.0},
- {"tx", DS_TYPE_COUNTER, 0, 4294967295.0}
-};
-
-static data_set_t octets_ds =
-{
- "if_octets", 2, octets_dsrc
-};
-
-static data_source_t load_dsrc[3] =
-{
- {"shortterm", DS_TYPE_GAUGE, 0.0, 100.0},
- {"midterm", DS_TYPE_GAUGE, 0.0, 100.0},
- {"longterm", DS_TYPE_GAUGE, 0.0, 100.0}
-};
-
-static data_set_t load_ds =
-{
- "load", 3, load_dsrc
-};
-
-static data_source_t threads_dsrc[1] =
-{
- {"value", DS_TYPE_GAUGE, 0.0, 65535.0}
-};
-
-static data_set_t threads_ds =
-{
- "vs_threads", 1, threads_dsrc
-};
-
-static data_source_t processes_dsrc[1] =
-{
- {"value", DS_TYPE_GAUGE, 0.0, 65535.0}
-};
-
-static data_set_t processes_ds =
-{
- "vs_processes", 1, processes_dsrc
-};
-
-static data_source_t memory_dsrc[1] =
-{
- {"value", DS_TYPE_GAUGE, 0.0, 9223372036854775807.0}
-};
-
-static data_set_t memory_ds =
-{
- "vs_memory", 1, memory_dsrc
-};
+#if !KERNEL_LINUX
+# error "No applicable input method."
+#endif
-#if VSERVER_HAVE_READ
static int pagesize = 0;
static int vserver_init (void)
} /* static void vserver_init(void) */
static void traffic_submit (const char *plugin_instance,
- const char *type_instance, counter_t rx, counter_t tx)
+ const char *type_instance, derive_t rx, derive_t tx)
{
value_t values[2];
value_list_t vl = VALUE_LIST_INIT;
- values[0].counter = rx;
- values[1].counter = tx;
+ values[0].derive = rx;
+ values[1].derive = tx;
vl.values = values;
vl.values_len = STATIC_ARRAY_SIZE (values);
- vl.time = time (NULL);
- strcpy (vl.host, hostname_g);
- strcpy (vl.plugin, "vserver");
- strncpy (vl.plugin_instance, plugin_instance, sizeof (vl.plugin_instance));
- strncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
+ sstrncpy (vl.host, hostname_g, sizeof (vl.host));
+ sstrncpy (vl.plugin, "vserver", sizeof (vl.plugin));
+ sstrncpy (vl.plugin_instance, plugin_instance, sizeof (vl.plugin_instance));
+ sstrncpy (vl.type, "if_octets", sizeof (vl.type));
+ sstrncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
- plugin_dispatch_values ("if_octets", &vl);
+ plugin_dispatch_values (&vl);
} /* void traffic_submit */
static void load_submit (const char *plugin_instance,
vl.values = values;
vl.values_len = STATIC_ARRAY_SIZE (values);
- vl.time = time (NULL);
- strcpy (vl.host, hostname_g);
- strcpy (vl.plugin, "vserver");
- strncpy (vl.plugin_instance, plugin_instance, sizeof (vl.plugin_instance));
+ sstrncpy (vl.host, hostname_g, sizeof (vl.host));
+ sstrncpy (vl.plugin, "vserver", sizeof (vl.plugin));
+ sstrncpy (vl.plugin_instance, plugin_instance, sizeof (vl.plugin_instance));
+ sstrncpy (vl.type, "load", sizeof (vl.type));
- plugin_dispatch_values ("load", &vl);
+ plugin_dispatch_values (&vl);
}
static void submit_gauge (const char *plugin_instance, const char *type,
vl.values = values;
vl.values_len = STATIC_ARRAY_SIZE (values);
- vl.time = time (NULL);
- strcpy (vl.host, hostname_g);
- strcpy (vl.plugin, "vserver");
- strncpy (vl.plugin_instance, plugin_instance, sizeof (vl.plugin_instance));
- strncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
+ sstrncpy (vl.host, hostname_g, sizeof (vl.host));
+ sstrncpy (vl.plugin, "vserver", sizeof (vl.plugin));
+ sstrncpy (vl.plugin_instance, plugin_instance, sizeof (vl.plugin_instance));
+ sstrncpy (vl.type, type, sizeof (vl.type));
+ sstrncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
- plugin_dispatch_values (type, &vl);
+ plugin_dispatch_values (&vl);
} /* void submit_gauge */
-static inline long long __get_sock_bytes(const char *s)
+static derive_t vserver_get_sock_bytes(const char *s)
{
+ value_t v;
+ int status;
+
while (s[0] != '/')
++s;
/* Remove '/' */
++s;
- return atoll(s);
+
+ status = parse_value (s, &v, DS_TYPE_DERIVE);
+ if (status != 0)
+ return (-1);
+ return (v.derive);
}
static int vserver_read (void)
{
+#if NAME_MAX < 1024
+# define DIRENT_BUFFER_SIZE (sizeof (struct dirent) + 1024 + 1)
+#else
+# define DIRENT_BUFFER_SIZE (sizeof (struct dirent) + NAME_MAX + 1)
+#endif
+
DIR *proc;
struct dirent *dent; /* 42 */
-
- static complain_t complain_obj;
+ char dirent_buffer[DIRENT_BUFFER_SIZE];
errno = 0;
- if (NULL == (proc = opendir (PROCDIR)))
+ proc = opendir (PROCDIR);
+ if (proc == NULL)
{
- plugin_complain (LOG_ERR, &complain_obj, "vserver plugin: "
- "fopen (%s) failed: %s", PROCDIR, strerror (errno));
+ char errbuf[1024];
+ ERROR ("vserver plugin: fopen (%s): %s", PROCDIR,
+ sstrerror (errno, errbuf, sizeof (errbuf)));
return (-1);
}
- plugin_relief (LOG_NOTICE, &complain_obj, "vserver plugin: "
- "fopen (%s) succeeded.", PROCDIR);
- while (NULL != (dent = readdir (proc)))
+ while (42)
{
- int len;
+ int len;
char file[BUFSIZE];
FILE *fh;
char buffer[BUFSIZE];
+ struct stat statbuf;
char *cols[4];
+ int status;
+
+ status = readdir_r (proc, (struct dirent *) dirent_buffer, &dent);
+ if (status != 0)
+ {
+ char errbuf[4096];
+ ERROR ("vserver plugin: readdir_r failed: %s",
+ sstrerror (errno, errbuf, sizeof (errbuf)));
+ closedir (proc);
+ return (-1);
+ }
+ else if (dent == NULL)
+ {
+ /* end of directory */
+ break;
+ }
+
if (dent->d_name[0] == '.')
continue;
- /* This is not a directory */
- if (dent->d_type != DT_DIR)
+ len = ssnprintf (file, sizeof (file), PROCDIR "/%s", dent->d_name);
+ if ((len < 0) || (len >= BUFSIZE))
+ continue;
+
+ status = stat (file, &statbuf);
+ if (status != 0)
+ {
+ char errbuf[4096];
+ WARNING ("vserver plugin: stat (%s) failed: %s",
+ file, sstrerror (errno, errbuf, sizeof (errbuf)));
+ continue;
+ }
+
+ if (!S_ISDIR (statbuf.st_mode))
continue;
/* socket message accounting */
- len = snprintf (file, BUFSIZE, PROCDIR "/%s/cacct", dent->d_name);
- if ((len < 0) || (len >= BUFSIZE))
+ len = ssnprintf (file, sizeof (file),
+ PROCDIR "/%s/cacct", dent->d_name);
+ if ((len < 0) || ((size_t) len >= sizeof (file)))
continue;
if (NULL == (fh = fopen (file, "r")))
- syslog (LOG_ERR, "Cannot open '%s': %s", file, strerror (errno));
+ {
+ char errbuf[1024];
+ ERROR ("Cannot open '%s': %s", file,
+ sstrerror (errno, errbuf, sizeof (errbuf)));
+ }
while ((fh != NULL) && (NULL != fgets (buffer, BUFSIZE, fh)))
{
- counter_t rx;
- counter_t tx;
+ derive_t rx;
+ derive_t tx;
char *type_instance;
if (strsplit (buffer, cols, 4) < 4)
else
continue;
- rx = __get_sock_bytes (cols[1]);
- tx = __get_sock_bytes (cols[2]);
+ rx = vserver_get_sock_bytes (cols[1]);
+ tx = vserver_get_sock_bytes (cols[2]);
/* cols[3] == errors */
traffic_submit (dent->d_name, type_instance, rx, tx);
}
/* thread information and load */
- len = snprintf (file, BUFSIZE, PROCDIR "/%s/cvirt", dent->d_name);
- if ((len < 0) || (len >= BUFSIZE))
+ len = ssnprintf (file, sizeof (file),
+ PROCDIR "/%s/cvirt", dent->d_name);
+ if ((len < 0) || ((size_t) len >= sizeof (file)))
continue;
if (NULL == (fh = fopen (file, "r")))
- syslog (LOG_ERR, "Cannot open '%s': %s", file, strerror (errno));
+ {
+ char errbuf[1024];
+ ERROR ("Cannot open '%s': %s", file,
+ sstrerror (errno, errbuf, sizeof (errbuf)));
+ }
while ((fh != NULL) && (NULL != fgets (buffer, BUFSIZE, fh)))
{
}
/* processes and memory usage */
- len = snprintf (file, BUFSIZE, PROCDIR "/%s/limit", dent->d_name);
- if ((len < 0) || (len >= BUFSIZE))
+ len = ssnprintf (file, sizeof (file),
+ PROCDIR "/%s/limit", dent->d_name);
+ if ((len < 0) || ((size_t) len >= sizeof (file)))
continue;
if (NULL == (fh = fopen (file, "r")))
- syslog (LOG_ERR, "Cannot open '%s': %s", file, strerror (errno));
+ {
+ char errbuf[1024];
+ ERROR ("Cannot open '%s': %s", file,
+ sstrerror (errno, errbuf, sizeof (errbuf)));
+ }
while ((fh != NULL) && (NULL != fgets (buffer, BUFSIZE, fh)))
{
return (0);
} /* int vserver_read */
-#endif /* VSERVER_HAVE_READ */
void module_register (void)
{
- plugin_register_data_set (&octets_ds);
- plugin_register_data_set (&load_ds);
- plugin_register_data_set (&threads_ds);
- plugin_register_data_set (&processes_ds);
- plugin_register_data_set (&memory_ds);
-
-#if VSERVER_HAVE_READ
plugin_register_init ("vserver", vserver_init);
plugin_register_read ("vserver", vserver_read);
-#endif /* VSERVER_HAVE_READ */
} /* void module_register(void) */
/* vim: set ts=4 sw=4 noexpandtab : */