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