utils.c 13 KB

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