utils_base.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. /*****************************************************************************
  2. *
  3. * utils_base.c
  4. *
  5. * License: GPL
  6. * Copyright (c) 2006 Nagios Plugins Development Team
  7. *
  8. * Last Modified: $Date$
  9. *
  10. * Library of useful functions for plugins
  11. *
  12. *
  13. * This program is free software: you can redistribute it and/or modify
  14. * it under the terms of the GNU General Public License as published by
  15. * the Free Software Foundation, either version 3 of the License, or
  16. * (at your option) any later version.
  17. *
  18. * This program is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU General Public License
  24. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  25. *
  26. * $Id$
  27. *
  28. *****************************************************************************/
  29. #include <stdarg.h>
  30. #include "common.h"
  31. #include "utils_base.h"
  32. void
  33. die (int result, const char *fmt, ...)
  34. {
  35. va_list ap;
  36. va_start (ap, fmt);
  37. vprintf (fmt, ap);
  38. va_end (ap);
  39. exit (result);
  40. }
  41. void set_range_start (range *this, double value) {
  42. this->start = value;
  43. this->start_infinity = FALSE;
  44. }
  45. void set_range_end (range *this, double value) {
  46. this->end = value;
  47. this->end_infinity = FALSE;
  48. }
  49. range
  50. *parse_range_string (char *str) {
  51. range *temp_range;
  52. double start;
  53. double end;
  54. char *end_str;
  55. temp_range = (range *) malloc(sizeof(range));
  56. /* Set defaults */
  57. temp_range->start = 0;
  58. temp_range->start_infinity = FALSE;
  59. temp_range->end = 0;
  60. temp_range->end_infinity = TRUE;
  61. temp_range->alert_on = OUTSIDE;
  62. if (str[0] == '@') {
  63. temp_range->alert_on = INSIDE;
  64. str++;
  65. }
  66. end_str = index(str, ':');
  67. if (end_str != NULL) {
  68. if (str[0] == '~') {
  69. temp_range->start_infinity = TRUE;
  70. } else {
  71. start = strtod(str, NULL); /* Will stop at the ':' */
  72. set_range_start(temp_range, start);
  73. }
  74. end_str++; /* Move past the ':' */
  75. } else {
  76. end_str = str;
  77. }
  78. end = strtod(end_str, NULL);
  79. if (strcmp(end_str, "") != 0) {
  80. set_range_end(temp_range, end);
  81. }
  82. if (temp_range->start_infinity == TRUE ||
  83. temp_range->end_infinity == TRUE ||
  84. temp_range->start <= temp_range->end) {
  85. return temp_range;
  86. }
  87. free(temp_range);
  88. return NULL;
  89. }
  90. /* returns 0 if okay, otherwise 1 */
  91. int
  92. _set_thresholds(thresholds **my_thresholds, char *warn_string, char *critical_string)
  93. {
  94. thresholds *temp_thresholds = NULL;
  95. temp_thresholds = malloc(sizeof(temp_thresholds));
  96. temp_thresholds->warning = NULL;
  97. temp_thresholds->critical = NULL;
  98. if (warn_string != NULL) {
  99. if ((temp_thresholds->warning = parse_range_string(warn_string)) == NULL) {
  100. return NP_RANGE_UNPARSEABLE;
  101. }
  102. }
  103. if (critical_string != NULL) {
  104. if ((temp_thresholds->critical = parse_range_string(critical_string)) == NULL) {
  105. return NP_RANGE_UNPARSEABLE;
  106. }
  107. }
  108. if (*my_thresholds > 0) { /* Not sure why, but sometimes could be -1 */
  109. /* printf("Freeing here: %d\n", *my_thresholds); */
  110. free(*my_thresholds);
  111. }
  112. *my_thresholds = temp_thresholds;
  113. return 0;
  114. }
  115. void
  116. set_thresholds(thresholds **my_thresholds, char *warn_string, char *critical_string)
  117. {
  118. switch (_set_thresholds(my_thresholds, warn_string, critical_string)) {
  119. case 0:
  120. return;
  121. case NP_RANGE_UNPARSEABLE:
  122. die(STATE_UNKNOWN, _("Range format incorrect"));
  123. case NP_WARN_WITHIN_CRIT:
  124. die(STATE_UNKNOWN, _("Warning level is a subset of critical and will not be alerted"));
  125. break;
  126. }
  127. }
  128. void print_thresholds(const char *threshold_name, thresholds *my_threshold) {
  129. printf("%s - ", threshold_name);
  130. if (! my_threshold) {
  131. printf("Threshold not set");
  132. } else {
  133. if (my_threshold->warning) {
  134. printf("Warning: start=%g end=%g; ", my_threshold->warning->start, my_threshold->warning->end);
  135. } else {
  136. printf("Warning not set; ");
  137. }
  138. if (my_threshold->critical) {
  139. printf("Critical: start=%g end=%g", my_threshold->critical->start, my_threshold->critical->end);
  140. } else {
  141. printf("Critical not set");
  142. }
  143. }
  144. printf("\n");
  145. }
  146. /* Returns TRUE if alert should be raised based on the range */
  147. int
  148. check_range(double value, range *my_range)
  149. {
  150. int no = FALSE;
  151. int yes = TRUE;
  152. if (my_range->alert_on == INSIDE) {
  153. no = TRUE;
  154. yes = FALSE;
  155. }
  156. if (my_range->end_infinity == FALSE && my_range->start_infinity == FALSE) {
  157. if ((my_range->start <= value) && (value <= my_range->end)) {
  158. return no;
  159. } else {
  160. return yes;
  161. }
  162. } else if (my_range->start_infinity == FALSE && my_range->end_infinity == TRUE) {
  163. if (my_range->start <= value) {
  164. return no;
  165. } else {
  166. return yes;
  167. }
  168. } else if (my_range->start_infinity == TRUE && my_range->end_infinity == FALSE) {
  169. if (value <= my_range->end) {
  170. return no;
  171. } else {
  172. return yes;
  173. }
  174. } else {
  175. return no;
  176. }
  177. }
  178. /* Returns status */
  179. int
  180. get_status(double value, thresholds *my_thresholds)
  181. {
  182. if (my_thresholds->critical != NULL) {
  183. if (check_range(value, my_thresholds->critical) == TRUE) {
  184. return STATE_CRITICAL;
  185. }
  186. }
  187. if (my_thresholds->warning != NULL) {
  188. if (check_range(value, my_thresholds->warning) == TRUE) {
  189. return STATE_WARNING;
  190. }
  191. }
  192. return STATE_OK;
  193. }
  194. char *np_escaped_string (const char *string) {
  195. char *data;
  196. int i, j=0;
  197. data = strdup(string);
  198. for (i=0; data[i]; i++) {
  199. if (data[i] == '\\') {
  200. switch(data[++i]) {
  201. case 'n':
  202. data[j++] = '\n';
  203. break;
  204. case 'r':
  205. data[j++] = '\r';
  206. break;
  207. case 't':
  208. data[j++] = '\t';
  209. break;
  210. case '\\':
  211. data[j++] = '\\';
  212. break;
  213. default:
  214. data[j++] = data[i];
  215. }
  216. } else {
  217. data[j++] = data[i];
  218. }
  219. }
  220. data[j] = '\0';
  221. return data;
  222. }
  223. int np_check_if_root(void) { return (geteuid() == 0); }
  224. int np_warn_if_not_root(void) {
  225. int status = np_check_if_root();
  226. if(!status) {
  227. printf(_("Warning: "));
  228. printf(_("This plugin must be either run as root or setuid root.\n"));
  229. printf(_("To run as root, you can use a tool like sudo.\n"));
  230. printf(_("To set the setuid permissions, use the command:\n"));
  231. /* XXX could we use something like progname? */
  232. printf("\tchmod u+s yourpluginfile\n");
  233. }
  234. return status;
  235. }