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