utils.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659
  1. /*****************************************************************************
  2. *
  3. * Library of useful functions for plugins
  4. *
  5. * License: GPL
  6. * Copyright (c) 2000 Karl DeBisschop (karl@debisschop.net)
  7. * Copyright (c) 2002-2014 Nagios Plugin Development Team
  8. *
  9. * This program is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation, either version 3 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  21. *
  22. *
  23. *****************************************************************************/
  24. #include "common.h"
  25. #include "utils.h"
  26. #include "utils_base.h"
  27. #include <stdarg.h>
  28. #include <limits.h>
  29. #include <arpa/inet.h>
  30. extern void print_usage (void);
  31. extern const char *progname;
  32. #define STRLEN 64
  33. #define TXTBLK 128
  34. unsigned int timeout_state = STATE_CRITICAL;
  35. unsigned int timeout_interval = DEFAULT_SOCKET_TIMEOUT;
  36. time_t start_time, end_time;
  37. /* **************************************************************************
  38. * max_state(STATE_x, STATE_y)
  39. * compares STATE_x to STATE_y and returns result based on the following
  40. * STATE_UNKNOWN < STATE_OK < STATE_WARNING < STATE_CRITICAL
  41. *
  42. * Note that numerically the above does not hold
  43. ****************************************************************************/
  44. int
  45. max_state (int a, int b)
  46. {
  47. if (a == STATE_CRITICAL || b == STATE_CRITICAL)
  48. return STATE_CRITICAL;
  49. else if (a == STATE_WARNING || b == STATE_WARNING)
  50. return STATE_WARNING;
  51. else if (a == STATE_OK || b == STATE_OK)
  52. return STATE_OK;
  53. else if (a == STATE_UNKNOWN || b == STATE_UNKNOWN)
  54. return STATE_UNKNOWN;
  55. else if (a == STATE_DEPENDENT || b == STATE_DEPENDENT)
  56. return STATE_DEPENDENT;
  57. else
  58. return max (a, b);
  59. }
  60. /* **************************************************************************
  61. * max_state_alt(STATE_x, STATE_y)
  62. * compares STATE_x to STATE_y and returns result based on the following
  63. * STATE_OK < STATE_DEPENDENT < STATE_UNKNOWN < STATE_WARNING < STATE_CRITICAL
  64. *
  65. * The main difference between max_state_alt and max_state it that it doesn't
  66. * allow setting a default to UNKNOWN. It will instead prioritixe any valid
  67. * non-OK state.
  68. ****************************************************************************/
  69. int
  70. max_state_alt (int a, int b)
  71. {
  72. if (a == STATE_CRITICAL || b == STATE_CRITICAL)
  73. return STATE_CRITICAL;
  74. else if (a == STATE_WARNING || b == STATE_WARNING)
  75. return STATE_WARNING;
  76. else if (a == STATE_UNKNOWN || b == STATE_UNKNOWN)
  77. return STATE_UNKNOWN;
  78. else if (a == STATE_DEPENDENT || b == STATE_DEPENDENT)
  79. return STATE_DEPENDENT;
  80. else if (a == STATE_OK || b == STATE_OK)
  81. return STATE_OK;
  82. else
  83. return max (a, b);
  84. }
  85. void usage (const char *msg)
  86. {
  87. printf ("%s\n", msg);
  88. print_usage ();
  89. exit (STATE_UNKNOWN);
  90. }
  91. void usage_va (const char *fmt, ...)
  92. {
  93. va_list ap;
  94. printf("%s: ", progname);
  95. va_start(ap, fmt);
  96. vprintf(fmt, ap);
  97. va_end(ap);
  98. printf("\n");
  99. exit (STATE_UNKNOWN);
  100. }
  101. void usage2(const char *msg, const char *arg)
  102. {
  103. printf ("%s: %s - %s\n", progname, msg, arg?arg:"(null)" );
  104. print_usage ();
  105. exit (STATE_UNKNOWN);
  106. }
  107. void
  108. usage3 (const char *msg, int arg)
  109. {
  110. printf ("%s: %s - %c\n", progname, msg, arg);
  111. print_usage();
  112. exit (STATE_UNKNOWN);
  113. }
  114. void
  115. usage4 (const char *msg)
  116. {
  117. printf ("%s: %s\n", progname, msg);
  118. print_usage();
  119. exit (STATE_UNKNOWN);
  120. }
  121. void
  122. usage5 (void)
  123. {
  124. print_usage();
  125. exit (STATE_UNKNOWN);
  126. }
  127. void
  128. print_revision (const char *command_name, const char *revision)
  129. {
  130. char plugin_revision[STRLEN];
  131. printf ("%s v%s (%s %s)\n",
  132. command_name, revision, PACKAGE, VERSION);
  133. }
  134. const char *
  135. state_text (int result)
  136. {
  137. switch (result) {
  138. case STATE_OK:
  139. return "OK";
  140. case STATE_WARNING:
  141. return "WARNING";
  142. case STATE_CRITICAL:
  143. return "CRITICAL";
  144. case STATE_DEPENDENT:
  145. return "DEPENDENT";
  146. default:
  147. return "UNKNOWN";
  148. }
  149. }
  150. void
  151. timeout_alarm_handler (int signo)
  152. {
  153. if (signo == SIGALRM) {
  154. printf (_("%s - Plugin timed out after %d seconds\n"),
  155. state_text(timeout_state), timeout_interval);
  156. exit (timeout_state);
  157. }
  158. }
  159. void
  160. set_timeout_state (char *state)
  161. {
  162. if ((timeout_state = translate_state(state)) == ERROR)
  163. usage4 (_("Timeout result must be a valid state name (OK, WARNING, CRITICAL, UNKNOWN) or integer (0-3)."));
  164. }
  165. int
  166. is_numeric (char *number)
  167. {
  168. char tmp[1];
  169. float x;
  170. if (!number)
  171. return FALSE;
  172. else if (sscanf (number, "%f%c", &x, tmp) == 1)
  173. return TRUE;
  174. else
  175. return FALSE;
  176. }
  177. int
  178. is_positive (char *number)
  179. {
  180. if (is_numeric (number) && atof (number) > 0.0)
  181. return TRUE;
  182. else
  183. return FALSE;
  184. }
  185. int
  186. is_negative (char *number)
  187. {
  188. if (is_numeric (number) && atof (number) < 0.0)
  189. return TRUE;
  190. else
  191. return FALSE;
  192. }
  193. int
  194. is_nonnegative (char *number)
  195. {
  196. if (is_numeric (number) && atof (number) >= 0.0)
  197. return TRUE;
  198. else
  199. return FALSE;
  200. }
  201. int
  202. is_percentage (char *number)
  203. {
  204. int x;
  205. if (is_numeric (number) && (x = atof (number)) >= 0 && x <= 100)
  206. return TRUE;
  207. else
  208. return FALSE;
  209. }
  210. int
  211. is_integer (char *number)
  212. {
  213. long int n;
  214. if (!number || (strspn (number, "-0123456789 ") != strlen (number)))
  215. return FALSE;
  216. n = strtol (number, NULL, 10);
  217. if (errno != ERANGE && n >= INT_MIN && n <= INT_MAX)
  218. return TRUE;
  219. else
  220. return FALSE;
  221. }
  222. int
  223. is_intpos (char *number)
  224. {
  225. if (is_integer (number) && atoi (number) > 0)
  226. return TRUE;
  227. else
  228. return FALSE;
  229. }
  230. int
  231. is_intneg (char *number)
  232. {
  233. if (is_integer (number) && atoi (number) < 0)
  234. return TRUE;
  235. else
  236. return FALSE;
  237. }
  238. int
  239. is_intnonneg (char *number)
  240. {
  241. if (is_integer (number) && atoi (number) >= 0)
  242. return TRUE;
  243. else
  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. else
  253. return FALSE;
  254. }
  255. int
  256. is_option (char *str)
  257. {
  258. if (!str)
  259. return FALSE;
  260. else if (strspn (str, "-") == 1 || strspn (str, "-") == 2)
  261. return TRUE;
  262. else
  263. return FALSE;
  264. }
  265. #ifdef NEED_GETTIMEOFDAY
  266. int
  267. gettimeofday (struct timeval *tv, struct timezone *tz)
  268. {
  269. tv->tv_usec = 0;
  270. tv->tv_sec = (long) time ((time_t) 0);
  271. }
  272. #endif
  273. double
  274. delta_time (struct timeval tv)
  275. {
  276. struct timeval now;
  277. gettimeofday (&now, NULL);
  278. return ((double)(now.tv_sec - tv.tv_sec) + (double)(now.tv_usec - tv.tv_usec) / (double)1000000);
  279. }
  280. long
  281. deltime (struct timeval tv)
  282. {
  283. struct timeval now;
  284. gettimeofday (&now, NULL);
  285. return (now.tv_sec - tv.tv_sec)*1000000 + now.tv_usec - tv.tv_usec;
  286. }
  287. void
  288. strip (char *buffer)
  289. {
  290. size_t x;
  291. int i;
  292. for (x = strlen (buffer); x >= 1; x--) {
  293. i = x - 1;
  294. if (buffer[i] == ' ' ||
  295. buffer[i] == '\r' || buffer[i] == '\n' || buffer[i] == '\t')
  296. buffer[i] = '\0';
  297. else
  298. break;
  299. }
  300. return;
  301. }
  302. /******************************************************************************
  303. *
  304. * Copies one string to another. Any previously existing data in
  305. * the destination string is lost.
  306. *
  307. * Example:
  308. *
  309. * char *str=NULL;
  310. * str = strscpy("This is a line of text with no trailing newline");
  311. *
  312. *****************************************************************************/
  313. char *
  314. strscpy (char *dest, const char *src)
  315. {
  316. if (src == NULL)
  317. return NULL;
  318. xasprintf (&dest, "%s", src);
  319. return dest;
  320. }
  321. /******************************************************************************
  322. *
  323. * Returns a pointer to the next line of a multiline string buffer
  324. *
  325. * Given a pointer string, find the text following the next sequence
  326. * of \r and \n characters. This has the effect of skipping blank
  327. * lines as well
  328. *
  329. * Example:
  330. *
  331. * Given text as follows:
  332. *
  333. * ==============================
  334. * This
  335. * is
  336. * a
  337. *
  338. * multiline string buffer
  339. * ==============================
  340. *
  341. * int i=0;
  342. * char *str=NULL;
  343. * char *ptr=NULL;
  344. * str = strscpy(str,"This\nis\r\na\n\nmultiline string buffer\n");
  345. * ptr = str;
  346. * while (ptr) {
  347. * printf("%d %s",i++,firstword(ptr));
  348. * ptr = strnl(ptr);
  349. * }
  350. *
  351. * Produces the following:
  352. *
  353. * 1 This
  354. * 2 is
  355. * 3 a
  356. * 4 multiline
  357. *
  358. * NOTE: The 'firstword()' function is conceptual only and does not
  359. * exist in this package.
  360. *
  361. * NOTE: Although the second 'ptr' variable is not strictly needed in
  362. * this example, it is good practice with these utilities. Once
  363. * the * pointer is advance in this manner, it may no longer be
  364. * handled with * realloc(). So at the end of the code fragment
  365. * above, * strscpy(str,"foo") work perfectly fine, but
  366. * strscpy(ptr,"foo") will * cause the the program to crash with
  367. * a segmentation fault.
  368. *
  369. *****************************************************************************/
  370. char *
  371. strnl (char *str)
  372. {
  373. size_t len;
  374. if (str == NULL)
  375. return NULL;
  376. str = strpbrk (str, "\r\n");
  377. if (str == NULL)
  378. return NULL;
  379. len = strspn (str, "\r\n");
  380. if (str[len] == '\0')
  381. return NULL;
  382. str += len;
  383. if (strlen (str) == 0)
  384. return NULL;
  385. return str;
  386. }
  387. /******************************************************************************
  388. *
  389. * Like strscpy, except only the portion of the source string up to
  390. * the provided delimiter is copied.
  391. *
  392. * Example:
  393. *
  394. * str = strpcpy(str,"This is a line of text with no trailing newline","x");
  395. * printf("%s\n",str);
  396. *
  397. * Produces:
  398. *
  399. *This is a line of te
  400. *
  401. *****************************************************************************/
  402. char *
  403. strpcpy (char *dest, const char *src, const char *str)
  404. {
  405. size_t len;
  406. if (src)
  407. len = strcspn (src, str);
  408. else
  409. return NULL;
  410. if (dest == NULL || strlen (dest) < len)
  411. dest = realloc (dest, len + 1);
  412. if (dest == NULL)
  413. die (STATE_UNKNOWN, _("failed realloc in strpcpy\n"));
  414. strncpy (dest, src, len);
  415. dest[len] = '\0';
  416. return dest;
  417. }
  418. /******************************************************************************
  419. *
  420. * Like strscat, except only the portion of the source string up to
  421. * the provided delimiter is copied.
  422. *
  423. * str = strpcpy(str,"This is a line of text with no trailing newline","x");
  424. * str = strpcat(str,"This is a line of text with no trailing newline","x");
  425. * printf("%s\n",str);
  426. *
  427. *This is a line of texThis is a line of tex
  428. *
  429. *****************************************************************************/
  430. char *
  431. strpcat (char *dest, const char *src, const char *str)
  432. {
  433. size_t len, l2;
  434. if (dest)
  435. len = strlen (dest);
  436. else
  437. len = 0;
  438. if (src) {
  439. l2 = strcspn (src, str);
  440. }
  441. else {
  442. return dest;
  443. }
  444. dest = realloc (dest, len + l2 + 1);
  445. if (dest == NULL)
  446. die (STATE_UNKNOWN, _("failed malloc in strscat\n"));
  447. strncpy (dest + len, src, l2);
  448. dest[len + l2] = '\0';
  449. return dest;
  450. }
  451. /******************************************************************************
  452. *
  453. * asprintf, but die on failure
  454. *
  455. ******************************************************************************/
  456. int
  457. xvasprintf (char **strp, const char *fmt, va_list ap)
  458. {
  459. int result = vasprintf (strp, fmt, ap);
  460. if (result == -1 || *strp == NULL)
  461. die (STATE_UNKNOWN, _("failed malloc in xvasprintf\n"));
  462. return result;
  463. }
  464. int
  465. xasprintf (char **strp, const char *fmt, ...)
  466. {
  467. va_list ap;
  468. int result;
  469. va_start (ap, fmt);
  470. result = xvasprintf (strp, fmt, ap);
  471. va_end (ap);
  472. return result;
  473. }
  474. /******************************************************************************
  475. *
  476. * Print perfdata in a standard format
  477. *
  478. ******************************************************************************/
  479. char *perfdata (const char *label,
  480. long int val,
  481. const char *uom,
  482. int warnp,
  483. long int warn,
  484. int critp,
  485. long int crit,
  486. int minp,
  487. long int minv,
  488. int maxp,
  489. long int maxv)
  490. {
  491. char *data = NULL;
  492. if (strpbrk (label, "'= "))
  493. xasprintf (&data, "'%s'=%ld%s;", label, val, uom);
  494. else
  495. xasprintf (&data, "%s=%ld%s;", label, val, uom);
  496. if (warnp)
  497. xasprintf (&data, "%s%ld;", data, warn);
  498. else
  499. xasprintf (&data, "%s;", data);
  500. if (critp)
  501. xasprintf (&data, "%s%ld;", data, crit);
  502. else
  503. xasprintf (&data, "%s;", data);
  504. if (minp)
  505. xasprintf (&data, "%s%ld", data, minv);
  506. if (maxp)
  507. xasprintf (&data, "%s;%ld", data, maxv);
  508. return data;
  509. }
  510. char *fperfdata (const char *label,
  511. double val,
  512. const char *uom,
  513. int warnp,
  514. double warn,
  515. int critp,
  516. double crit,
  517. int minp,
  518. double minv,
  519. int maxp,
  520. double maxv)
  521. {
  522. char *data = NULL;
  523. if (strpbrk (label, "'= "))
  524. xasprintf (&data, "'%s'=", label);
  525. else
  526. xasprintf (&data, "%s=", label);
  527. xasprintf (&data, "%s%f", data, val);
  528. xasprintf (&data, "%s%s;", data, uom);
  529. if (warnp)
  530. xasprintf (&data, "%s%f", data, warn);
  531. xasprintf (&data, "%s;", data);
  532. if (critp)
  533. xasprintf (&data, "%s%f", data, crit);
  534. xasprintf (&data, "%s;", data);
  535. if (minp)
  536. xasprintf (&data, "%s%f", data, minv);
  537. if (maxp) {
  538. xasprintf (&data, "%s;", data);
  539. xasprintf (&data, "%s%f", data, maxv);
  540. }
  541. return data;
  542. }
  543. /* set entire string to lower, no need to return as it works on string in place */
  544. void strntolower (char * test_char, int size) {
  545. char * ptr = test_char;
  546. for (; ptr < test_char+size; ++ptr)
  547. *ptr = tolower(*ptr);
  548. }
  549. /* set entire string to lower, no need to return as it works on string in place */
  550. void strntoupper (char * test_char, int size) {
  551. char * ptr = test_char;
  552. for (; ptr < test_char+size; ++ptr)
  553. *ptr = toupper(*ptr);
  554. }