utils.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648
  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. void
  271. strip (char *buffer)
  272. {
  273. size_t x;
  274. int i;
  275. for (x = strlen (buffer); x >= 1; x--) {
  276. i = x - 1;
  277. if (buffer[i] == ' ' ||
  278. buffer[i] == '\r' || buffer[i] == '\n' || buffer[i] == '\t')
  279. buffer[i] = '\0';
  280. else
  281. break;
  282. }
  283. return;
  284. }
  285. /******************************************************************************
  286. *
  287. * Copies one string to another
  288. *
  289. * Given a pointer destination string, which may or may not already
  290. * hold some text, and a source string with additional text (possibly
  291. * NULL or empty), returns a pointer to a a copy of the source
  292. * string. Uses realloc to free memory held by the dest argument if
  293. * new storage space is required, and any previously existing data in
  294. * the destination string is lost.
  295. *
  296. * Example:
  297. *
  298. * char *str=NULL;
  299. * str = strscpy("This is a line of text with no trailing newline");
  300. *
  301. *****************************************************************************/
  302. char *
  303. strscpy (char *dest, const char *src)
  304. {
  305. size_t len;
  306. if (src == NULL)
  307. return dest;
  308. len = strlen (src) + 1;
  309. if (dest == NULL)
  310. dest = malloc (len);
  311. else if (strlen (dest) < len)
  312. dest = realloc (dest, len);
  313. if (dest == NULL)
  314. terminate (STATE_UNKNOWN, "failed realloc in strscpy\n");
  315. strncpy (dest, src, len);
  316. return dest;
  317. }
  318. /******************************************************************************
  319. *
  320. * Concatenates one string to the end of another
  321. *
  322. * Given a pointer destination string, which may or may not already
  323. * hold some text, and a source string with additional text (possibly
  324. * NULL or empty), returns a pointer to a string that is the first
  325. * string with the second concatenated to it. Uses realloc to free
  326. * memory held by the dest argument if new storage space is required.
  327. *
  328. * Example:
  329. *
  330. * char *str=NULL;
  331. * str = strscpy("This is a line of text with no trailing newline");
  332. * str = strscat(str,"\n");
  333. *
  334. *****************************************************************************/
  335. char *
  336. strscat (char *dest, const char *src)
  337. {
  338. size_t len, l2;
  339. if (src)
  340. l2 = strlen (src);
  341. else
  342. return dest;
  343. if (dest)
  344. len = strlen (dest);
  345. else
  346. len = 0;
  347. dest = realloc (dest, len + l2 + 1);
  348. if (dest == NULL)
  349. terminate (STATE_UNKNOWN, "failed malloc in strscat\n");
  350. strncpy (dest + len, src, l2);
  351. dest[len + l2] = '\0';
  352. return dest;
  353. }
  354. /******************************************************************************
  355. *
  356. * Returns a pointer to the next line of a multiline string buffer
  357. *
  358. * Given a pointer string, find the text following the next sequence
  359. * of \r and \n characters. This has the effect of skipping blank
  360. * lines as well
  361. *
  362. * Example:
  363. *
  364. * Given text as follows:
  365. *
  366. * ==============================
  367. * This
  368. * is
  369. * a
  370. *
  371. * multiline string buffer
  372. * ==============================
  373. *
  374. * int i=0;
  375. * char *str=NULL;
  376. * char *ptr=NULL;
  377. * str = strscpy(str,"This\nis\r\na\n\nmultiline string buffer\n");
  378. * ptr = str;
  379. * while (ptr) {
  380. * printf("%d %s",i++,firstword(ptr));
  381. * ptr = strnl(ptr);
  382. * }
  383. *
  384. * Produces the following:
  385. *
  386. * 1 This
  387. * 2 is
  388. * 3 a
  389. * 4 multiline
  390. *
  391. * NOTE: The 'firstword()' function is conceptual only and does not
  392. * exist in this package.
  393. *
  394. * NOTE: Although the second 'ptr' variable is not strictly needed in
  395. * this example, it is good practice with these utilities. Once
  396. * the * pointer is advance in this manner, it may no longer be
  397. * handled with * realloc(). So at the end of the code fragment
  398. * above, * strscpy(str,"foo") work perfectly fine, but
  399. * strscpy(ptr,"foo") will * cause the the program to crash with
  400. * a segmentation fault.
  401. *
  402. *****************************************************************************/
  403. char *
  404. strnl (char *str)
  405. {
  406. size_t len;
  407. if (str == NULL)
  408. return NULL;
  409. str = strpbrk (str, "\r\n");
  410. if (str == NULL)
  411. return NULL;
  412. len = strspn (str, "\r\n");
  413. if (str[len] == '\0')
  414. return NULL;
  415. str += len;
  416. if (strlen (str) == 0)
  417. return NULL;
  418. return str;
  419. }
  420. /******************************************************************************
  421. *
  422. * Does a formatted print to a string variable
  423. *
  424. * Given a pointer destination string, which may or may not already
  425. * hold some text, and a source string with additional text (possibly
  426. * NULL or empty), returns a pointer to a string that cntains the
  427. * results of the specified formatted print
  428. *
  429. * Example:
  430. *
  431. * char *str=NULL;
  432. * str = ssprintf(str,"%d %s",1,"string");
  433. *
  434. *****************************************************************************/
  435. char *
  436. ssprintf (char *ptr, const char *fmt, ...)
  437. {
  438. va_list ap;
  439. int nchars;
  440. size_t size;
  441. char *str = NULL;
  442. if (str == NULL) {
  443. str = malloc (TXTBLK);
  444. if (str == NULL)
  445. terminate (STATE_UNKNOWN, "malloc failed in ssprintf");
  446. size = TXTBLK;
  447. }
  448. else
  449. size = max (strlen (str), TXTBLK);
  450. va_start (ap, fmt);
  451. while (1) {
  452. nchars = vsnprintf (str, size, fmt, ap);
  453. if (nchars > -1)
  454. if (nchars < (int) size) {
  455. va_end (ap);
  456. str[nchars] = '\0';
  457. if (ptr)
  458. free (ptr);
  459. return str;
  460. }
  461. else {
  462. size = (size_t) (nchars + 1);
  463. }
  464. else
  465. size *= 2;
  466. str = realloc (str, size);
  467. if (str == NULL)
  468. terminate (STATE_UNKNOWN, "realloc failed in ssprintf");
  469. }
  470. }
  471. /******************************************************************************
  472. *
  473. * Like strscpy, except only the portion of the source string up to
  474. * the provided delimiter is copied.
  475. *
  476. * Example:
  477. *
  478. * str = strpcpy(str,"This is a line of text with no trailing newline","x");
  479. * printf("%s\n",str);
  480. *
  481. * Produces:
  482. *
  483. *This is a line of te
  484. *
  485. *****************************************************************************/
  486. char *
  487. strpcpy (char *dest, const char *src, const char *str)
  488. {
  489. size_t len;
  490. if (src)
  491. len = strcspn (src, str);
  492. else
  493. return NULL;
  494. if (dest == NULL || strlen (dest) < len)
  495. dest = realloc (dest, len + 1);
  496. if (dest == NULL)
  497. terminate (STATE_UNKNOWN, "failed realloc in strpcpy\n");
  498. strncpy (dest, src, len);
  499. dest[len] = '\0';
  500. return dest;
  501. }
  502. /******************************************************************************
  503. *
  504. * Like strscat, except only the portion of the source string up to
  505. * the provided delimiter is copied.
  506. *
  507. * str = strpcpy(str,"This is a line of text with no trailing newline","x");
  508. * str = strpcat(str,"This is a line of text with no trailing newline","x");
  509. * printf("%s\n",str);
  510. *
  511. *This is a line of texThis is a line of tex
  512. *
  513. *****************************************************************************/
  514. char *
  515. strpcat (char *dest, const char *src, const char *str)
  516. {
  517. size_t len, l2;
  518. if (dest)
  519. len = strlen (dest);
  520. else
  521. len = 0;
  522. if (src) {
  523. l2 = strcspn (src, str);
  524. }
  525. else {
  526. return dest;
  527. }
  528. dest = realloc (dest, len + l2 + 1);
  529. if (dest == NULL)
  530. terminate (STATE_UNKNOWN, "failed malloc in strscat\n");
  531. strncpy (dest + len, src, l2);
  532. dest[len + l2] = '\0';
  533. return dest;
  534. }