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