Merge pull request #1806 from rubenk/network-plugin-size_t
[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 "filter_chain.h"
30
31 #define SATISFY_ALL 0
32 #define SATISFY_ANY 1
33
34 /*
35  * private data types
36  */
37 struct mt_match_s;
38 typedef struct mt_match_s mt_match_t;
39 struct mt_match_s
40 {
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   int i;
53
54   m = calloc (1, sizeof (*m));
55   if (m == NULL)
56   {
57     ERROR ("mt_create: calloc failed.");
58     return (-ENOMEM);
59   }
60
61   m->future = 0;
62   m->past = 0;
63
64   status = 0;
65   for (i = 0; i < ci->children_num; i++)
66   {
67     oconfig_item_t *child = ci->children + i;
68
69     if (strcasecmp ("Future", child->key) == 0)
70       status = cf_util_get_cdtime (child, &m->future);
71     else if (strcasecmp ("Past", child->key) == 0)
72       status = cf_util_get_cdtime (child, &m->past);
73     else
74     {
75       ERROR ("timediff match: The `%s' configuration option is not "
76           "understood and will be ignored.", child->key);
77       status = 0;
78     }
79
80     if (status != 0)
81       break;
82   }
83
84   /* Additional sanity-checking */
85   while (status == 0)
86   {
87     if ((m->future == 0) && (m->past == 0))
88     {
89       ERROR ("timediff match: Either `Future' or `Past' must be configured. "
90           "This match will be ignored.");
91       status = -1;
92     }
93
94     break;
95   }
96
97   if (status != 0)
98   {
99     free (m);
100     return (status);
101   }
102
103   *user_data = m;
104   return (0);
105 } /* }}} int mt_create */
106
107 static int mt_destroy (void **user_data) /* {{{ */
108 {
109   if (user_data != NULL)
110   {
111     sfree (*user_data);
112   }
113
114   return (0);
115 } /* }}} int mt_destroy */
116
117 static int mt_match (const data_set_t __attribute__((unused)) *ds, /* {{{ */
118     const value_list_t *vl,
119     notification_meta_t __attribute__((unused)) **meta, void **user_data)
120 {
121   mt_match_t *m;
122   cdtime_t now;
123
124   if ((user_data == NULL) || (*user_data == NULL))
125     return (-1);
126
127   m = *user_data;
128   now = cdtime ();
129
130   if (m->future != 0)
131   {
132     if (vl->time >= (now + m->future))
133       return (FC_MATCH_MATCHES);
134   }
135
136   if (m->past != 0)
137   {
138     if (vl->time <= (now - m->past))
139       return (FC_MATCH_MATCHES);
140   }
141
142   return (FC_MATCH_NO_MATCH);
143 } /* }}} int mt_match */
144
145 void module_register (void)
146 {
147   match_proc_t mproc;
148
149   memset (&mproc, 0, sizeof (mproc));
150   mproc.create  = mt_create;
151   mproc.destroy = mt_destroy;
152   mproc.match   = mt_match;
153   fc_register_match ("timediff", mproc);
154 } /* module_register */
155
156 /* vim: set sw=2 sts=2 tw=78 et fdm=marker : */