utils.c 14 KB

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