+ liblatency: Added utils_latency_config code
[collectd.git] / src / utils_latency_config.c
1 /**
2  * collectd - src/utils_latency_config.c
3  * Copyright (C) 2013-2016   Florian octo Forster
4  *
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:
11  *
12  * The above copyright notice and this permission notice shall be included in
13  * all copies or substantial portions of the Software.
14  *
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.
22  *
23  * Authors:
24  *   Florian octo Forster <octo at collectd.org>
25  *   Pavel Rochnyack <pavel2000 at ngs.ru>
26  */
27
28 #include "collectd.h"
29 #include "common.h"
30 #include "utils_latency_config.h"
31
32 int latency_config_add_percentile (const char *plugin, latency_config_t *cl,
33     oconfig_item_t *ci)
34 {
35   if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_NUMBER))
36   {
37     ERROR ("%s plugin: \"%s\" requires exactly one numeric argument.",
38              plugin, ci->key);
39     return (-1);
40   }
41
42   double percent = ci->values[0].value.number;
43   double *tmp;
44
45   if ((percent <= 0.0) || (percent >= 100))
46   {
47     ERROR ("%s plugin: The value for \"%s\" must be between 0 and 100, "
48         "exclusively.", plugin, ci->key);
49     return (ERANGE);
50   }
51
52   tmp = realloc (cl->percentile,
53       sizeof (*cl->percentile) * (cl->percentile_num + 1));
54   if (tmp == NULL)
55   {
56     ERROR ("%s plugin: realloc failed.", plugin);
57     return (ENOMEM);
58   }
59   cl->percentile = tmp;
60   cl->percentile[cl->percentile_num] = percent;
61   cl->percentile_num++;
62
63   return (0);
64 } /* int latency_config_add_percentile */
65
66 int latency_config_add_rate (const char *plugin, latency_config_t *cl,
67     oconfig_item_t *ci)
68 {
69
70   if ((ci->values_num != 2)
71     ||(ci->values[0].type != OCONFIG_TYPE_NUMBER)
72     ||(ci->values[1].type != OCONFIG_TYPE_NUMBER))
73   {
74     ERROR ("%s plugin: \"%s\" requires exactly two numeric arguments.",
75              plugin, ci->key);
76     return (-1);
77   }
78
79   if (ci->values[1].value.number &&
80       ci->values[1].value.number <= ci->values[0].value.number)
81   {
82     ERROR ("%s plugin: MIN must be less than MAX in \"%s\".",
83              plugin, ci->key);
84     return (-1);
85   }
86
87   if (ci->values[0].value.number < 0.001)
88   {
89     ERROR ("%s plugin: MIN must be greater or equal to 0.001 in \"%s\".",
90              plugin, ci->key);
91     return (-1);
92   }
93
94   cdtime_t lower = DOUBLE_TO_CDTIME_T(ci->values[0].value.number);
95   cdtime_t upper = DOUBLE_TO_CDTIME_T(ci->values[1].value.number);
96   cdtime_t *tmp;
97
98   tmp = realloc (cl->rates,
99       sizeof (*cl->rates) * (cl->rates_num + 1) * 2);
100   if (tmp == NULL)
101   {
102     ERROR ("%s plugin: realloc failed.", plugin);
103     return (ENOMEM);
104   }
105   cl->rates = tmp;
106   cl->rates[cl->rates_num * 2] = lower;
107   cl->rates[cl->rates_num * 2 + 1] = upper;
108   cl->rates_num++;
109
110   return (0);
111 } /* int latency_config_add_rate */
112
113
114 int latency_config_copy (latency_config_t *dst, const latency_config_t src)
115 {
116   /* Copy percentiles configuration */
117   dst->percentile_num = src.percentile_num;
118   dst->percentile = malloc(sizeof (*dst->percentile) * (src.percentile_num));
119   if (dst->percentile == NULL)
120     goto nomem;
121
122   memcpy (dst->percentile, src.percentile,
123           (sizeof (*dst->percentile) * (src.percentile_num)));
124
125   if (src.percentile_type != NULL)
126   {
127     dst->percentile_type = strdup(src.percentile_type);
128     if (dst->percentile_type == NULL)
129       goto nomem;
130   }
131
132   /* Copy rates configuration */
133   dst->rates_num = src.rates_num;
134   dst->rates = malloc(sizeof (*dst->rates) * (src.rates_num) * 2);
135   if (dst->rates == NULL)
136     goto nomem;
137
138   memcpy (dst->rates, src.rates,
139           (sizeof (*dst->rates) * (src.rates_num) * 2));
140
141   if (src.rates_type != NULL)
142   {
143     dst->rates_type = strdup(src.rates_type);
144     if (dst->rates_type == NULL)
145       goto nomem;
146   }
147
148   return (0);
149 nomem:
150   free (dst->rates);
151   free (dst->rates_type);
152   free (dst->percentile);
153   free (dst->percentile_type);
154   return (-1);
155 } /* int latency_config_copy */
156
157 void latency_config_free (latency_config_t lc)
158 {
159   sfree (lc.rates);
160   sfree (lc.rates_type);
161   sfree (lc.percentile);
162   sfree (lc.percentile_type);
163 } /* void latency_config_free */