Merge branch 'collectd-5.1' into collectd-5.2
[collectd.git] / src / common.h
1 /**
2  * collectd - src/common.h
3  * Copyright (C) 2005-2010  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 collectd.org>
20  *   Niki W. Waibel <niki.waibel@gmx.net>
21 **/
22
23 #ifndef COMMON_H
24 #define COMMON_H
25
26 #include "collectd.h"
27 #include "plugin.h"
28
29 #if HAVE_PWD_H
30 # include <pwd.h>
31 #endif
32
33 #define sfree(ptr) \
34         do { \
35                 if((ptr) != NULL) { \
36                         free(ptr); \
37                 } \
38                 (ptr) = NULL; \
39         } while (0)
40
41 #define STATIC_ARRAY_SIZE(a) (sizeof (a) / sizeof (*(a)))
42
43 #define IS_TRUE(s) ((strcasecmp ("true", (s)) == 0) \
44                 || (strcasecmp ("yes", (s)) == 0) \
45                 || (strcasecmp ("on", (s)) == 0))
46 #define IS_FALSE(s) ((strcasecmp ("false", (s)) == 0) \
47                 || (strcasecmp ("no", (s)) == 0) \
48                 || (strcasecmp ("off", (s)) == 0))
49
50 struct rate_to_value_state_s
51 {
52   value_t last_value;
53   cdtime_t last_time;
54   gauge_t residual;
55 };
56 typedef struct rate_to_value_state_s rate_to_value_state_t;
57
58 char *sstrncpy (char *dest, const char *src, size_t n);
59 int ssnprintf (char *dest, size_t n, const char *format, ...);
60 char *sstrdup(const char *s);
61 void *smalloc(size_t size);
62 char *sstrerror (int errnum, char *buf, size_t buflen);
63
64 /*
65  * NAME
66  *   sread
67  *
68  * DESCRIPTION
69  *   Reads exactly `n' bytes or fails. Syntax and other behavior is analogous
70  *   to `read(2)'. If EOF is received the file descriptor is closed and an
71  *   error is returned.
72  *
73  * PARAMETERS
74  *   `fd'          File descriptor to write to.
75  *   `buf'         Buffer that is to be written.
76  *   `count'       Number of bytes in the buffer.
77  *
78  * RETURN VALUE
79  *   Zero upon success or non-zero if an error occurred. `errno' is set in this
80  *   case.
81  */
82 ssize_t sread (int fd, void *buf, size_t count);
83
84 /*
85  * NAME
86  *   swrite
87  *
88  * DESCRIPTION
89  *   Writes exactly `n' bytes or fails. Syntax and other behavior is analogous
90  *   to `write(2)'.
91  *
92  * PARAMETERS
93  *   `fd'          File descriptor to write to.
94  *   `buf'         Buffer that is to be written.
95  *   `count'       Number of bytes in the buffer.
96  *
97  * RETURN VALUE
98  *   Zero upon success or non-zero if an error occurred. `errno' is set in this
99  *   case.
100  */
101 ssize_t swrite (int fd, const void *buf, size_t count);
102
103 /*
104  * NAME
105  *   strsplit
106  *
107  * DESCRIPTION
108  *   Splits a string into parts and stores pointers to the parts in `fields'.
109  *   The characters split at are: " ", "\t", "\r", and "\n".
110  *
111  * PARAMETERS
112  *   `string'      String to split. This string will be modified. `fields' will
113  *                 contain pointers to parts of this string, so free'ing it
114  *                 will destroy `fields' as well.
115  *   `fields'      Array of strings where pointers to the parts will be stored.
116  *   `size'        Number of elements in the array. No more than `size'
117  *                 pointers will be stored in `fields'.
118  *
119  * RETURN VALUE
120  *    Returns the number of parts stored in `fields'.
121  */
122 int strsplit (char *string, char **fields, size_t size);
123
124 /*
125  * NAME
126  *   strjoin
127  *
128  * DESCRIPTION
129  *   Joins together several parts of a string using `sep' as a separator. This
130  *   is equivalent to the Perl built-in `join'.
131  *
132  * PARAMETERS
133  *   `dst'         Buffer where the result is stored.
134  *   `dst_len'     Length of the destination buffer. No more than this many
135  *                 bytes will be written to the memory pointed to by `dst',
136  *                 including the trailing null-byte.
137  *   `fields'      Array of strings to be joined.
138  *   `fields_num'  Number of elements in the `fields' array.
139  *   `sep'         String to be inserted between any two elements of `fields'.
140  *                 This string is neither prepended nor appended to the result.
141  *                 Instead of passing "" (empty string) one can pass NULL.
142  *
143  * RETURN VALUE
144  *   Returns the number of characters in `dst', NOT including the trailing
145  *   null-byte. If an error occurred (empty array or `dst' too small) a value
146  *   smaller than zero will be returned.
147  */
148 int strjoin (char *dst, size_t dst_len, char **fields, size_t fields_num, const char *sep);
149
150 /*
151  * NAME
152  *   escape_slashes
153  *
154  * DESCRIPTION
155  *   Removes slashes from the string `buf' and substitutes them with something
156  *   appropriate. This function should be used whenever a path is to be used as
157  *   (part of) an instance.
158  *
159  * PARAMETERS
160  *   `buf'         String to be escaped.
161  *   `buf_len'     Length of the buffer. No more then this many bytes will be
162  *   written to `buf', including the trailing null-byte.
163  *
164  * RETURN VALUE
165  *   Returns zero upon success and a value smaller than zero upon failure.
166  */
167 int escape_slashes (char *buf, int buf_len);
168
169 /*
170  * NAME
171  *   replace_special
172  *
173  * DESCRIPTION
174  *   Replaces any special characters (anything that's not alpha-numeric or a
175  *   dash) with an underscore.
176  *
177  *   E.g. "foo$bar&" would become "foo_bar_".
178  *
179  * PARAMETERS
180  *   `buffer'      String to be handled.
181  *   `buffer_size' Length of the string. The function returns after
182  *                 encountering a null-byte or reading this many bytes.
183  */
184 void replace_special (char *buffer, size_t buffer_size);
185
186 int strsubstitute (char *str, char c_from, char c_to);
187
188 /*
189  * NAME
190  *   strunescape
191  *
192  * DESCRIPTION
193  *   Replaces any escaped characters in a string with the appropriate special
194  *   characters. The following escaped characters are recognized:
195  *
196  *     \t -> <tab>
197  *     \n -> <newline>
198  *     \r -> <carriage return>
199  *
200  *   For all other escacped characters only the backslash will be removed.
201  *
202  * PARAMETERS
203  *   `buf'         String to be unescaped.
204  *   `buf_len'     Length of the string, including the terminating null-byte.
205  *
206  * RETURN VALUE
207  *   Returns zero upon success, a value less than zero else.
208  */
209 int strunescape (char *buf, size_t buf_len);
210
211 /*
212  * NAME
213  *   timeval_cmp
214  *
215  * DESCRIPTION
216  *   Compare the two time values `tv0' and `tv1' and store the absolut value
217  *   of the difference in the time value pointed to by `delta' if it does not
218  *   equal NULL.
219  *
220  * RETURN VALUE
221  *   Returns an integer less than, equal to, or greater than zero if `tv0' is
222  *   less than, equal to, or greater than `tv1' respectively.
223  */
224 int timeval_cmp (struct timeval tv0, struct timeval tv1, struct timeval *delta);
225
226 /* make sure tv_usec stores less than a second */
227 #define NORMALIZE_TIMEVAL(tv) \
228         do { \
229                 (tv).tv_sec += (tv).tv_usec / 1000000; \
230                 (tv).tv_usec = (tv).tv_usec % 1000000; \
231         } while (0)
232
233 /* make sure tv_sec stores less than a second */
234 #define NORMALIZE_TIMESPEC(tv) \
235         do { \
236                 (tv).tv_sec += (tv).tv_nsec / 1000000000; \
237                 (tv).tv_nsec = (tv).tv_nsec % 1000000000; \
238         } while (0)
239
240 int check_create_dir (const char *file_orig);
241
242 #ifdef HAVE_LIBKSTAT
243 int get_kstat (kstat_t **ksp_ptr, char *module, int instance, char *name);
244 long long get_kstat_value (kstat_t *ksp, char *name);
245 #endif
246
247 #ifndef HAVE_HTONLL
248 unsigned long long ntohll (unsigned long long n);
249 unsigned long long htonll (unsigned long long n);
250 #endif
251
252 #if FP_LAYOUT_NEED_NOTHING
253 # define ntohd(d) (d)
254 # define htond(d) (d)
255 #elif FP_LAYOUT_NEED_ENDIANFLIP || FP_LAYOUT_NEED_INTSWAP
256 double ntohd (double d);
257 double htond (double d);
258 #else
259 # error "Don't know how to convert between host and network representation of doubles."
260 #endif
261
262 int format_name (char *ret, int ret_len,
263                 const char *hostname,
264                 const char *plugin, const char *plugin_instance,
265                 const char *type, const char *type_instance);
266 #define FORMAT_VL(ret, ret_len, vl) \
267         format_name (ret, ret_len, (vl)->host, (vl)->plugin, (vl)->plugin_instance, \
268                         (vl)->type, (vl)->type_instance)
269 int format_values (char *ret, size_t ret_len,
270                 const data_set_t *ds, const value_list_t *vl,
271                 _Bool store_rates);
272
273 int parse_identifier (char *str, char **ret_host,
274                 char **ret_plugin, char **ret_plugin_instance,
275                 char **ret_type, char **ret_type_instance);
276 int parse_identifier_vl (const char *str, value_list_t *vl);
277 int parse_value (const char *value, value_t *ret_value, int ds_type);
278 int parse_values (char *buffer, value_list_t *vl, const data_set_t *ds);
279
280 #if !HAVE_GETPWNAM_R
281 int getpwnam_r (const char *name, struct passwd *pwbuf, char *buf,
282                 size_t buflen, struct passwd **pwbufp);
283 #endif
284
285 int notification_init (notification_t *n, int severity, const char *message,
286                 const char *host,
287                 const char *plugin, const char *plugin_instance,
288                 const char *type, const char *type_instance);
289 #define NOTIFICATION_INIT_VL(n, vl) \
290         notification_init (n, NOTIF_FAILURE, NULL, \
291                         (vl)->host, (vl)->plugin, (vl)->plugin_instance, \
292                         (vl)->type, (vl)->type_instance)
293
294 typedef int (*dirwalk_callback_f)(const char *dirname, const char *filename,
295                 void *user_data);
296 int walk_directory (const char *dir, dirwalk_callback_f callback,
297                 void *user_data, int hidden);
298 /* Returns the number of bytes read or negative on error. */
299 int read_file_contents (const char *filename, char *buf, int bufsize);
300
301 counter_t counter_diff (counter_t old_value, counter_t new_value);
302
303 /* Convert a rate back to a value_t. When converting to a derive_t, counter_t
304  * or absoltue_t, take fractional residuals into account. This is important
305  * when scaling counters, for example.
306  * Returns zero on success. Returns EAGAIN when called for the first time; in
307  * this case the value_t is invalid and the next call should succeed. Other
308  * return values indicate an error. */
309 int rate_to_value (value_t *ret_value, gauge_t rate,
310                 rate_to_value_state_t *state, int ds_type, cdtime_t t);
311
312 /* Converts a service name (a string) to a port number
313  * (in the range [1-65535]). Returns less than zero on error. */
314 int service_name_to_port_number (const char *service_name);
315
316 /** Parse a string to a derive_t value. Returns zero on success or non-zero on
317  * failure. If failure is returned, ret_value is not touched. */
318 int strtoderive (const char *string, derive_t *ret_value);
319
320 #endif /* COMMON_H */