1 /**
2 * collectd - src/common.c
3 * Copyright (C) 2005-2008 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 * Niki W. Waibel <niki.waibel@gmx.net>
21 **/
23 #if HAVE_CONFIG_H
24 # include "config.h"
25 #endif
27 #include "collectd.h"
28 #include "common.h"
29 #include "plugin.h"
31 #if HAVE_PTHREAD_H
32 # include <pthread.h>
33 #endif
35 #ifdef HAVE_MATH_H
36 # include <math.h>
37 #endif
39 /* for ntohl and htonl */
40 #if HAVE_ARPA_INET_H
41 # include <arpa/inet.h>
42 #endif
44 #ifdef HAVE_LIBKSTAT
45 extern kstat_ctl_t *kc;
46 #endif
48 #if !HAVE_GETPWNAM_R
49 static pthread_mutex_t getpwnam_r_lock = PTHREAD_MUTEX_INITIALIZER;
50 #endif
52 #if !HAVE_STRERROR_R
53 static pthread_mutex_t strerror_r_lock = PTHREAD_MUTEX_INITIALIZER;
54 #endif
56 char *sstrncpy (char *dest, const char *src, size_t n)
57 {
58 strncpy (dest, src, n);
59 dest[n - 1] = '\0';
61 return (dest);
62 } /* char *sstrncpy */
64 int ssnprintf (char *dest, size_t n, const char *format, ...)
65 {
66 int ret = 0;
67 va_list ap;
69 va_start (ap, format);
70 ret = vsnprintf (dest, n, format, ap);
71 dest[n - 1] = '\0';
72 va_end (ap);
74 return (ret);
75 } /* int ssnprintf */
77 char *sstrdup (const char *s)
78 {
79 char *r;
80 size_t sz;
82 if (s == NULL)
83 return (NULL);
85 /* Do not use `strdup' here, because it's not specified in POSIX. It's
86 * ``only'' an XSI extension. */
87 sz = strlen (s) + 1;
88 r = (char *) malloc (sizeof (char) * sz);
89 if (r == NULL)
90 {
91 ERROR ("sstrdup: Out of memory.");
92 exit (3);
93 }
94 memcpy (r, s, sizeof (char) * sz);
96 return (r);
97 } /* char *sstrdup */
99 /* Even though Posix requires "strerror_r" to return an "int",
100 * some systems (e.g. the GNU libc) return a "char *" _and_
101 * ignore the second argument ... -tokkee */
102 char *sstrerror (int errnum, char *buf, size_t buflen)
103 {
104 buf[0] = '\0';
106 #if !HAVE_STRERROR_R
107 {
108 char *temp;
110 pthread_mutex_lock (&strerror_r_lock);
112 temp = strerror (errnum);
113 sstrncpy (buf, temp, buflen);
115 pthread_mutex_unlock (&strerror_r_lock);
116 }
117 /* #endif !HAVE_STRERROR_R */
119 #elif STRERROR_R_CHAR_P
120 {
121 char *temp;
122 temp = strerror_r (errnum, buf, buflen);
123 if (buf[0] == '\0')
124 {
125 if ((temp != NULL) && (temp != buf) && (temp[0] != '\0'))
126 sstrncpy (buf, temp, buflen);
127 else
128 sstrncpy (buf, "strerror_r did not return "
129 "an error message", buflen);
130 }
131 }
132 /* #endif STRERROR_R_CHAR_P */
134 #else
135 if (strerror_r (errnum, buf, buflen) != 0)
136 {
137 ssnprintf (buf, buflen, "Error #%i; "
138 "Additionally, strerror_r failed.",
139 errnum);
140 }
141 #endif /* STRERROR_R_CHAR_P */
143 return (buf);
144 } /* char *sstrerror */
146 void *smalloc (size_t size)
147 {
148 void *r;
150 if ((r = malloc (size)) == NULL)
151 {
152 ERROR ("Not enough memory.");
153 exit (3);
154 }
156 return (r);
157 } /* void *smalloc */
159 #if 0
160 void sfree (void **ptr)
161 {
162 if (ptr == NULL)
163 return;
165 if (*ptr != NULL)
166 free (*ptr);
168 *ptr = NULL;
169 }
170 #endif
172 ssize_t sread (int fd, void *buf, size_t count)
173 {
174 char *ptr;
175 size_t nleft;
176 ssize_t status;
178 ptr = (char *) buf;
179 nleft = count;
181 while (nleft > 0)
182 {
183 status = read (fd, (void *) ptr, nleft);
185 if ((status < 0) && ((errno == EAGAIN) || (errno == EINTR)))
186 continue;
188 if (status < 0)
189 return (status);
191 if (status == 0)
192 {
193 DEBUG ("Received EOF from fd %i. "
194 "Closing fd and returning error.",
195 fd);
196 close (fd);
197 return (-1);
198 }
200 assert ((0 > status) || (nleft >= (size_t)status));
202 nleft = nleft - status;
203 ptr = ptr + status;
204 }
206 return (0);
207 }
210 ssize_t swrite (int fd, const void *buf, size_t count)
211 {
212 const char *ptr;
213 size_t nleft;
214 ssize_t status;
216 ptr = (const char *) buf;
217 nleft = count;
219 while (nleft > 0)
220 {
221 status = write (fd, (const void *) ptr, nleft);
223 if ((status < 0) && ((errno == EAGAIN) || (errno == EINTR)))
224 continue;
226 if (status < 0)
227 return (status);
229 nleft = nleft - status;
230 ptr = ptr + status;
231 }
233 return (0);
234 }
236 int strsplit (char *string, char **fields, size_t size)
237 {
238 size_t i;
239 char *ptr;
240 char *saveptr;
242 i = 0;
243 ptr = string;
244 saveptr = NULL;
245 while ((fields[i] = strtok_r (ptr, " \t\r\n", &saveptr)) != NULL)
246 {
247 ptr = NULL;
248 i++;
250 if (i >= size)
251 break;
252 }
254 return (i);
255 }
257 int strjoin (char *dst, size_t dst_len,
258 char **fields, size_t fields_num,
259 const char *sep)
260 {
261 size_t field_len;
262 size_t sep_len;
263 int i;
265 memset (dst, '\0', dst_len);
267 if (fields_num <= 0)
268 return (-1);
270 sep_len = 0;
271 if (sep != NULL)
272 sep_len = strlen (sep);
274 for (i = 0; i < (int)fields_num; i++)
275 {
276 if ((i > 0) && (sep_len > 0))
277 {
278 if (dst_len <= sep_len)
279 return (-1);
281 strncat (dst, sep, dst_len);
282 dst_len -= sep_len;
283 }
285 field_len = strlen (fields[i]);
287 if (dst_len <= field_len)
288 return (-1);
290 strncat (dst, fields[i], dst_len);
291 dst_len -= field_len;
292 }
294 return (strlen (dst));
295 }
297 int strsubstitute (char *str, char c_from, char c_to)
298 {
299 int ret;
301 if (str == NULL)
302 return (-1);
304 ret = 0;
305 while (*str != '\0')
306 {
307 if (*str == c_from)
308 {
309 *str = c_to;
310 ret++;
311 }
312 str++;
313 }
315 return (ret);
316 } /* int strsubstitute */
318 int escape_slashes (char *buf, int buf_len)
319 {
320 int i;
322 if (strcmp (buf, "/") == 0)
323 {
324 if (buf_len < 5)
325 return (-1);
327 strncpy (buf, "root", buf_len);
328 return (0);
329 }
331 if (buf_len <= 1)
332 return (0);
334 /* Move one to the left */
335 if (buf[0] == '/')
336 memmove (buf, buf + 1, buf_len - 1);
338 for (i = 0; i < buf_len - 1; i++)
339 {
340 if (buf[i] == '\0')
341 break;
342 else if (buf[i] == '/')
343 buf[i] = '_';
344 }
345 buf[i] = '\0';
347 return (0);
348 } /* int escape_slashes */
350 int timeval_cmp (struct timeval tv0, struct timeval tv1, struct timeval *delta)
351 {
352 struct timeval *larger;
353 struct timeval *smaller;
355 int status;
357 NORMALIZE_TIMEVAL (tv0);
358 NORMALIZE_TIMEVAL (tv1);
360 if ((tv0.tv_sec == tv1.tv_sec) && (tv0.tv_usec == tv1.tv_usec))
361 {
362 if (delta != NULL) {
363 delta->tv_sec = 0;
364 delta->tv_usec = 0;
365 }
366 return (0);
367 }
369 if ((tv0.tv_sec < tv1.tv_sec)
370 || ((tv0.tv_sec == tv1.tv_sec) && (tv0.tv_usec < tv1.tv_usec)))
371 {
372 larger = &tv1;
373 smaller = &tv0;
374 status = -1;
375 }
376 else
377 {
378 larger = &tv0;
379 smaller = &tv1;
380 status = 1;
381 }
383 if (delta != NULL) {
384 delta->tv_sec = larger->tv_sec - smaller->tv_sec;
386 if (smaller->tv_usec <= larger->tv_usec)
387 delta->tv_usec = larger->tv_usec - smaller->tv_usec;
388 else
389 {
390 --delta->tv_sec;
391 delta->tv_usec = 1000000 + larger->tv_usec - smaller->tv_usec;
392 }
393 }
395 assert ((delta == NULL)
396 || ((0 <= delta->tv_usec) && (delta->tv_usec < 1000000)));
398 return (status);
399 } /* int timeval_cmp */
401 int check_create_dir (const char *file_orig)
402 {
403 struct stat statbuf;
405 char file_copy[512];
406 char dir[512];
407 int dir_len = 512;
408 char *fields[16];
409 int fields_num;
410 char *ptr;
411 char *saveptr;
412 int last_is_file = 1;
413 int path_is_absolute = 0;
414 size_t len;
415 int i;
417 /*
418 * Sanity checks first
419 */
420 if (file_orig == NULL)
421 return (-1);
423 if ((len = strlen (file_orig)) < 1)
424 return (-1);
425 else if (len >= sizeof (file_copy))
426 return (-1);
428 /*
429 * If `file_orig' ends in a slash the last component is a directory,
430 * otherwise it's a file. Act accordingly..
431 */
432 if (file_orig[len - 1] == '/')
433 last_is_file = 0;
434 if (file_orig[0] == '/')
435 path_is_absolute = 1;
437 /*
438 * Create a copy for `strtok_r' to destroy
439 */
440 sstrncpy (file_copy, file_orig, sizeof (file_copy));
442 /*
443 * Break into components. This will eat up several slashes in a row and
444 * remove leading and trailing slashes..
445 */
446 ptr = file_copy;
447 saveptr = NULL;
448 fields_num = 0;
449 while ((fields[fields_num] = strtok_r (ptr, "/", &saveptr)) != NULL)
450 {
451 ptr = NULL;
452 fields_num++;
454 if (fields_num >= 16)
455 break;
456 }
458 /*
459 * For each component, do..
460 */
461 for (i = 0; i < (fields_num - last_is_file); i++)
462 {
463 /*
464 * Do not create directories that start with a dot. This
465 * prevents `../../' attacks and other likely malicious
466 * behavior.
467 */
468 if (fields[i][0] == '.')
469 {
470 ERROR ("Cowardly refusing to create a directory that begins with a `.' (dot): `%s'", file_orig);
471 return (-2);
472 }
474 /*
475 * Join the components together again
476 */
477 dir[0] = '/';
478 if (strjoin (dir + path_is_absolute, dir_len - path_is_absolute,
479 fields, i + 1, "/") < 0)
480 {
481 ERROR ("strjoin failed: `%s', component #%i", file_orig, i);
482 return (-1);
483 }
485 if (stat (dir, &statbuf) == -1)
486 {
487 if (errno == ENOENT)
488 {
489 if (mkdir (dir, 0755) == -1)
490 {
491 char errbuf[1024];
492 ERROR ("check_create_dir: mkdir (%s): %s", dir,
493 sstrerror (errno,
494 errbuf, sizeof (errbuf)));
495 return (-1);
496 }
497 }
498 else
499 {
500 char errbuf[1024];
501 ERROR ("stat (%s): %s", dir,
502 sstrerror (errno, errbuf,
503 sizeof (errbuf)));
504 return (-1);
505 }
506 }
507 else if (!S_ISDIR (statbuf.st_mode))
508 {
509 ERROR ("stat (%s): Not a directory!", dir);
510 return (-1);
511 }
512 }
514 return (0);
515 } /* check_create_dir */
517 #ifdef HAVE_LIBKSTAT
518 int get_kstat (kstat_t **ksp_ptr, char *module, int instance, char *name)
519 {
520 char ident[128];
522 if (kc == NULL)
523 return (-1);
525 ssnprintf (ident, sizeof (ident), "%s,%i,%s", module, instance, name);
527 if (*ksp_ptr == NULL)
528 {
529 if ((*ksp_ptr = kstat_lookup (kc, module, instance, name)) == NULL)
530 {
531 ERROR ("Cound not find kstat %s", ident);
532 return (-1);
533 }
535 if ((*ksp_ptr)->ks_type != KSTAT_TYPE_NAMED)
536 {
537 WARNING ("kstat %s has wrong type", ident);
538 *ksp_ptr = NULL;
539 return (-1);
540 }
541 }
543 #ifdef assert
544 assert (*ksp_ptr != NULL);
545 assert ((*ksp_ptr)->ks_type == KSTAT_TYPE_NAMED);
546 #endif
548 if (kstat_read (kc, *ksp_ptr, NULL) == -1)
549 {
550 WARNING ("kstat %s could not be read", ident);
551 return (-1);
552 }
554 if ((*ksp_ptr)->ks_type != KSTAT_TYPE_NAMED)
555 {
556 WARNING ("kstat %s has wrong type", ident);
557 return (-1);
558 }
560 return (0);
561 }
563 long long get_kstat_value (kstat_t *ksp, char *name)
564 {
565 kstat_named_t *kn;
566 long long retval = -1LL;
568 #ifdef assert
569 assert (ksp != NULL);
570 assert (ksp->ks_type == KSTAT_TYPE_NAMED);
571 #else
572 if (ksp == NULL)
573 {
574 ERROR ("ERROR: %s:%i: ksp == NULL\n", __FILE__, __LINE__);
575 return (-1LL);
576 }
577 else if (ksp->ks_type != KSTAT_TYPE_NAMED)
578 {
579 ERROR ("ERROR: %s:%i: ksp->ks_type != KSTAT_TYPE_NAMED\n", __FILE__, __LINE__);
580 return (-1LL);
581 }
582 #endif
584 if ((kn = (kstat_named_t *) kstat_data_lookup (ksp, name)) == NULL)
585 return (retval);
587 if (kn->data_type == KSTAT_DATA_INT32)
588 retval = (long long) kn->value.i32;
589 else if (kn->data_type == KSTAT_DATA_UINT32)
590 retval = (long long) kn->value.ui32;
591 else if (kn->data_type == KSTAT_DATA_INT64)
592 retval = (long long) kn->value.i64; /* According to ANSI C99 `long long' must hold at least 64 bits */
593 else if (kn->data_type == KSTAT_DATA_UINT64)
594 retval = (long long) kn->value.ui64; /* XXX: Might overflow! */
595 else
596 WARNING ("get_kstat_value: Not a numeric value: %s", name);
598 return (retval);
599 }
600 #endif /* HAVE_LIBKSTAT */
602 unsigned long long ntohll (unsigned long long n)
603 {
604 #if BYTE_ORDER == BIG_ENDIAN
605 return (n);
606 #else
607 return (((unsigned long long) ntohl (n)) << 32) + ntohl (n >> 32);
608 #endif
609 } /* unsigned long long ntohll */
611 unsigned long long htonll (unsigned long long n)
612 {
613 #if BYTE_ORDER == BIG_ENDIAN
614 return (n);
615 #else
616 return (((unsigned long long) htonl (n)) << 32) + htonl (n >> 32);
617 #endif
618 } /* unsigned long long htonll */
620 #if FP_LAYOUT_NEED_NOTHING
621 /* Well, we need nothing.. */
622 /* #endif FP_LAYOUT_NEED_NOTHING */
624 #elif FP_LAYOUT_NEED_ENDIANFLIP || FP_LAYOUT_NEED_INTSWAP
625 # if FP_LAYOUT_NEED_ENDIANFLIP
626 # define FP_CONVERT(A) ((((uint64_t)(A) & 0xff00000000000000LL) >> 56) | \
627 (((uint64_t)(A) & 0x00ff000000000000LL) >> 40) | \
628 (((uint64_t)(A) & 0x0000ff0000000000LL) >> 24) | \
629 (((uint64_t)(A) & 0x000000ff00000000LL) >> 8) | \
630 (((uint64_t)(A) & 0x00000000ff000000LL) << 8) | \
631 (((uint64_t)(A) & 0x0000000000ff0000LL) << 24) | \
632 (((uint64_t)(A) & 0x000000000000ff00LL) << 40) | \
633 (((uint64_t)(A) & 0x00000000000000ffLL) << 56))
634 # else
635 # define FP_CONVERT(A) ((((uint64_t)(A) & 0xffffffff00000000LL) >> 32) | \
636 (((uint64_t)(A) & 0x00000000ffffffffLL) << 32))
637 # endif
639 double ntohd (double d)
640 {
641 union
642 {
643 uint8_t byte[8];
644 uint64_t integer;
645 double floating;
646 } ret;
648 ret.floating = d;
650 /* NAN in x86 byte order */
651 if ((ret.byte[0] == 0x00) && (ret.byte[1] == 0x00)
652 && (ret.byte[2] == 0x00) && (ret.byte[3] == 0x00)
653 && (ret.byte[4] == 0x00) && (ret.byte[5] == 0x00)
654 && (ret.byte[6] == 0xf8) && (ret.byte[7] == 0x7f))
655 {
656 return (NAN);
657 }
658 else
659 {
660 uint64_t tmp;
662 tmp = ret.integer;
663 ret.integer = FP_CONVERT (tmp);
664 return (ret.floating);
665 }
666 } /* double ntohd */
668 double htond (double d)
669 {
670 union
671 {
672 uint8_t byte[8];
673 uint64_t integer;
674 double floating;
675 } ret;
677 if (isnan (d))
678 {
679 ret.byte[0] = ret.byte[1] = ret.byte[2] = ret.byte[3] = 0x00;
680 ret.byte[4] = ret.byte[5] = 0x00;
681 ret.byte[6] = 0xf8;
682 ret.byte[7] = 0x7f;
683 return (ret.floating);
684 }
685 else
686 {
687 uint64_t tmp;
689 ret.floating = d;
690 tmp = FP_CONVERT (ret.integer);
691 ret.integer = tmp;
692 return (ret.floating);
693 }
694 } /* double htond */
695 #endif /* FP_LAYOUT_NEED_ENDIANFLIP || FP_LAYOUT_NEED_INTSWAP */
697 int format_name (char *ret, int ret_len,
698 const char *hostname,
699 const char *plugin, const char *plugin_instance,
700 const char *type, const char *type_instance)
701 {
702 int status;
704 assert (plugin != NULL);
705 assert (type != NULL);
707 if ((plugin_instance == NULL) || (strlen (plugin_instance) == 0))
708 {
709 if ((type_instance == NULL) || (strlen (type_instance) == 0))
710 status = ssnprintf (ret, ret_len, "%s/%s/%s",
711 hostname, plugin, type);
712 else
713 status = ssnprintf (ret, ret_len, "%s/%s/%s-%s",
714 hostname, plugin, type,
715 type_instance);
716 }
717 else
718 {
719 if ((type_instance == NULL) || (strlen (type_instance) == 0))
720 status = ssnprintf (ret, ret_len, "%s/%s-%s/%s",
721 hostname, plugin, plugin_instance,
722 type);
723 else
724 status = ssnprintf (ret, ret_len, "%s/%s-%s/%s-%s",
725 hostname, plugin, plugin_instance,
726 type, type_instance);
727 }
729 if ((status < 1) || (status >= ret_len))
730 return (-1);
731 return (0);
732 } /* int format_name */
734 int parse_identifier (char *str, char **ret_host,
735 char **ret_plugin, char **ret_plugin_instance,
736 char **ret_type, char **ret_type_instance)
737 {
738 char *hostname = NULL;
739 char *plugin = NULL;
740 char *plugin_instance = NULL;
741 char *type = NULL;
742 char *type_instance = NULL;
744 hostname = str;
745 if (hostname == NULL)
746 return (-1);
748 plugin = strchr (hostname, '/');
749 if (plugin == NULL)
750 return (-1);
751 *plugin = '\0'; plugin++;
753 type = strchr (plugin, '/');
754 if (type == NULL)
755 return (-1);
756 *type = '\0'; type++;
758 plugin_instance = strchr (plugin, '-');
759 if (plugin_instance != NULL)
760 {
761 *plugin_instance = '\0';
762 plugin_instance++;
763 }
765 type_instance = strchr (type, '-');
766 if (type_instance != NULL)
767 {
768 *type_instance = '\0';
769 type_instance++;
770 }
772 *ret_host = hostname;
773 *ret_plugin = plugin;
774 *ret_plugin_instance = plugin_instance;
775 *ret_type = type;
776 *ret_type_instance = type_instance;
777 return (0);
778 } /* int parse_identifier */
780 int parse_values (char *buffer, value_list_t *vl, const data_set_t *ds)
781 {
782 int i;
783 char *dummy;
784 char *ptr;
785 char *saveptr;
787 i = -1;
788 dummy = buffer;
789 saveptr = NULL;
790 while ((ptr = strtok_r (dummy, ":", &saveptr)) != NULL)
791 {
792 dummy = NULL;
794 if (i >= vl->values_len)
795 break;
797 if (i == -1)
798 {
799 if (strcmp ("N", ptr) == 0)
800 vl->time = time (NULL);
801 else
802 vl->time = (time_t) atoi (ptr);
803 }
804 else
805 {
806 if (strcmp ("U", ptr) == 0)
807 vl->values[i].gauge = NAN;
808 else if (ds->ds[i].type == DS_TYPE_COUNTER)
809 vl->values[i].counter = atoll (ptr);
810 else if (ds->ds[i].type == DS_TYPE_GAUGE)
811 vl->values[i].gauge = atof (ptr);
812 }
814 i++;
815 } /* while (strtok_r) */
817 if ((ptr != NULL) || (i != vl->values_len))
818 return (-1);
819 return (0);
820 } /* int parse_values */
822 #if !HAVE_GETPWNAM_R
823 int getpwnam_r (const char *name, struct passwd *pwbuf, char *buf,
824 size_t buflen, struct passwd **pwbufp)
825 {
826 int status = 0;
827 struct passwd *pw;
829 memset (pwbuf, '\0', sizeof (struct passwd));
831 pthread_mutex_lock (&getpwnam_r_lock);
833 do
834 {
835 pw = getpwnam (name);
836 if (pw == NULL)
837 {
838 status = (errno != 0) ? errno : ENOENT;
839 break;
840 }
842 #define GETPWNAM_COPY_MEMBER(member) \
843 if (pw->member != NULL) \
844 { \
845 int len = strlen (pw->member); \
846 if (len >= buflen) \
847 { \
848 status = ENOMEM; \
849 break; \
850 } \
851 sstrncpy (buf, pw->member, buflen); \
852 pwbuf->member = buf; \
853 buf += (len + 1); \
854 buflen -= (len + 1); \
855 }
856 GETPWNAM_COPY_MEMBER(pw_name);
857 GETPWNAM_COPY_MEMBER(pw_passwd);
858 GETPWNAM_COPY_MEMBER(pw_gecos);
859 GETPWNAM_COPY_MEMBER(pw_dir);
860 GETPWNAM_COPY_MEMBER(pw_shell);
862 pwbuf->pw_uid = pw->pw_uid;
863 pwbuf->pw_gid = pw->pw_gid;
865 if (pwbufp != NULL)
866 *pwbufp = pwbuf;
867 } while (0);
869 pthread_mutex_unlock (&getpwnam_r_lock);
871 return (status);
872 } /* int getpwnam_r */
873 #endif /* !HAVE_GETPWNAM_R */
875 int notification_init (notification_t *n, int severity, const char *message,
876 const char *host,
877 const char *plugin, const char *plugin_instance,
878 const char *type, const char *type_instance)
879 {
880 memset (n, '\0', sizeof (notification_t));
882 n->severity = severity;
884 if (message != NULL)
885 sstrncpy (n->message, message, sizeof (n->message));
886 if (host != NULL)
887 sstrncpy (n->host, host, sizeof (n->host));
888 if (plugin != NULL)
889 sstrncpy (n->plugin, plugin, sizeof (n->plugin));
890 if (plugin_instance != NULL)
891 sstrncpy (n->plugin_instance, plugin_instance,
892 sizeof (n->plugin_instance));
893 if (type != NULL)
894 sstrncpy (n->type, type, sizeof (n->type));
895 if (type_instance != NULL)
896 sstrncpy (n->type_instance, type_instance,
897 sizeof (n->type_instance));
899 return (0);
900 } /* int notification_init */
902 int walk_directory (const char *dir, dirwalk_callback_f callback,
903 void *user_data)
904 {
905 struct dirent *ent;
906 DIR *dh;
907 int success;
908 int failure;
910 success = 0;
911 failure = 0;
913 if ((dh = opendir (dir)) == NULL)
914 {
915 char errbuf[1024];
916 ERROR ("walk_directory: Cannot open '%s': %s", dir,
917 sstrerror (errno, errbuf, sizeof (errbuf)));
918 return -1;
919 }
921 while ((ent = readdir (dh)) != NULL)
922 {
923 int status;
925 if (ent->d_name[0] == '.')
926 continue;
928 status = (*callback) (dir, ent->d_name, user_data);
929 if (status != 0)
930 failure++;
931 else
932 success++;
933 }
935 closedir (dh);
937 if ((success == 0) && (failure > 0))
938 return (-1);
939 return (0);
940 }
942 int read_file_contents (const char *filename, char *buf, int bufsize)
943 {
944 FILE *fh;
945 int n;
947 if ((fh = fopen (filename, "r")) == NULL)
948 return -1;
950 n = fread(buf, 1, bufsize, fh);
951 fclose(fh);
953 return n;
954 }
956 counter_t counter_diff (counter_t old_value, counter_t new_value)
957 {
958 counter_t diff;
960 if (old_value > new_value)
961 {
962 if (old_value <= 4294967295U)
963 diff = (4294967295U - old_value) + new_value;
964 else
965 diff = (18446744073709551615ULL - old_value)
966 + new_value;
967 }
968 else
969 {
970 diff = new_value - old_value;
971 }
973 return (diff);
974 } /* counter_t counter_to_gauge */