utils.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798
  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. printf ("%s v%s (%s %s)\n",
  131. command_name, revision, PACKAGE, VERSION);
  132. }
  133. const char *
  134. state_text (int result)
  135. {
  136. switch (result) {
  137. case STATE_OK:
  138. return "OK";
  139. case STATE_WARNING:
  140. return "WARNING";
  141. case STATE_CRITICAL:
  142. return "CRITICAL";
  143. case STATE_DEPENDENT:
  144. return "DEPENDENT";
  145. default:
  146. return "UNKNOWN";
  147. }
  148. }
  149. void
  150. timeout_alarm_handler (int signo)
  151. {
  152. const char msg[] = " - Plugin timed out\n";
  153. if (signo == SIGALRM) {
  154. /* printf (_("%s - Plugin timed out after %d seconds\n"),
  155. state_text(timeout_state), timeout_interval); */
  156. switch(timeout_state) {
  157. case STATE_OK:
  158. write(STDOUT_FILENO, "OK", 2);
  159. break;
  160. case STATE_WARNING:
  161. write(STDOUT_FILENO, "WARNING", 7);
  162. break;
  163. case STATE_CRITICAL:
  164. write(STDOUT_FILENO, "CRITICAL", 8);
  165. break;
  166. case STATE_DEPENDENT:
  167. write(STDOUT_FILENO, "DEPENDENT", 9);
  168. break;
  169. default:
  170. write(STDOUT_FILENO, "UNKNOWN", 7);
  171. break;
  172. }
  173. write(STDOUT_FILENO, msg, sizeof(msg) - 1);
  174. exit (timeout_state);
  175. }
  176. }
  177. void
  178. set_timeout_state (char *state) {
  179. if ((timeout_state = translate_state(state)) == ERROR)
  180. usage4 (_("Timeout result must be a valid state name (OK, WARNING, CRITICAL, UNKNOWN) or integer (0-3)."));
  181. }
  182. int
  183. parse_timeout_string (char *timeout_str)
  184. {
  185. char *seperated_str;
  186. char *timeout_val = "";
  187. char *timeout_sta = NULL;
  188. if (strstr(timeout_str, ":") == NULL) {
  189. timeout_val = timeout_str;
  190. }
  191. else if (strncmp(timeout_str, ":", 1) == 0) {
  192. seperated_str = strtok(timeout_str, ":");
  193. if (seperated_str != NULL) {
  194. timeout_sta = seperated_str;
  195. }
  196. }
  197. else {
  198. seperated_str = strtok(timeout_str, ":");
  199. timeout_val = seperated_str;
  200. seperated_str = strtok(NULL, ":");
  201. if (seperated_str != NULL) {
  202. timeout_sta = seperated_str;
  203. }
  204. }
  205. if (timeout_sta != NULL) {
  206. set_timeout_state(timeout_sta);
  207. }
  208. if ((timeout_val == NULL) || (timeout_val[0] == '\0')) {
  209. return timeout_interval;
  210. }
  211. else if (is_intpos(timeout_val)) {
  212. return atoi(timeout_val);
  213. }
  214. else {
  215. usage4 (_("Timeout value must be a positive integer"));
  216. exit (STATE_UNKNOWN);
  217. }
  218. }
  219. int
  220. is_numeric (char *number)
  221. {
  222. char tmp[1];
  223. float x;
  224. if (!number)
  225. return FALSE;
  226. else if (sscanf (number, "%f%c", &x, tmp) == 1)
  227. return TRUE;
  228. else
  229. return FALSE;
  230. }
  231. int
  232. is_positive (char *number)
  233. {
  234. if (is_numeric (number) && atof (number) > 0.0)
  235. return TRUE;
  236. else
  237. return FALSE;
  238. }
  239. int
  240. is_negative (char *number)
  241. {
  242. if (is_numeric (number) && atof (number) < 0.0)
  243. return TRUE;
  244. else
  245. return FALSE;
  246. }
  247. int
  248. is_nonnegative (char *number)
  249. {
  250. if (is_numeric (number) && atof (number) >= 0.0)
  251. return TRUE;
  252. else
  253. return FALSE;
  254. }
  255. int
  256. is_percentage (char *number)
  257. {
  258. int x;
  259. if (is_numeric (number) && (x = atof (number)) >= 0 && x <= 100)
  260. return TRUE;
  261. else
  262. return FALSE;
  263. }
  264. int
  265. is_integer (char *number)
  266. {
  267. long int n;
  268. if (!number || (strspn (number, "-0123456789 ") != strlen (number)))
  269. return FALSE;
  270. n = strtol (number, NULL, 10);
  271. if (errno != ERANGE && n >= INT_MIN && n <= INT_MAX)
  272. return TRUE;
  273. else
  274. return FALSE;
  275. }
  276. int
  277. is_intpos (char *number)
  278. {
  279. if (is_integer (number) && atoi (number) > 0)
  280. return TRUE;
  281. else
  282. return FALSE;
  283. }
  284. int
  285. is_intneg (char *number)
  286. {
  287. if (is_integer (number) && atoi (number) < 0)
  288. return TRUE;
  289. else
  290. return FALSE;
  291. }
  292. int
  293. is_intnonneg (char *number)
  294. {
  295. if (is_integer (number) && atoi (number) >= 0)
  296. return TRUE;
  297. else
  298. return FALSE;
  299. }
  300. int
  301. is_intpercent (char *number)
  302. {
  303. int i;
  304. if (is_integer (number) && (i = atoi (number)) >= 0 && i <= 100)
  305. return TRUE;
  306. else
  307. return FALSE;
  308. }
  309. int
  310. is_option (char *str)
  311. {
  312. if (!str)
  313. return FALSE;
  314. else if (strspn (str, "-") == 1 || strspn (str, "-") == 2)
  315. return TRUE;
  316. else
  317. return FALSE;
  318. }
  319. #ifdef NEED_GETTIMEOFDAY
  320. int
  321. gettimeofday (struct timeval *tv, struct timezone *tz)
  322. {
  323. tv->tv_usec = 0;
  324. tv->tv_sec = (long) time ((time_t) 0);
  325. }
  326. #endif
  327. double
  328. delta_time (struct timeval tv)
  329. {
  330. struct timeval now;
  331. gettimeofday (&now, NULL);
  332. return ((double)(now.tv_sec - tv.tv_sec) + (double)(now.tv_usec - tv.tv_usec) / (double)1000000);
  333. }
  334. long
  335. deltime (struct timeval tv)
  336. {
  337. struct timeval now;
  338. gettimeofday (&now, NULL);
  339. return (now.tv_sec - tv.tv_sec)*1000000 + now.tv_usec - tv.tv_usec;
  340. }
  341. void
  342. strip (char *buffer)
  343. {
  344. size_t x;
  345. int i;
  346. for (x = strlen (buffer); x >= 1; x--) {
  347. i = x - 1;
  348. if (buffer[i] == ' ' ||
  349. buffer[i] == '\r' || buffer[i] == '\n' || buffer[i] == '\t')
  350. buffer[i] = '\0';
  351. else
  352. break;
  353. }
  354. return;
  355. }
  356. /******************************************************************************
  357. *
  358. * Copies one string to another. Any previously existing data in
  359. * the destination string is lost.
  360. *
  361. * Example:
  362. *
  363. * char *str=NULL;
  364. * str = strscpy("This is a line of text with no trailing newline");
  365. *
  366. *****************************************************************************/
  367. char *
  368. strscpy (char *dest, const char *src)
  369. {
  370. if (src == NULL)
  371. return NULL;
  372. xasprintf (&dest, "%s", src);
  373. return dest;
  374. }
  375. /******************************************************************************
  376. *
  377. * Returns a pointer to the next line of a multiline string buffer
  378. *
  379. * Given a pointer string, find the text following the next sequence
  380. * of \r and \n characters. This has the effect of skipping blank
  381. * lines as well
  382. *
  383. * Example:
  384. *
  385. * Given text as follows:
  386. *
  387. * ==============================
  388. * This
  389. * is
  390. * a
  391. *
  392. * multiline string buffer
  393. * ==============================
  394. *
  395. * int i=0;
  396. * char *str=NULL;
  397. * char *ptr=NULL;
  398. * str = strscpy(str,"This\nis\r\na\n\nmultiline string buffer\n");
  399. * ptr = str;
  400. * while (ptr) {
  401. * printf("%d %s",i++,firstword(ptr));
  402. * ptr = strnl(ptr);
  403. * }
  404. *
  405. * Produces the following:
  406. *
  407. * 1 This
  408. * 2 is
  409. * 3 a
  410. * 4 multiline
  411. *
  412. * NOTE: The 'firstword()' function is conceptual only and does not
  413. * exist in this package.
  414. *
  415. * NOTE: Although the second 'ptr' variable is not strictly needed in
  416. * this example, it is good practice with these utilities. Once
  417. * the * pointer is advance in this manner, it may no longer be
  418. * handled with * realloc(). So at the end of the code fragment
  419. * above, * strscpy(str,"foo") work perfectly fine, but
  420. * strscpy(ptr,"foo") will * cause the the program to crash with
  421. * a segmentation fault.
  422. *
  423. *****************************************************************************/
  424. char *
  425. strnl (char *str)
  426. {
  427. size_t len;
  428. if (str == NULL)
  429. return NULL;
  430. str = strpbrk (str, "\r\n");
  431. if (str == NULL)
  432. return NULL;
  433. len = strspn (str, "\r\n");
  434. if (str[len] == '\0')
  435. return NULL;
  436. str += len;
  437. if (strlen (str) == 0)
  438. return NULL;
  439. return str;
  440. }
  441. /******************************************************************************
  442. *
  443. * Like strscpy, except only the portion of the source string up to
  444. * the provided delimiter is copied.
  445. *
  446. * Example:
  447. *
  448. * str = strpcpy(str,"This is a line of text with no trailing newline","x");
  449. * printf("%s\n",str);
  450. *
  451. * Produces:
  452. *
  453. *This is a line of te
  454. *
  455. *****************************************************************************/
  456. char *
  457. strpcpy (char *dest, const char *src, const char *str)
  458. {
  459. size_t len;
  460. if (src)
  461. len = strcspn (src, str);
  462. else
  463. return NULL;
  464. if (dest == NULL || strlen (dest) < len)
  465. dest = realloc (dest, len + 1);
  466. if (dest == NULL)
  467. die (STATE_UNKNOWN, _("failed realloc in strpcpy\n"));
  468. strncpy (dest, src, len);
  469. dest[len] = '\0';
  470. return dest;
  471. }
  472. /******************************************************************************
  473. *
  474. * Like strscat, except only the portion of the source string up to
  475. * the provided delimiter is copied.
  476. *
  477. * str = strpcpy(str,"This is a line of text with no trailing newline","x");
  478. * str = strpcat(str,"This is a line of text with no trailing newline","x");
  479. * printf("%s\n",str);
  480. *
  481. *This is a line of texThis is a line of tex
  482. *
  483. *****************************************************************************/
  484. char *
  485. strpcat (char *dest, const char *src, const char *str)
  486. {
  487. size_t len, l2;
  488. if (dest)
  489. len = strlen (dest);
  490. else
  491. len = 0;
  492. if (src) {
  493. l2 = strcspn (src, str);
  494. }
  495. else {
  496. return dest;
  497. }
  498. dest = realloc (dest, len + l2 + 1);
  499. if (dest == NULL)
  500. die (STATE_UNKNOWN, _("failed malloc in strscat\n"));
  501. strncpy (dest + len, src, l2);
  502. dest[len + l2] = '\0';
  503. return dest;
  504. }
  505. /******************************************************************************
  506. *
  507. * asprintf, but die on failure
  508. *
  509. ******************************************************************************/
  510. int
  511. xvasprintf (char **strp, const char *fmt, va_list ap)
  512. {
  513. int result = vasprintf (strp, fmt, ap);
  514. if (result == -1 || *strp == NULL)
  515. die (STATE_UNKNOWN, _("failed malloc in xvasprintf\n"));
  516. return result;
  517. }
  518. int
  519. xasprintf (char **strp, const char *fmt, ...)
  520. {
  521. va_list ap;
  522. int result;
  523. va_start (ap, fmt);
  524. result = xvasprintf (strp, fmt, ap);
  525. va_end (ap);
  526. return result;
  527. }
  528. /******************************************************************************
  529. *
  530. * Print perfdata in a standard format
  531. *
  532. ******************************************************************************/
  533. char *perfdata (const char *label,
  534. long int val,
  535. const char *uom,
  536. int warnp,
  537. long int warn,
  538. int critp,
  539. long int crit,
  540. int minp,
  541. long int minv,
  542. int maxp,
  543. long int maxv)
  544. {
  545. char *data = NULL;
  546. if (strpbrk (label, "'= "))
  547. xasprintf (&data, "'%s'=%ld%s;", label, val, uom);
  548. else
  549. xasprintf (&data, "%s=%ld%s;", label, val, uom);
  550. if (warnp)
  551. xasprintf (&data, "%s%ld;", data, warn);
  552. else
  553. xasprintf (&data, "%s;", data);
  554. if (critp)
  555. xasprintf (&data, "%s%ld;", data, crit);
  556. else
  557. xasprintf (&data, "%s;", data);
  558. if (minp)
  559. xasprintf (&data, "%s%ld", data, minv);
  560. if (maxp)
  561. xasprintf (&data, "%s;%ld", data, maxv);
  562. return data;
  563. }
  564. char *fperfdata (const char *label,
  565. double val,
  566. const char *uom,
  567. int warnp,
  568. double warn,
  569. int critp,
  570. double crit,
  571. int minp,
  572. double minv,
  573. int maxp,
  574. double maxv)
  575. {
  576. char *data = NULL;
  577. if (strpbrk (label, "'= "))
  578. xasprintf (&data, "'%s'=", label);
  579. else
  580. xasprintf (&data, "%s=", label);
  581. xasprintf (&data, "%s%f", data, val);
  582. xasprintf (&data, "%s%s;", data, uom);
  583. if (warnp)
  584. xasprintf (&data, "%s%f", data, warn);
  585. xasprintf (&data, "%s;", data);
  586. if (critp)
  587. xasprintf (&data, "%s%f", data, crit);
  588. xasprintf (&data, "%s;", data);
  589. if (minp)
  590. xasprintf (&data, "%s%f", data, minv);
  591. if (maxp) {
  592. xasprintf (&data, "%s;", data);
  593. xasprintf (&data, "%s%f", data, maxv);
  594. }
  595. return data;
  596. }
  597. char *sperfdata (const char *label,
  598. double val,
  599. const char *uom,
  600. char *warn,
  601. char *crit,
  602. int minp,
  603. double minv,
  604. int maxp,
  605. double maxv)
  606. {
  607. char *data = NULL;
  608. if (strpbrk (label, "'= "))
  609. xasprintf (&data, "'%s'=", label);
  610. else
  611. xasprintf (&data, "%s=", label);
  612. xasprintf (&data, "%s%f", data, val);
  613. xasprintf (&data, "%s%s;", data, uom);
  614. if (warn!=NULL)
  615. xasprintf (&data, "%s%s", data, warn);
  616. xasprintf (&data, "%s;", data);
  617. if (crit!=NULL)
  618. xasprintf (&data, "%s%s", data, crit);
  619. xasprintf (&data, "%s;", data);
  620. if (minp)
  621. xasprintf (&data, "%s%f", data, minv);
  622. if (maxp) {
  623. xasprintf (&data, "%s;", data);
  624. xasprintf (&data, "%s%f", data, maxv);
  625. }
  626. return data;
  627. }
  628. char *sperfdata_int (const char *label,
  629. int val,
  630. const char *uom,
  631. char *warn,
  632. char *crit,
  633. int minp,
  634. int minv,
  635. int maxp,
  636. int maxv)
  637. {
  638. char *data = NULL;
  639. if (strpbrk (label, "'= "))
  640. xasprintf (&data, "'%s'=", label);
  641. else
  642. xasprintf (&data, "%s=", label);
  643. xasprintf (&data, "%s%d", data, val);
  644. xasprintf (&data, "%s%s;", data, uom);
  645. if (warn!=NULL)
  646. xasprintf (&data, "%s%s", data, warn);
  647. xasprintf (&data, "%s;", data);
  648. if (crit!=NULL)
  649. xasprintf (&data, "%s%s", data, crit);
  650. xasprintf (&data, "%s;", data);
  651. if (minp)
  652. xasprintf (&data, "%s%d", data, minv);
  653. if (maxp) {
  654. xasprintf (&data, "%s;", data);
  655. xasprintf (&data, "%s%d", data, maxv);
  656. }
  657. return data;
  658. }
  659. /* set entire string to lower, no need to return as it works on string in place */
  660. void strntolower (char * test_char, int size) {
  661. char * ptr = test_char;
  662. for (; ptr < test_char+size; ++ptr)
  663. *ptr = tolower(*ptr);
  664. }
  665. /* set entire string to lower, no need to return as it works on string in place */
  666. void strntoupper (char * test_char, int size) {
  667. char * ptr = test_char;
  668. for (; ptr < test_char+size; ++ptr)
  669. *ptr = toupper(*ptr);
  670. }