utils.c 11 KB

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