utils.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490
  1. /*****************************************************************************
  2. *
  3. * utils.c
  4. *
  5. * Library of useful functions for plugins
  6. *
  7. * Copyright (c) 2000 Karl DeBisschop (karl@debisschop.net)
  8. * License: GPL
  9. *
  10. * $Revision$
  11. * $Date$
  12. ****************************************************************************/
  13. #define LOCAL_TIMEOUT_ALARM_HANDLER
  14. #include "config.h"
  15. #include "common.h"
  16. #include "utils.h"
  17. #include <stdarg.h>
  18. #include <limits.h>
  19. #include <arpa/inet.h>
  20. extern void print_usage (void);
  21. extern const char *progname;
  22. #define STRLEN 64
  23. #define TXTBLK 128
  24. /* **************************************************************************
  25. * max_state(STATE_x, STATE_y)
  26. * compares STATE_x to STATE_y and returns result based on the following
  27. * STATE_UNKNOWN < STATE_OK < STATE_WARNING < STATE_CRITICAL
  28. *
  29. * Note that numerically the above does not hold
  30. ****************************************************************************/
  31. int
  32. max_state (int a, int b)
  33. {
  34. if (a == STATE_CRITICAL || b == STATE_CRITICAL)
  35. return STATE_CRITICAL;
  36. else if (a == STATE_WARNING || b == STATE_WARNING)
  37. return STATE_WARNING;
  38. else if (a == STATE_OK || b == STATE_OK)
  39. return STATE_OK;
  40. else if (a == STATE_UNKNOWN || b == STATE_UNKNOWN)
  41. return STATE_UNKNOWN;
  42. else if (a == STATE_DEPENDENT || b == STATE_DEPENDENT)
  43. return STATE_DEPENDENT;
  44. else
  45. return max (a, b);
  46. }
  47. void usage (char *msg)
  48. {
  49. printf (msg);
  50. print_usage ();
  51. exit (STATE_UNKNOWN);
  52. }
  53. void usage2(char *msg, char *arg)
  54. {
  55. printf ("%s: %s - %s\n",progname,msg,arg);
  56. print_usage ();
  57. exit (STATE_UNKNOWN);
  58. }
  59. void
  60. usage3 (char *msg, int arg)
  61. {
  62. printf ("%s: %s - %c\n", progname, msg, arg);
  63. print_usage();
  64. exit (STATE_UNKNOWN);
  65. }
  66. void
  67. support (void)
  68. {
  69. printf (_("\n\
  70. Send email to nagios-users@lists.sourceforge.net if you have questions\n\
  71. regarding use of this software. To submit patches or suggest improvements,\n\
  72. send email to nagiosplug-devel@lists.sourceforge.net\n"));
  73. }
  74. char *
  75. clean_revstring (const char *revstring)
  76. {
  77. char plugin_revision[STRLEN];
  78. if (sscanf (revstring,"$Revision: %[0-9.]",plugin_revision) == 1)
  79. return strscpy (NULL, plugin_revision);
  80. else
  81. return strscpy (NULL, "N/A");
  82. }
  83. void
  84. print_revision (const char *command_name, const char *revision_string)
  85. {
  86. char plugin_revision[STRLEN];
  87. if (sscanf (revision_string, "$Revision: %[0-9.]", plugin_revision) != 1)
  88. strncpy (plugin_revision, "N/A", STRLEN);
  89. printf ("%s (%s %s) %s\n",
  90. command_name, PACKAGE, VERSION, plugin_revision);
  91. printf (_("\
  92. The nagios plugins come with ABSOLUTELY NO WARRANTY. You may redistribute\n\
  93. copies of the plugins under the terms of the GNU General Public License.\n\
  94. For more information about these matters, see the file named COPYING.\n"));
  95. }
  96. const char *
  97. state_text (int result)
  98. {
  99. switch (result) {
  100. case STATE_OK:
  101. return "OK";
  102. case STATE_WARNING:
  103. return "WARNING";
  104. case STATE_CRITICAL:
  105. return "CRITICAL";
  106. case STATE_DEPENDENT:
  107. return "DEPENDENT";
  108. default:
  109. return "UNKNOWN";
  110. }
  111. }
  112. void
  113. die (int result, const char *fmt, ...)
  114. {
  115. va_list ap;
  116. va_start (ap, fmt);
  117. vprintf (fmt, ap);
  118. va_end (ap);
  119. exit (result);
  120. }
  121. void
  122. timeout_alarm_handler (int signo)
  123. {
  124. if (signo == SIGALRM) {
  125. printf ("CRITICAL - Plugin timed out after %d seconds\n",
  126. timeout_interval);
  127. exit (STATE_CRITICAL);
  128. }
  129. }
  130. int
  131. is_numeric (char *number)
  132. {
  133. char tmp[1];
  134. float x;
  135. if (!number)
  136. return FALSE;
  137. else if (sscanf (number, "%f%c", &x, tmp) == 1)
  138. return TRUE;
  139. else
  140. return FALSE;
  141. }
  142. int
  143. is_positive (char *number)
  144. {
  145. if (is_numeric (number) && atof (number) > 0.0)
  146. return TRUE;
  147. else
  148. return FALSE;
  149. }
  150. int
  151. is_negative (char *number)
  152. {
  153. if (is_numeric (number) && atof (number) < 0.0)
  154. return TRUE;
  155. else
  156. return FALSE;
  157. }
  158. int
  159. is_nonnegative (char *number)
  160. {
  161. if (is_numeric (number) && atof (number) >= 0.0)
  162. return TRUE;
  163. else
  164. return FALSE;
  165. }
  166. int
  167. is_percentage (char *number)
  168. {
  169. int x;
  170. if (is_numeric (number) && (x = atof (number)) >= 0 && x <= 100)
  171. return TRUE;
  172. else
  173. return FALSE;
  174. }
  175. int
  176. is_integer (char *number)
  177. {
  178. long int n;
  179. if (!number || (strspn (number, "-0123456789 ") != strlen (number)))
  180. return FALSE;
  181. n = strtol (number, NULL, 10);
  182. if (errno != ERANGE && n >= INT_MIN && n <= INT_MAX)
  183. return TRUE;
  184. else
  185. return FALSE;
  186. }
  187. int
  188. is_intpos (char *number)
  189. {
  190. if (is_integer (number) && atoi (number) > 0)
  191. return TRUE;
  192. else
  193. return FALSE;
  194. }
  195. int
  196. is_intneg (char *number)
  197. {
  198. if (is_integer (number) && atoi (number) < 0)
  199. return TRUE;
  200. else
  201. return FALSE;
  202. }
  203. int
  204. is_intnonneg (char *number)
  205. {
  206. if (is_integer (number) && atoi (number) >= 0)
  207. return TRUE;
  208. else
  209. return FALSE;
  210. }
  211. int
  212. is_intpercent (char *number)
  213. {
  214. int i;
  215. if (is_integer (number) && (i = atoi (number)) >= 0 && i <= 100)
  216. return TRUE;
  217. else
  218. return FALSE;
  219. }
  220. int
  221. is_option (char *str)
  222. {
  223. if (!str)
  224. return FALSE;
  225. else if (strspn (str, "-") == 1 || strspn (str, "-") == 2)
  226. return TRUE;
  227. else
  228. return FALSE;
  229. }
  230. #ifdef NEED_GETTIMEOFDAY
  231. int
  232. gettimeofday (struct timeval *tv, struct timezone *tz)
  233. {
  234. tv->tv_usec = 0;
  235. tv->tv_sec = (long) time ((time_t) 0);
  236. }
  237. #endif
  238. double
  239. delta_time (struct timeval tv)
  240. {
  241. struct timeval now;
  242. gettimeofday (&now, NULL);
  243. return ((double)(now.tv_sec - tv.tv_sec) + (double)(now.tv_usec - tv.tv_usec) / (double)1000000);
  244. }
  245. void
  246. strip (char *buffer)
  247. {
  248. size_t x;
  249. int i;
  250. for (x = strlen (buffer); x >= 1; x--) {
  251. i = x - 1;
  252. if (buffer[i] == ' ' ||
  253. buffer[i] == '\r' || buffer[i] == '\n' || buffer[i] == '\t')
  254. buffer[i] = '\0';
  255. else
  256. break;
  257. }
  258. return;
  259. }
  260. /******************************************************************************
  261. *
  262. * Copies one string to another. Any previously existing data in
  263. * the destination string is lost.
  264. *
  265. * Example:
  266. *
  267. * char *str=NULL;
  268. * str = strscpy("This is a line of text with no trailing newline");
  269. *
  270. *****************************************************************************/
  271. char *
  272. strscpy (char *dest, const char *src)
  273. {
  274. if (src == NULL)
  275. return NULL;
  276. asprintf (&dest, "%s", src);
  277. return dest;
  278. }
  279. /******************************************************************************
  280. *
  281. * Returns a pointer to the next line of a multiline string buffer
  282. *
  283. * Given a pointer string, find the text following the next sequence
  284. * of \r and \n characters. This has the effect of skipping blank
  285. * lines as well
  286. *
  287. * Example:
  288. *
  289. * Given text as follows:
  290. *
  291. * ==============================
  292. * This
  293. * is
  294. * a
  295. *
  296. * multiline string buffer
  297. * ==============================
  298. *
  299. * int i=0;
  300. * char *str=NULL;
  301. * char *ptr=NULL;
  302. * str = strscpy(str,"This\nis\r\na\n\nmultiline string buffer\n");
  303. * ptr = str;
  304. * while (ptr) {
  305. * printf("%d %s",i++,firstword(ptr));
  306. * ptr = strnl(ptr);
  307. * }
  308. *
  309. * Produces the following:
  310. *
  311. * 1 This
  312. * 2 is
  313. * 3 a
  314. * 4 multiline
  315. *
  316. * NOTE: The 'firstword()' function is conceptual only and does not
  317. * exist in this package.
  318. *
  319. * NOTE: Although the second 'ptr' variable is not strictly needed in
  320. * this example, it is good practice with these utilities. Once
  321. * the * pointer is advance in this manner, it may no longer be
  322. * handled with * realloc(). So at the end of the code fragment
  323. * above, * strscpy(str,"foo") work perfectly fine, but
  324. * strscpy(ptr,"foo") will * cause the the program to crash with
  325. * a segmentation fault.
  326. *
  327. *****************************************************************************/
  328. char *
  329. strnl (char *str)
  330. {
  331. size_t len;
  332. if (str == NULL)
  333. return NULL;
  334. str = strpbrk (str, "\r\n");
  335. if (str == NULL)
  336. return NULL;
  337. len = strspn (str, "\r\n");
  338. if (str[len] == '\0')
  339. return NULL;
  340. str += len;
  341. if (strlen (str) == 0)
  342. return NULL;
  343. return str;
  344. }
  345. /******************************************************************************
  346. *
  347. * Like strscpy, except only the portion of the source string up to
  348. * the provided delimiter is copied.
  349. *
  350. * Example:
  351. *
  352. * str = strpcpy(str,"This is a line of text with no trailing newline","x");
  353. * printf("%s\n",str);
  354. *
  355. * Produces:
  356. *
  357. *This is a line of te
  358. *
  359. *****************************************************************************/
  360. char *
  361. strpcpy (char *dest, const char *src, const char *str)
  362. {
  363. size_t len;
  364. if (src)
  365. len = strcspn (src, str);
  366. else
  367. return NULL;
  368. if (dest == NULL || strlen (dest) < len)
  369. dest = realloc (dest, len + 1);
  370. if (dest == NULL)
  371. die (STATE_UNKNOWN, "failed realloc in strpcpy\n");
  372. strncpy (dest, src, len);
  373. dest[len] = '\0';
  374. return dest;
  375. }
  376. /******************************************************************************
  377. *
  378. * Like strscat, except only the portion of the source string up to
  379. * the provided delimiter is copied.
  380. *
  381. * str = strpcpy(str,"This is a line of text with no trailing newline","x");
  382. * str = strpcat(str,"This is a line of text with no trailing newline","x");
  383. * printf("%s\n",str);
  384. *
  385. *This is a line of texThis is a line of tex
  386. *
  387. *****************************************************************************/
  388. char *
  389. strpcat (char *dest, const char *src, const char *str)
  390. {
  391. size_t len, l2;
  392. if (dest)
  393. len = strlen (dest);
  394. else
  395. len = 0;
  396. if (src) {
  397. l2 = strcspn (src, str);
  398. }
  399. else {
  400. return dest;
  401. }
  402. dest = realloc (dest, len + l2 + 1);
  403. if (dest == NULL)
  404. die (STATE_UNKNOWN, "failed malloc in strscat\n");
  405. strncpy (dest + len, src, l2);
  406. dest[len + l2] = '\0';
  407. return dest;
  408. }