Merge branch 'collectd-4.5'
[collectd.git] / src / filter_ignore.c
1 /**
2  * collectd - src/entropy.c
3  * Copyright (C) 2008  Florian octo 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 octo Forster <octo at verplant.org>
20  **/
21
22 #include "collectd.h"
23 #include "common.h"
24 #include "plugin.h"
25 #include "configfile.h"
26 #include "utils_ignorelist.h"
27
28 /*
29  * Variables
30  */
31 static ignorelist_t *il_host   = NULL;
32 static ignorelist_t *il_plugin = NULL;
33 static ignorelist_t *il_type   = NULL;
34
35 static const char *config_keys[] =
36 {
37   "IgnoreHost",
38   "IgnorePlugin",
39   "IgnoreType"
40 };
41 static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
42
43 /*
44  * Functions
45  */
46 static int ignorelist_add_create (ignorelist_t **il_ptr, const char *entry)
47 {
48   ignorelist_t *il;
49   int status;
50
51   il = *il_ptr;
52
53   if (il == NULL)
54   {
55     il = ignorelist_create (/* ignore = */ 0);
56     if (il == NULL)
57     {
58       ERROR ("filter_ignore plugin: ignorelist_create failed.");
59       return (-1);
60     }
61     *il_ptr = il;
62   }
63
64   status = ignorelist_add (il, entry);
65   if (status != 0)
66   {
67     ERROR ("filter_ignore plugin: ignorelist_add failed with error %i.",
68         status);
69     return (status);
70   }
71
72   return (0);
73 } /* int ignorelist_add_create */
74
75 static int fi_config (const char *key, const char *value)
76 {
77   int status;
78
79   status = 0;
80
81   if (strcasecmp ("IgnoreHost", key) == 0)
82     status = ignorelist_add_create (&il_host, value);
83   else if (strcasecmp ("IgnorePlugin", key) == 0)
84     status = ignorelist_add_create (&il_plugin, value);
85   else if (strcasecmp ("IgnoreType", key) == 0)
86     status = ignorelist_add_create (&il_type, value);
87   else
88     return (-1);
89     
90   if (status < 0)
91     status = status * (-1);
92
93   return (status);
94 } /* int fi_config */
95
96 static int fi_filter (const data_set_t *ds, value_list_t *vl)
97 {
98   int status;
99
100   if (il_host != NULL)
101   {
102     status = ignorelist_match (il_host, vl->host);
103     if (status != 0)
104       return (FILTER_IGNORE);
105   }
106
107   if (il_plugin != NULL)
108   {
109     char buffer[2 * DATA_MAX_NAME_LEN];
110
111     if (vl->plugin_instance[0] == 0)
112       sstrncpy (buffer, vl->plugin, sizeof (buffer));
113     else
114       ssnprintf (buffer, sizeof (buffer), "%s-%s",
115           vl->plugin, vl->plugin_instance);
116
117     status = ignorelist_match (il_plugin, buffer);
118     if (status != 0)
119       return (FILTER_IGNORE);
120   }
121
122   if (il_type != NULL)
123   {
124     char buffer[2 * DATA_MAX_NAME_LEN];
125
126     if (vl->type_instance[0] == 0)
127       sstrncpy (buffer, vl->type, sizeof (buffer));
128     else
129       ssnprintf (buffer, sizeof (buffer), "%s-%s",
130           vl->type, vl->type_instance);
131
132     status = ignorelist_match (il_type, buffer);
133     if (status != 0)
134       return (FILTER_IGNORE);
135   }
136
137   return (0);
138 } /* int fi_filter */
139
140 void module_register (void)
141 {
142   plugin_register_config ("filter_ignore", fi_config,
143       config_keys, config_keys_num);
144   plugin_register_filter ("filter_ignore", fi_filter);
145 } /* void module_register */
146
147 /* vim: set sw=2 sts=2 et fdm=marker : */