de6fce3eed11e6251aed251041694484072e7e62
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 size_t 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 = calloc (1, sizeof (*ds));
116 if (ds == NULL)
117 return;
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));
123 if (ds->ds == NULL)
124 {
125 sfree (ds);
126 return;
127 }
129 for (i = 0; i < ds->ds_num; i++)
130 if (parse_ds (ds->ds + i, fields[i + 1], strlen (fields[i + 1])) != 0)
131 {
132 ERROR ("types_list: parse_line: Cannot parse data source #%zu "
133 "of data set %s", i, ds->type);
134 sfree (ds->ds);
135 sfree (ds);
136 return;
137 }
139 plugin_register_data_set (ds);
141 sfree (ds->ds);
142 sfree (ds);
143 } /* void parse_line */
145 static void parse_file (FILE *fh)
146 {
147 char buf[4096];
148 size_t buf_len;
150 while (fgets (buf, sizeof (buf), fh) != NULL)
151 {
152 buf_len = strlen (buf);
154 if (buf_len >= 4095)
155 {
156 NOTICE ("Skipping line with more than 4095 characters.");
157 do
158 {
159 if (fgets (buf, sizeof (buf), fh) == NULL)
160 break;
161 buf_len = strlen (buf);
162 } while (buf_len >= 4095);
163 continue;
164 } /* if (buf_len >= 4095) */
166 if ((buf_len == 0) || (buf[0] == '#'))
167 continue;
169 while ((buf_len > 0) && ((buf[buf_len - 1] == '\n')
170 || (buf[buf_len - 1] == '\r')))
171 buf[--buf_len] = '\0';
173 if (buf_len == 0)
174 continue;
176 parse_line (buf);
177 } /* while (fgets) */
178 } /* void parse_file */
180 int read_types_list (const char *file)
181 {
182 FILE *fh;
184 if (file == NULL)
185 return (-1);
187 fh = fopen (file, "r");
188 if (fh == NULL)
189 {
190 char errbuf[1024];
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)));
195 return (-1);
196 }
198 parse_file (fh);
200 fclose (fh);
201 fh = NULL;
203 DEBUG ("Done parsing `%s'", file);
205 return (0);
206 } /* int read_types_list */
208 /*
209 * vim: shiftwidth=2:softtabstop=2:tabstop=8
210 */