4
0

utils.c 16 KB

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