a5ae4719a83e5887c13867c2ceb4cbbbe40b5f5e
[collectd.git] / src / utils / latency / 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 "utils/common/common.h"
30 #include "utils/latency/latency_config.h"
31
32 static int latency_config_add_percentile(latency_config_t *conf,
33                                          oconfig_item_t *ci) {
34   double percent;
35   int status = cf_util_get_double(ci, &percent);
36   if (status != 0)
37     return status;
38
39   if ((percent <= 0.0) || (percent >= 100)) {
40     P_ERROR("The value for \"%s\" must be between 0 and 100, "
41             "exclusively.",
42             ci->key);
43     return ERANGE;
44   }
45
46   double *tmp = realloc(conf->percentile,
47                         sizeof(*conf->percentile) * (conf->percentile_num + 1));
48   if (tmp == NULL) {
49     P_ERROR("realloc failed.");
50     return ENOMEM;
51   }
52   conf->percentile = tmp;
53   conf->percentile[conf->percentile_num] = percent;
54   conf->percentile_num++;
55
56   return 0;
57 } /* int latency_config_add_percentile */
58
59 static int latency_config_add_bucket(latency_config_t *conf,
60                                      oconfig_item_t *ci) {
61   if ((ci->values_num != 2) || (ci->values[0].type != OCONFIG_TYPE_NUMBER) ||
62       (ci->values[1].type != OCONFIG_TYPE_NUMBER)) {
63     P_ERROR("\"%s\" requires exactly two numeric arguments.", ci->key);
64     return EINVAL;
65   }
66
67   if (ci->values[1].value.number &&
68       ci->values[1].value.number <= ci->values[0].value.number) {
69     P_ERROR("MIN must be less than MAX in \"%s\".", ci->key);
70     return ERANGE;
71   }
72
73   if (ci->values[0].value.number < 0) {
74     P_ERROR("MIN must be greater then or equal to zero in \"%s\".", ci->key);
75     return ERANGE;
76   }
77
78   latency_bucket_t *tmp =
79       realloc(conf->buckets, sizeof(*conf->buckets) * (conf->buckets_num + 1));
80   if (tmp == NULL) {
81     P_ERROR("realloc failed.");
82     return ENOMEM;
83   }
84   conf->buckets = tmp;
85   conf->buckets[conf->buckets_num].lower_bound =
86       DOUBLE_TO_CDTIME_T(ci->values[0].value.number);
87   conf->buckets[conf->buckets_num].upper_bound =
88       DOUBLE_TO_CDTIME_T(ci->values[1].value.number);
89   conf->buckets_num++;
90
91   return 0;
92 } /* int latency_config_add_bucket */
93
94 int latency_config(latency_config_t *conf, oconfig_item_t *ci) {
95   int status = 0;
96
97   for (int i = 0; i < ci->children_num; i++) {
98     oconfig_item_t *child = ci->children + i;
99
100     if (strcasecmp("Percentile", child->key) == 0)
101       status = latency_config_add_percentile(conf, child);
102     else if (strcasecmp("Bucket", child->key) == 0)
103       status = latency_config_add_bucket(conf, child);
104     else if (strcasecmp("BucketType", child->key) == 0)
105       status = cf_util_get_string(child, &conf->bucket_type);
106     else
107       P_WARNING("\"%s\" is not a valid option within a \"%s\" block.",
108                 child->key, ci->key);
109
110     if (status != 0)
111       return status;
112   }
113
114   if ((status == 0) && (conf->percentile_num == 0) &&
115       (conf->buckets_num == 0)) {
116     P_ERROR("The \"%s\" block must contain at least one "
117             "\"Percentile\" or \"Bucket\" option.",
118             ci->key);
119     return EINVAL;
120   }
121
122   return 0;
123 }
124
125 int latency_config_copy(latency_config_t *dst, const latency_config_t src) {
126   *dst = (latency_config_t){
127       .percentile_num = src.percentile_num, .buckets_num = src.buckets_num,
128   };
129
130   dst->percentile = calloc(dst->percentile_num, sizeof(*dst->percentile));
131   dst->buckets = calloc(dst->buckets_num, sizeof(*dst->buckets));
132
133   if ((dst->percentile == NULL) || (dst->buckets == NULL)) {
134     latency_config_free(*dst);
135     return ENOMEM;
136   }
137
138   if (src.bucket_type != NULL) {
139     dst->bucket_type = strdup(src.bucket_type);
140     if (dst->bucket_type == NULL) {
141       latency_config_free(*dst);
142       return ENOMEM;
143     }
144   }
145
146   memmove(dst->percentile, src.percentile,
147           dst->percentile_num * sizeof(*dst->percentile));
148   memmove(dst->buckets, src.buckets, dst->buckets_num * sizeof(*dst->buckets));
149
150   return 0;
151 } /* int latency_config_copy */
152
153 void latency_config_free(latency_config_t conf) {
154   sfree(conf.percentile);
155   sfree(conf.buckets);
156   sfree(conf.bucket_type);
157 } /* void latency_config_free */