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 #include "common.h"
29
30 #include "plugin.h"
31 #include "configfile.h"
32 #include "types_list.h"
33
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;
40
41   if (buf_len < 11)
42   {
43     ERROR ("parse_ds: (buf_len = %zu) < 11", buf_len);
44     return (-1);
45   }
46
47   if (buf[buf_len - 1] == ',')
48   {
49     buf_len--;
50     buf[buf_len] = '\0';
51   }
52
53   dummy = buf;
54   saveptr = NULL;
55
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   }
64
65   if (fields_num != 4)
66   {
67     ERROR ("parse_ds: (fields_num = %i) != 4", fields_num);
68     return (-1);
69   }
70
71   sstrncpy (dsrc->name, fields[0], sizeof (dsrc->name));
72
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   }
86
87   if (strcasecmp (fields[2], "U") == 0)
88     dsrc->min = NAN;
89   else
90     dsrc->min = atof (fields[2]);
91
92   if (strcasecmp (fields[3], "U") == 0)
93     dsrc->max = NAN;
94   else
95     dsrc->max = atof (fields[3]);
96
97   return (0);
98 } /* int parse_ds */
99
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;
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 (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  */