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 **/
27 #include "collectd.h"
29 #include "common.h"
31 #include "configfile.h"
32 #include "plugin.h"
33 #include "types_list.h"
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;
41 if (buf_len < 11) {
42 ERROR("parse_ds: (buf_len = %zu) < 11", buf_len);
43 return (-1);
44 }
46 if (buf[buf_len - 1] == ',') {
47 buf_len--;
48 buf[buf_len] = '\0';
49 }
51 dummy = buf;
52 saveptr = NULL;
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 }
62 if (fields_num != 4) {
63 ERROR("parse_ds: (fields_num = %i) != 4", fields_num);
64 return (-1);
65 }
67 sstrncpy(dsrc->name, fields[0], sizeof(dsrc->name));
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 }
83 if (strcasecmp(fields[2], "U") == 0)
84 dsrc->min = NAN;
85 else
86 dsrc->min = atof(fields[2]);
88 if (strcasecmp(fields[3], "U") == 0)
89 dsrc->max = NAN;
90 else
91 dsrc->max = atof(fields[3]);
93 return (0);
94 } /* int parse_ds */
96 static void parse_line(char *buf) {
97 char *fields[64];
98 size_t fields_num;
99 data_set_t *ds;
101 fields_num = strsplit(buf, fields, 64);
102 if (fields_num < 2)
103 return;
105 /* Ignore lines which begin with a hash sign. */
106 if (fields[0][0] == '#')
107 return;
109 ds = calloc(1, sizeof(*ds));
110 if (ds == NULL)
111 return;
113 sstrncpy(ds->type, fields[0], sizeof(ds->type));
115 ds->ds_num = fields_num - 1;
116 ds->ds = (data_source_t *)calloc(ds->ds_num, sizeof(data_source_t));
117 if (ds->ds == NULL) {
118 sfree(ds);
119 return;
120 }
122 for (size_t i = 0; i < ds->ds_num; i++)
123 if (parse_ds(ds->ds + i, fields[i + 1], strlen(fields[i + 1])) != 0) {
124 ERROR("types_list: parse_line: Cannot parse data source #%zu "
125 "of data set %s",
126 i, ds->type);
127 sfree(ds->ds);
128 sfree(ds);
129 return;
130 }
132 plugin_register_data_set(ds);
134 sfree(ds->ds);
135 sfree(ds);
136 } /* void parse_line */
138 static void parse_file(FILE *fh) {
139 char buf[4096];
140 size_t buf_len;
142 while (fgets(buf, sizeof(buf), fh) != NULL) {
143 buf_len = strlen(buf);
145 if (buf_len >= 4095) {
146 NOTICE("Skipping line with more than 4095 characters.");
147 do {
148 if (fgets(buf, sizeof(buf), fh) == NULL)
149 break;
150 buf_len = strlen(buf);
151 } while (buf_len >= 4095);
152 continue;
153 } /* if (buf_len >= 4095) */
155 if ((buf_len == 0) || (buf[0] == '#'))
156 continue;
158 while ((buf_len > 0) &&
159 ((buf[buf_len - 1] == '\n') || (buf[buf_len - 1] == '\r')))
160 buf[--buf_len] = '\0';
162 if (buf_len == 0)
163 continue;
165 parse_line(buf);
166 } /* while (fgets) */
167 } /* void parse_file */
169 int read_types_list(const char *file) {
170 FILE *fh;
172 if (file == NULL)
173 return (-1);
175 fh = fopen(file, "r");
176 if (fh == NULL) {
177 char errbuf[1024];
178 fprintf(stderr, "Failed to open types database `%s': %s.\n", file,
179 sstrerror(errno, errbuf, sizeof(errbuf)));
180 ERROR("Failed to open types database `%s': %s", file,
181 sstrerror(errno, errbuf, sizeof(errbuf)));
182 return (-1);
183 }
185 parse_file(fh);
187 fclose(fh);
188 fh = NULL;
190 DEBUG("Done parsing `%s'", file);
192 return (0);
193 } /* int read_types_list */