utils.c 14 KB

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