utils.c 12 KB

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