Merge branch 'collectd-4.4'
[collectd.git] / src / utils_complain.c
1 /**
2  * collectd - src/utils_complain.c
3  * Copyright (C) 2006-2007  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  **/
23
24 #include "utils_complain.h"
25 #include "plugin.h"
26
27 /* vcomplain returns 0 if it did not report, 1 else */
28 static int vcomplain (int level, c_complain_t *c,
29                 const char *format, va_list ap)
30 {
31         time_t now;
32         char   message[512];
33
34         now = time (NULL);
35
36         if (c->last + c->interval > now)
37                 return 0;
38
39         c->last = now;
40
41         if (c->interval < interval_g)
42                 c->interval = interval_g;
43         else
44                 c->interval *= 2;
45
46         if (c->interval > 86400)
47                 c->interval = 86400;
48
49         vsnprintf (message, sizeof (message), format, ap);
50         message[sizeof (message) - 1] = '\0';
51
52         plugin_log (level, message);
53         return 1;
54 } /* vcomplain */
55
56 void c_complain (int level, c_complain_t *c, const char *format, ...)
57 {
58         va_list ap;
59
60         /* reset the old interval */
61         if (c->interval < 0)
62                 c->interval *= -1;
63
64         va_start (ap, format);
65         vcomplain (level, c, format, ap);
66         va_end (ap);
67 } /* c_complain */
68
69 void c_complain_once (int level, c_complain_t *c, const char *format, ...)
70 {
71         va_list ap;
72
73         if (c->interval < 0)
74                 return;
75
76         va_start (ap, format);
77         if (vcomplain (level, c, format, ap))
78                 c->interval *= -1;
79         va_end (ap);
80 } /* c_complain_once */
81
82 void c_do_release (int level, c_complain_t *c, const char *format, ...)
83 {
84         char message[512];
85         va_list ap;
86
87         if (c->interval == 0)
88                 return;
89
90         c->interval = 0;
91
92         va_start (ap, format);
93         vsnprintf (message, sizeof (message), format, ap);
94         message[sizeof (message) - 1] = '\0';
95         va_end (ap);
96
97         plugin_log (level, message);
98 } /* c_release */
99
100 /* vim: set sw=4 ts=4 tw=78 noexpandtab : */
101