utils.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603
  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 "utils_base.h"
  18. #include <stdarg.h>
  19. #include <limits.h>
  20. #include <arpa/inet.h>
  21. extern void print_usage (void);
  22. extern const char *progname;
  23. #define STRLEN 64
  24. #define TXTBLK 128
  25. /* **************************************************************************
  26. * max_state(STATE_x, STATE_y)
  27. * compares STATE_x to STATE_y and returns result based on the following
  28. * STATE_UNKNOWN < STATE_OK < STATE_WARNING < STATE_CRITICAL
  29. *
  30. * Note that numerically the above does not hold
  31. ****************************************************************************/
  32. int
  33. max_state (int a, int b)
  34. {
  35. if (a == STATE_CRITICAL || b == STATE_CRITICAL)
  36. return STATE_CRITICAL;
  37. else if (a == STATE_WARNING || b == STATE_WARNING)
  38. return STATE_WARNING;
  39. else if (a == STATE_OK || b == STATE_OK)
  40. return STATE_OK;
  41. else if (a == STATE_UNKNOWN || b == STATE_UNKNOWN)
  42. return STATE_UNKNOWN;
  43. else if (a == STATE_DEPENDENT || b == STATE_DEPENDENT)
  44. return STATE_DEPENDENT;
  45. else
  46. return max (a, b);
  47. }
  48. /* **************************************************************************
  49. * max_state_alt(STATE_x, STATE_y)
  50. * compares STATE_x to STATE_y and returns result based on the following
  51. * STATE_OK < STATE_DEPENDENT < STATE_UNKNOWN < STATE_WARNING < STATE_CRITICAL
  52. *
  53. * The main difference between max_state_alt and max_state it that it doesn't
  54. * allow setting a default to UNKNOWN. It will instead prioritixe any valid
  55. * non-OK state.
  56. ****************************************************************************/
  57. int
  58. max_state_alt (int a, int b)
  59. {
  60. if (a == STATE_CRITICAL || b == STATE_CRITICAL)
  61. return STATE_CRITICAL;
  62. else if (a == STATE_WARNING || b == STATE_WARNING)
  63. return STATE_WARNING;
  64. else if (a == STATE_UNKNOWN || b == STATE_UNKNOWN)
  65. return STATE_UNKNOWN;
  66. else if (a == STATE_DEPENDENT || b == STATE_DEPENDENT)
  67. return STATE_DEPENDENT;
  68. else if (a == STATE_OK || b == STATE_OK)
  69. return STATE_OK;
  70. else
  71. return max (a, b);
  72. }
  73. void usage (const char *msg)
  74. {
  75. printf ("%s\n", msg);
  76. print_usage ();
  77. exit (STATE_UNKNOWN);
  78. }
  79. void usage_va (const char *fmt, ...)
  80. {
  81. va_list ap;
  82. printf("%s: ", progname);
  83. va_start(ap, fmt);
  84. vprintf(fmt, ap);
  85. va_end(ap);
  86. printf("\n");
  87. exit (STATE_UNKNOWN);
  88. }
  89. void usage2(const char *msg, const char *arg)
  90. {
  91. printf ("%s: %s - %s\n", progname, msg, arg?arg:"(null)" );
  92. print_usage ();
  93. exit (STATE_UNKNOWN);
  94. }
  95. void
  96. usage3 (const char *msg, int arg)
  97. {
  98. printf ("%s: %s - %c\n", progname, msg, arg);
  99. print_usage();
  100. exit (STATE_UNKNOWN);
  101. }
  102. void
  103. usage4 (const char *msg)
  104. {
  105. printf ("%s: %s\n", progname, msg);
  106. print_usage();
  107. exit (STATE_UNKNOWN);
  108. }
  109. void
  110. usage5 (void)
  111. {
  112. print_usage();
  113. exit (STATE_UNKNOWN);
  114. }
  115. char *
  116. clean_revstring (const char *revstring)
  117. {
  118. char plugin_revision[STRLEN];
  119. plugin_revision[0] = 'v';
  120. if (sscanf (revstring,"$Revision: %[0-9.]", plugin_revision + 1) == 1)
  121. return strscpy (NULL, plugin_revision);
  122. else
  123. return strscpy (NULL, "N/A");
  124. }
  125. void
  126. print_revision (const char *command_name, const char *revision_string)
  127. {
  128. char plugin_revision[STRLEN];
  129. printf ("%s %s (%s %s)\n",
  130. command_name, clean_revstring(revision_string), PACKAGE, VERSION);
  131. }
  132. const char *
  133. state_text (int result)
  134. {
  135. switch (result) {
  136. case STATE_OK:
  137. return "OK";
  138. case STATE_WARNING:
  139. return "WARNING";
  140. case STATE_CRITICAL:
  141. return "CRITICAL";
  142. case STATE_DEPENDENT:
  143. return "DEPENDENT";
  144. default:
  145. return "UNKNOWN";
  146. }
  147. }
  148. void
  149. timeout_alarm_handler (int signo)
  150. {
  151. if (signo == SIGALRM) {
  152. printf (_("CRITICAL - Plugin timed out after %d seconds\n"),
  153. timeout_interval);
  154. exit (STATE_CRITICAL);
  155. }
  156. }
  157. int
  158. is_numeric (char *number)
  159. {
  160. char tmp[1];
  161. float x;
  162. if (!number)
  163. return FALSE;
  164. else if (sscanf (number, "%f%c", &x, tmp) == 1)
  165. return TRUE;
  166. else
  167. return FALSE;
  168. }
  169. int
  170. is_positive (char *number)
  171. {
  172. if (is_numeric (number) && atof (number) > 0.0)
  173. return TRUE;
  174. else
  175. return FALSE;
  176. }
  177. int
  178. is_negative (char *number)
  179. {
  180. if (is_numeric (number) && atof (number) < 0.0)
  181. return TRUE;
  182. else
  183. return FALSE;
  184. }
  185. int
  186. is_nonnegative (char *number)
  187. {
  188. if (is_numeric (number) && atof (number) >= 0.0)
  189. return TRUE;
  190. else
  191. return FALSE;
  192. }
  193. int
  194. is_percentage (char *number)
  195. {
  196. int x;
  197. if (is_numeric (number) && (x = atof (number)) >= 0 && x <= 100)
  198. return TRUE;
  199. else
  200. return FALSE;
  201. }
  202. int
  203. is_integer (char *number)
  204. {
  205. long int n;
  206. if (!number || (strspn (number, "-0123456789 ") != strlen (number)))
  207. return FALSE;
  208. n = strtol (number, NULL, 10);
  209. if (errno != ERANGE && n >= INT_MIN && n <= INT_MAX)
  210. return TRUE;
  211. else
  212. return FALSE;
  213. }
  214. int
  215. is_intpos (char *number)
  216. {
  217. if (is_integer (number) && atoi (number) > 0)
  218. return TRUE;
  219. else
  220. return FALSE;
  221. }
  222. int
  223. is_intneg (char *number)
  224. {
  225. if (is_integer (number) && atoi (number) < 0)
  226. return TRUE;
  227. else
  228. return FALSE;
  229. }
  230. int
  231. is_intnonneg (char *number)
  232. {
  233. if (is_integer (number) && atoi (number) >= 0)
  234. return TRUE;
  235. else
  236. return FALSE;
  237. }
  238. int
  239. is_intpercent (char *number)
  240. {
  241. int i;
  242. if (is_integer (number) && (i = atoi (number)) >= 0 && i <= 100)
  243. return TRUE;
  244. else
  245. return FALSE;
  246. }
  247. int
  248. is_option (char *str)
  249. {
  250. if (!str)
  251. return FALSE;
  252. else if (strspn (str, "-") == 1 || strspn (str, "-") == 2)
  253. return TRUE;
  254. else
  255. return FALSE;
  256. }
  257. #ifdef NEED_GETTIMEOFDAY
  258. int
  259. gettimeofday (struct timeval *tv, struct timezone *tz)
  260. {
  261. tv->tv_usec = 0;
  262. tv->tv_sec = (long) time ((time_t) 0);
  263. }
  264. #endif
  265. double
  266. delta_time (struct timeval tv)
  267. {
  268. struct timeval now;
  269. gettimeofday (&now, NULL);
  270. return ((double)(now.tv_sec - tv.tv_sec) + (double)(now.tv_usec - tv.tv_usec) / (double)1000000);
  271. }
  272. long
  273. deltime (struct timeval tv)
  274. {
  275. struct timeval now;
  276. gettimeofday (&now, NULL);
  277. return (now.tv_sec - tv.tv_sec)*1000000 + now.tv_usec - tv.tv_usec;
  278. }
  279. void
  280. strip (char *buffer)
  281. {
  282. size_t x;
  283. int i;
  284. for (x = strlen (buffer); x >= 1; x--) {
  285. i = x - 1;
  286. if (buffer[i] == ' ' ||
  287. buffer[i] == '\r' || buffer[i] == '\n' || buffer[i] == '\t')
  288. buffer[i] = '\0';
  289. else
  290. break;
  291. }
  292. return;
  293. }
  294. /******************************************************************************
  295. *
  296. * Copies one string to another. Any previously existing data in
  297. * the destination string is lost.
  298. *
  299. * Example:
  300. *
  301. * char *str=NULL;
  302. * str = strscpy("This is a line of text with no trailing newline");
  303. *
  304. *****************************************************************************/
  305. char *
  306. strscpy (char *dest, const char *src)
  307. {
  308. if (src == NULL)
  309. return NULL;
  310. asprintf (&dest, "%s", src);
  311. return dest;
  312. }
  313. /******************************************************************************
  314. *
  315. * Returns a pointer to the next line of a multiline string buffer
  316. *
  317. * Given a pointer string, find the text following the next sequence
  318. * of \r and \n characters. This has the effect of skipping blank
  319. * lines as well
  320. *
  321. * Example:
  322. *
  323. * Given text as follows:
  324. *
  325. * ==============================
  326. * This
  327. * is
  328. * a
  329. *
  330. * multiline string buffer
  331. * ==============================
  332. *
  333. * int i=0;
  334. * char *str=NULL;
  335. * char *ptr=NULL;
  336. * str = strscpy(str,"This\nis\r\na\n\nmultiline string buffer\n");
  337. * ptr = str;
  338. * while (ptr) {
  339. * printf("%d %s",i++,firstword(ptr));
  340. * ptr = strnl(ptr);
  341. * }
  342. *
  343. * Produces the following:
  344. *
  345. * 1 This
  346. * 2 is
  347. * 3 a
  348. * 4 multiline
  349. *
  350. * NOTE: The 'firstword()' function is conceptual only and does not
  351. * exist in this package.
  352. *
  353. * NOTE: Although the second 'ptr' variable is not strictly needed in
  354. * this example, it is good practice with these utilities. Once
  355. * the * pointer is advance in this manner, it may no longer be
  356. * handled with * realloc(). So at the end of the code fragment
  357. * above, * strscpy(str,"foo") work perfectly fine, but
  358. * strscpy(ptr,"foo") will * cause the the program to crash with
  359. * a segmentation fault.
  360. *
  361. *****************************************************************************/
  362. char *
  363. strnl (char *str)
  364. {
  365. size_t len;
  366. if (str == NULL)
  367. return NULL;
  368. str = strpbrk (str, "\r\n");
  369. if (str == NULL)
  370. return NULL;
  371. len = strspn (str, "\r\n");
  372. if (str[len] == '\0')
  373. return NULL;
  374. str += len;
  375. if (strlen (str) == 0)
  376. return NULL;
  377. return str;
  378. }
  379. /******************************************************************************
  380. *
  381. * Like strscpy, except only the portion of the source string up to
  382. * the provided delimiter is copied.
  383. *
  384. * Example:
  385. *
  386. * str = strpcpy(str,"This is a line of text with no trailing newline","x");
  387. * printf("%s\n",str);
  388. *
  389. * Produces:
  390. *
  391. *This is a line of te
  392. *
  393. *****************************************************************************/
  394. char *
  395. strpcpy (char *dest, const char *src, const char *str)
  396. {
  397. size_t len;
  398. if (src)
  399. len = strcspn (src, str);
  400. else
  401. return NULL;
  402. if (dest == NULL || strlen (dest) < len)
  403. dest = realloc (dest, len + 1);
  404. if (dest == NULL)
  405. die (STATE_UNKNOWN, _("failed realloc in strpcpy\n"));
  406. strncpy (dest, src, len);
  407. dest[len] = '\0';
  408. return dest;
  409. }
  410. /******************************************************************************
  411. *
  412. * Like strscat, except only the portion of the source string up to
  413. * the provided delimiter is copied.
  414. *
  415. * str = strpcpy(str,"This is a line of text with no trailing newline","x");
  416. * str = strpcat(str,"This is a line of text with no trailing newline","x");
  417. * printf("%s\n",str);
  418. *
  419. *This is a line of texThis is a line of tex
  420. *
  421. *****************************************************************************/
  422. char *
  423. strpcat (char *dest, const char *src, const char *str)
  424. {
  425. size_t len, l2;
  426. if (dest)
  427. len = strlen (dest);
  428. else
  429. len = 0;
  430. if (src) {
  431. l2 = strcspn (src, str);
  432. }
  433. else {
  434. return dest;
  435. }
  436. dest = realloc (dest, len + l2 + 1);
  437. if (dest == NULL)
  438. die (STATE_UNKNOWN, _("failed malloc in strscat\n"));
  439. strncpy (dest + len, src, l2);
  440. dest[len + l2] = '\0';
  441. return dest;
  442. }
  443. /******************************************************************************
  444. *
  445. * Print perfdata in a standard format
  446. *
  447. ******************************************************************************/
  448. char *perfdata (const char *label,
  449. long int val,
  450. const char *uom,
  451. int warnp,
  452. long int warn,
  453. int critp,
  454. long int crit,
  455. int minp,
  456. long int minv,
  457. int maxp,
  458. long int maxv)
  459. {
  460. char *data = NULL;
  461. if (strpbrk (label, "'= "))
  462. asprintf (&data, "'%s'=%ld%s;", label, val, uom);
  463. else
  464. asprintf (&data, "%s=%ld%s;", label, val, uom);
  465. if (warnp)
  466. asprintf (&data, "%s%ld;", data, warn);
  467. else
  468. asprintf (&data, "%s;", data);
  469. if (critp)
  470. asprintf (&data, "%s%ld;", data, crit);
  471. else
  472. asprintf (&data, "%s;", data);
  473. if (minp)
  474. asprintf (&data, "%s%ld", data, minv);
  475. if (maxp)
  476. asprintf (&data, "%s;%ld", data, maxv);
  477. return data;
  478. }
  479. char *fperfdata (const char *label,
  480. double val,
  481. const char *uom,
  482. int warnp,
  483. double warn,
  484. int critp,
  485. double crit,
  486. int minp,
  487. double minv,
  488. int maxp,
  489. double maxv)
  490. {
  491. char *data = NULL;
  492. if (strpbrk (label, "'= "))
  493. asprintf (&data, "'%s'=", label);
  494. else
  495. asprintf (&data, "%s=", label);
  496. asprintf (&data, "%s%f", data, val);
  497. asprintf (&data, "%s%s;", data, uom);
  498. if (warnp)
  499. asprintf (&data, "%s%f", data, warn);
  500. asprintf (&data, "%s;", data);
  501. if (critp)
  502. asprintf (&data, "%s%f", data, crit);
  503. asprintf (&data, "%s;", data);
  504. if (minp)
  505. asprintf (&data, "%s%f", data, minv);
  506. if (maxp) {
  507. asprintf (&data, "%s;", data);
  508. asprintf (&data, "%s%f", data, maxv);
  509. }
  510. return data;
  511. }