utils.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562
  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 (sscanf (address, "%d.%d.%d.%d%c", &o1, &o2, &o3, &o4, c) != 4)
  142. return FALSE;
  143. else if (o1 > 255 || o2 > 255 || o3 > 255 || o4 > 255)
  144. return FALSE;
  145. else if (o1 < 0 || o2 < 0 || o3 < 0 || o4 < 0)
  146. return FALSE;
  147. else
  148. return TRUE;
  149. }
  150. /* from RFC-1035
  151. *
  152. * The labels must follow the rules for ARPANET host names. They must
  153. * start with a letter, end with a letter or digit, and have as interior
  154. * characters only letters, digits, and hyphen. There are also some
  155. * restrictions on the length. Labels must be 63 characters or less. */
  156. int
  157. is_hostname (char *s1)
  158. {
  159. if (strlen (s1) > 63)
  160. return FALSE;
  161. if (strcspn
  162. (s1,
  163. "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUWVXYZ0123456789-.") !=
  164. 0) return FALSE;
  165. if (strspn (s1, "0123456789-.") == 1)
  166. return FALSE;
  167. while ((s1 = index (s1, '.'))) {
  168. s1++;
  169. if (strspn (s1, "0123456789-.") == 1) {
  170. printf ("%s\n", s1);
  171. return FALSE;
  172. }
  173. }
  174. return TRUE;
  175. }
  176. int
  177. is_numeric (char *number)
  178. {
  179. char tmp[1];
  180. float x;
  181. if (sscanf (number, "%f%c", &x, tmp) == 1)
  182. return (TRUE);
  183. return (FALSE);
  184. }
  185. int
  186. is_positive (char *number)
  187. {
  188. if (is_numeric (number) && atof (number) > 0.0)
  189. return (TRUE);
  190. return (FALSE);
  191. }
  192. int
  193. is_negative (char *number)
  194. {
  195. if (is_numeric (number) && atof (number) < 0.0)
  196. return (TRUE);
  197. return (FALSE);
  198. }
  199. int
  200. is_nonnegative (char *number)
  201. {
  202. if (is_numeric (number) && atof (number) >= 0.0)
  203. return (TRUE);
  204. return (FALSE);
  205. }
  206. int
  207. is_percentage (char *number)
  208. {
  209. int x;
  210. if (is_numeric (number) && (x = atof (number)) >= 0 && x <= 100)
  211. return (TRUE);
  212. return (FALSE);
  213. }
  214. int
  215. is_integer (char *number)
  216. {
  217. long int n;
  218. if (strspn (number, "-0123456789 ") != strlen (number))
  219. return (FALSE);
  220. n = strtol (number, NULL, 10);
  221. if (errno != ERANGE && n >= INT_MIN && n <= INT_MAX)
  222. return (TRUE);
  223. return (FALSE);
  224. }
  225. int
  226. is_intpos (char *number)
  227. {
  228. if (is_integer (number) && atoi (number) > 0)
  229. return (TRUE);
  230. return (FALSE);
  231. }
  232. int
  233. is_intneg (char *number)
  234. {
  235. if (is_integer (number) && atoi (number) < 0)
  236. return (TRUE);
  237. return (FALSE);
  238. }
  239. int
  240. is_intnonneg (char *number)
  241. {
  242. if (is_integer (number) && atoi (number) >= 0)
  243. return (TRUE);
  244. return (FALSE);
  245. }
  246. int
  247. is_intpercent (char *number)
  248. {
  249. int i;
  250. if (is_integer (number) && (i = atoi (number)) >= 0 && i <= 100)
  251. return (TRUE);
  252. return (FALSE);
  253. }
  254. int
  255. is_option (char *str)
  256. {
  257. if (strspn (str, "-") == 1 || strspn (str, "-") == 2)
  258. return TRUE;
  259. return FALSE;
  260. }
  261. #ifdef NEED_GETTIMEOFDAY
  262. int
  263. gettimeofday (struct timeval *tv, struct timezone *tz)
  264. {
  265. tv->tv_usec = 0;
  266. tv->tv_sec = (long) time ((time_t) 0);
  267. }
  268. #endif
  269. double
  270. delta_time (struct timeval tv)
  271. {
  272. struct timeval now;
  273. gettimeofday (&now, NULL);
  274. return ((double)(now.tv_sec - tv.tv_sec) + (double)(now.tv_usec - tv.tv_usec) / (double)1000000);
  275. }
  276. void
  277. strip (char *buffer)
  278. {
  279. size_t x;
  280. int i;
  281. for (x = strlen (buffer); x >= 1; x--) {
  282. i = x - 1;
  283. if (buffer[i] == ' ' ||
  284. buffer[i] == '\r' || buffer[i] == '\n' || buffer[i] == '\t')
  285. buffer[i] = '\0';
  286. else
  287. break;
  288. }
  289. return;
  290. }
  291. /******************************************************************************
  292. *
  293. * Copies one string to another. 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. if (src == NULL)
  306. return NULL;
  307. asprintf (&dest, "%s", src);
  308. return dest;
  309. }
  310. /******************************************************************************
  311. *
  312. * Concatenates one string to the end of another
  313. *
  314. * Given a pointer destination string, which may or may not already
  315. * hold some text, and a source string with additional text (possibly
  316. * NULL or empty), returns a pointer to a string that is the first
  317. * string with the second concatenated to it. Uses realloc to free
  318. * memory held by the dest argument if new storage space is required.
  319. *
  320. * Example:
  321. *
  322. * char *str=NULL;
  323. * str = strscpy("This is a line of text with no trailing newline");
  324. * str = strscat(str,"\n");
  325. *
  326. *****************************************************************************/
  327. char *
  328. strscat (char *dest, char *src)
  329. {
  330. if (dest == NULL)
  331. return src;
  332. if (src != NULL)
  333. asprintf (&dest, "%s%s", dest, src);
  334. return dest;
  335. }
  336. /******************************************************************************
  337. *
  338. * Returns a pointer to the next line of a multiline string buffer
  339. *
  340. * Given a pointer string, find the text following the next sequence
  341. * of \r and \n characters. This has the effect of skipping blank
  342. * lines as well
  343. *
  344. * Example:
  345. *
  346. * Given text as follows:
  347. *
  348. * ==============================
  349. * This
  350. * is
  351. * a
  352. *
  353. * multiline string buffer
  354. * ==============================
  355. *
  356. * int i=0;
  357. * char *str=NULL;
  358. * char *ptr=NULL;
  359. * str = strscpy(str,"This\nis\r\na\n\nmultiline string buffer\n");
  360. * ptr = str;
  361. * while (ptr) {
  362. * printf("%d %s",i++,firstword(ptr));
  363. * ptr = strnl(ptr);
  364. * }
  365. *
  366. * Produces the following:
  367. *
  368. * 1 This
  369. * 2 is
  370. * 3 a
  371. * 4 multiline
  372. *
  373. * NOTE: The 'firstword()' function is conceptual only and does not
  374. * exist in this package.
  375. *
  376. * NOTE: Although the second 'ptr' variable is not strictly needed in
  377. * this example, it is good practice with these utilities. Once
  378. * the * pointer is advance in this manner, it may no longer be
  379. * handled with * realloc(). So at the end of the code fragment
  380. * above, * strscpy(str,"foo") work perfectly fine, but
  381. * strscpy(ptr,"foo") will * cause the the program to crash with
  382. * a segmentation fault.
  383. *
  384. *****************************************************************************/
  385. char *
  386. strnl (char *str)
  387. {
  388. size_t len;
  389. if (str == NULL)
  390. return NULL;
  391. str = strpbrk (str, "\r\n");
  392. if (str == NULL)
  393. return NULL;
  394. len = strspn (str, "\r\n");
  395. if (str[len] == '\0')
  396. return NULL;
  397. str += len;
  398. if (strlen (str) == 0)
  399. return NULL;
  400. return str;
  401. }
  402. /******************************************************************************
  403. *
  404. * Like strscpy, except only the portion of the source string up to
  405. * the provided delimiter is copied.
  406. *
  407. * Example:
  408. *
  409. * str = strpcpy(str,"This is a line of text with no trailing newline","x");
  410. * printf("%s\n",str);
  411. *
  412. * Produces:
  413. *
  414. *This is a line of te
  415. *
  416. *****************************************************************************/
  417. char *
  418. strpcpy (char *dest, const char *src, const char *str)
  419. {
  420. size_t len;
  421. if (src)
  422. len = strcspn (src, str);
  423. else
  424. return NULL;
  425. if (dest == NULL || strlen (dest) < len)
  426. dest = realloc (dest, len + 1);
  427. if (dest == NULL)
  428. terminate (STATE_UNKNOWN, "failed realloc in strpcpy\n");
  429. strncpy (dest, src, len);
  430. dest[len] = '\0';
  431. return dest;
  432. }
  433. /******************************************************************************
  434. *
  435. * Like strscat, except only the portion of the source string up to
  436. * the provided delimiter is copied.
  437. *
  438. * str = strpcpy(str,"This is a line of text with no trailing newline","x");
  439. * str = strpcat(str,"This is a line of text with no trailing newline","x");
  440. * printf("%s\n",str);
  441. *
  442. *This is a line of texThis is a line of tex
  443. *
  444. *****************************************************************************/
  445. char *
  446. strpcat (char *dest, const char *src, const char *str)
  447. {
  448. size_t len, l2;
  449. if (dest)
  450. len = strlen (dest);
  451. else
  452. len = 0;
  453. if (src) {
  454. l2 = strcspn (src, str);
  455. }
  456. else {
  457. return dest;
  458. }
  459. dest = realloc (dest, len + l2 + 1);
  460. if (dest == NULL)
  461. terminate (STATE_UNKNOWN, "failed malloc in strscat\n");
  462. strncpy (dest + len, src, l2);
  463. dest[len + l2] = '\0';
  464. return dest;
  465. }