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