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"
28 #include "common.h"
30 #include "plugin.h"
31 #include "configfile.h"
32 #include "types_list.h"
34 static int parse_ds (data_source_t *dsrc, char *buf, size_t buf_len)
35 {
36 char *dummy;
37 char *saveptr;
38 char *fields[8];
39 int fields_num;
41 if (buf_len < 11)
42 {
43 ERROR ("parse_ds: (buf_len = %zu) < 11", buf_len);
44 return (-1);
45 }
47 if (buf[buf_len - 1] == ',')
48 {
49 buf_len--;
50 buf[buf_len] = '\0';
51 }
53 dummy = buf;
54 saveptr = NULL;
56 fields_num = 0;
57 while (fields_num < 8)
58 {
59 if ((fields[fields_num] = strtok_r (dummy, ":", &saveptr)) == NULL)
60 break;
61 dummy = NULL;
62 fields_num++;
63 }
65 if (fields_num != 4)
66 {
67 ERROR ("parse_ds: (fields_num = %i) != 4", fields_num);
68 return (-1);
69 }
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;
81 else
82 {
83 ERROR ("(fields[1] = %s) != (GAUGE || COUNTER || DERIVE || ABSOLUTE)", fields[1]);
84 return (-1);
85 }
87 if (strcasecmp (fields[2], "U") == 0)
88 dsrc->min = NAN;
89 else
90 dsrc->min = atof (fields[2]);
92 if (strcasecmp (fields[3], "U") == 0)
93 dsrc->max = NAN;
94 else
95 dsrc->max = atof (fields[3]);
97 return (0);
98 } /* int parse_ds */
100 static void parse_line (char *buf)
101 {
102 char *fields[64];
103 size_t fields_num;
104 data_set_t *ds;
105 int i;
107 fields_num = strsplit (buf, fields, 64);
108 if (fields_num < 2)
109 return;
111 /* Ignore lines which begin with a hash sign. */
112 if (fields[0][0] == '#')
113 return;
115 ds = (data_set_t *) malloc (sizeof (data_set_t));
116 if (ds == NULL)
117 return;
119 memset (ds, '\0', sizeof (data_set_t));
121 sstrncpy (ds->type, fields[0], sizeof (ds->type));
123 ds->ds_num = fields_num - 1;
124 ds->ds = (data_source_t *) calloc (ds->ds_num, sizeof (data_source_t));
125 if (ds->ds == NULL)
126 return;
128 for (i = 0; i < ds->ds_num; i++)
129 if (parse_ds (ds->ds + i, fields[i + 1], strlen (fields[i + 1])) != 0)
130 {
131 sfree (ds->ds);
132 ERROR ("types_list: parse_line: Cannot parse data source #%i "
133 "of data set %s", i, ds->type);
134 return;
135 }
137 plugin_register_data_set (ds);
139 sfree (ds->ds);
140 sfree (ds);
141 } /* void parse_line */
143 static void parse_file (FILE *fh)
144 {
145 char buf[4096];
146 size_t buf_len;
148 while (fgets (buf, sizeof (buf), fh) != NULL)
149 {
150 buf_len = strlen (buf);
152 if (buf_len >= 4095)
153 {
154 NOTICE ("Skipping line with more than 4095 characters.");
155 do
156 {
157 if (fgets (buf, sizeof (buf), fh) == NULL)
158 break;
159 buf_len = strlen (buf);
160 } while (buf_len >= 4095);
161 continue;
162 } /* if (buf_len >= 4095) */
164 if ((buf_len == 0) || (buf[0] == '#'))
165 continue;
167 while ((buf_len > 0) && ((buf[buf_len - 1] == '\n')
168 || (buf[buf_len - 1] == '\r')))
169 buf[--buf_len] = '\0';
171 if (buf_len == 0)
172 continue;
174 parse_line (buf);
175 } /* while (fgets) */
176 } /* void parse_file */
178 int read_types_list (const char *file)
179 {
180 FILE *fh;
182 if (file == NULL)
183 return (-1);
185 fh = fopen (file, "r");
186 if (fh == NULL)
187 {
188 char errbuf[1024];
189 fprintf (stderr, "Failed to open types database `%s': %s.\n",
190 file, sstrerror (errno, errbuf, sizeof (errbuf)));
191 ERROR ("Failed to open types database `%s': %s",
192 file, sstrerror (errno, errbuf, sizeof (errbuf)));
193 return (-1);
194 }
196 parse_file (fh);
198 fclose (fh);
199 fh = NULL;
201 DEBUG ("Done parsing `%s'", file);
203 return (0);
204 } /* int read_types_list */
206 /*
207 * vim: shiftwidth=2:softtabstop=2:tabstop=8
208 */