1 /**
2 * collectd - src/types_list.c
3 * Copyright (C) 2007 Florian octo Forster
4 *
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.
8 *
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.
13 *
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
17 *
18 * Authors:
19 * Florian octo Forster <octo at verplant.org>
20 **/
22 #include "collectd.h"
23 #include "common.h"
25 #include "plugin.h"
26 #include "configfile.h"
28 static int parse_ds (data_source_t *dsrc, char *buf, size_t buf_len)
29 {
30 char *dummy;
31 char *saveptr;
32 char *fields[8];
33 int fields_num;
35 if (buf_len < 11)
36 return (-1);
38 if (buf[buf_len - 1] == ',')
39 {
40 buf_len--;
41 buf[buf_len] = '\0';
42 }
44 dummy = buf;
45 saveptr = NULL;
47 fields_num = 0;
48 while (fields_num < 8)
49 {
50 if ((fields[fields_num] = strtok_r (dummy, ":", &saveptr)) == NULL)
51 break;
52 dummy = NULL;
53 fields_num++;
54 }
56 if (fields_num != 4)
57 return (-1);
59 strncpy (dsrc->name, fields[0], sizeof (dsrc->name));
60 dsrc->name[sizeof (dsrc->name) - 1] = '\0';
62 if (strcasecmp (fields[1], "GAUGE") == 0)
63 dsrc->type = DS_TYPE_GAUGE;
64 else if (strcasecmp (fields[1], "COUNTER") == 0)
65 dsrc->type = DS_TYPE_COUNTER;
66 else
67 return (-1);
69 if (strcasecmp (fields[2], "U") == 0)
70 dsrc->min = NAN;
71 else
72 dsrc->min = atof (fields[2]);
74 if (strcasecmp (fields[3], "U") == 0)
75 dsrc->max = NAN;
76 else
77 dsrc->max = atof (fields[3]);
79 DEBUG ("parse_ds: dsrc = {%s, %i, %lf, %lf};",
80 dsrc->name, dsrc->type, dsrc->min, dsrc->max);
82 return (0);
83 } /* int parse_ds */
85 static void parse_line (char *buf, size_t buf_len)
86 {
87 char *fields[64];
88 size_t fields_num;
89 data_set_t ds;
90 int i;
92 fields_num = strsplit (buf, fields, 64);
93 if (fields_num < 2)
94 return;
96 memset (&ds, '\0', sizeof (ds));
98 strncpy (ds.type, fields[0], sizeof (ds.type));
99 ds.type[sizeof (ds.type) - 1] = '\0';
101 ds.ds_num = fields_num - 1;
102 ds.ds = (data_source_t *) calloc (ds.ds_num, sizeof (data_source_t));
103 if (ds.ds == NULL)
104 return;
106 for (i = 0; i < ds.ds_num; i++)
107 if (parse_ds (ds.ds + i, fields[i + 1], strlen (fields[i + 1])) != 0)
108 {
109 sfree (ds.ds);
110 return;
111 }
113 DEBUG ("parse_line: ds = {%s, %i, %p};",
114 ds.type, ds.ds_num, (void *) ds.ds);
116 plugin_register_data_set (&ds);
117 sfree (ds.ds);
118 } /* void parse_line */
120 static void parse_file (FILE *fh)
121 {
122 char buf[4096];
123 size_t buf_len;
125 while (fgets (buf, sizeof (buf), fh) != NULL)
126 {
127 buf_len = strlen (buf);
129 if (buf_len >= 4095)
130 {
131 NOTICE ("Skipping line with more than 4095 characters.");
132 do
133 {
134 if (fgets (buf, sizeof (buf), fh) == NULL)
135 break;
136 buf_len = strlen (buf);
137 } while (buf_len >= 4095);
138 continue;
139 } /* if (buf_len >= 4095) */
141 if ((buf_len == 0) || (buf[0] == '#'))
142 continue;
144 parse_line (buf, buf_len);
145 } /* while (fgets) */
146 } /* void parse_file */
148 int read_types_list (void)
149 {
150 const char *file;
151 FILE *fh;
153 file = global_option_get ("TypesDB");
154 if (file == NULL)
155 return (-1);
157 fh = fopen (file, "r");
158 if (fh == NULL)
159 {
160 char errbuf[1024];
161 ERROR ("open (%s) failed: %s",
162 file, sstrerror (errno, errbuf, sizeof (errbuf)));
163 return (-1);
164 }
166 parse_file (fh);
168 fclose (fh);
169 fh = NULL;
171 DEBUG ("Done parsing `%s'", file);
173 return (0);
174 } /* int read_types_list */
176 /*
177 * vim: shiftwidth=2:softtabstop=2:tabstop=8
178 */