utils_base.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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 NP_RANGE_UNPARSEABLE;
  86. }
  87. }
  88. if (critical_string != NULL) {
  89. if ((temp_thresholds->critical = parse_range_string(critical_string)) == NULL) {
  90. return NP_RANGE_UNPARSEABLE;
  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. switch (_set_thresholds(my_thresholds, warn_string, critical_string)) {
  104. case 0:
  105. return;
  106. case NP_RANGE_UNPARSEABLE:
  107. die(STATE_UNKNOWN, _("Range format incorrect"));
  108. case NP_WARN_WITHIN_CRIT:
  109. die(STATE_UNKNOWN, _("Warning level is a subset of critical and will not be alerted"));
  110. break;
  111. }
  112. }
  113. void print_thresholds(const char *threshold_name, thresholds *my_threshold) {
  114. printf("%s - ", threshold_name);
  115. if (! my_threshold) {
  116. printf("Threshold not set");
  117. } else {
  118. if (my_threshold->warning) {
  119. printf("Warning: start=%g end=%g; ", my_threshold->warning->start, my_threshold->warning->end);
  120. } else {
  121. printf("Warning not set; ");
  122. }
  123. if (my_threshold->critical) {
  124. printf("Critical: start=%g end=%g", my_threshold->critical->start, my_threshold->critical->end);
  125. } else {
  126. printf("Critical not set");
  127. }
  128. }
  129. printf("\n");
  130. }
  131. /* Returns TRUE if alert should be raised based on the range */
  132. int
  133. check_range(double value, range *my_range)
  134. {
  135. int false = FALSE;
  136. int true = TRUE;
  137. if (my_range->alert_on == INSIDE) {
  138. false = TRUE;
  139. true = FALSE;
  140. }
  141. if (my_range->end_infinity == FALSE && my_range->start_infinity == FALSE) {
  142. if ((my_range->start <= value) && (value <= my_range->end)) {
  143. return false;
  144. } else {
  145. return true;
  146. }
  147. } else if (my_range->start_infinity == FALSE && my_range->end_infinity == TRUE) {
  148. if (my_range->start <= value) {
  149. return false;
  150. } else {
  151. return true;
  152. }
  153. } else if (my_range->start_infinity == TRUE && my_range->end_infinity == FALSE) {
  154. if (value <= my_range->end) {
  155. return false;
  156. } else {
  157. return true;
  158. }
  159. } else {
  160. return false;
  161. }
  162. }
  163. /* Returns status */
  164. int
  165. get_status(double value, thresholds *my_thresholds)
  166. {
  167. if (my_thresholds->critical != NULL) {
  168. if (check_range(value, my_thresholds->critical) == TRUE) {
  169. return STATE_CRITICAL;
  170. }
  171. }
  172. if (my_thresholds->warning != NULL) {
  173. if (check_range(value, my_thresholds->warning) == TRUE) {
  174. return STATE_WARNING;
  175. }
  176. }
  177. return STATE_OK;
  178. }
  179. char *np_escaped_string (const char *string) {
  180. char *data;
  181. int i, j=0;
  182. data = strdup(string);
  183. for (i=0; data[i]; i++) {
  184. if (data[i] == '\\') {
  185. switch(data[++i]) {
  186. case 'n':
  187. data[j++] = '\n';
  188. break;
  189. case 'r':
  190. data[j++] = '\r';
  191. break;
  192. case 't':
  193. data[j++] = '\t';
  194. break;
  195. case '\\':
  196. data[j++] = '\\';
  197. break;
  198. default:
  199. data[j++] = data[i];
  200. }
  201. } else {
  202. data[j++] = data[i];
  203. }
  204. }
  205. data[j] = '\0';
  206. return data;
  207. }