utils.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581
  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. #ifdef NEED_GETTIMEOFDAY
  235. int
  236. gettimeofday (struct timeval *tv, struct timezone *tz)
  237. {
  238. tv->tv_usec = 0;
  239. tv->tv_sec = (long) time ((time_t) 0);
  240. }
  241. #endif
  242. double
  243. delta_time (struct timeval tv)
  244. {
  245. struct timeval now;
  246. gettimeofday (&now, NULL);
  247. return ((double)(now.tv_sec - tv.tv_sec) + (double)(now.tv_usec - tv.tv_usec) / (double)1000000);
  248. }
  249. long
  250. deltime (struct timeval tv)
  251. {
  252. struct timeval now;
  253. gettimeofday (&now, NULL);
  254. return (now.tv_sec - tv.tv_sec)*1000000 + now.tv_usec - tv.tv_usec;
  255. }
  256. void
  257. strip (char *buffer)
  258. {
  259. size_t x;
  260. int i;
  261. for (x = strlen (buffer); x >= 1; x--) {
  262. i = x - 1;
  263. if (buffer[i] == ' ' ||
  264. buffer[i] == '\r' || buffer[i] == '\n' || buffer[i] == '\t')
  265. buffer[i] = '\0';
  266. else
  267. break;
  268. }
  269. return;
  270. }
  271. /******************************************************************************
  272. *
  273. * Copies one string to another. Any previously existing data in
  274. * the destination string is lost.
  275. *
  276. * Example:
  277. *
  278. * char *str=NULL;
  279. * str = strscpy("This is a line of text with no trailing newline");
  280. *
  281. *****************************************************************************/
  282. char *
  283. strscpy (char *dest, const char *src)
  284. {
  285. if (src == NULL)
  286. return NULL;
  287. asprintf (&dest, "%s", src);
  288. return dest;
  289. }
  290. /******************************************************************************
  291. *
  292. * Returns a pointer to the next line of a multiline string buffer
  293. *
  294. * Given a pointer string, find the text following the next sequence
  295. * of \r and \n characters. This has the effect of skipping blank
  296. * lines as well
  297. *
  298. * Example:
  299. *
  300. * Given text as follows:
  301. *
  302. * ==============================
  303. * This
  304. * is
  305. * a
  306. *
  307. * multiline string buffer
  308. * ==============================
  309. *
  310. * int i=0;
  311. * char *str=NULL;
  312. * char *ptr=NULL;
  313. * str = strscpy(str,"This\nis\r\na\n\nmultiline string buffer\n");
  314. * ptr = str;
  315. * while (ptr) {
  316. * printf("%d %s",i++,firstword(ptr));
  317. * ptr = strnl(ptr);
  318. * }
  319. *
  320. * Produces the following:
  321. *
  322. * 1 This
  323. * 2 is
  324. * 3 a
  325. * 4 multiline
  326. *
  327. * NOTE: The 'firstword()' function is conceptual only and does not
  328. * exist in this package.
  329. *
  330. * NOTE: Although the second 'ptr' variable is not strictly needed in
  331. * this example, it is good practice with these utilities. Once
  332. * the * pointer is advance in this manner, it may no longer be
  333. * handled with * realloc(). So at the end of the code fragment
  334. * above, * strscpy(str,"foo") work perfectly fine, but
  335. * strscpy(ptr,"foo") will * cause the the program to crash with
  336. * a segmentation fault.
  337. *
  338. *****************************************************************************/
  339. char *
  340. strnl (char *str)
  341. {
  342. size_t len;
  343. if (str == NULL)
  344. return NULL;
  345. str = strpbrk (str, "\r\n");
  346. if (str == NULL)
  347. return NULL;
  348. len = strspn (str, "\r\n");
  349. if (str[len] == '\0')
  350. return NULL;
  351. str += len;
  352. if (strlen (str) == 0)
  353. return NULL;
  354. return str;
  355. }
  356. /******************************************************************************
  357. *
  358. * Like strscpy, except only the portion of the source string up to
  359. * the provided delimiter is copied.
  360. *
  361. * Example:
  362. *
  363. * str = strpcpy(str,"This is a line of text with no trailing newline","x");
  364. * printf("%s\n",str);
  365. *
  366. * Produces:
  367. *
  368. *This is a line of te
  369. *
  370. *****************************************************************************/
  371. char *
  372. strpcpy (char *dest, const char *src, const char *str)
  373. {
  374. size_t len;
  375. if (src)
  376. len = strcspn (src, str);
  377. else
  378. return NULL;
  379. if (dest == NULL || strlen (dest) < len)
  380. dest = realloc (dest, len + 1);
  381. if (dest == NULL)
  382. die (STATE_UNKNOWN, _("failed realloc in strpcpy\n"));
  383. strncpy (dest, src, len);
  384. dest[len] = '\0';
  385. return dest;
  386. }
  387. /******************************************************************************
  388. *
  389. * Like strscat, except only the portion of the source string up to
  390. * the provided delimiter is copied.
  391. *
  392. * str = strpcpy(str,"This is a line of text with no trailing newline","x");
  393. * str = strpcat(str,"This is a line of text with no trailing newline","x");
  394. * printf("%s\n",str);
  395. *
  396. *This is a line of texThis is a line of tex
  397. *
  398. *****************************************************************************/
  399. char *
  400. strpcat (char *dest, const char *src, const char *str)
  401. {
  402. size_t len, l2;
  403. if (dest)
  404. len = strlen (dest);
  405. else
  406. len = 0;
  407. if (src) {
  408. l2 = strcspn (src, str);
  409. }
  410. else {
  411. return dest;
  412. }
  413. dest = realloc (dest, len + l2 + 1);
  414. if (dest == NULL)
  415. die (STATE_UNKNOWN, _("failed malloc in strscat\n"));
  416. strncpy (dest + len, src, l2);
  417. dest[len + l2] = '\0';
  418. return dest;
  419. }
  420. /******************************************************************************
  421. *
  422. * Print perfdata in a standard format
  423. *
  424. ******************************************************************************/
  425. char *perfdata (const char *label,
  426. long int val,
  427. const char *uom,
  428. int warnp,
  429. long int warn,
  430. int critp,
  431. long int crit,
  432. int minp,
  433. long int minv,
  434. int maxp,
  435. long int maxv)
  436. {
  437. char *data = NULL;
  438. if (strpbrk (label, "'= "))
  439. asprintf (&data, "'%s'=%ld%s;", label, val, uom);
  440. else
  441. asprintf (&data, "%s=%ld%s;", label, val, uom);
  442. if (warnp)
  443. asprintf (&data, "%s%ld;", data, warn);
  444. else
  445. asprintf (&data, "%s;", data);
  446. if (critp)
  447. asprintf (&data, "%s%ld;", data, crit);
  448. else
  449. asprintf (&data, "%s;", data);
  450. if (minp)
  451. asprintf (&data, "%s%ld", data, minv);
  452. if (maxp)
  453. asprintf (&data, "%s;%ld", data, maxv);
  454. return data;
  455. }
  456. char *fperfdata (const char *label,
  457. double val,
  458. const char *uom,
  459. int warnp,
  460. double warn,
  461. int critp,
  462. double crit,
  463. int minp,
  464. double minv,
  465. int maxp,
  466. double maxv)
  467. {
  468. char *data = NULL;
  469. if (strpbrk (label, "'= "))
  470. asprintf (&data, "'%s'=", label);
  471. else
  472. asprintf (&data, "%s=", label);
  473. asprintf (&data, "%s%f", data, val);
  474. asprintf (&data, "%s%s;", data, uom);
  475. if (warnp)
  476. asprintf (&data, "%s%f", data, warn);
  477. asprintf (&data, "%s;", data);
  478. if (critp)
  479. asprintf (&data, "%s%f", data, crit);
  480. asprintf (&data, "%s;", data);
  481. if (minp)
  482. asprintf (&data, "%s%f", data, minv);
  483. if (maxp) {
  484. asprintf (&data, "%s;", data);
  485. asprintf (&data, "%s%f", data, maxv);
  486. }
  487. return data;
  488. }