Merge pull request #1579 from rubenk/fix-typo-in-comment
[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 = (data_set_t *) malloc (sizeof (data_set_t));
116   if (ds == NULL)
117     return;
118
119   memset (ds, '\0', sizeof (data_set_t));
120
121   sstrncpy (ds->type, fields[0], sizeof (ds->type));
122
123   ds->ds_num = fields_num - 1;
124   ds->ds = (data_source_t *) calloc (ds->ds_num, sizeof (data_source_t));
125   if (ds->ds == NULL)
126   {
127     sfree (ds);
128     return;
129   }
130
131   for (i = 0; i < ds->ds_num; i++)
132     if (parse_ds (ds->ds + i, fields[i + 1], strlen (fields[i + 1])) != 0)
133     {
134       ERROR ("types_list: parse_line: Cannot parse data source #%zu "
135           "of data set %s", i, ds->type);
136       sfree (ds->ds);
137       sfree (ds);
138       return;
139     }
140
141   plugin_register_data_set (ds);
142
143   sfree (ds->ds);
144   sfree (ds);
145 } /* void parse_line */
146
147 static void parse_file (FILE *fh)
148 {
149   char buf[4096];
150   size_t buf_len;
151
152   while (fgets (buf, sizeof (buf), fh) != NULL)
153   {
154     buf_len = strlen (buf);
155
156     if (buf_len >= 4095)
157     {
158       NOTICE ("Skipping line with more than 4095 characters.");
159       do
160       {
161         if (fgets (buf, sizeof (buf), fh) == NULL)
162           break;
163         buf_len = strlen (buf);
164       } while (buf_len >= 4095);
165       continue;
166     } /* if (buf_len >= 4095) */
167
168     if ((buf_len == 0) || (buf[0] == '#'))
169       continue;
170
171     while ((buf_len > 0) && ((buf[buf_len - 1] == '\n')
172           || (buf[buf_len - 1] == '\r')))
173       buf[--buf_len] = '\0';
174
175     if (buf_len == 0)
176       continue;
177
178     parse_line (buf);
179   } /* while (fgets) */
180 } /* void parse_file */
181
182 int read_types_list (const char *file)
183 {
184   FILE *fh;
185
186   if (file == NULL)
187     return (-1);
188
189   fh = fopen (file, "r");
190   if (fh == NULL)
191   {
192     char errbuf[1024];
193     fprintf (stderr, "Failed to open types database `%s': %s.\n",
194         file, sstrerror (errno, errbuf, sizeof (errbuf)));
195     ERROR ("Failed to open types database `%s': %s",
196         file, sstrerror (errno, errbuf, sizeof (errbuf)));
197     return (-1);
198   }
199
200   parse_file (fh);
201
202   fclose (fh);
203   fh = NULL;
204
205   DEBUG ("Done parsing `%s'", file);
206
207   return (0);
208 } /* int read_types_list */
209
210 /*
211  * vim: shiftwidth=2:softtabstop=2:tabstop=8
212  */