{GPL, other}: Relicense to MIT license.
[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 #include "common.h"
29 #include "utils_cache.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 {
42   cdtime_t future;
43   cdtime_t past;
44 };
45
46 /*
47  * internal helper functions
48  */
49 static int mt_create (const oconfig_item_t *ci, void **user_data) /* {{{ */
50 {
51   mt_match_t *m;
52   int status;
53   int i;
54
55   m = (mt_match_t *) malloc (sizeof (*m));
56   if (m == NULL)
57   {
58     ERROR ("mt_create: malloc failed.");
59     return (-ENOMEM);
60   }
61   memset (m, 0, sizeof (*m));
62
63   m->future = 0;
64   m->past = 0;
65
66   status = 0;
67   for (i = 0; i < ci->children_num; i++)
68   {
69     oconfig_item_t *child = ci->children + i;
70
71     if (strcasecmp ("Future", child->key) == 0)
72       status = cf_util_get_cdtime (child, &m->future);
73     else if (strcasecmp ("Past", child->key) == 0)
74       status = cf_util_get_cdtime (child, &m->past);
75     else
76     {
77       ERROR ("timediff match: The `%s' configuration option is not "
78           "understood and will be ignored.", child->key);
79       status = 0;
80     }
81
82     if (status != 0)
83       break;
84   }
85
86   /* Additional sanity-checking */
87   while (status == 0)
88   {
89     if ((m->future == 0) && (m->past == 0))
90     {
91       ERROR ("timediff match: Either `Future' or `Past' must be configured. "
92           "This match will be ignored.");
93       status = -1;
94     }
95
96     break;
97   }
98
99   if (status != 0)
100   {
101     free (m);
102     return (status);
103   }
104
105   *user_data = m;
106   return (0);
107 } /* }}} int mt_create */
108
109 static int mt_destroy (void **user_data) /* {{{ */
110 {
111   if (user_data != NULL)
112   {
113     sfree (*user_data);
114   }
115
116   return (0);
117 } /* }}} int mt_destroy */
118
119 static int mt_match (const data_set_t __attribute__((unused)) *ds, /* {{{ */
120     const value_list_t *vl,
121     notification_meta_t __attribute__((unused)) **meta, void **user_data)
122 {
123   mt_match_t *m;
124   cdtime_t now;
125
126   if ((user_data == NULL) || (*user_data == NULL))
127     return (-1);
128
129   m = *user_data;
130   now = cdtime ();
131
132   if (m->future != 0)
133   {
134     if (vl->time >= (now + m->future))
135       return (FC_MATCH_MATCHES);
136   }
137
138   if (m->past != 0)
139   {
140     if (vl->time <= (now - m->past))
141       return (FC_MATCH_MATCHES);
142   }
143
144   return (FC_MATCH_NO_MATCH);
145 } /* }}} int mt_match */
146
147 void module_register (void)
148 {
149   match_proc_t mproc;
150
151   memset (&mproc, 0, sizeof (mproc));
152   mproc.create  = mt_create;
153   mproc.destroy = mt_destroy;
154   mproc.match   = mt_match;
155   fc_register_match ("timediff", mproc);
156 } /* module_register */
157
158 /* vim: set sw=2 sts=2 tw=78 et fdm=marker : */