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