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