utils.c 13 KB

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