2 * collectd - src/match_timediff.c
3 * Copyright (C) 2008,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"
34 typedef struct mt_match_s mt_match_t;
42 * internal helper functions
44 static int mt_config_add_time_t (time_t *ret_value, /* {{{ */
48 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_NUMBER))
50 ERROR ("timediff match: `%s' needs exactly one numeric argument.",
55 *ret_value = (time_t) ci->values[0].value.number;
58 } /* }}} int mt_config_add_time_t */
60 static int mt_create (const oconfig_item_t *ci, void **user_data) /* {{{ */
66 m = (mt_match_t *) malloc (sizeof (*m));
69 ERROR ("mt_create: malloc failed.");
72 memset (m, 0, sizeof (*m));
78 for (i = 0; i < ci->children_num; i++)
80 oconfig_item_t *child = ci->children + i;
82 if (strcasecmp ("Future", child->key) == 0)
83 status = mt_config_add_time_t (&m->future, child);
84 else if (strcasecmp ("Past", child->key) == 0)
85 status = mt_config_add_time_t (&m->past, child);
88 ERROR ("timediff match: The `%s' configuration option is not "
89 "understood and will be ignored.", child->key);
97 /* Additional sanity-checking */
100 if ((m->future == 0) && (m->past == 0))
102 ERROR ("timediff match: Either `Future' or `Past' must be configured. "
103 "This match will be ignored.");
118 } /* }}} int mt_create */
120 static int mt_destroy (void **user_data) /* {{{ */
122 if (user_data != NULL)
128 } /* }}} int mt_destroy */
130 static int mt_match (const data_set_t __attribute__((unused)) *ds, /* {{{ */
131 const value_list_t *vl,
132 notification_meta_t __attribute__((unused)) **meta, void **user_data)
137 if ((user_data == NULL) || (*user_data == NULL))
145 if (vl->time >= (now + m->future))
146 return (FC_MATCH_MATCHES);
151 if (vl->time <= (now - m->past))
152 return (FC_MATCH_MATCHES);
155 return (FC_MATCH_NO_MATCH);
156 } /* }}} int mt_match */
158 void module_register (void)
162 memset (&mproc, 0, sizeof (mproc));
163 mproc.create = mt_create;
164 mproc.destroy = mt_destroy;
165 mproc.match = mt_match;
166 fc_register_match ("timediff", mproc);
167 } /* module_register */
169 /* vim: set sw=2 sts=2 tw=78 et fdm=marker : */