4
0

utils.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786
  1. /*****************************************************************************
  2. *
  3. * utils.c
  4. *
  5. * Library of useful functions for plugins
  6. *
  7. * Copyright (c) 2000 Karl DeBisschop (karl@debisschop.net)
  8. * Copyright (c) 2006 Nagios Plugin Development Team
  9. * License: GPL
  10. *
  11. * $Revision$
  12. * $Date$
  13. ****************************************************************************/
  14. #define LOCAL_TIMEOUT_ALARM_HANDLER
  15. #include "common.h"
  16. #include "utils.h"
  17. #include <stdarg.h>
  18. #include <limits.h>
  19. #include <arpa/inet.h>
  20. extern void print_usage (void);
  21. extern const char *progname;
  22. #define STRLEN 64
  23. #define TXTBLK 128
  24. /* **************************************************************************
  25. * max_state(STATE_x, STATE_y)
  26. * compares STATE_x to STATE_y and returns result based on the following
  27. * STATE_UNKNOWN < STATE_OK < STATE_WARNING < STATE_CRITICAL
  28. *
  29. * Note that numerically the above does not hold
  30. ****************************************************************************/
  31. int
  32. max_state (int a, int b)
  33. {
  34. if (a == STATE_CRITICAL || b == STATE_CRITICAL)
  35. return STATE_CRITICAL;
  36. else if (a == STATE_WARNING || b == STATE_WARNING)
  37. return STATE_WARNING;
  38. else if (a == STATE_OK || b == STATE_OK)
  39. return STATE_OK;
  40. else if (a == STATE_UNKNOWN || b == STATE_UNKNOWN)
  41. return STATE_UNKNOWN;
  42. else if (a == STATE_DEPENDENT || b == STATE_DEPENDENT)
  43. return STATE_DEPENDENT;
  44. else
  45. return max (a, b);
  46. }
  47. void usage (const char *msg)
  48. {
  49. printf ("%s\n", msg);
  50. print_usage ();
  51. exit (STATE_UNKNOWN);
  52. }
  53. void usage_va (const char *fmt, ...)
  54. {
  55. va_list ap;
  56. printf("%s: ", progname);
  57. va_start(ap, fmt);
  58. vprintf(fmt, ap);
  59. va_end(ap);
  60. printf("\n");
  61. exit (STATE_UNKNOWN);
  62. }
  63. void usage2(const char *msg, const char *arg)
  64. {
  65. printf ("%s: %s - %s\n", progname, msg, arg?arg:"(null)" );
  66. print_usage ();
  67. exit (STATE_UNKNOWN);
  68. }
  69. void
  70. usage3 (const char *msg, int arg)
  71. {
  72. printf ("%s: %s - %c\n", progname, msg, arg);
  73. print_usage();
  74. exit (STATE_UNKNOWN);
  75. }
  76. void
  77. usage4 (const char *msg)
  78. {
  79. printf ("%s: %s\n", progname, msg);
  80. print_usage();
  81. exit (STATE_UNKNOWN);
  82. }
  83. char *
  84. clean_revstring (const char *revstring)
  85. {
  86. char plugin_revision[STRLEN];
  87. if (sscanf (revstring,"$Revision: %[0-9.]",plugin_revision) == 1)
  88. return strscpy (NULL, plugin_revision);
  89. else
  90. return strscpy (NULL, "N/A");
  91. }
  92. void
  93. print_revision (const char *command_name, const char *revision_string)
  94. {
  95. char plugin_revision[STRLEN];
  96. if (sscanf (revision_string, "$Revision: %[0-9.]", plugin_revision) != 1)
  97. strncpy (plugin_revision, "N/A", STRLEN);
  98. printf ("%s (%s %s) %s\n",
  99. command_name, PACKAGE, VERSION, plugin_revision);
  100. }
  101. const char *
  102. state_text (int result)
  103. {
  104. switch (result) {
  105. case STATE_OK:
  106. return "OK";
  107. case STATE_WARNING:
  108. return "WARNING";
  109. case STATE_CRITICAL:
  110. return "CRITICAL";
  111. case STATE_DEPENDENT:
  112. return "DEPENDENT";
  113. default:
  114. return "UNKNOWN";
  115. }
  116. }
  117. void
  118. die (int result, const char *fmt, ...)
  119. {
  120. va_list ap;
  121. va_start (ap, fmt);
  122. vprintf (fmt, ap);
  123. va_end (ap);
  124. exit (result);
  125. }
  126. void
  127. timeout_alarm_handler (int signo)
  128. {
  129. if (signo == SIGALRM) {
  130. printf (_("CRITICAL - Plugin timed out after %d seconds\n"),
  131. timeout_interval);
  132. exit (STATE_CRITICAL);
  133. }
  134. }
  135. int
  136. is_numeric (char *number)
  137. {
  138. char tmp[1];
  139. float x;
  140. if (!number)
  141. return FALSE;
  142. else if (sscanf (number, "%f%c", &x, tmp) == 1)
  143. return TRUE;
  144. else
  145. return FALSE;
  146. }
  147. int
  148. is_positive (char *number)
  149. {
  150. if (is_numeric (number) && atof (number) > 0.0)
  151. return TRUE;
  152. else
  153. return FALSE;
  154. }
  155. int
  156. is_negative (char *number)
  157. {
  158. if (is_numeric (number) && atof (number) < 0.0)
  159. return TRUE;
  160. else
  161. return FALSE;
  162. }
  163. int
  164. is_nonnegative (char *number)
  165. {
  166. if (is_numeric (number) && atof (number) >= 0.0)
  167. return TRUE;
  168. else
  169. return FALSE;
  170. }
  171. int
  172. is_percentage (char *number)
  173. {
  174. int x;
  175. if (is_numeric (number) && (x = atof (number)) >= 0 && x <= 100)
  176. return TRUE;
  177. else
  178. return FALSE;
  179. }
  180. int
  181. is_integer (char *number)
  182. {
  183. long int n;
  184. if (!number || (strspn (number, "-0123456789 ") != strlen (number)))
  185. return FALSE;
  186. n = strtol (number, NULL, 10);
  187. if (errno != ERANGE && n >= INT_MIN && n <= INT_MAX)
  188. return TRUE;
  189. else
  190. return FALSE;
  191. }
  192. int
  193. is_intpos (char *number)
  194. {
  195. if (is_integer (number) && atoi (number) > 0)
  196. return TRUE;
  197. else
  198. return FALSE;
  199. }
  200. int
  201. is_intneg (char *number)
  202. {
  203. if (is_integer (number) && atoi (number) < 0)
  204. return TRUE;
  205. else
  206. return FALSE;
  207. }
  208. int
  209. is_intnonneg (char *number)
  210. {
  211. if (is_integer (number) && atoi (number) >= 0)
  212. return TRUE;
  213. else
  214. return FALSE;
  215. }
  216. int
  217. is_intpercent (char *number)
  218. {
  219. int i;
  220. if (is_integer (number) && (i = atoi (number)) >= 0 && i <= 100)
  221. return TRUE;
  222. else
  223. return FALSE;
  224. }
  225. int
  226. is_option (char *str)
  227. {
  228. if (!str)
  229. return FALSE;
  230. else if (strspn (str, "-") == 1 || strspn (str, "-") == 2)
  231. return TRUE;
  232. else
  233. return FALSE;
  234. }
  235. void set_range_start (range *this, double value) {
  236. this->start = value;
  237. this->start_infinity = FALSE;
  238. }
  239. void set_range_end (range *this, double value) {
  240. this->end = value;
  241. this->end_infinity = FALSE;
  242. }
  243. range
  244. *parse_range_string (char *str) {
  245. range *temp_range;
  246. double start;
  247. double end;
  248. char *end_str;
  249. temp_range = (range *) malloc(sizeof(range));
  250. /* Set defaults */
  251. temp_range->start = 0;
  252. temp_range->start_infinity = FALSE;
  253. temp_range->end = 0;
  254. temp_range->end_infinity = TRUE;
  255. temp_range->alert_on = OUTSIDE;
  256. if (str[0] == '@') {
  257. temp_range->alert_on = INSIDE;
  258. str++;
  259. }
  260. end_str = index(str, ':');
  261. if (end_str != NULL) {
  262. if (str[0] == '~') {
  263. temp_range->start_infinity = TRUE;
  264. } else {
  265. start = strtod(str, NULL); /* Will stop at the ':' */
  266. set_range_start(temp_range, start);
  267. }
  268. end_str++; /* Move past the ':' */
  269. } else {
  270. end_str = str;
  271. }
  272. end = strtod(end_str, NULL);
  273. if (strcmp(end_str, "") != 0) {
  274. set_range_end(temp_range, end);
  275. }
  276. if (temp_range->start_infinity == TRUE ||
  277. temp_range->end_infinity == TRUE ||
  278. temp_range->start <= temp_range->end) {
  279. return temp_range;
  280. }
  281. free(temp_range);
  282. return NULL;
  283. }
  284. /* returns 0 if okay, otherwise 1 */
  285. int
  286. _set_thresholds(thresholds **my_thresholds, char *warn_string, char *critical_string)
  287. {
  288. thresholds *temp_thresholds = NULL;
  289. temp_thresholds = malloc(sizeof(temp_thresholds));
  290. temp_thresholds->warning = NULL;
  291. temp_thresholds->critical = NULL;
  292. if (warn_string != NULL) {
  293. if ((temp_thresholds->warning = parse_range_string(warn_string)) == NULL) {
  294. return 1;
  295. }
  296. }
  297. if (critical_string != NULL) {
  298. if ((temp_thresholds->critical = parse_range_string(critical_string)) == NULL) {
  299. return 1;
  300. }
  301. }
  302. if (*my_thresholds != 0) {
  303. /* printf("Freeing here: %d\n", *my_thresholds); */
  304. free(*my_thresholds);
  305. }
  306. *my_thresholds = temp_thresholds;
  307. return 0;
  308. }
  309. void
  310. set_thresholds(thresholds **my_thresholds, char *warn_string, char *critical_string)
  311. {
  312. if (_set_thresholds(my_thresholds, warn_string, critical_string) == 0) {
  313. return;
  314. } else {
  315. usage("Range format incorrect");
  316. }
  317. }
  318. /* Returns TRUE if alert should be raised based on the range */
  319. int
  320. check_range(double value, range *my_range)
  321. {
  322. int false = FALSE;
  323. int true = TRUE;
  324. if (my_range->alert_on == INSIDE) {
  325. false = TRUE;
  326. true = FALSE;
  327. }
  328. if (my_range->end_infinity == FALSE && my_range->start_infinity == FALSE) {
  329. if ((my_range->start <= value) && (value <= my_range->end)) {
  330. return false;
  331. } else {
  332. return true;
  333. }
  334. } else if (my_range->start_infinity == FALSE && my_range->end_infinity == TRUE) {
  335. if (my_range->start <= value) {
  336. return false;
  337. } else {
  338. return true;
  339. }
  340. } else if (my_range->start_infinity == TRUE && my_range->end_infinity == FALSE) {
  341. if (value <= my_range->end) {
  342. return false;
  343. } else {
  344. return true;
  345. }
  346. } else {
  347. return false;
  348. }
  349. }
  350. /* Returns status */
  351. int
  352. get_status(double value, thresholds *my_thresholds)
  353. {
  354. if (my_thresholds->critical != NULL) {
  355. if (check_range(value, my_thresholds->critical) == TRUE) {
  356. return STATE_CRITICAL;
  357. }
  358. }
  359. if (my_thresholds->warning != NULL) {
  360. if (check_range(value, my_thresholds->warning) == TRUE) {
  361. return STATE_WARNING;
  362. }
  363. }
  364. return STATE_OK;
  365. }
  366. #ifdef NEED_GETTIMEOFDAY
  367. int
  368. gettimeofday (struct timeval *tv, struct timezone *tz)
  369. {
  370. tv->tv_usec = 0;
  371. tv->tv_sec = (long) time ((time_t) 0);
  372. }
  373. #endif
  374. double
  375. delta_time (struct timeval tv)
  376. {
  377. struct timeval now;
  378. gettimeofday (&now, NULL);
  379. return ((double)(now.tv_sec - tv.tv_sec) + (double)(now.tv_usec - tv.tv_usec) / (double)1000000);
  380. }
  381. long
  382. deltime (struct timeval tv)
  383. {
  384. struct timeval now;
  385. gettimeofday (&now, NULL);
  386. return (now.tv_sec - tv.tv_sec)*1000000 + now.tv_usec - tv.tv_usec;
  387. }
  388. void
  389. strip (char *buffer)
  390. {
  391. size_t x;
  392. int i;
  393. for (x = strlen (buffer); x >= 1; x--) {
  394. i = x - 1;
  395. if (buffer[i] == ' ' ||
  396. buffer[i] == '\r' || buffer[i] == '\n' || buffer[i] == '\t')
  397. buffer[i] = '\0';
  398. else
  399. break;
  400. }
  401. return;
  402. }
  403. /******************************************************************************
  404. *
  405. * Copies one string to another. Any previously existing data in
  406. * the destination string is lost.
  407. *
  408. * Example:
  409. *
  410. * char *str=NULL;
  411. * str = strscpy("This is a line of text with no trailing newline");
  412. *
  413. *****************************************************************************/
  414. char *
  415. strscpy (char *dest, const char *src)
  416. {
  417. if (src == NULL)
  418. return NULL;
  419. asprintf (&dest, "%s", src);
  420. return dest;
  421. }
  422. /******************************************************************************
  423. *
  424. * Returns a pointer to the next line of a multiline string buffer
  425. *
  426. * Given a pointer string, find the text following the next sequence
  427. * of \r and \n characters. This has the effect of skipping blank
  428. * lines as well
  429. *
  430. * Example:
  431. *
  432. * Given text as follows:
  433. *
  434. * ==============================
  435. * This
  436. * is
  437. * a
  438. *
  439. * multiline string buffer
  440. * ==============================
  441. *
  442. * int i=0;
  443. * char *str=NULL;
  444. * char *ptr=NULL;
  445. * str = strscpy(str,"This\nis\r\na\n\nmultiline string buffer\n");
  446. * ptr = str;
  447. * while (ptr) {
  448. * printf("%d %s",i++,firstword(ptr));
  449. * ptr = strnl(ptr);
  450. * }
  451. *
  452. * Produces the following:
  453. *
  454. * 1 This
  455. * 2 is
  456. * 3 a
  457. * 4 multiline
  458. *
  459. * NOTE: The 'firstword()' function is conceptual only and does not
  460. * exist in this package.
  461. *
  462. * NOTE: Although the second 'ptr' variable is not strictly needed in
  463. * this example, it is good practice with these utilities. Once
  464. * the * pointer is advance in this manner, it may no longer be
  465. * handled with * realloc(). So at the end of the code fragment
  466. * above, * strscpy(str,"foo") work perfectly fine, but
  467. * strscpy(ptr,"foo") will * cause the the program to crash with
  468. * a segmentation fault.
  469. *
  470. *****************************************************************************/
  471. char *
  472. strnl (char *str)
  473. {
  474. size_t len;
  475. if (str == NULL)
  476. return NULL;
  477. str = strpbrk (str, "\r\n");
  478. if (str == NULL)
  479. return NULL;
  480. len = strspn (str, "\r\n");
  481. if (str[len] == '\0')
  482. return NULL;
  483. str += len;
  484. if (strlen (str) == 0)
  485. return NULL;
  486. return str;
  487. }
  488. /******************************************************************************
  489. *
  490. * Like strscpy, except only the portion of the source string up to
  491. * the provided delimiter is copied.
  492. *
  493. * Example:
  494. *
  495. * str = strpcpy(str,"This is a line of text with no trailing newline","x");
  496. * printf("%s\n",str);
  497. *
  498. * Produces:
  499. *
  500. *This is a line of te
  501. *
  502. *****************************************************************************/
  503. char *
  504. strpcpy (char *dest, const char *src, const char *str)
  505. {
  506. size_t len;
  507. if (src)
  508. len = strcspn (src, str);
  509. else
  510. return NULL;
  511. if (dest == NULL || strlen (dest) < len)
  512. dest = realloc (dest, len + 1);
  513. if (dest == NULL)
  514. die (STATE_UNKNOWN, _("failed realloc in strpcpy\n"));
  515. strncpy (dest, src, len);
  516. dest[len] = '\0';
  517. return dest;
  518. }
  519. /******************************************************************************
  520. *
  521. * Like strscat, except only the portion of the source string up to
  522. * the provided delimiter is copied.
  523. *
  524. * str = strpcpy(str,"This is a line of text with no trailing newline","x");
  525. * str = strpcat(str,"This is a line of text with no trailing newline","x");
  526. * printf("%s\n",str);
  527. *
  528. *This is a line of texThis is a line of tex
  529. *
  530. *****************************************************************************/
  531. char *
  532. strpcat (char *dest, const char *src, const char *str)
  533. {
  534. size_t len, l2;
  535. if (dest)
  536. len = strlen (dest);
  537. else
  538. len = 0;
  539. if (src) {
  540. l2 = strcspn (src, str);
  541. }
  542. else {
  543. return dest;
  544. }
  545. dest = realloc (dest, len + l2 + 1);
  546. if (dest == NULL)
  547. die (STATE_UNKNOWN, _("failed malloc in strscat\n"));
  548. strncpy (dest + len, src, l2);
  549. dest[len + l2] = '\0';
  550. return dest;
  551. }
  552. #ifndef HAVE_BASENAME
  553. /* function modified from coreutils base_name function - see ACKNOWLEDGEMENTS */
  554. char *basename(const char *path) {
  555. char const *base = path;
  556. char const *p;
  557. for (p = base; *p; p++) {
  558. if (*p == '/') {
  559. /* Treat multiple adjacent slashes like single slash */
  560. do p++;
  561. while (*p == '/');
  562. /* If filename ends in slash, use trailing slash
  563. as basename if no non-slashes found */
  564. if (! *p) {
  565. if (*base == '/')
  566. base = p - 1;
  567. break;
  568. }
  569. /* *p is non-slash preceded by slash */
  570. base = p;
  571. }
  572. }
  573. return (char *) base;
  574. }
  575. #endif
  576. /******************************************************************************
  577. *
  578. * Print perfdata in a standard format
  579. *
  580. ******************************************************************************/
  581. char *perfdata (const char *label,
  582. long int val,
  583. const char *uom,
  584. int warnp,
  585. long int warn,
  586. int critp,
  587. long int crit,
  588. int minp,
  589. long int minv,
  590. int maxp,
  591. long int maxv)
  592. {
  593. char *data = NULL;
  594. if (strpbrk (label, "'= "))
  595. asprintf (&data, "'%s'=%ld%s;", label, val, uom);
  596. else
  597. asprintf (&data, "%s=%ld%s;", label, val, uom);
  598. if (warnp)
  599. asprintf (&data, "%s%ld;", data, warn);
  600. else
  601. asprintf (&data, "%s;", data);
  602. if (critp)
  603. asprintf (&data, "%s%ld;", data, crit);
  604. else
  605. asprintf (&data, "%s;", data);
  606. if (minp)
  607. asprintf (&data, "%s%ld", data, minv);
  608. if (maxp)
  609. asprintf (&data, "%s;%ld", data, maxv);
  610. return data;
  611. }
  612. char *fperfdata (const char *label,
  613. double val,
  614. const char *uom,
  615. int warnp,
  616. double warn,
  617. int critp,
  618. double crit,
  619. int minp,
  620. double minv,
  621. int maxp,
  622. double maxv)
  623. {
  624. char *data = NULL;
  625. if (strpbrk (label, "'= "))
  626. asprintf (&data, "'%s'=", label);
  627. else
  628. asprintf (&data, "%s=", label);
  629. asprintf (&data, "%s%f", data, val);
  630. asprintf (&data, "%s%s;", data, uom);
  631. if (warnp)
  632. asprintf (&data, "%s%f", data, warn);
  633. asprintf (&data, "%s;", data);
  634. if (critp)
  635. asprintf (&data, "%s%f", data, crit);
  636. asprintf (&data, "%s;", data);
  637. if (minp)
  638. asprintf (&data, "%s%f", data, minv);
  639. if (maxp) {
  640. asprintf (&data, "%s;", data);
  641. asprintf (&data, "%s%f", data, maxv);
  642. }
  643. return data;
  644. }
  645. char *np_escaped_string (const char *string) {
  646. char *data;
  647. int i, j=0;
  648. data = strdup(string);
  649. for (i=0; data[i]; i++) {
  650. if (data[i] == '\\') {
  651. switch(data[++i]) {
  652. case 'n':
  653. data[j++] = '\n';
  654. break;
  655. case 'r':
  656. data[j++] = '\r';
  657. break;
  658. case 't':
  659. data[j++] = '\t';
  660. break;
  661. case '\\':
  662. data[j++] = '\\';
  663. break;
  664. default:
  665. data[j++] = data[i];
  666. }
  667. } else {
  668. data[j++] = data[i];
  669. }
  670. }
  671. data[j] = '\0';
  672. return data;
  673. }