Merge pull request #1874 from rubenk/utils-dns-fix-compiler-warning
[collectd.git] / src / daemon / utils_complain.c
1 /**
2  * collectd - src/utils_complain.c
3  * Copyright (C) 2006-2013  Florian octo Forster
4  * Copyright (C) 2008       Sebastian tokkee Harl
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a
7  * copy of this software and associated documentation files (the "Software"),
8  * to deal in the Software without restriction, including without limitation
9  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10  * and/or sell copies of the Software, and to permit persons to whom the
11  * Software is furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included in
14  * all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22  * DEALINGS IN THE SOFTWARE.
23  *
24  * Authors:
25  *   Florian octo Forster <octo at collectd.org>
26  *   Sebastian tokkee Harl <sh at tokkee.org>
27  **/
28
29 #include "collectd.h"
30
31 #include "utils_complain.h"
32 #include "plugin.h"
33
34 /* vcomplain returns 0 if it did not report, 1 else */
35 __attribute__ ((format (printf, 3, 0)))
36 static int vcomplain (int level, c_complain_t *c,
37                 const char *format, va_list ap)
38 {
39         cdtime_t now;
40         char   message[512];
41
42         now = cdtime ();
43
44         if (c->last + c->interval > now)
45                 return 0;
46
47         c->last = now;
48
49         if (c->interval < plugin_get_interval ())
50                 c->interval = plugin_get_interval ();
51         else
52                 c->interval *= 2;
53
54         if (c->interval > TIME_T_TO_CDTIME_T (86400))
55                 c->interval = TIME_T_TO_CDTIME_T (86400);
56
57         vsnprintf (message, sizeof (message), format, ap);
58         message[sizeof (message) - 1] = '\0';
59
60         plugin_log (level, "%s", message);
61         return 1;
62 } /* vcomplain */
63
64 void c_complain (int level, c_complain_t *c, const char *format, ...)
65 {
66         va_list ap;
67
68         va_start (ap, format);
69         if (vcomplain (level, c, format, ap))
70                 c->complained_once = 1;
71         va_end (ap);
72 } /* c_complain */
73
74 void c_complain_once (int level, c_complain_t *c, const char *format, ...)
75 {
76         va_list ap;
77
78         if (c->complained_once)
79                 return;
80
81         va_start (ap, format);
82         if (vcomplain (level, c, format, ap))
83                 c->complained_once = 1;
84         va_end (ap);
85 } /* c_complain_once */
86
87 void c_do_release (int level, c_complain_t *c, const char *format, ...)
88 {
89         char message[512];
90         va_list ap;
91
92         if (c->interval == 0)
93                 return;
94
95         c->interval = 0;
96         c->complained_once = 0;
97
98         va_start (ap, format);
99         vsnprintf (message, sizeof (message), format, ap);
100         message[sizeof (message) - 1] = '\0';
101         va_end (ap);
102
103         plugin_log (level, "%s", message);
104 } /* c_release */
105
106 /* vim: set sw=4 ts=4 tw=78 noexpandtab : */