Fix compile time issues
[collectd.git] / src / daemon / types_list.c
1 /**
2  * collectd - src/types_list.c
3  * Copyright (C) 2007       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  **/
26
27 #include "collectd.h"
28
29 #include "utils/common/common.h"
30
31 #include "configfile.h"
32 #include "plugin.h"
33 #include "types_list.h"
34
35 static int parse_ds(data_source_t *dsrc, char *buf, size_t buf_len) {
36   char *dummy;
37   char *saveptr;
38   char *fields[8];
39   int fields_num;
40
41   if (buf_len < 11) {
42     ERROR("parse_ds: (buf_len = %" PRIsz ") < 11", buf_len);
43     return -1;
44   }
45
46   if (buf[buf_len - 1] == ',') {
47     buf_len--;
48     buf[buf_len] = '\0';
49   }
50
51   dummy = buf;
52   saveptr = NULL;
53
54   fields_num = 0;
55   while (fields_num < 8) {
56     if ((fields[fields_num] = strtok_r(dummy, ":", &saveptr)) == NULL)
57       break;
58     dummy = NULL;
59     fields_num++;
60   }
61
62   if (fields_num != 4) {
63     ERROR("parse_ds: (fields_num = %i) != 4", fields_num);
64     return -1;
65   }
66
67   sstrncpy(dsrc->name, fields[0], sizeof(dsrc->name));
68
69   if (strcasecmp(fields[1], "GAUGE") == 0)
70     dsrc->type = DS_TYPE_GAUGE;
71   else if (strcasecmp(fields[1], "COUNTER") == 0)
72     dsrc->type = DS_TYPE_COUNTER;
73   else if (strcasecmp(fields[1], "DERIVE") == 0)
74     dsrc->type = DS_TYPE_DERIVE;
75   else if (strcasecmp(fields[1], "ABSOLUTE") == 0)
76     dsrc->type = DS_TYPE_ABSOLUTE;
77   else {
78     ERROR("(fields[1] = %s) != (GAUGE || COUNTER || DERIVE || ABSOLUTE)",
79           fields[1]);
80     return -1;
81   }
82
83   if (strcasecmp(fields[2], "U") == 0)
84     dsrc->min = NAN;
85   else
86     dsrc->min = atof(fields[2]);
87
88   if (strcasecmp(fields[3], "U") == 0)
89     dsrc->max = NAN;
90   else
91     dsrc->max = atof(fields[3]);
92
93   return 0;
94 } /* int parse_ds */
95
96 static void parse_line(char *buf) {
97   char *fields[64];
98   size_t fields_num;
99   fields_num = strsplit(buf, fields, 64);
100   if (fields_num < 2)
101     return;
102
103   /* Ignore lines which begin with a hash sign. */
104   if (fields[0][0] == '#')
105     return;
106
107   data_set_t ds = {{0}};
108
109   sstrncpy(ds.type, fields[0], sizeof(ds.type));
110
111   ds.ds_num = fields_num - 1;
112   ds.ds = calloc(ds.ds_num, sizeof(*ds.ds));
113   if (ds.ds == NULL)
114     return;
115
116   for (size_t i = 0; i < ds.ds_num; i++)
117     if (parse_ds(ds.ds + i, fields[i + 1], strlen(fields[i + 1])) != 0) {
118       ERROR("types_list: parse_line: Cannot parse data source #%" PRIsz
119             " of data set %s",
120             i, ds.type);
121       sfree(ds.ds);
122       return;
123     }
124
125   plugin_register_data_set(&ds);
126
127   sfree(ds.ds);
128 } /* void parse_line */
129
130 static void parse_file(FILE *fh) {
131   char buf[4096];
132   size_t buf_len;
133
134   while (fgets(buf, sizeof(buf), fh) != NULL) {
135     buf_len = strlen(buf);
136
137     if (buf_len >= 4095) {
138       NOTICE("Skipping line with more than 4095 characters.");
139       do {
140         if (fgets(buf, sizeof(buf), fh) == NULL)
141           break;
142         buf_len = strlen(buf);
143       } while (buf_len >= 4095);
144       continue;
145     } /* if (buf_len >= 4095) */
146
147     if ((buf_len == 0) || (buf[0] == '#'))
148       continue;
149
150     while ((buf_len > 0) &&
151            ((buf[buf_len - 1] == '\n') || (buf[buf_len - 1] == '\r')))
152       buf[--buf_len] = '\0';
153
154     if (buf_len == 0)
155       continue;
156
157     parse_line(buf);
158   } /* while (fgets) */
159 } /* void parse_file */
160
161 int read_types_list(const char *file) {
162   FILE *fh;
163
164   if (file == NULL)
165     return -1;
166
167   fh = fopen(file, "r");
168   if (fh == NULL) {
169     fprintf(stderr, "Failed to open types database `%s': %s.\n", file,
170             STRERRNO);
171     ERROR("Failed to open types database `%s': %s", file, STRERRNO);
172     return -1;
173   }
174
175   parse_file(fh);
176
177   fclose(fh);
178   fh = NULL;
179
180   DEBUG("Done parsing `%s'", file);
181
182   return 0;
183 } /* int read_types_list */