treewide: remove unused includes
[collectd.git] / src / match_hashed.c
1 /**
2  * collectd - src/match_hashed.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 "filter_chain.h"
30
31 /*
32  * private data types
33  */
34 struct mh_hash_match_s
35 {
36   uint32_t match;
37   uint32_t total;
38 };
39 typedef struct mh_hash_match_s mh_hash_match_t;
40
41 struct mh_match_s;
42 typedef struct mh_match_s mh_match_t;
43 struct mh_match_s
44 {
45   mh_hash_match_t *matches;
46   size_t           matches_num;
47 };
48
49 /*
50  * internal helper functions
51  */
52 static int mh_config_match (const oconfig_item_t *ci, /* {{{ */
53     mh_match_t *m)
54 {
55   mh_hash_match_t *tmp;
56
57   if ((ci->values_num != 2)
58       || (ci->values[0].type != OCONFIG_TYPE_NUMBER)
59       || (ci->values[1].type != OCONFIG_TYPE_NUMBER))
60   {
61     ERROR ("hashed match: The `Match' option requires "
62         "exactly two numeric arguments.");
63     return (-1);
64   }
65
66   if ((ci->values[0].value.number < 0)
67       || (ci->values[1].value.number < 0))
68   {
69     ERROR ("hashed match: The arguments of the `Match' "
70         "option must be positive.");
71     return (-1);
72   }
73
74   tmp = realloc (m->matches, sizeof (*tmp) * (m->matches_num + 1));
75   if (tmp == NULL)
76   {
77     ERROR ("hashed match: realloc failed.");
78     return (-1);
79   }
80   m->matches = tmp;
81   tmp = m->matches + m->matches_num;
82
83   tmp->match = (uint32_t) (ci->values[0].value.number + .5);
84   tmp->total = (uint32_t) (ci->values[1].value.number + .5);
85
86   if (tmp->match >= tmp->total)
87   {
88     ERROR ("hashed match: The first argument of the `Match' option "
89         "must be smaller than the second argument.");
90     return (-1);
91   }
92   assert (tmp->total != 0);
93
94   m->matches_num++;
95   return (0);
96 } /* }}} int mh_config_match */
97
98 static int mh_create (const oconfig_item_t *ci, void **user_data) /* {{{ */
99 {
100   mh_match_t *m;
101   int i;
102
103   m = calloc (1, sizeof (*m));
104   if (m == NULL)
105   {
106     ERROR ("mh_create: calloc failed.");
107     return (-ENOMEM);
108   }
109
110   for (i = 0; i < ci->children_num; i++)
111   {
112     oconfig_item_t *child = ci->children + i;
113
114     if (strcasecmp ("Match", child->key) == 0)
115       mh_config_match (child, m);
116     else
117       ERROR ("hashed match: No such config option: %s", child->key);
118   }
119
120   if (m->matches_num == 0)
121   {
122     sfree (m->matches);
123     sfree (m);
124     ERROR ("hashed match: No matches were configured. Not creating match.");
125     return (-1);
126   }
127
128   *user_data = m;
129   return (0);
130 } /* }}} int mh_create */
131
132 static int mh_destroy (void **user_data) /* {{{ */
133 {
134   mh_match_t *mh;
135
136   if ((user_data == NULL) || (*user_data == NULL))
137     return (0);
138
139   mh = *user_data;
140   sfree (mh->matches);
141   sfree (mh);
142
143   return (0);
144 } /* }}} int mh_destroy */
145
146 static int mh_match (const data_set_t __attribute__((unused)) *ds, /* {{{ */
147     const value_list_t *vl,
148     notification_meta_t __attribute__((unused)) **meta, void **user_data)
149 {
150   mh_match_t *m;
151   uint32_t hash_val;
152   const char *host_ptr;
153   size_t i;
154
155   if ((user_data == NULL) || (*user_data == NULL))
156     return (-1);
157
158   m = *user_data;
159
160   hash_val = 0;
161
162   for (host_ptr = vl->host; *host_ptr != 0; host_ptr++)
163   {
164     /* 2184401929 is some appropriately sized prime number. */
165     hash_val = (hash_val * UINT32_C (2184401929)) + ((uint32_t) *host_ptr);
166   }
167   DEBUG ("hashed match: host = %s; hash_val = %"PRIu32";", vl->host, hash_val);
168
169   for (i = 0; i < m->matches_num; i++)
170     if ((hash_val % m->matches[i].total) == m->matches[i].match)
171       return (FC_MATCH_MATCHES);
172
173   return (FC_MATCH_NO_MATCH);
174 } /* }}} int mh_match */
175
176 void module_register (void)
177 {
178   match_proc_t mproc;
179
180   memset (&mproc, 0, sizeof (mproc));
181   mproc.create  = mh_create;
182   mproc.destroy = mh_destroy;
183   mproc.match   = mh_match;
184   fc_register_match ("hashed", mproc);
185 } /* module_register */
186
187 /* vim: set sw=2 sts=2 tw=78 et fdm=marker : */