utils.c 12 KB

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