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