2 * collectd - src/types_list.c
3 * Copyright (C) 2007 Florian octo Forster
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:
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
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.
24 * Florian octo Forster <octo at collectd.org>
31 #include "configfile.h"
32 #include "types_list.h"
34 static int parse_ds (data_source_t *dsrc, char *buf, size_t buf_len)
43 ERROR ("parse_ds: (buf_len = %zu) < 11", buf_len);
47 if (buf[buf_len - 1] == ',')
57 while (fields_num < 8)
59 if ((fields[fields_num] = strtok_r (dummy, ":", &saveptr)) == NULL)
67 ERROR ("parse_ds: (fields_num = %i) != 4", fields_num);
71 sstrncpy (dsrc->name, fields[0], sizeof (dsrc->name));
73 if (strcasecmp (fields[1], "GAUGE") == 0)
74 dsrc->type = DS_TYPE_GAUGE;
75 else if (strcasecmp (fields[1], "COUNTER") == 0)
76 dsrc->type = DS_TYPE_COUNTER;
77 else if (strcasecmp (fields[1], "DERIVE") == 0)
78 dsrc->type = DS_TYPE_DERIVE;
79 else if (strcasecmp (fields[1], "ABSOLUTE") == 0)
80 dsrc->type = DS_TYPE_ABSOLUTE;
83 ERROR ("(fields[1] = %s) != (GAUGE || COUNTER || DERIVE || ABSOLUTE)", fields[1]);
87 if (strcasecmp (fields[2], "U") == 0)
90 dsrc->min = atof (fields[2]);
92 if (strcasecmp (fields[3], "U") == 0)
95 dsrc->max = atof (fields[3]);
100 static void parse_line (char *buf)
107 fields_num = strsplit (buf, fields, 64);
111 /* Ignore lines which begin with a hash sign. */
112 if (fields[0][0] == '#')
115 ds = calloc (1, sizeof (*ds));
119 sstrncpy (ds->type, fields[0], sizeof (ds->type));
121 ds->ds_num = fields_num - 1;
122 ds->ds = (data_source_t *) calloc (ds->ds_num, sizeof (data_source_t));
129 for (i = 0; i < ds->ds_num; i++)
130 if (parse_ds (ds->ds + i, fields[i + 1], strlen (fields[i + 1])) != 0)
132 ERROR ("types_list: parse_line: Cannot parse data source #%zu "
133 "of data set %s", i, ds->type);
139 plugin_register_data_set (ds);
143 } /* void parse_line */
145 static void parse_file (FILE *fh)
150 while (fgets (buf, sizeof (buf), fh) != NULL)
152 buf_len = strlen (buf);
156 NOTICE ("Skipping line with more than 4095 characters.");
159 if (fgets (buf, sizeof (buf), fh) == NULL)
161 buf_len = strlen (buf);
162 } while (buf_len >= 4095);
164 } /* if (buf_len >= 4095) */
166 if ((buf_len == 0) || (buf[0] == '#'))
169 while ((buf_len > 0) && ((buf[buf_len - 1] == '\n')
170 || (buf[buf_len - 1] == '\r')))
171 buf[--buf_len] = '\0';
177 } /* while (fgets) */
178 } /* void parse_file */
180 int read_types_list (const char *file)
187 fh = fopen (file, "r");
191 fprintf (stderr, "Failed to open types database `%s': %s.\n",
192 file, sstrerror (errno, errbuf, sizeof (errbuf)));
193 ERROR ("Failed to open types database `%s': %s",
194 file, sstrerror (errno, errbuf, sizeof (errbuf)));
203 DEBUG ("Done parsing `%s'", file);
206 } /* int read_types_list */
209 * vim: shiftwidth=2:softtabstop=2:tabstop=8