2 * collectd - src/common.h
3 * Copyright (C) 2005-2015 Florian octo Forster
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:
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
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.
24 * Florian octo Forster <octo at collectd.org>
25 * Niki W. Waibel <niki.waibel@gmx.net>
46 #define STATIC_ARRAY_SIZE(a) (sizeof (a) / sizeof (*(a)))
48 #define IS_TRUE(s) ((strcasecmp ("true", (s)) == 0) \
49 || (strcasecmp ("yes", (s)) == 0) \
50 || (strcasecmp ("on", (s)) == 0))
51 #define IS_FALSE(s) ((strcasecmp ("false", (s)) == 0) \
52 || (strcasecmp ("no", (s)) == 0) \
53 || (strcasecmp ("off", (s)) == 0))
55 struct rate_to_value_state_s
61 typedef struct rate_to_value_state_s rate_to_value_state_t;
63 struct value_to_rate_state_s
68 typedef struct value_to_rate_state_s value_to_rate_state_t;
70 char *sstrncpy (char *dest, const char *src, size_t n);
72 __attribute__ ((format(printf,3,4)))
73 int ssnprintf (char *dest, size_t n, const char *format, ...);
75 __attribute__ ((format(printf,1,2)))
76 char *ssnprintf_alloc (char const *format, ...);
78 char *sstrdup(const char *s);
79 void *smalloc(size_t size);
80 char *sstrerror (int errnum, char *buf, size_t buflen);
87 * Reads exactly `n' bytes or fails. Syntax and other behavior is analogous
88 * to `read(2)'. If EOF is received the file descriptor is closed and an
92 * `fd' File descriptor to write to.
93 * `buf' Buffer that is to be written.
94 * `count' Number of bytes in the buffer.
97 * Zero upon success or non-zero if an error occurred. `errno' is set in this
100 ssize_t sread (int fd, void *buf, size_t count);
102 /* read_file reads the contents of "file" into memory and stores a pointer in
103 * "ret_data", which must be freed by the caller. The number of bytes read is
104 * stored in "ret_data_size". On success, 0 is returned. On failure, an error
105 * code is stored in "errno" and -1 is returned; "ret_data" and "ret_data_size"
106 * are left unmodified. */
107 int read_file (char const *file, void **ret_data, size_t *ret_data_size);
114 * Writes exactly `n' bytes or fails. Syntax and other behavior is analogous
118 * `fd' File descriptor to write to.
119 * `buf' Buffer that is to be written.
120 * `count' Number of bytes in the buffer.
123 * Zero upon success or non-zero if an error occurred. `errno' is set in this
126 ssize_t swrite (int fd, const void *buf, size_t count);
133 * Splits a string into parts and stores pointers to the parts in `fields'.
134 * The characters split at are: " ", "\t", "\r", and "\n".
137 * `string' String to split. This string will be modified. `fields' will
138 * contain pointers to parts of this string, so free'ing it
139 * will destroy `fields' as well.
140 * `fields' Array of strings where pointers to the parts will be stored.
141 * `size' Number of elements in the array. No more than `size'
142 * pointers will be stored in `fields'.
145 * Returns the number of parts stored in `fields'.
147 int strsplit (char *string, char **fields, size_t size);
154 * Joins together several parts of a string using `sep' as a separator. This
155 * is equivalent to the Perl built-in `join'.
158 * `dst' Buffer where the result is stored.
159 * `dst_len' Length of the destination buffer. No more than this many
160 * bytes will be written to the memory pointed to by `dst',
161 * including the trailing null-byte.
162 * `fields' Array of strings to be joined.
163 * `fields_num' Number of elements in the `fields' array.
164 * `sep' String to be inserted between any two elements of `fields'.
165 * This string is neither prepended nor appended to the result.
166 * Instead of passing "" (empty string) one can pass NULL.
169 * Returns the number of characters in `dst', NOT including the trailing
170 * null-byte. If an error occurred (empty array or `dst' too small) a value
171 * smaller than zero will be returned.
173 int strjoin (char *dst, size_t dst_len, char **fields, size_t fields_num, const char *sep);
180 * Removes slashes ("/") from "buffer". If buffer contains a single slash,
181 * the result will be "root". Leading slashes are removed. All other slashes
182 * are replaced with underscores ("_").
183 * This function is used by plugin_dispatch_values() to escape all parts of
187 * `buffer' String to be escaped.
188 * `buffer_size' Size of the buffer. No more then this many bytes will be
189 * written to `buffer', including the trailing null-byte.
192 * Returns zero upon success and a value smaller than zero upon failure.
194 int escape_slashes (char *buffer, size_t buffer_size);
201 * escape_string quotes and escapes a string to be usable with collectd's
202 * plain text protocol. "simple" strings are left as they are, for example if
203 * buffer is 'simple' before the call, it will remain 'simple'. However, if
204 * buffer contains 'more "complex"' before the call, the returned buffer will
205 * contain '"more \"complex\""'.
207 * If the buffer is too small to contain the escaped string, the string will
208 * be truncated. However, leading and trailing double quotes, as well as an
209 * ending null byte are guaranteed.
212 * Returns zero on success, even if the string was truncated. Non-zero on
215 int escape_string (char *buffer, size_t buffer_size);
222 * Replaces any special characters (anything that's not alpha-numeric or a
223 * dash) with an underscore.
225 * E.g. "foo$bar&" would become "foo_bar_".
228 * `buffer' String to be handled.
229 * `buffer_size' Length of the string. The function returns after
230 * encountering a null-byte or reading this many bytes.
232 void replace_special (char *buffer, size_t buffer_size);
234 int strsubstitute (char *str, char c_from, char c_to);
241 * Replaces any escaped characters in a string with the appropriate special
242 * characters. The following escaped characters are recognized:
246 * \r -> <carriage return>
248 * For all other escacped characters only the backslash will be removed.
251 * `buf' String to be unescaped.
252 * `buf_len' Length of the string, including the terminating null-byte.
255 * Returns zero upon success, a value less than zero else.
257 int strunescape (char *buf, size_t buf_len);
260 * Removed trailing newline characters (CR and LF) from buffer, which must be
261 * null terminated. Returns the length of the resulting string.
263 __attribute__((nonnull (1)))
264 size_t strstripnewline (char *buffer);
271 * Compare the two time values `tv0' and `tv1' and store the absolut value
272 * of the difference in the time value pointed to by `delta' if it does not
276 * Returns an integer less than, equal to, or greater than zero if `tv0' is
277 * less than, equal to, or greater than `tv1' respectively.
279 int timeval_cmp (struct timeval tv0, struct timeval tv1, struct timeval *delta);
281 /* make sure tv_usec stores less than a second */
282 #define NORMALIZE_TIMEVAL(tv) \
284 (tv).tv_sec += (tv).tv_usec / 1000000; \
285 (tv).tv_usec = (tv).tv_usec % 1000000; \
288 /* make sure tv_sec stores less than a second */
289 #define NORMALIZE_TIMESPEC(tv) \
291 (tv).tv_sec += (tv).tv_nsec / 1000000000; \
292 (tv).tv_nsec = (tv).tv_nsec % 1000000000; \
295 int check_create_dir (const char *file_orig);
298 int get_kstat (kstat_t **ksp_ptr, char *module, int instance, char *name);
299 long long get_kstat_value (kstat_t *ksp, char *name);
303 unsigned long long ntohll (unsigned long long n);
304 unsigned long long htonll (unsigned long long n);
307 #if FP_LAYOUT_NEED_NOTHING
308 # define ntohd(d) (d)
309 # define htond(d) (d)
310 #elif FP_LAYOUT_NEED_ENDIANFLIP || FP_LAYOUT_NEED_INTSWAP
311 double ntohd (double d);
312 double htond (double d);
314 # error "Don't know how to convert between host and network representation of doubles."
317 int format_name (char *ret, int ret_len,
318 const char *hostname,
319 const char *plugin, const char *plugin_instance,
320 const char *type, const char *type_instance);
321 #define FORMAT_VL(ret, ret_len, vl) \
322 format_name (ret, ret_len, (vl)->host, (vl)->plugin, (vl)->plugin_instance, \
323 (vl)->type, (vl)->type_instance)
324 int format_values (char *ret, size_t ret_len,
325 const data_set_t *ds, const value_list_t *vl,
328 int parse_identifier (char *str, char **ret_host,
329 char **ret_plugin, char **ret_plugin_instance,
330 char **ret_type, char **ret_type_instance);
331 int parse_identifier_vl (const char *str, value_list_t *vl);
332 int parse_value (const char *value, value_t *ret_value, int ds_type);
333 int parse_values (char *buffer, value_list_t *vl, const data_set_t *ds);
336 int getpwnam_r (const char *name, struct passwd *pwbuf, char *buf,
337 size_t buflen, struct passwd **pwbufp);
340 int notification_init (notification_t *n, int severity, const char *message,
342 const char *plugin, const char *plugin_instance,
343 const char *type, const char *type_instance);
344 #define NOTIFICATION_INIT_VL(n, vl) \
345 notification_init (n, NOTIF_FAILURE, NULL, \
346 (vl)->host, (vl)->plugin, (vl)->plugin_instance, \
347 (vl)->type, (vl)->type_instance)
349 typedef int (*dirwalk_callback_f)(const char *dirname, const char *filename,
351 int walk_directory (const char *dir, dirwalk_callback_f callback,
352 void *user_data, int hidden);
353 /* Returns the number of bytes read or negative on error. */
354 ssize_t read_file_contents (char const *filename, char *buf, size_t bufsize);
356 counter_t counter_diff (counter_t old_value, counter_t new_value);
358 /* Convert a rate back to a value_t. When converting to a derive_t, counter_t
359 * or absoltue_t, take fractional residuals into account. This is important
360 * when scaling counters, for example.
361 * Returns zero on success. Returns EAGAIN when called for the first time; in
362 * this case the value_t is invalid and the next call should succeed. Other
363 * return values indicate an error. */
364 int rate_to_value (value_t *ret_value, gauge_t rate,
365 rate_to_value_state_t *state, int ds_type, cdtime_t t);
367 int value_to_rate (value_t *ret_rate, derive_t value,
368 value_to_rate_state_t *state, int ds_type, cdtime_t t);
370 /* Converts a service name (a string) to a port number
371 * (in the range [1-65535]). Returns less than zero on error. */
372 int service_name_to_port_number (const char *service_name);
374 /** Parse a string to a derive_t value. Returns zero on success or non-zero on
375 * failure. If failure is returned, ret_value is not touched. */
376 int strtoderive (const char *string, derive_t *ret_value);
378 /** Parse a string to a gauge_t value. Returns zero on success or non-zero on
379 * failure. If failure is returned, ret_value is not touched. */
380 int strtogauge (const char *string, gauge_t *ret_value);
382 int strarray_add (char ***ret_array, size_t *ret_array_len, char const *str);
383 void strarray_free (char **array, size_t array_len);
385 #endif /* COMMON_H */