utils_base.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  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. *my_thresholds = temp_thresholds;
  109. return 0;
  110. }
  111. void
  112. set_thresholds(thresholds **my_thresholds, char *warn_string, char *critical_string)
  113. {
  114. switch (_set_thresholds(my_thresholds, warn_string, critical_string)) {
  115. case 0:
  116. return;
  117. case NP_RANGE_UNPARSEABLE:
  118. die(STATE_UNKNOWN, _("Range format incorrect"));
  119. case NP_WARN_WITHIN_CRIT:
  120. die(STATE_UNKNOWN, _("Warning level is a subset of critical and will not be alerted"));
  121. break;
  122. }
  123. }
  124. void print_thresholds(const char *threshold_name, thresholds *my_threshold) {
  125. printf("%s - ", threshold_name);
  126. if (! my_threshold) {
  127. printf("Threshold not set");
  128. } else {
  129. if (my_threshold->warning) {
  130. printf("Warning: start=%g end=%g; ", my_threshold->warning->start, my_threshold->warning->end);
  131. } else {
  132. printf("Warning not set; ");
  133. }
  134. if (my_threshold->critical) {
  135. printf("Critical: start=%g end=%g", my_threshold->critical->start, my_threshold->critical->end);
  136. } else {
  137. printf("Critical not set");
  138. }
  139. }
  140. printf("\n");
  141. }
  142. /* Returns TRUE if alert should be raised based on the range */
  143. int
  144. check_range(double value, range *my_range)
  145. {
  146. int no = FALSE;
  147. int yes = TRUE;
  148. if (my_range->alert_on == INSIDE) {
  149. no = TRUE;
  150. yes = FALSE;
  151. }
  152. if (my_range->end_infinity == FALSE && my_range->start_infinity == FALSE) {
  153. if ((my_range->start <= value) && (value <= my_range->end)) {
  154. return no;
  155. } else {
  156. return yes;
  157. }
  158. } else if (my_range->start_infinity == FALSE && my_range->end_infinity == TRUE) {
  159. if (my_range->start <= value) {
  160. return no;
  161. } else {
  162. return yes;
  163. }
  164. } else if (my_range->start_infinity == TRUE && my_range->end_infinity == FALSE) {
  165. if (value <= my_range->end) {
  166. return no;
  167. } else {
  168. return yes;
  169. }
  170. } else {
  171. return no;
  172. }
  173. }
  174. /* Returns status */
  175. int
  176. get_status(double value, thresholds *my_thresholds)
  177. {
  178. if (my_thresholds->critical != NULL) {
  179. if (check_range(value, my_thresholds->critical) == TRUE) {
  180. return STATE_CRITICAL;
  181. }
  182. }
  183. if (my_thresholds->warning != NULL) {
  184. if (check_range(value, my_thresholds->warning) == TRUE) {
  185. return STATE_WARNING;
  186. }
  187. }
  188. return STATE_OK;
  189. }
  190. char *np_escaped_string (const char *string) {
  191. char *data;
  192. int i, j=0;
  193. data = strdup(string);
  194. for (i=0; data[i]; i++) {
  195. if (data[i] == '\\') {
  196. switch(data[++i]) {
  197. case 'n':
  198. data[j++] = '\n';
  199. break;
  200. case 'r':
  201. data[j++] = '\r';
  202. break;
  203. case 't':
  204. data[j++] = '\t';
  205. break;
  206. case '\\':
  207. data[j++] = '\\';
  208. break;
  209. default:
  210. data[j++] = data[i];
  211. }
  212. } else {
  213. data[j++] = data[i];
  214. }
  215. }
  216. data[j] = '\0';
  217. return data;
  218. }
  219. int np_check_if_root(void) { return (geteuid() == 0); }
  220. int np_warn_if_not_root(void) {
  221. int status = np_check_if_root();
  222. if(!status) {
  223. printf(_("Warning: "));
  224. printf(_("This plugin must be either run as root or setuid root.\n"));
  225. printf(_("To run as root, you can use a tool like sudo.\n"));
  226. printf(_("To set the setuid permissions, use the command:\n"));
  227. /* XXX could we use something like progname? */
  228. printf("\tchmod u+s yourpluginfile\n");
  229. }
  230. return status;
  231. }