{GPL, other}: Relicense to MIT license.
[collectd.git] / src / match_empty_counter.c
1 /**
2  * collectd - src/match_empty_counter.c
3  * Copyright (C) 2009       Florian Forster
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining a
6  * copy of this software and associated documentation files (the "Software"),
7  * to deal in the Software without restriction, including without limitation
8  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9  * and/or sell copies of the Software, and to permit persons to whom the
10  * Software is furnished to do so, subject to the following conditions:
11  *
12  * The above copyright notice and this permission notice shall be included in
13  * all copies or substantial portions of the Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21  * DEALINGS IN THE SOFTWARE.
22  *
23  * Authors:
24  *   Florian Forster <octo at collectd.org>
25  **/
26
27 #include "collectd.h"
28 #include "common.h"
29 #include "utils_cache.h"
30 #include "filter_chain.h"
31
32 /*
33  * private data types
34  */
35 struct mec_match_s;
36 typedef struct mec_match_s mec_match_t;
37 struct mec_match_s
38 {
39   int dummy;
40 };
41
42 /*
43  * internal helper functions
44  */
45 static int mec_create (const oconfig_item_t *ci, void **user_data) /* {{{ */
46 {
47   mec_match_t *m;
48
49   m = (mec_match_t *) malloc (sizeof (*m));
50   if (m == NULL)
51   {
52     ERROR ("mec_create: malloc failed.");
53     return (-ENOMEM);
54   }
55   memset (m, 0, sizeof (*m));
56
57   if (ci->children_num != 0)
58   {
59     ERROR ("empty_counter match: This match does not take any additional "
60         "configuration.");
61   }
62
63   *user_data = m;
64   return (0);
65 } /* }}} int mec_create */
66
67 static int mec_destroy (void **user_data) /* {{{ */
68 {
69   if (user_data != NULL)
70   {
71     sfree (*user_data);
72   }
73
74   return (0);
75 } /* }}} int mec_destroy */
76
77 static int mec_match (const data_set_t __attribute__((unused)) *ds, /* {{{ */
78     const value_list_t *vl,
79     notification_meta_t __attribute__((unused)) **meta, void **user_data)
80 {
81   int num_counters;
82   int num_empty;
83   int i;
84
85   if ((user_data == NULL) || (*user_data == NULL))
86     return (-1);
87
88
89   num_counters = 0;
90   num_empty = 0;
91
92   for (i = 0; i < ds->ds_num; i++)
93   {
94     if (ds->ds[i].type != DS_TYPE_COUNTER)
95       continue;
96
97     num_counters++;
98     if (vl->values[i].counter == 0)
99       num_empty++;
100   }
101
102   if (num_counters == 0)
103     return (FC_MATCH_NO_MATCH);
104   else if (num_counters == num_empty)
105     return (FC_MATCH_MATCHES);
106   else
107     return (FC_MATCH_NO_MATCH);
108 } /* }}} int mec_match */
109
110 void module_register (void)
111 {
112   match_proc_t mproc;
113
114   memset (&mproc, 0, sizeof (mproc));
115   mproc.create  = mec_create;
116   mproc.destroy = mec_destroy;
117   mproc.match   = mec_match;
118   fc_register_match ("empty_counter", mproc);
119 } /* module_register */
120
121 /* vim: set sw=2 sts=2 tw=78 et fdm=marker : */