utils.c 12 KB

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