Auto-Merge pull request #2736 from rpv-tomsk/collectd-collectd-5.8
[collectd.git] / src / match_empty_counter.c
index 1ab445a..27817fe 100644 (file)
 /**
  * collectd - src/match_empty_counter.c
- * Copyright (C) 2009  Florian Forster
+ * Copyright (C) 2009-2016  Florian 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:
- *   Florian Forster <octo at verplant.org>
+ *   Florian Forster <octo at collectd.org>
  **/
 
 #include "collectd.h"
+
 #include "common.h"
-#include "utils_cache.h"
 #include "filter_chain.h"
 
 /*
- * private data types
- */
-struct mec_match_s;
-typedef struct mec_match_s mec_match_t;
-struct mec_match_s
-{
-  int dummy;
-};
-
-/*
  * internal helper functions
  */
-static int mec_create (const oconfig_item_t *ci, void **user_data) /* {{{ */
+static int mec_create(const oconfig_item_t *ci, void **user_data) /* {{{ */
 {
-  mec_match_t *m;
-
-  m = (mec_match_t *) malloc (sizeof (*m));
-  if (m == NULL)
-  {
-    ERROR ("mec_create: malloc failed.");
-    return (-ENOMEM);
-  }
-  memset (m, 0, sizeof (*m));
-
-  if (ci->children_num != 0)
-  {
-    ERROR ("empty_counter match: This match does not take any additional "
-        "configuration.");
+  if (ci->children_num != 0) {
+    ERROR("empty_counter match: This match does not take any additional "
+          "configuration.");
   }
 
-  *user_data = m;
-  return (0);
+  *user_data = NULL;
+  return 0;
 } /* }}} int mec_create */
 
-static int mec_destroy (void **user_data) /* {{{ */
+static int mec_destroy(__attribute__((unused)) void **user_data) /* {{{ */
 {
-  if (user_data != NULL)
-  {
-    sfree (*user_data);
-  }
-
-  return (0);
+  return 0;
 } /* }}} int mec_destroy */
 
-static int mec_match (const data_set_t __attribute__((unused)) *ds, /* {{{ */
-    const value_list_t *vl,
-    notification_meta_t __attribute__((unused)) **meta, void **user_data)
-{
-  int num_counters;
-  int num_empty;
-  int i;
-
-  if ((user_data == NULL) || (*user_data == NULL))
-    return (-1);
-
-
-  num_counters = 0;
-  num_empty = 0;
+static int mec_match(__attribute__((unused)) const data_set_t *ds, /* {{{ */
+                     const value_list_t *vl,
+                     __attribute__((unused)) notification_meta_t **meta,
+                     __attribute__((unused)) void **user_data) {
+  int num_counters = 0;
+  int num_empty = 0;
 
-  for (i = 0; i < ds->ds_num; i++)
-  {
-    if (ds->ds[i].type != DS_TYPE_COUNTER)
+  for (size_t i = 0; i < ds->ds_num; i++) {
+    if ((ds->ds[i].type != DS_TYPE_DERIVE) &&
+        (ds->ds[i].type != DS_TYPE_COUNTER))
       continue;
 
     num_counters++;
-    if (vl->values[i].counter == 0)
+    if (((ds->ds[i].type == DS_TYPE_DERIVE) && (vl->values[i].derive == 0)) ||
+        ((ds->ds[i].type == DS_TYPE_COUNTER) && (vl->values[i].counter == 0)))
       num_empty++;
   }
 
-  if (num_counters == 0)
-    return (FC_MATCH_NO_MATCH);
-  else if (num_counters == num_empty)
-    return (FC_MATCH_MATCHES);
-  else
-    return (FC_MATCH_NO_MATCH);
-} /* }}} int mec_match */
+  if ((num_counters != 0) && (num_counters == num_empty))
+    return FC_MATCH_MATCHES;
 
-void module_register (void)
-{
-  match_proc_t mproc;
+  return FC_MATCH_NO_MATCH;
+} /* }}} int mec_match */
 
-  memset (&mproc, 0, sizeof (mproc));
-  mproc.create  = mec_create;
-  mproc.destroy = mec_destroy;
-  mproc.match   = mec_match;
-  fc_register_match ("empty_counter", mproc);
+void module_register(void) {
+  fc_register_match(
+      "empty_counter",
+      (match_proc_t){
+          .create = mec_create, .destroy = mec_destroy, .match = mec_match,
+      });
 } /* module_register */
-
-/* vim: set sw=2 sts=2 tw=78 et fdm=marker : */