utils.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636
  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_threshold_start (threshold *this, double value) {
  235. this->start = value;
  236. this->start_infinity = FALSE;
  237. }
  238. void set_threshold_end (threshold *this, double value) {
  239. this->end = value;
  240. this->end_infinity = FALSE;
  241. }
  242. threshold
  243. *parse_threshold (char *str) {
  244. threshold *temp_threshold;
  245. double start;
  246. double end;
  247. char *end_str;
  248. temp_threshold = (threshold *) malloc(sizeof(threshold));
  249. /* Set defaults */
  250. temp_threshold->start = 0;
  251. temp_threshold->start_infinity = FALSE;
  252. temp_threshold->end = 0;
  253. temp_threshold->end_infinity = TRUE;
  254. temp_threshold->alert_on = OUTSIDE;
  255. if (str[0] == '@') {
  256. temp_threshold->alert_on = INSIDE;
  257. str++;
  258. }
  259. end_str = index(str, ':');
  260. if (end_str != NULL) {
  261. if (str[0] == '~') {
  262. temp_threshold->start_infinity = TRUE;
  263. } else {
  264. start = strtod(str, NULL); /* Will stop at the ':' */
  265. set_threshold_start(temp_threshold, 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_threshold_end(temp_threshold, end);
  274. }
  275. if (temp_threshold->start_infinity == TRUE ||
  276. temp_threshold->end_infinity == TRUE ||
  277. temp_threshold->start <= temp_threshold->end) {
  278. return temp_threshold;
  279. }
  280. free(temp_threshold);
  281. return NULL;
  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. asprintf (&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. * Print perfdata in a standard format
  472. *
  473. ******************************************************************************/
  474. char *perfdata (const char *label,
  475. long int val,
  476. const char *uom,
  477. int warnp,
  478. long int warn,
  479. int critp,
  480. long int crit,
  481. int minp,
  482. long int minv,
  483. int maxp,
  484. long int maxv)
  485. {
  486. char *data = NULL;
  487. if (strpbrk (label, "'= "))
  488. asprintf (&data, "'%s'=%ld%s;", label, val, uom);
  489. else
  490. asprintf (&data, "%s=%ld%s;", label, val, uom);
  491. if (warnp)
  492. asprintf (&data, "%s%ld;", data, warn);
  493. else
  494. asprintf (&data, "%s;", data);
  495. if (critp)
  496. asprintf (&data, "%s%ld;", data, crit);
  497. else
  498. asprintf (&data, "%s;", data);
  499. if (minp)
  500. asprintf (&data, "%s%ld", data, minv);
  501. if (maxp)
  502. asprintf (&data, "%s;%ld", data, maxv);
  503. return data;
  504. }
  505. char *fperfdata (const char *label,
  506. double val,
  507. const char *uom,
  508. int warnp,
  509. double warn,
  510. int critp,
  511. double crit,
  512. int minp,
  513. double minv,
  514. int maxp,
  515. double maxv)
  516. {
  517. char *data = NULL;
  518. if (strpbrk (label, "'= "))
  519. asprintf (&data, "'%s'=", label);
  520. else
  521. asprintf (&data, "%s=", label);
  522. asprintf (&data, "%s%f", data, val);
  523. asprintf (&data, "%s%s;", data, uom);
  524. if (warnp)
  525. asprintf (&data, "%s%f", data, warn);
  526. asprintf (&data, "%s;", data);
  527. if (critp)
  528. asprintf (&data, "%s%f", data, crit);
  529. asprintf (&data, "%s;", data);
  530. if (minp)
  531. asprintf (&data, "%s%f", data, minv);
  532. if (maxp) {
  533. asprintf (&data, "%s;", data);
  534. asprintf (&data, "%s%f", data, maxv);
  535. }
  536. return data;
  537. }