Merge branch 'collectd-5.5'
[collectd.git] / src / daemon / types_list.c
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  **/
26
27 #include "collectd.h"
28
29 #include "common.h"
30
31 #include "plugin.h"
32 #include "configfile.h"
33 #include "types_list.h"
34
35 static int parse_ds (data_source_t *dsrc, char *buf, size_t buf_len)
36 {
37   char *dummy;
38   char *saveptr;
39   char *fields[8];
40   int   fields_num;
41
42   if (buf_len < 11)
43   {
44     ERROR ("parse_ds: (buf_len = %zu) < 11", buf_len);
45     return (-1);
46   }
47
48   if (buf[buf_len - 1] == ',')
49   {
50     buf_len--;
51     buf[buf_len] = '\0';
52   }
53
54   dummy = buf;
55   saveptr = NULL;
56
57   fields_num = 0;
58   while (fields_num < 8)
59   {
60     if ((fields[fields_num] = strtok_r (dummy, ":", &saveptr)) == NULL)
61       break;
62     dummy = NULL;
63     fields_num++;
64   }
65
66   if (fields_num != 4)
67   {
68     ERROR ("parse_ds: (fields_num = %i) != 4", fields_num);
69     return (-1);
70   }
71
72   sstrncpy (dsrc->name, fields[0], sizeof (dsrc->name));
73
74   if (strcasecmp (fields[1], "GAUGE") == 0)
75     dsrc->type = DS_TYPE_GAUGE;
76   else if (strcasecmp (fields[1], "COUNTER") == 0)
77     dsrc->type = DS_TYPE_COUNTER;
78   else if (strcasecmp (fields[1], "DERIVE") == 0)
79     dsrc->type = DS_TYPE_DERIVE;
80   else if (strcasecmp (fields[1], "ABSOLUTE") == 0)
81     dsrc->type = DS_TYPE_ABSOLUTE;
82   else
83   {
84     ERROR ("(fields[1] = %s) != (GAUGE || COUNTER || DERIVE || ABSOLUTE)", fields[1]);
85     return (-1);
86   }
87
88   if (strcasecmp (fields[2], "U") == 0)
89     dsrc->min = NAN;
90   else
91     dsrc->min = atof (fields[2]);
92
93   if (strcasecmp (fields[3], "U") == 0)
94     dsrc->max = NAN;
95   else
96     dsrc->max = atof (fields[3]);
97
98   return (0);
99 } /* int parse_ds */
100
101 static void parse_line (char *buf)
102 {
103   char  *fields[64];
104   size_t fields_num;
105   data_set_t *ds;
106
107   fields_num = strsplit (buf, fields, 64);
108   if (fields_num < 2)
109     return;
110
111   /* Ignore lines which begin with a hash sign. */
112   if (fields[0][0] == '#')
113     return;
114
115   ds = calloc (1, sizeof (*ds));
116   if (ds == NULL)
117     return;
118
119   sstrncpy (ds->type, fields[0], sizeof (ds->type));
120
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   }
128
129   for (size_t 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     }
138
139   plugin_register_data_set (ds);
140
141   sfree (ds->ds);
142   sfree (ds);
143 } /* void parse_line */
144
145 static void parse_file (FILE *fh)
146 {
147   char buf[4096];
148   size_t buf_len;
149
150   while (fgets (buf, sizeof (buf), fh) != NULL)
151   {
152     buf_len = strlen (buf);
153
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) */
165
166     if ((buf_len == 0) || (buf[0] == '#'))
167       continue;
168
169     while ((buf_len > 0) && ((buf[buf_len - 1] == '\n')
170           || (buf[buf_len - 1] == '\r')))
171       buf[--buf_len] = '\0';
172
173     if (buf_len == 0)
174       continue;
175
176     parse_line (buf);
177   } /* while (fgets) */
178 } /* void parse_file */
179
180 int read_types_list (const char *file)
181 {
182   FILE *fh;
183
184   if (file == NULL)
185     return (-1);
186
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   }
197
198   parse_file (fh);
199
200   fclose (fh);
201   fh = NULL;
202
203   DEBUG ("Done parsing `%s'", file);
204
205   return (0);
206 } /* int read_types_list */
207
208 /*
209  * vim: shiftwidth=2:softtabstop=2:tabstop=8
210  */