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