utils.c 14 KB

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