2 * collectd - src/match_hashed.c
3 * Copyright (C) 2009 Florian Forster
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.
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.
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
19 * Florian Forster <octo at verplant.org>
24 #include "utils_cache.h"
25 #include "filter_chain.h"
30 struct mh_hash_match_s
35 typedef struct mh_hash_match_s mh_hash_match_t;
38 typedef struct mh_match_s mh_match_t;
41 mh_hash_match_t *matches;
46 * internal helper functions
48 static int mh_config_match (const oconfig_item_t *ci, /* {{{ */
53 if ((ci->values_num != 2)
54 || (ci->values[0].type != OCONFIG_TYPE_NUMBER)
55 || (ci->values[1].type != OCONFIG_TYPE_NUMBER))
57 ERROR ("hashed match: The `Match' option requires "
58 "exactly two numeric arguments.");
62 if ((ci->values[0].value.number < 0)
63 || (ci->values[1].value.number < 0))
65 ERROR ("hashed match: The arguments of the `Match' "
66 "option must be positive.");
70 tmp = realloc (m->matches, sizeof (*tmp) * (m->matches_num + 1));
73 ERROR ("hashed match: realloc failed.");
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)
84 ERROR ("hashed match: The first argument of the `Match' option "
85 "must be smaller than the second argument.");
88 assert (tmp->total != 0);
92 } /* }}} int mh_config_match */
94 static int mh_create (const oconfig_item_t *ci, void **user_data) /* {{{ */
99 m = (mh_match_t *) malloc (sizeof (*m));
102 ERROR ("mh_create: malloc failed.");
105 memset (m, 0, sizeof (*m));
107 for (i = 0; i < ci->children_num; i++)
109 oconfig_item_t *child = ci->children + i;
111 if (strcasecmp ("Match", child->key) == 0)
112 mh_config_match (child, m);
114 ERROR ("hashed match: No such config option: %s", child->key);
117 if (m->matches_num == 0)
121 ERROR ("hashed match: No matches were configured. Not creating match.");
127 } /* }}} int mh_create */
129 static int mh_destroy (void **user_data) /* {{{ */
133 if ((user_data == NULL) || (*user_data == NULL))
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)
149 const char *host_ptr;
152 if ((user_data == NULL) || (*user_data == NULL))
159 for (host_ptr = vl->host; *host_ptr != 0; host_ptr++)
161 /* 251 is the largest prime smaller than 256. */
162 hash_val = (hash_val * 251) + ((uint32_t) *host_ptr);
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)
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 : */