2 * collectd - src/target_notification.c
3 * Copyright (C) 2008 Florian Forster
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:
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
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.
24 * Florian Forster <octo at collectd.org>
29 #include "filter_chain.h"
30 #include "utils_cache.h"
31 #include "utils_subst.h"
38 typedef struct tn_data_s tn_data_t;
40 static int tn_config_add_severity (tn_data_t *data, /* {{{ */
41 const oconfig_item_t *ci)
43 if ((ci->values_num != 1)
44 || (ci->values[0].type != OCONFIG_TYPE_STRING))
46 ERROR ("Target `notification': The `%s' option requires exactly one string "
47 "argument.", ci->key);
51 if ((strcasecmp ("FAILURE", ci->values[0].value.string) == 0)
52 || (strcasecmp ("CRITICAL", ci->values[0].value.string) == 0))
53 data->severity = NOTIF_FAILURE;
54 else if ((strcasecmp ("WARNING", ci->values[0].value.string) == 0)
55 || (strcasecmp ("WARN", ci->values[0].value.string) == 0))
56 data->severity = NOTIF_WARNING;
57 else if (strcasecmp ("OKAY", ci->values[0].value.string) == 0)
58 data->severity = NOTIF_OKAY;
61 WARNING ("Target `notification': Unknown severity `%s'. "
62 "Will use `FAILURE' instead.",
63 ci->values[0].value.string);
64 data->severity = NOTIF_FAILURE;
68 } /* }}} int tn_config_add_severity */
70 static int tn_config_add_string (char **dest, /* {{{ */
71 const oconfig_item_t *ci)
78 if ((ci->values_num != 1)
79 || (ci->values[0].type != OCONFIG_TYPE_STRING))
81 ERROR ("Target `notification': The `%s' option requires exactly one string "
82 "argument.", ci->key);
86 if (ci->values[0].value.string[0] == 0)
88 ERROR ("Target `notification': The `%s' option does not accept empty strings.",
93 temp = sstrdup (ci->values[0].value.string);
96 ERROR ("tn_config_add_string: sstrdup failed.");
104 } /* }}} int tn_config_add_string */
106 static int tn_destroy (void **user_data) /* {{{ */
110 if (user_data == NULL)
117 sfree (data->message);
121 } /* }}} int tn_destroy */
123 static int tn_create (const oconfig_item_t *ci, void **user_data) /* {{{ */
129 data = calloc (1, sizeof (*data));
132 ERROR ("tn_create: calloc failed.");
136 data->message = NULL;
140 for (i = 0; i < ci->children_num; i++)
142 oconfig_item_t *child = ci->children + i;
144 if (strcasecmp ("Message", child->key) == 0)
145 status = tn_config_add_string (&data->message, child);
146 else if (strcasecmp ("Severity", child->key) == 0)
147 status = tn_config_add_severity (data, child);
150 ERROR ("Target `notification': The `%s' configuration option is not understood "
151 "and will be ignored.", child->key);
159 /* Additional sanity-checking */
162 if ((data->severity != NOTIF_FAILURE)
163 && (data->severity != NOTIF_WARNING)
164 && (data->severity != NOTIF_OKAY))
166 DEBUG ("Target `notification': Setting "
167 "the default severity `WARNING'.");
168 data->severity = NOTIF_WARNING;
171 if (data->message == NULL)
173 ERROR ("Target `notification': No `Message' option has been specified. "
174 "Without it, the `Notification' target is useless.");
183 tn_destroy ((void *) &data);
189 } /* }}} int tn_create */
191 static int tn_invoke (const data_set_t *ds, value_list_t *vl, /* {{{ */
192 notification_meta_t __attribute__((unused)) **meta, void **user_data)
196 char temp[NOTIF_MAX_MSG_LEN];
203 if ((ds == NULL) || (vl == NULL) || (user_data == NULL))
209 ERROR ("Target `notification': Invoke: `data' is NULL.");
213 /* Initialize the structure. */
214 memset (&n, 0, sizeof (n));
215 n.severity = data->severity;
217 sstrncpy (n.message, data->message, sizeof (n.message));
218 sstrncpy (n.host, vl->host, sizeof (n.host));
219 sstrncpy (n.plugin, vl->plugin, sizeof (n.plugin));
220 sstrncpy (n.plugin_instance, vl->plugin_instance,
221 sizeof (n.plugin_instance));
222 sstrncpy (n.type, vl->type, sizeof (n.type));
223 sstrncpy (n.type_instance, vl->type_instance,
224 sizeof (n.type_instance));
227 #define REPLACE_FIELD(t,v) \
228 if (subst_string (temp, sizeof (temp), n.message, t, v) != NULL) \
229 sstrncpy (n.message, temp, sizeof (n.message));
230 REPLACE_FIELD ("%{host}", n.host);
231 REPLACE_FIELD ("%{plugin}", n.plugin);
232 REPLACE_FIELD ("%{plugin_instance}", n.plugin_instance);
233 REPLACE_FIELD ("%{type}", n.type);
234 REPLACE_FIELD ("%{type_instance}", n.type_instance);
238 for (i = 0; i < ds->ds_num; i++)
240 char template[DATA_MAX_NAME_LEN];
241 char value_str[DATA_MAX_NAME_LEN];
243 ssnprintf (template, sizeof (template), "%%{ds:%s}", ds->ds[i].name);
245 if (ds->ds[i].type != DS_TYPE_GAUGE)
247 if ((rates == NULL) && (rates_failed == 0))
249 rates = uc_get_rate (ds, vl);
255 /* If this is a gauge value, use the current value. */
256 if (ds->ds[i].type == DS_TYPE_GAUGE)
257 ssnprintf (value_str, sizeof (value_str),
258 GAUGE_FORMAT, (double) vl->values[i].gauge);
259 /* If it's a counter, try to use the current rate. This may fail, if the
260 * value has been renamed. */
261 else if (rates != NULL)
262 ssnprintf (value_str, sizeof (value_str),
263 GAUGE_FORMAT, (double) rates[i]);
264 /* Since we don't know any better, use the string `unknown'. */
266 sstrncpy (value_str, "unknown", sizeof (value_str));
268 REPLACE_FIELD (template, value_str);
272 plugin_dispatch_notification (&n);
274 return (FC_TARGET_CONTINUE);
275 } /* }}} int tn_invoke */
277 void module_register (void)
281 memset (&tproc, 0, sizeof (tproc));
282 tproc.create = tn_create;
283 tproc.destroy = tn_destroy;
284 tproc.invoke = tn_invoke;
285 fc_register_target ("notification", tproc);
286 } /* module_register */
288 /* vim: set sw=2 sts=2 tw=78 et fdm=marker : */