1 /**
2 * collectd - src/match_timediff.c
3 * Copyright (C) 2008,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 #define SATISFY_ALL 0
28 #define SATISFY_ANY 1
30 /*
31 * private data types
32 */
33 struct mt_match_s;
34 typedef struct mt_match_s mt_match_t;
35 struct mt_match_s
36 {
37 time_t future;
38 time_t past;
39 };
41 /*
42 * internal helper functions
43 */
44 static int mt_config_add_time_t (time_t *ret_value, /* {{{ */
45 oconfig_item_t *ci)
46 {
48 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_NUMBER))
49 {
50 ERROR ("timediff match: `%s' needs exactly one numeric argument.",
51 ci->key);
52 return (-1);
53 }
55 *ret_value = (time_t) ci->values[0].value.number;
57 return (0);
58 } /* }}} int mt_config_add_time_t */
60 static int mt_create (const oconfig_item_t *ci, void **user_data) /* {{{ */
61 {
62 mt_match_t *m;
63 int status;
64 int i;
66 m = (mt_match_t *) malloc (sizeof (*m));
67 if (m == NULL)
68 {
69 ERROR ("mt_create: malloc failed.");
70 return (-ENOMEM);
71 }
72 memset (m, 0, sizeof (*m));
74 m->future = 0;
75 m->past = 0;
77 status = 0;
78 for (i = 0; i < ci->children_num; i++)
79 {
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);
86 else
87 {
88 ERROR ("timediff match: The `%s' configuration option is not "
89 "understood and will be ignored.", child->key);
90 status = 0;
91 }
93 if (status != 0)
94 break;
95 }
97 /* Additional sanity-checking */
98 while (status == 0)
99 {
100 if ((m->future == 0) && (m->past == 0))
101 {
102 ERROR ("timediff match: Either `Future' or `Past' must be configured. "
103 "This match will be ignored.");
104 status = -1;
105 }
107 break;
108 }
110 if (status != 0)
111 {
112 free (m);
113 return (status);
114 }
116 *user_data = m;
117 return (0);
118 } /* }}} int mt_create */
120 static int mt_destroy (void **user_data) /* {{{ */
121 {
122 if (user_data != NULL)
123 {
124 sfree (*user_data);
125 }
127 return (0);
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)
133 {
134 mt_match_t *m;
135 time_t now;
137 if ((user_data == NULL) || (*user_data == NULL))
138 return (-1);
140 m = *user_data;
141 now = time (NULL);
143 if (m->future != 0)
144 {
145 if (vl->time >= (now + m->future))
146 return (FC_MATCH_MATCHES);
147 }
149 if (m->past != 0)
150 {
151 if (vl->time <= (now - m->past))
152 return (FC_MATCH_MATCHES);
153 }
155 return (FC_MATCH_NO_MATCH);
156 } /* }}} int mt_match */
158 void module_register (void)
159 {
160 match_proc_t mproc;
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 : */