utils.c 12 KB

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