utils.c 12 KB

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