utils.c 15 KB

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