2 * collectd - src/types_list.c
3 * Copyright (C) 2007 Florian octo Forster
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; only version 2 of the License is applicable.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 * Florian octo Forster <octo at verplant.org>
26 #include "configfile.h"
28 static int parse_ds (data_source_t *dsrc, char *buf, size_t buf_len)
37 ERROR ("parse_ds: (buf_len = %u) < 11", buf_len);
41 if (buf[buf_len - 1] == ',')
51 while (fields_num < 8)
53 if ((fields[fields_num] = strtok_r (dummy, ":", &saveptr)) == NULL)
61 ERROR ("parse_ds: (fields_num = %i) != 4", fields_num);
65 strncpy (dsrc->name, fields[0], sizeof (dsrc->name));
66 dsrc->name[sizeof (dsrc->name) - 1] = '\0';
68 if (strcasecmp (fields[1], "GAUGE") == 0)
69 dsrc->type = DS_TYPE_GAUGE;
70 else if (strcasecmp (fields[1], "COUNTER") == 0)
71 dsrc->type = DS_TYPE_COUNTER;
74 ERROR ("(fields[1] = %s) != (GAUGE || COUNTER)", fields[1]);
78 if (strcasecmp (fields[2], "U") == 0)
81 dsrc->min = atof (fields[2]);
83 if (strcasecmp (fields[3], "U") == 0)
86 dsrc->max = atof (fields[3]);
91 static void parse_line (char *buf)
98 fields_num = strsplit (buf, fields, 64);
102 ds = (data_set_t *) malloc (sizeof (data_set_t));
106 memset (ds, '\0', sizeof (data_set_t));
108 strncpy (ds->type, fields[0], sizeof (ds->type));
109 ds->type[sizeof (ds->type) - 1] = '\0';
111 ds->ds_num = fields_num - 1;
112 ds->ds = (data_source_t *) calloc (ds->ds_num, sizeof (data_source_t));
116 for (i = 0; i < ds->ds_num; i++)
117 if (parse_ds (ds->ds + i, fields[i + 1], strlen (fields[i + 1])) != 0)
120 ERROR ("types_list: parse_line: Cannot parse data source #%i "
121 "of data set %s", i, ds->type);
125 plugin_register_data_set (ds);
129 } /* void parse_line */
131 static void parse_file (FILE *fh)
136 while (fgets (buf, sizeof (buf), fh) != NULL)
138 buf_len = strlen (buf);
142 NOTICE ("Skipping line with more than 4095 characters.");
145 if (fgets (buf, sizeof (buf), fh) == NULL)
147 buf_len = strlen (buf);
148 } while (buf_len >= 4095);
150 } /* if (buf_len >= 4095) */
152 if ((buf_len == 0) || (buf[0] == '#'))
155 while ((buf_len > 0) && ((buf[buf_len - 1] == '\n')
156 || (buf[buf_len - 1] == '\n')))
157 buf[--buf_len] = '\0';
163 } /* while (fgets) */
164 } /* void parse_file */
166 int read_types_list (const char *file)
173 fh = fopen (file, "r");
177 ERROR ("open (%s) failed: %s",
178 file, sstrerror (errno, errbuf, sizeof (errbuf)));
187 DEBUG ("Done parsing `%s'", file);
190 } /* int read_types_list */
193 * vim: shiftwidth=2:softtabstop=2:tabstop=8