utils.c 12 KB

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