{GPL, other}: Relicense to MIT license.
[collectd.git] / src / vserver.c
index dac4392..bd2e867 100644 (file)
@@ -1,22 +1,29 @@
 /**
  * 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; only version 2 of the license is applicable.
+ * 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"
@@ -46,17 +53,16 @@ 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);
        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));
@@ -78,7 +84,6 @@ static void load_submit (const char *plugin_instance,
 
        vl.values = values;
        vl.values_len = STATIC_ARRAY_SIZE (values);
-       vl.time = time (NULL);
        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));
@@ -98,7 +103,6 @@ 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);
        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));
@@ -108,14 +112,21 @@ static void submit_gauge (const char *plugin_instance, const char *type,
        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)
@@ -142,7 +153,7 @@ static int vserver_read (void)
 
        while (42)
        {
-               size_t len;
+               int len;
                char file[BUFSIZE];
 
                FILE *fh;
@@ -171,7 +182,7 @@ static int vserver_read (void)
                if (dent->d_name[0] == '.')
                        continue;
 
-               len = snprintf (file, sizeof (file), PROCDIR "/%s", dent->d_name);
+               len = ssnprintf (file, sizeof (file), PROCDIR "/%s", dent->d_name);
                if ((len < 0) || (len >= BUFSIZE))
                        continue;
                
@@ -190,7 +201,7 @@ static int vserver_read (void)
                /* socket message accounting */
                len = ssnprintf (file, sizeof (file),
                                PROCDIR "/%s/cacct", dent->d_name);
-               if ((len < 0) || (len >= sizeof (file)))
+               if ((len < 0) || ((size_t) len >= sizeof (file)))
                        continue;
 
                if (NULL == (fh = fopen (file, "r")))
@@ -202,8 +213,8 @@ static int vserver_read (void)
 
                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)
@@ -222,8 +233,8 @@ static int vserver_read (void)
                        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);
@@ -238,7 +249,7 @@ static int vserver_read (void)
                /* thread information and load */
                len = ssnprintf (file, sizeof (file),
                                PROCDIR "/%s/cvirt", dent->d_name);
-               if ((len < 0) || (len >= sizeof (file)))
+               if ((len < 0) || ((size_t) len >= sizeof (file)))
                        continue;
 
                if (NULL == (fh = fopen (file, "r")))
@@ -291,7 +302,7 @@ static int vserver_read (void)
                /* processes and memory usage */
                len = ssnprintf (file, sizeof (file),
                                PROCDIR "/%s/limit", dent->d_name);
-               if ((len < 0) || (len >= sizeof (file)))
+               if ((len < 0) || ((size_t) len >= sizeof (file)))
                        continue;
 
                if (NULL == (fh = fopen (file, "r")))