utils_base.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. /*****************************************************************************
  2. *
  3. * utils_base.c
  4. *
  5. * Library of useful functions for plugins
  6. * These functions are tested with libtap. See tests/ directory
  7. *
  8. * Copyright (c) 2006 Nagios Plugin Development Team
  9. * License: GPL
  10. *
  11. * $Revision$
  12. * $Date$
  13. ****************************************************************************/
  14. #include <stdarg.h>
  15. #include "common.h"
  16. #include "utils_base.h"
  17. void
  18. die (int result, const char *fmt, ...)
  19. {
  20. va_list ap;
  21. va_start (ap, fmt);
  22. vprintf (fmt, ap);
  23. va_end (ap);
  24. exit (result);
  25. }
  26. void set_range_start (range *this, double value) {
  27. this->start = value;
  28. this->start_infinity = FALSE;
  29. }
  30. void set_range_end (range *this, double value) {
  31. this->end = value;
  32. this->end_infinity = FALSE;
  33. }
  34. range
  35. *parse_range_string (char *str) {
  36. range *temp_range;
  37. double start;
  38. double end;
  39. char *end_str;
  40. temp_range = (range *) malloc(sizeof(range));
  41. /* Set defaults */
  42. temp_range->start = 0;
  43. temp_range->start_infinity = FALSE;
  44. temp_range->end = 0;
  45. temp_range->end_infinity = TRUE;
  46. temp_range->alert_on = OUTSIDE;
  47. if (str[0] == '@') {
  48. temp_range->alert_on = INSIDE;
  49. str++;
  50. }
  51. end_str = index(str, ':');
  52. if (end_str != NULL) {
  53. if (str[0] == '~') {
  54. temp_range->start_infinity = TRUE;
  55. } else {
  56. start = strtod(str, NULL); /* Will stop at the ':' */
  57. set_range_start(temp_range, start);
  58. }
  59. end_str++; /* Move past the ':' */
  60. } else {
  61. end_str = str;
  62. }
  63. end = strtod(end_str, NULL);
  64. if (strcmp(end_str, "") != 0) {
  65. set_range_end(temp_range, end);
  66. }
  67. if (temp_range->start_infinity == TRUE ||
  68. temp_range->end_infinity == TRUE ||
  69. temp_range->start <= temp_range->end) {
  70. return temp_range;
  71. }
  72. free(temp_range);
  73. return NULL;
  74. }
  75. /* returns 0 if okay, otherwise 1 */
  76. int
  77. _set_thresholds(thresholds **my_thresholds, char *warn_string, char *critical_string)
  78. {
  79. thresholds *temp_thresholds = NULL;
  80. temp_thresholds = malloc(sizeof(temp_thresholds));
  81. temp_thresholds->warning = NULL;
  82. temp_thresholds->critical = NULL;
  83. if (warn_string != NULL) {
  84. if ((temp_thresholds->warning = parse_range_string(warn_string)) == NULL) {
  85. return 1;
  86. }
  87. }
  88. if (critical_string != NULL) {
  89. if ((temp_thresholds->critical = parse_range_string(critical_string)) == NULL) {
  90. return 1;
  91. }
  92. }
  93. if (*my_thresholds > 0) { /* Not sure why, but sometimes could be -1 */
  94. /* printf("Freeing here: %d\n", *my_thresholds); */
  95. free(*my_thresholds);
  96. }
  97. *my_thresholds = temp_thresholds;
  98. return 0;
  99. }
  100. void
  101. set_thresholds(thresholds **my_thresholds, char *warn_string, char *critical_string)
  102. {
  103. if (_set_thresholds(my_thresholds, warn_string, critical_string) == 0) {
  104. return;
  105. } else {
  106. die(STATE_UNKNOWN, _("Range format incorrect"));
  107. }
  108. }
  109. void print_thresholds(const char *threshold_name, thresholds *my_threshold) {
  110. printf("%s - ", threshold_name);
  111. if (! my_threshold) {
  112. printf("Threshold not set");
  113. } else {
  114. if (my_threshold->warning) {
  115. printf("Warning: start=%g end=%g; ", my_threshold->warning->start, my_threshold->warning->end);
  116. } else {
  117. printf("Warning not set; ");
  118. }
  119. if (my_threshold->critical) {
  120. printf("Critical: start=%g end=%g", my_threshold->critical->start, my_threshold->critical->end);
  121. } else {
  122. printf("Critical not set");
  123. }
  124. }
  125. printf("\n");
  126. }
  127. /* Returns TRUE if alert should be raised based on the range */
  128. int
  129. check_range(double value, range *my_range)
  130. {
  131. int false = FALSE;
  132. int true = TRUE;
  133. if (my_range->alert_on == INSIDE) {
  134. false = TRUE;
  135. true = FALSE;
  136. }
  137. if (my_range->end_infinity == FALSE && my_range->start_infinity == FALSE) {
  138. if ((my_range->start <= value) && (value <= my_range->end)) {
  139. return false;
  140. } else {
  141. return true;
  142. }
  143. } else if (my_range->start_infinity == FALSE && my_range->end_infinity == TRUE) {
  144. if (my_range->start <= value) {
  145. return false;
  146. } else {
  147. return true;
  148. }
  149. } else if (my_range->start_infinity == TRUE && my_range->end_infinity == FALSE) {
  150. if (value <= my_range->end) {
  151. return false;
  152. } else {
  153. return true;
  154. }
  155. } else {
  156. return false;
  157. }
  158. }
  159. /* Returns status */
  160. int
  161. get_status(double value, thresholds *my_thresholds)
  162. {
  163. if (my_thresholds->critical != NULL) {
  164. if (check_range(value, my_thresholds->critical) == TRUE) {
  165. return STATE_CRITICAL;
  166. }
  167. }
  168. if (my_thresholds->warning != NULL) {
  169. if (check_range(value, my_thresholds->warning) == TRUE) {
  170. return STATE_WARNING;
  171. }
  172. }
  173. return STATE_OK;
  174. }
  175. char *np_escaped_string (const char *string) {
  176. char *data;
  177. int i, j=0;
  178. data = strdup(string);
  179. for (i=0; data[i]; i++) {
  180. if (data[i] == '\\') {
  181. switch(data[++i]) {
  182. case 'n':
  183. data[j++] = '\n';
  184. break;
  185. case 'r':
  186. data[j++] = '\r';
  187. break;
  188. case 't':
  189. data[j++] = '\t';
  190. break;
  191. case '\\':
  192. data[j++] = '\\';
  193. break;
  194. default:
  195. data[j++] = data[i];
  196. }
  197. } else {
  198. data[j++] = data[i];
  199. }
  200. }
  201. data[j] = '\0';
  202. return data;
  203. }