Merge branch 'collectd-4.3' into collectd-4.4
[collectd.git] / src / types_list.c
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  **/
21
22 #include "collectd.h"
23 #include "common.h"
24
25 #include "plugin.h"
26 #include "configfile.h"
27
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;
34
35   if (buf_len < 11)
36   {
37     ERROR ("parse_ds: (buf_len = %u) < 11", buf_len);
38     return (-1);
39   }
40
41   if (buf[buf_len - 1] == ',')
42   {
43     buf_len--;
44     buf[buf_len] = '\0';
45   }
46
47   dummy = buf;
48   saveptr = NULL;
49
50   fields_num = 0;
51   while (fields_num < 8)
52   {
53     if ((fields[fields_num] = strtok_r (dummy, ":", &saveptr)) == NULL)
54       break;
55     dummy = NULL;
56     fields_num++;
57   }
58
59   if (fields_num != 4)
60   {
61     ERROR ("parse_ds: (fields_num = %i) != 4", fields_num);
62     return (-1);
63   }
64
65   strncpy (dsrc->name, fields[0], sizeof (dsrc->name));
66   dsrc->name[sizeof (dsrc->name) - 1] = '\0';
67
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;
72   else
73   {
74     ERROR ("(fields[1] = %s) != (GAUGE || COUNTER)", fields[1]);
75     return (-1);
76   }
77
78   if (strcasecmp (fields[2], "U") == 0)
79     dsrc->min = NAN;
80   else
81     dsrc->min = atof (fields[2]);
82
83   if (strcasecmp (fields[3], "U") == 0)
84     dsrc->max = NAN;
85   else
86     dsrc->max = atof (fields[3]);
87
88   return (0);
89 } /* int parse_ds */
90
91 static void parse_line (char *buf)
92 {
93   char  *fields[64];
94   size_t fields_num;
95   data_set_t *ds;
96   int i;
97
98   fields_num = strsplit (buf, fields, 64);
99   if (fields_num < 2)
100     return;
101
102   ds = (data_set_t *) malloc (sizeof (data_set_t));
103   if (ds == NULL)
104     return;
105
106   memset (ds, '\0', sizeof (data_set_t));
107
108   strncpy (ds->type, fields[0], sizeof (ds->type));
109   ds->type[sizeof (ds->type) - 1] = '\0';
110
111   ds->ds_num = fields_num - 1;
112   ds->ds = (data_source_t *) calloc (ds->ds_num, sizeof (data_source_t));
113   if (ds->ds == NULL)
114     return;
115
116   for (i = 0; i < ds->ds_num; i++)
117     if (parse_ds (ds->ds + i, fields[i + 1], strlen (fields[i + 1])) != 0)
118     {
119       sfree (ds->ds);
120       ERROR ("types_list: parse_line: Cannot parse data source #%i "
121           "of data set %s", i, ds->type);
122       return;
123     }
124
125   plugin_register_data_set (ds);
126
127   sfree (ds->ds);
128   sfree (ds);
129 } /* void parse_line */
130
131 static void parse_file (FILE *fh)
132 {
133   char buf[4096];
134   size_t buf_len;
135
136   while (fgets (buf, sizeof (buf), fh) != NULL)
137   {
138     buf_len = strlen (buf);
139
140     if (buf_len >= 4095)
141     {
142       NOTICE ("Skipping line with more than 4095 characters.");
143       do
144       {
145         if (fgets (buf, sizeof (buf), fh) == NULL)
146           break;
147         buf_len = strlen (buf);
148       } while (buf_len >= 4095);
149       continue;
150     } /* if (buf_len >= 4095) */
151
152     if ((buf_len == 0) || (buf[0] == '#'))
153       continue;
154
155     while ((buf_len > 0) && ((buf[buf_len - 1] == '\n')
156           || (buf[buf_len - 1] == '\n')))
157       buf[--buf_len] = '\0';
158
159     if (buf_len == 0)
160       continue;
161
162     parse_line (buf);
163   } /* while (fgets) */
164 } /* void parse_file */
165
166 int read_types_list (const char *file)
167 {
168   FILE *fh;
169
170   if (file == NULL)
171     return (-1);
172
173   fh = fopen (file, "r");
174   if (fh == NULL)
175   {
176     char errbuf[1024];
177     ERROR ("open (%s) failed: %s", 
178         file, sstrerror (errno, errbuf, sizeof (errbuf)));
179     return (-1);
180   }
181
182   parse_file (fh);
183
184   fclose (fh);
185   fh = NULL;
186
187   DEBUG ("Done parsing `%s'", file);
188
189   return (0);
190 } /* int read_types_list */
191
192 /*
193  * vim: shiftwidth=2:softtabstop=2:tabstop=8
194  */