1 /**
2 * collectd - src/utils_complain.c
3 * Copyright (C) 2006-2013 Florian octo Forster
4 * Copyright (C) 2008 Sebastian tokkee Harl
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; only version 2 of the License is applicable.
9 *
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18 *
19 * Authors:
20 * Florian octo Forster <octo at verplant.org>
21 * Sebastian tokkee Harl <sh at tokkee.org>
22 **/
24 #include "collectd.h"
25 #include "utils_complain.h"
26 #include "plugin.h"
28 /* vcomplain returns 0 if it did not report, 1 else */
29 static int vcomplain (int level, c_complain_t *c,
30 const char *format, va_list ap)
31 {
32 cdtime_t now;
33 char message[512];
35 now = cdtime ();
37 if (c->last + c->interval > now)
38 return 0;
40 c->last = now;
42 if (c->interval < plugin_get_interval ())
43 c->interval = plugin_get_interval ();
44 else
45 c->interval *= 2;
47 if (c->interval > TIME_T_TO_CDTIME_T (86400))
48 c->interval = TIME_T_TO_CDTIME_T (86400);
50 vsnprintf (message, sizeof (message), format, ap);
51 message[sizeof (message) - 1] = '\0';
53 plugin_log (level, "%s", message);
54 return 1;
55 } /* vcomplain */
57 void c_complain (int level, c_complain_t *c, const char *format, ...)
58 {
59 va_list ap;
61 va_start (ap, format);
62 if (vcomplain (level, c, format, ap))
63 c->complained_once = 1;
64 va_end (ap);
65 } /* c_complain */
67 void c_complain_once (int level, c_complain_t *c, const char *format, ...)
68 {
69 va_list ap;
71 if (c->complained_once)
72 return;
74 va_start (ap, format);
75 if (vcomplain (level, c, format, ap))
76 c->complained_once = 1;
77 va_end (ap);
78 } /* c_complain_once */
80 void c_do_release (int level, c_complain_t *c, const char *format, ...)
81 {
82 char message[512];
83 va_list ap;
85 if (c->interval == 0)
86 return;
88 c->interval = 0;
89 c->complained_once = 0;
91 va_start (ap, format);
92 vsnprintf (message, sizeof (message), format, ap);
93 message[sizeof (message) - 1] = '\0';
94 va_end (ap);
96 plugin_log (level, "%s", message);
97 } /* c_release */
99 /* vim: set sw=4 ts=4 tw=78 noexpandtab : */