Merge branch 'collectd-5.5' into collectd-5.6
[collectd.git] / src / match_empty_counter.c
1 /**
2  * collectd - src/match_empty_counter.c
3  * Copyright (C) 2009-2016  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
29 #include "common.h"
30 #include "filter_chain.h"
31
32 /*
33  * internal helper functions
34  */
35 static int mec_create (const oconfig_item_t *ci, void **user_data) /* {{{ */
36 {
37   if (ci->children_num != 0)
38   {
39     ERROR ("empty_counter match: This match does not take any additional "
40         "configuration.");
41   }
42
43   *user_data = NULL;
44   return (0);
45 } /* }}} int mec_create */
46
47 static int mec_destroy (__attribute__((unused)) void **user_data) /* {{{ */
48 {
49   return (0);
50 } /* }}} int mec_destroy */
51
52 static int mec_match (__attribute__((unused)) const data_set_t *ds, /* {{{ */
53     const value_list_t *vl,
54     __attribute__((unused)) notification_meta_t **meta,
55     __attribute__((unused)) void **user_data)
56 {
57   int num_counters = 0;
58   int num_empty = 0;
59
60   for (size_t i = 0; i < ds->ds_num; i++)
61   {
62     if ((ds->ds[i].type != DS_TYPE_DERIVE)
63         && (ds->ds[i].type != DS_TYPE_COUNTER))
64       continue;
65
66     num_counters++;
67     if (((ds->ds[i].type == DS_TYPE_DERIVE) && (vl->values[i].derive == 0))
68         || ((ds->ds[i].type == DS_TYPE_COUNTER) && (vl->values[i].counter == 0)))
69       num_empty++;
70   }
71
72   if ((num_counters != 0) && (num_counters == num_empty))
73     return (FC_MATCH_MATCHES);
74
75   return (FC_MATCH_NO_MATCH);
76 } /* }}} int mec_match */
77
78 void module_register (void)
79 {
80   fc_register_match ("empty_counter", (match_proc_t) {
81     .create  = mec_create,
82     .destroy = mec_destroy,
83     .match   = mec_match,
84   });
85 } /* module_register */
86
87 /* vim: set sw=2 sts=2 tw=78 et fdm=marker : */