utils.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618
  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. #include "config.h"
  14. #include "common.h"
  15. #include <stdarg.h>
  16. #include <limits.h>
  17. #include <arpa/inet.h>
  18. extern int timeout_interval;
  19. extern const char *progname;
  20. void support (void);
  21. char *clean_revstring (const char *);
  22. void print_revision (const char *, const char *);
  23. void terminate (int, const char *fmt, ...);
  24. RETSIGTYPE timeout_alarm_handler (int);
  25. int is_host (char *);
  26. int is_addr (char *);
  27. int resolve_host_or_addr (char *, int);
  28. int is_inet_addr (char *);
  29. #ifdef USE_IPV6
  30. int is_inet6_addr (char *);
  31. #endif
  32. int is_hostname (char *);
  33. int is_integer (char *);
  34. int is_intpos (char *);
  35. int is_intneg (char *);
  36. int is_intnonneg (char *);
  37. int is_intpercent (char *);
  38. int is_numeric (char *);
  39. int is_positive (char *);
  40. int is_negative (char *);
  41. int is_nonnegative (char *);
  42. int is_percentage (char *);
  43. int is_option (char *str);
  44. double delta_time (struct timeval tv);
  45. void strip (char *);
  46. char *strscpy (char *dest, const char *src);
  47. char *strscat (char *dest, char *src);
  48. char *strnl (char *str);
  49. char *strpcpy (char *dest, const char *src, const char *str);
  50. char *strpcat (char *dest, const char *src, const char *str);
  51. #define LABELLEN 63
  52. #define STRLEN 64
  53. #define TXTBLK 128
  54. /* **************************************************************************
  55. /* max_state(STATE_x, STATE_y)
  56. * compares STATE_x to STATE_y and returns result based on the following
  57. * STATE_UNKNOWN < STATE_OK < STATE_WARNING < STATE_CRITICAL
  58. *
  59. * Note that numerically the above does not hold
  60. ****************************************************************************/
  61. #define max(a,b) (((a)>(b))?(a):(b))
  62. int
  63. max_state (int a, int b)
  64. {
  65. if (a == STATE_CRITICAL || b == STATE_CRITICAL)
  66. return STATE_CRITICAL;
  67. else if (a == STATE_WARNING || b == STATE_WARNING)
  68. return STATE_WARNING;
  69. else if (a == STATE_OK || b == STATE_OK)
  70. return STATE_OK;
  71. else if (a == STATE_UNKNOWN || b == STATE_UNKNOWN)
  72. return STATE_UNKNOWN;
  73. else if (a == STATE_DEPENDENT || b == STATE_DEPENDENT)
  74. return STATE_DEPENDENT;
  75. else
  76. return max (a, b);
  77. }
  78. void usage (char *msg)
  79. {
  80. printf (msg);
  81. print_usage ();
  82. exit (STATE_UNKNOWN);
  83. }
  84. void usage2(char *msg, char *arg)
  85. {
  86. printf ("%s: %s - %s\n",progname,msg,arg);
  87. print_usage ();
  88. exit (STATE_UNKNOWN);
  89. }
  90. void
  91. usage3 (char *msg, char arg)
  92. {
  93. printf ("%s: %s - %c\n", progname, msg, arg);
  94. print_usage();
  95. exit (STATE_UNKNOWN);
  96. }
  97. void
  98. support (void)
  99. {
  100. printf
  101. ("Send email to nagios-users@lists.sourceforge.net if you have questions\n"
  102. "regarding use of this software. To submit patches or suggest improvements,\n"
  103. "send email to nagiosplug-devel@lists.sourceforge.net\n");
  104. }
  105. char *
  106. clean_revstring (const char *revstring)
  107. {
  108. char plugin_revision[STRLEN];
  109. if (sscanf (revstring,"$Revision: %[0-9.]",plugin_revision) == 1)
  110. return strscpy (NULL, plugin_revision);
  111. else
  112. return strscpy (NULL, "N/A");
  113. }
  114. void
  115. print_revision (const char *command_name, const char *revision_string)
  116. {
  117. char plugin_revision[STRLEN];
  118. if (sscanf (revision_string, "$Revision: %[0-9.]", plugin_revision) != 1)
  119. strncpy (plugin_revision, "N/A", STRLEN);
  120. printf ("%s (nagios-plugins %s) %s\n",
  121. progname, VERSION, plugin_revision);
  122. printf
  123. ("The nagios plugins come with ABSOLUTELY NO WARRANTY. You may redistribute\n"
  124. "copies of the plugins under the terms of the GNU General Public License.\n"
  125. "For more information about these matters, see the file named COPYING.\n");
  126. }
  127. void
  128. terminate (int result, const char *fmt, ...)
  129. {
  130. va_list ap;
  131. va_start (ap, fmt);
  132. vprintf (fmt, ap);
  133. va_end (ap);
  134. exit (result);
  135. }
  136. void
  137. timeout_alarm_handler (int signo)
  138. {
  139. if (signo == SIGALRM) {
  140. printf ("CRITICAL - Plugin timed out after %d seconds\n",
  141. timeout_interval);
  142. exit (STATE_CRITICAL);
  143. }
  144. }
  145. int
  146. is_host (char *address)
  147. {
  148. if (is_addr (address) || is_hostname (address))
  149. return (TRUE);
  150. return (FALSE);
  151. }
  152. int
  153. is_addr (char *address)
  154. {
  155. #ifdef USE_IPV6
  156. if (is_inet_addr (address) || is_inet6_addr (address))
  157. #else
  158. if (is_inet_addr (address))
  159. #endif
  160. return (TRUE);
  161. return (FALSE);
  162. }
  163. int
  164. resolve_host_or_addr (char *address, int family)
  165. {
  166. struct addrinfo hints;
  167. struct addrinfo *res;
  168. int retval;
  169. memset (&hints, 0, sizeof (hints));
  170. hints.ai_family = family;
  171. retval = getaddrinfo (address, NULL, &hints, &res);
  172. if (retval != 0)
  173. return FALSE;
  174. else {
  175. freeaddrinfo (res);
  176. return TRUE;
  177. }
  178. }
  179. int
  180. is_inet_addr (char *address)
  181. {
  182. return resolve_host_or_addr (address, AF_INET);
  183. }
  184. #ifdef USE_IPV6
  185. int
  186. is_inet6_addr (char *address)
  187. {
  188. return resolve_host_or_addr (address, AF_INET6);
  189. }
  190. #endif
  191. /* from RFC-1035
  192. *
  193. * The labels must follow the rules for ARPANET host names. They must
  194. * start with a letter, end with a letter or digit, and have as interior
  195. * characters only letters, digits, and hyphen. There are also some
  196. * restrictions on the length. Labels must be 63 characters or less. */
  197. int
  198. is_hostname (char *s1)
  199. {
  200. #ifdef USE_IPV6
  201. return resolve_host_or_addr (s1, AF_UNSPEC);
  202. #else
  203. return resolve_host_or_addr (s1, AF_INET);
  204. #endif
  205. }
  206. int
  207. is_numeric (char *number)
  208. {
  209. char tmp[1];
  210. float x;
  211. if (!number)
  212. return FALSE;
  213. else if (sscanf (number, "%f%c", &x, tmp) == 1)
  214. return TRUE;
  215. else
  216. return FALSE;
  217. }
  218. int
  219. is_positive (char *number)
  220. {
  221. if (is_numeric (number) && atof (number) > 0.0)
  222. return TRUE;
  223. else
  224. return FALSE;
  225. }
  226. int
  227. is_negative (char *number)
  228. {
  229. if (is_numeric (number) && atof (number) < 0.0)
  230. return TRUE;
  231. else
  232. return FALSE;
  233. }
  234. int
  235. is_nonnegative (char *number)
  236. {
  237. if (is_numeric (number) && atof (number) >= 0.0)
  238. return TRUE;
  239. else
  240. return FALSE;
  241. }
  242. int
  243. is_percentage (char *number)
  244. {
  245. int x;
  246. if (is_numeric (number) && (x = atof (number)) >= 0 && x <= 100)
  247. return TRUE;
  248. else
  249. return FALSE;
  250. }
  251. int
  252. is_integer (char *number)
  253. {
  254. long int n;
  255. if (!number || (strspn (number, "-0123456789 ") != strlen (number)))
  256. return FALSE;
  257. n = strtol (number, NULL, 10);
  258. if (errno != ERANGE && n >= INT_MIN && n <= INT_MAX)
  259. return TRUE;
  260. else
  261. return FALSE;
  262. }
  263. int
  264. is_intpos (char *number)
  265. {
  266. if (is_integer (number) && atoi (number) > 0)
  267. return TRUE;
  268. else
  269. return FALSE;
  270. }
  271. int
  272. is_intneg (char *number)
  273. {
  274. if (is_integer (number) && atoi (number) < 0)
  275. return TRUE;
  276. else
  277. return FALSE;
  278. }
  279. int
  280. is_intnonneg (char *number)
  281. {
  282. if (is_integer (number) && atoi (number) >= 0)
  283. return TRUE;
  284. else
  285. return FALSE;
  286. }
  287. int
  288. is_intpercent (char *number)
  289. {
  290. int i;
  291. if (is_integer (number) && (i = atoi (number)) >= 0 && i <= 100)
  292. return TRUE;
  293. else
  294. return FALSE;
  295. }
  296. int
  297. is_option (char *str)
  298. {
  299. if (!str)
  300. return FALSE;
  301. else if (strspn (str, "-") == 1 || strspn (str, "-") == 2)
  302. return TRUE;
  303. else
  304. return FALSE;
  305. }
  306. #ifdef NEED_GETTIMEOFDAY
  307. int
  308. gettimeofday (struct timeval *tv, struct timezone *tz)
  309. {
  310. tv->tv_usec = 0;
  311. tv->tv_sec = (long) time ((time_t) 0);
  312. }
  313. #endif
  314. double
  315. delta_time (struct timeval tv)
  316. {
  317. struct timeval now;
  318. gettimeofday (&now, NULL);
  319. return ((double)(now.tv_sec - tv.tv_sec) + (double)(now.tv_usec - tv.tv_usec) / (double)1000000);
  320. }
  321. void
  322. strip (char *buffer)
  323. {
  324. size_t x;
  325. int i;
  326. for (x = strlen (buffer); x >= 1; x--) {
  327. i = x - 1;
  328. if (buffer[i] == ' ' ||
  329. buffer[i] == '\r' || buffer[i] == '\n' || buffer[i] == '\t')
  330. buffer[i] = '\0';
  331. else
  332. break;
  333. }
  334. return;
  335. }
  336. /******************************************************************************
  337. *
  338. * Copies one string to another. Any previously existing data in
  339. * the destination string is lost.
  340. *
  341. * Example:
  342. *
  343. * char *str=NULL;
  344. * str = strscpy("This is a line of text with no trailing newline");
  345. *
  346. *****************************************************************************/
  347. char *
  348. strscpy (char *dest, const char *src)
  349. {
  350. if (src == NULL)
  351. return NULL;
  352. asprintf (&dest, "%s", src);
  353. return dest;
  354. }
  355. /******************************************************************************
  356. *
  357. * Concatenates one string to the end of another
  358. *
  359. * Given a pointer destination string, which may or may not already
  360. * hold some text, and a source string with additional text (possibly
  361. * NULL or empty), returns a pointer to a string that is the first
  362. * string with the second concatenated to it. Uses realloc to free
  363. * memory held by the dest argument if new storage space is required.
  364. *
  365. * Example:
  366. *
  367. * char *str=NULL;
  368. * str = strscpy("This is a line of text with no trailing newline");
  369. * str = strscat(str,"\n");
  370. *
  371. *****************************************************************************/
  372. char *
  373. strscat (char *dest, char *src)
  374. {
  375. if (dest == NULL)
  376. return src;
  377. if (src != NULL)
  378. asprintf (&dest, "%s%s", dest, src);
  379. return dest;
  380. }
  381. /******************************************************************************
  382. *
  383. * Returns a pointer to the next line of a multiline string buffer
  384. *
  385. * Given a pointer string, find the text following the next sequence
  386. * of \r and \n characters. This has the effect of skipping blank
  387. * lines as well
  388. *
  389. * Example:
  390. *
  391. * Given text as follows:
  392. *
  393. * ==============================
  394. * This
  395. * is
  396. * a
  397. *
  398. * multiline string buffer
  399. * ==============================
  400. *
  401. * int i=0;
  402. * char *str=NULL;
  403. * char *ptr=NULL;
  404. * str = strscpy(str,"This\nis\r\na\n\nmultiline string buffer\n");
  405. * ptr = str;
  406. * while (ptr) {
  407. * printf("%d %s",i++,firstword(ptr));
  408. * ptr = strnl(ptr);
  409. * }
  410. *
  411. * Produces the following:
  412. *
  413. * 1 This
  414. * 2 is
  415. * 3 a
  416. * 4 multiline
  417. *
  418. * NOTE: The 'firstword()' function is conceptual only and does not
  419. * exist in this package.
  420. *
  421. * NOTE: Although the second 'ptr' variable is not strictly needed in
  422. * this example, it is good practice with these utilities. Once
  423. * the * pointer is advance in this manner, it may no longer be
  424. * handled with * realloc(). So at the end of the code fragment
  425. * above, * strscpy(str,"foo") work perfectly fine, but
  426. * strscpy(ptr,"foo") will * cause the the program to crash with
  427. * a segmentation fault.
  428. *
  429. *****************************************************************************/
  430. char *
  431. strnl (char *str)
  432. {
  433. size_t len;
  434. if (str == NULL)
  435. return NULL;
  436. str = strpbrk (str, "\r\n");
  437. if (str == NULL)
  438. return NULL;
  439. len = strspn (str, "\r\n");
  440. if (str[len] == '\0')
  441. return NULL;
  442. str += len;
  443. if (strlen (str) == 0)
  444. return NULL;
  445. return str;
  446. }
  447. /******************************************************************************
  448. *
  449. * Like strscpy, except only the portion of the source string up to
  450. * the provided delimiter is copied.
  451. *
  452. * Example:
  453. *
  454. * str = strpcpy(str,"This is a line of text with no trailing newline","x");
  455. * printf("%s\n",str);
  456. *
  457. * Produces:
  458. *
  459. *This is a line of te
  460. *
  461. *****************************************************************************/
  462. char *
  463. strpcpy (char *dest, const char *src, const char *str)
  464. {
  465. size_t len;
  466. if (src)
  467. len = strcspn (src, str);
  468. else
  469. return NULL;
  470. if (dest == NULL || strlen (dest) < len)
  471. dest = realloc (dest, len + 1);
  472. if (dest == NULL)
  473. terminate (STATE_UNKNOWN, "failed realloc in strpcpy\n");
  474. strncpy (dest, src, len);
  475. dest[len] = '\0';
  476. return dest;
  477. }
  478. /******************************************************************************
  479. *
  480. * Like strscat, except only the portion of the source string up to
  481. * the provided delimiter is copied.
  482. *
  483. * str = strpcpy(str,"This is a line of text with no trailing newline","x");
  484. * str = strpcat(str,"This is a line of text with no trailing newline","x");
  485. * printf("%s\n",str);
  486. *
  487. *This is a line of texThis is a line of tex
  488. *
  489. *****************************************************************************/
  490. char *
  491. strpcat (char *dest, const char *src, const char *str)
  492. {
  493. size_t len, l2;
  494. if (dest)
  495. len = strlen (dest);
  496. else
  497. len = 0;
  498. if (src) {
  499. l2 = strcspn (src, str);
  500. }
  501. else {
  502. return dest;
  503. }
  504. dest = realloc (dest, len + l2 + 1);
  505. if (dest == NULL)
  506. terminate (STATE_UNKNOWN, "failed malloc in strscat\n");
  507. strncpy (dest + len, src, l2);
  508. dest[len + l2] = '\0';
  509. return dest;
  510. }