utils_base.c 4.8 KB

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