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