utils.c 13 KB

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