utils.c 15 KB

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