1ab445a7af0d793a825da5e72fd1465dbc59ebad
[collectd.git] / src / match_empty_counter.c
1 /**
2  * collectd - src/match_empty_counter.c
3  * Copyright (C) 2009  Florian Forster
4  *
5  * This program is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License as published by the
7  * Free Software Foundation; only version 2 of the License is applicable.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License along
15  * with this program; if not, write to the Free Software Foundation, Inc.,
16  * 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
17  *
18  * Authors:
19  *   Florian Forster <octo at verplant.org>
20  **/
21
22 #include "collectd.h"
23 #include "common.h"
24 #include "utils_cache.h"
25 #include "filter_chain.h"
26
27 /*
28  * private data types
29  */
30 struct mec_match_s;
31 typedef struct mec_match_s mec_match_t;
32 struct mec_match_s
33 {
34   int dummy;
35 };
36
37 /*
38  * internal helper functions
39  */
40 static int mec_create (const oconfig_item_t *ci, void **user_data) /* {{{ */
41 {
42   mec_match_t *m;
43
44   m = (mec_match_t *) malloc (sizeof (*m));
45   if (m == NULL)
46   {
47     ERROR ("mec_create: malloc failed.");
48     return (-ENOMEM);
49   }
50   memset (m, 0, sizeof (*m));
51
52   if (ci->children_num != 0)
53   {
54     ERROR ("empty_counter match: This match does not take any additional "
55         "configuration.");
56   }
57
58   *user_data = m;
59   return (0);
60 } /* }}} int mec_create */
61
62 static int mec_destroy (void **user_data) /* {{{ */
63 {
64   if (user_data != NULL)
65   {
66     sfree (*user_data);
67   }
68
69   return (0);
70 } /* }}} int mec_destroy */
71
72 static int mec_match (const data_set_t __attribute__((unused)) *ds, /* {{{ */
73     const value_list_t *vl,
74     notification_meta_t __attribute__((unused)) **meta, void **user_data)
75 {
76   int num_counters;
77   int num_empty;
78   int i;
79
80   if ((user_data == NULL) || (*user_data == NULL))
81     return (-1);
82
83
84   num_counters = 0;
85   num_empty = 0;
86
87   for (i = 0; i < ds->ds_num; i++)
88   {
89     if (ds->ds[i].type != DS_TYPE_COUNTER)
90       continue;
91
92     num_counters++;
93     if (vl->values[i].counter == 0)
94       num_empty++;
95   }
96
97   if (num_counters == 0)
98     return (FC_MATCH_NO_MATCH);
99   else if (num_counters == num_empty)
100     return (FC_MATCH_MATCHES);
101   else
102     return (FC_MATCH_NO_MATCH);
103 } /* }}} int mec_match */
104
105 void module_register (void)
106 {
107   match_proc_t mproc;
108
109   memset (&mproc, 0, sizeof (mproc));
110   mproc.create  = mec_create;
111   mproc.destroy = mec_destroy;
112   mproc.match   = mec_match;
113   fc_register_match ("empty_counter", mproc);
114 } /* module_register */
115
116 /* vim: set sw=2 sts=2 tw=78 et fdm=marker : */