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 **/
27 #include "collectd.h"
29 #include "common.h"
30 #include "filter_chain.h"
32 /*
33 * private data types
34 */
35 struct mh_hash_match_s {
36 uint32_t match;
37 uint32_t total;
38 };
39 typedef struct mh_hash_match_s mh_hash_match_t;
41 struct mh_match_s;
42 typedef struct mh_match_s mh_match_t;
43 struct mh_match_s {
44 mh_hash_match_t *matches;
45 size_t matches_num;
46 };
48 /*
49 * internal helper functions
50 */
51 static int mh_config_match(const oconfig_item_t *ci, /* {{{ */
52 mh_match_t *m) {
53 mh_hash_match_t *tmp;
55 if ((ci->values_num != 2) || (ci->values[0].type != OCONFIG_TYPE_NUMBER) ||
56 (ci->values[1].type != OCONFIG_TYPE_NUMBER)) {
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) || (ci->values[1].value.number < 0)) {
63 ERROR("hashed match: The arguments of the `Match' "
64 "option must be positive.");
65 return (-1);
66 }
68 tmp = realloc(m->matches, sizeof(*tmp) * (m->matches_num + 1));
69 if (tmp == NULL) {
70 ERROR("hashed match: realloc failed.");
71 return (-1);
72 }
73 m->matches = tmp;
74 tmp = m->matches + m->matches_num;
76 tmp->match = (uint32_t)(ci->values[0].value.number + .5);
77 tmp->total = (uint32_t)(ci->values[1].value.number + .5);
79 if (tmp->match >= tmp->total) {
80 ERROR("hashed match: The first argument of the `Match' option "
81 "must be smaller than the second argument.");
82 return (-1);
83 }
84 assert(tmp->total != 0);
86 m->matches_num++;
87 return (0);
88 } /* }}} int mh_config_match */
90 static int mh_create(const oconfig_item_t *ci, void **user_data) /* {{{ */
91 {
92 mh_match_t *m;
94 m = calloc(1, sizeof(*m));
95 if (m == NULL) {
96 ERROR("mh_create: calloc failed.");
97 return (-ENOMEM);
98 }
100 for (int i = 0; i < ci->children_num; i++) {
101 oconfig_item_t *child = ci->children + i;
103 if (strcasecmp("Match", child->key) == 0)
104 mh_config_match(child, m);
105 else
106 ERROR("hashed match: No such config option: %s", child->key);
107 }
109 if (m->matches_num == 0) {
110 sfree(m->matches);
111 sfree(m);
112 ERROR("hashed match: No matches were configured. Not creating match.");
113 return (-1);
114 }
116 *user_data = m;
117 return (0);
118 } /* }}} int mh_create */
120 static int mh_destroy(void **user_data) /* {{{ */
121 {
122 mh_match_t *mh;
124 if ((user_data == NULL) || (*user_data == NULL))
125 return (0);
127 mh = *user_data;
128 sfree(mh->matches);
129 sfree(mh);
131 return (0);
132 } /* }}} int mh_destroy */
134 static int mh_match(const data_set_t __attribute__((unused)) * ds, /* {{{ */
135 const value_list_t *vl,
136 notification_meta_t __attribute__((unused)) * *meta,
137 void **user_data) {
138 mh_match_t *m;
139 uint32_t hash_val;
141 if ((user_data == NULL) || (*user_data == NULL))
142 return (-1);
144 m = *user_data;
146 hash_val = 0;
148 for (const char *host_ptr = vl->host; *host_ptr != 0; host_ptr++) {
149 /* 2184401929 is some appropriately sized prime number. */
150 hash_val = (hash_val * UINT32_C(2184401929)) + ((uint32_t)*host_ptr);
151 }
152 DEBUG("hashed match: host = %s; hash_val = %" PRIu32 ";", vl->host, hash_val);
154 for (size_t i = 0; i < m->matches_num; i++)
155 if ((hash_val % m->matches[i].total) == m->matches[i].match)
156 return (FC_MATCH_MATCHES);
158 return (FC_MATCH_NO_MATCH);
159 } /* }}} int mh_match */
161 void module_register(void) {
162 match_proc_t mproc = {0};
164 mproc.create = mh_create;
165 mproc.destroy = mh_destroy;
166 mproc.match = mh_match;
167 fc_register_match("hashed", mproc);
168 } /* module_register */
170 /* vim: set sw=2 sts=2 tw=78 et fdm=marker : */