X-Git-Url: https://git.octo.it/?a=blobdiff_plain;f=src%2Fvserver.c;h=1615ca04a1fc1bdbb2c22650e4397f49f80ee552;hb=61a1fa91ba73e4fe3a34949f77c5f017056f2b7a;hp=8d524e77c982869f47828e17525adee078dbd934;hpb=5afa2f82d43e2a7952d8f347f6f40b5778c59db8;p=collectd.git diff --git a/src/vserver.c b/src/vserver.c index 8d524e77..1615ca04 100644 --- a/src/vserver.c +++ b/src/vserver.c @@ -1,6 +1,7 @@ /** * collectd - src/vserver.c * Copyright (C) 2006,2007 Sebastian Harl + * Copyright (C) 2007,2008 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 @@ -17,6 +18,7 @@ * * Authors: * Sebastian Harl + * Florian octo Forster **/ #include "collectd.h" @@ -28,16 +30,12 @@ #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) */ +#if !KERNEL_LINUX +# error "No applicable input method." +#endif -#if VSERVER_HAVE_READ static int pagesize = 0; static int vserver_init (void) @@ -60,13 +58,13 @@ static void traffic_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)); - 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, @@ -81,12 +79,12 @@ 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, @@ -100,13 +98,13 @@ 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) @@ -121,11 +119,19 @@ static inline long long __get_sock_bytes(const char *s) 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 */ + char dirent_buffer[DIRENT_BUFFER_SIZE]; errno = 0; - if (NULL == (proc = opendir (PROCDIR))) + proc = opendir (PROCDIR); + if (proc == NULL) { char errbuf[1024]; ERROR ("vserver plugin: fopen (%s): %s", PROCDIR, @@ -133,26 +139,57 @@ static int vserver_read (void) return (-1); } - 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"))) @@ -198,8 +235,9 @@ static int vserver_read (void) } /* 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"))) @@ -250,8 +288,9 @@ static int vserver_read (void) } /* 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"))) @@ -306,14 +345,11 @@ static int vserver_read (void) return (0); } /* int vserver_read */ -#endif /* VSERVER_HAVE_READ */ void module_register (void) { -#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 : */