b9afd81764b1a0ba2227000966f984f33b7825fd
[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   mec_match_t *m;
77   int num_counters;
78   int num_empty;
79   int i;
80
81   if ((user_data == NULL) || (*user_data == NULL))
82     return (-1);
83
84   m = *user_data;
85
86   num_counters = 0;
87   num_empty = 0;
88
89   for (i = 0; i < ds->ds_num; i++)
90   {
91     if (ds->ds[i].type != DS_TYPE_COUNTER)
92       continue;
93
94     num_counters++;
95     if (vl->values[i].counter == 0)
96       num_empty++;
97   }
98
99   if (num_counters == 0)
100     return (FC_MATCH_NO_MATCH);
101   else if (num_counters == num_empty)
102     return (FC_MATCH_MATCHES);
103   else
104     return (FC_MATCH_NO_MATCH);
105 } /* }}} int mec_match */
106
107 void module_register (void)
108 {
109   match_proc_t mproc;
110
111   memset (&mproc, 0, sizeof (mproc));
112   mproc.create  = mec_create;
113   mproc.destroy = mec_destroy;
114   mproc.match   = mec_match;
115   fc_register_match ("empty_counter", mproc);
116 } /* module_register */
117
118 /* vim: set sw=2 sts=2 tw=78 et fdm=marker : */