Merge branch 'collectd-5.7' into collectd-5.8
[collectd.git] / src / match_timediff.c
1 /**
2  * collectd - src/match_timediff.c
3  * Copyright (C) 2008,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  **/
26
27 #include "collectd.h"
28
29 #include "common.h"
30 #include "filter_chain.h"
31
32 #define SATISFY_ALL 0
33 #define SATISFY_ANY 1
34
35 /*
36  * private data types
37  */
38 struct mt_match_s;
39 typedef struct mt_match_s mt_match_t;
40 struct mt_match_s {
41   cdtime_t future;
42   cdtime_t past;
43 };
44
45 /*
46  * internal helper functions
47  */
48 static int mt_create(const oconfig_item_t *ci, void **user_data) /* {{{ */
49 {
50   mt_match_t *m;
51   int status;
52
53   m = calloc(1, sizeof(*m));
54   if (m == NULL) {
55     ERROR("mt_create: calloc failed.");
56     return -ENOMEM;
57   }
58
59   m->future = 0;
60   m->past = 0;
61
62   status = 0;
63   for (int i = 0; i < ci->children_num; i++) {
64     oconfig_item_t *child = ci->children + i;
65
66     if (strcasecmp("Future", child->key) == 0)
67       status = cf_util_get_cdtime(child, &m->future);
68     else if (strcasecmp("Past", child->key) == 0)
69       status = cf_util_get_cdtime(child, &m->past);
70     else {
71       ERROR("timediff match: The `%s' configuration option is not "
72             "understood and will be ignored.",
73             child->key);
74       status = 0;
75     }
76
77     if (status != 0)
78       break;
79   }
80
81   /* Additional sanity-checking */
82   while (status == 0) {
83     if ((m->future == 0) && (m->past == 0)) {
84       ERROR("timediff match: Either `Future' or `Past' must be configured. "
85             "This match will be ignored.");
86       status = -1;
87     }
88
89     break;
90   }
91
92   if (status != 0) {
93     free(m);
94     return status;
95   }
96
97   *user_data = m;
98   return 0;
99 } /* }}} int mt_create */
100
101 static int mt_destroy(void **user_data) /* {{{ */
102 {
103   if (user_data != NULL) {
104     sfree(*user_data);
105   }
106
107   return 0;
108 } /* }}} int mt_destroy */
109
110 static int mt_match(const data_set_t __attribute__((unused)) * ds, /* {{{ */
111                     const value_list_t *vl,
112                     notification_meta_t __attribute__((unused)) * *meta,
113                     void **user_data) {
114   mt_match_t *m;
115   cdtime_t now;
116
117   if ((user_data == NULL) || (*user_data == NULL))
118     return -1;
119
120   m = *user_data;
121   now = cdtime();
122
123   if (m->future != 0) {
124     if (vl->time >= (now + m->future))
125       return FC_MATCH_MATCHES;
126   }
127
128   if (m->past != 0) {
129     if (vl->time <= (now - m->past))
130       return FC_MATCH_MATCHES;
131   }
132
133   return FC_MATCH_NO_MATCH;
134 } /* }}} int mt_match */
135
136 void module_register(void) {
137   match_proc_t mproc = {0};
138
139   mproc.create = mt_create;
140   mproc.destroy = mt_destroy;
141   mproc.match = mt_match;
142   fc_register_match("timediff", mproc);
143 } /* module_register */