utils.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566
  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. #include "config.h"
  14. #include "common.h"
  15. #include <stdarg.h>
  16. #include <limits.h>
  17. #include <arpa/inet.h>
  18. extern int timeout_interval;
  19. extern const char *progname;
  20. void support (void);
  21. char *clean_revstring (const char *);
  22. void print_revision (const char *, const char *);
  23. void die (int result, const char *fmt, ...);
  24. void terminate (int result, const char *fmt, ...);
  25. RETSIGTYPE timeout_alarm_handler (int);
  26. int is_integer (char *);
  27. int is_intpos (char *);
  28. int is_intneg (char *);
  29. int is_intnonneg (char *);
  30. int is_intpercent (char *);
  31. int is_numeric (char *);
  32. int is_positive (char *);
  33. int is_negative (char *);
  34. int is_nonnegative (char *);
  35. int is_percentage (char *);
  36. int is_option (char *str);
  37. double delta_time (struct timeval tv);
  38. void strip (char *);
  39. char *strscpy (char *dest, const char *src);
  40. char *strscat (char *dest, char *src);
  41. char *strnl (char *str);
  42. char *strpcpy (char *dest, const char *src, const char *str);
  43. char *strpcat (char *dest, const char *src, const char *str);
  44. char *state_text (int result);
  45. #define STRLEN 64
  46. #define TXTBLK 128
  47. /* **************************************************************************
  48. /* max_state(STATE_x, STATE_y)
  49. * compares STATE_x to STATE_y and returns result based on the following
  50. * STATE_UNKNOWN < STATE_OK < STATE_WARNING < STATE_CRITICAL
  51. *
  52. * Note that numerically the above does not hold
  53. ****************************************************************************/
  54. #define max(a,b) (((a)>(b))?(a):(b))
  55. int
  56. max_state (int a, int b)
  57. {
  58. if (a == STATE_CRITICAL || b == STATE_CRITICAL)
  59. return STATE_CRITICAL;
  60. else if (a == STATE_WARNING || b == STATE_WARNING)
  61. return STATE_WARNING;
  62. else if (a == STATE_OK || b == STATE_OK)
  63. return STATE_OK;
  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
  69. return max (a, b);
  70. }
  71. void usage (char *msg)
  72. {
  73. printf (msg);
  74. print_usage ();
  75. exit (STATE_UNKNOWN);
  76. }
  77. void usage2(char *msg, char *arg)
  78. {
  79. printf ("%s: %s - %s\n",progname,msg,arg);
  80. print_usage ();
  81. exit (STATE_UNKNOWN);
  82. }
  83. void
  84. usage3 (char *msg, char arg)
  85. {
  86. printf ("%s: %s - %c\n", progname, msg, arg);
  87. print_usage();
  88. exit (STATE_UNKNOWN);
  89. }
  90. void
  91. support (void)
  92. {
  93. printf
  94. ("Send email to nagios-users@lists.sourceforge.net if you have questions\n"
  95. "regarding use of this software. To submit patches or suggest improvements,\n"
  96. "send email to nagiosplug-devel@lists.sourceforge.net\n");
  97. }
  98. char *
  99. clean_revstring (const char *revstring)
  100. {
  101. char plugin_revision[STRLEN];
  102. if (sscanf (revstring,"$Revision: %[0-9.]",plugin_revision) == 1)
  103. return strscpy (NULL, plugin_revision);
  104. else
  105. return strscpy (NULL, "N/A");
  106. }
  107. void
  108. print_revision (const char *command_name, const char *revision_string)
  109. {
  110. char plugin_revision[STRLEN];
  111. if (sscanf (revision_string, "$Revision: %[0-9.]", plugin_revision) != 1)
  112. strncpy (plugin_revision, "N/A", STRLEN);
  113. printf ("%s (%s %s) %s\n",
  114. progname, PACKAGE, VERSION, plugin_revision);
  115. printf
  116. ("The nagios plugins come with ABSOLUTELY NO WARRANTY. You may redistribute\n"
  117. "copies of the plugins under the terms of the GNU General Public License.\n"
  118. "For more information about these matters, see the file named COPYING.\n");
  119. }
  120. char *
  121. state_text (int result)
  122. {
  123. switch (result) {
  124. case STATE_OK:
  125. return "OK";
  126. case STATE_WARNING:
  127. return "WARNING";
  128. case STATE_CRITICAL:
  129. return "CRITICAL";
  130. case STATE_DEPENDENT:
  131. return "DEPENDENT";
  132. default:
  133. return "UNKNOWN";
  134. }
  135. }
  136. void
  137. die (int result, const char *fmt, ...)
  138. {
  139. va_list ap;
  140. printf ("%s %s: ", sizeof (char) + index(progname, '_'), state_text(result));
  141. va_start (ap, fmt);
  142. vprintf (fmt, ap);
  143. va_end (ap);
  144. exit (result);
  145. }
  146. void
  147. terminate (int result, const char *fmt, ...)
  148. {
  149. va_list ap;
  150. va_start (ap, fmt);
  151. vprintf (fmt, ap);
  152. va_end (ap);
  153. exit (result);
  154. }
  155. void
  156. timeout_alarm_handler (int signo)
  157. {
  158. if (signo == SIGALRM) {
  159. printf ("CRITICAL - Plugin timed out after %d seconds\n",
  160. timeout_interval);
  161. exit (STATE_CRITICAL);
  162. }
  163. }
  164. int
  165. is_numeric (char *number)
  166. {
  167. char tmp[1];
  168. float x;
  169. if (!number)
  170. return FALSE;
  171. else if (sscanf (number, "%f%c", &x, tmp) == 1)
  172. return TRUE;
  173. else
  174. return FALSE;
  175. }
  176. int
  177. is_positive (char *number)
  178. {
  179. if (is_numeric (number) && atof (number) > 0.0)
  180. return TRUE;
  181. else
  182. return FALSE;
  183. }
  184. int
  185. is_negative (char *number)
  186. {
  187. if (is_numeric (number) && atof (number) < 0.0)
  188. return TRUE;
  189. else
  190. return FALSE;
  191. }
  192. int
  193. is_nonnegative (char *number)
  194. {
  195. if (is_numeric (number) && atof (number) >= 0.0)
  196. return TRUE;
  197. else
  198. return FALSE;
  199. }
  200. int
  201. is_percentage (char *number)
  202. {
  203. int x;
  204. if (is_numeric (number) && (x = atof (number)) >= 0 && x <= 100)
  205. return TRUE;
  206. else
  207. return FALSE;
  208. }
  209. int
  210. is_integer (char *number)
  211. {
  212. long int n;
  213. if (!number || (strspn (number, "-0123456789 ") != strlen (number)))
  214. return FALSE;
  215. n = strtol (number, NULL, 10);
  216. if (errno != ERANGE && n >= INT_MIN && n <= INT_MAX)
  217. return TRUE;
  218. else
  219. return FALSE;
  220. }
  221. int
  222. is_intpos (char *number)
  223. {
  224. if (is_integer (number) && atoi (number) > 0)
  225. return TRUE;
  226. else
  227. return FALSE;
  228. }
  229. int
  230. is_intneg (char *number)
  231. {
  232. if (is_integer (number) && atoi (number) < 0)
  233. return TRUE;
  234. else
  235. return FALSE;
  236. }
  237. int
  238. is_intnonneg (char *number)
  239. {
  240. if (is_integer (number) && atoi (number) >= 0)
  241. return TRUE;
  242. else
  243. return FALSE;
  244. }
  245. int
  246. is_intpercent (char *number)
  247. {
  248. int i;
  249. if (is_integer (number) && (i = atoi (number)) >= 0 && i <= 100)
  250. return TRUE;
  251. else
  252. return FALSE;
  253. }
  254. int
  255. is_option (char *str)
  256. {
  257. if (!str)
  258. return FALSE;
  259. else if (strspn (str, "-") == 1 || strspn (str, "-") == 2)
  260. return TRUE;
  261. else
  262. return FALSE;
  263. }
  264. #ifdef NEED_GETTIMEOFDAY
  265. int
  266. gettimeofday (struct timeval *tv, struct timezone *tz)
  267. {
  268. tv->tv_usec = 0;
  269. tv->tv_sec = (long) time ((time_t) 0);
  270. }
  271. #endif
  272. double
  273. delta_time (struct timeval tv)
  274. {
  275. struct timeval now;
  276. gettimeofday (&now, NULL);
  277. return ((double)(now.tv_sec - tv.tv_sec) + (double)(now.tv_usec - tv.tv_usec) / (double)1000000);
  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. * Concatenates one string to the end of another
  316. *
  317. * Given a pointer destination string, which may or may not already
  318. * hold some text, and a source string with additional text (possibly
  319. * NULL or empty), returns a pointer to a string that is the first
  320. * string with the second concatenated to it. Uses realloc to free
  321. * memory held by the dest argument if new storage space is required.
  322. *
  323. * Example:
  324. *
  325. * char *str=NULL;
  326. * str = strscpy("This is a line of text with no trailing newline");
  327. * str = strscat(str,"\n");
  328. *
  329. *****************************************************************************/
  330. char *
  331. strscat (char *dest, char *src)
  332. {
  333. if (dest == NULL)
  334. return src;
  335. if (src != NULL)
  336. asprintf (&dest, "%s%s", dest, src);
  337. return dest;
  338. }
  339. /******************************************************************************
  340. *
  341. * Returns a pointer to the next line of a multiline string buffer
  342. *
  343. * Given a pointer string, find the text following the next sequence
  344. * of \r and \n characters. This has the effect of skipping blank
  345. * lines as well
  346. *
  347. * Example:
  348. *
  349. * Given text as follows:
  350. *
  351. * ==============================
  352. * This
  353. * is
  354. * a
  355. *
  356. * multiline string buffer
  357. * ==============================
  358. *
  359. * int i=0;
  360. * char *str=NULL;
  361. * char *ptr=NULL;
  362. * str = strscpy(str,"This\nis\r\na\n\nmultiline string buffer\n");
  363. * ptr = str;
  364. * while (ptr) {
  365. * printf("%d %s",i++,firstword(ptr));
  366. * ptr = strnl(ptr);
  367. * }
  368. *
  369. * Produces the following:
  370. *
  371. * 1 This
  372. * 2 is
  373. * 3 a
  374. * 4 multiline
  375. *
  376. * NOTE: The 'firstword()' function is conceptual only and does not
  377. * exist in this package.
  378. *
  379. * NOTE: Although the second 'ptr' variable is not strictly needed in
  380. * this example, it is good practice with these utilities. Once
  381. * the * pointer is advance in this manner, it may no longer be
  382. * handled with * realloc(). So at the end of the code fragment
  383. * above, * strscpy(str,"foo") work perfectly fine, but
  384. * strscpy(ptr,"foo") will * cause the the program to crash with
  385. * a segmentation fault.
  386. *
  387. *****************************************************************************/
  388. char *
  389. strnl (char *str)
  390. {
  391. size_t len;
  392. if (str == NULL)
  393. return NULL;
  394. str = strpbrk (str, "\r\n");
  395. if (str == NULL)
  396. return NULL;
  397. len = strspn (str, "\r\n");
  398. if (str[len] == '\0')
  399. return NULL;
  400. str += len;
  401. if (strlen (str) == 0)
  402. return NULL;
  403. return str;
  404. }
  405. /******************************************************************************
  406. *
  407. * Like strscpy, except only the portion of the source string up to
  408. * the provided delimiter is copied.
  409. *
  410. * Example:
  411. *
  412. * str = strpcpy(str,"This is a line of text with no trailing newline","x");
  413. * printf("%s\n",str);
  414. *
  415. * Produces:
  416. *
  417. *This is a line of te
  418. *
  419. *****************************************************************************/
  420. char *
  421. strpcpy (char *dest, const char *src, const char *str)
  422. {
  423. size_t len;
  424. if (src)
  425. len = strcspn (src, str);
  426. else
  427. return NULL;
  428. if (dest == NULL || strlen (dest) < len)
  429. dest = realloc (dest, len + 1);
  430. if (dest == NULL)
  431. terminate (STATE_UNKNOWN, "failed realloc in strpcpy\n");
  432. strncpy (dest, src, len);
  433. dest[len] = '\0';
  434. return dest;
  435. }
  436. /******************************************************************************
  437. *
  438. * Like strscat, except only the portion of the source string up to
  439. * the provided delimiter is copied.
  440. *
  441. * str = strpcpy(str,"This is a line of text with no trailing newline","x");
  442. * str = strpcat(str,"This is a line of text with no trailing newline","x");
  443. * printf("%s\n",str);
  444. *
  445. *This is a line of texThis is a line of tex
  446. *
  447. *****************************************************************************/
  448. char *
  449. strpcat (char *dest, const char *src, const char *str)
  450. {
  451. size_t len, l2;
  452. if (dest)
  453. len = strlen (dest);
  454. else
  455. len = 0;
  456. if (src) {
  457. l2 = strcspn (src, str);
  458. }
  459. else {
  460. return dest;
  461. }
  462. dest = realloc (dest, len + l2 + 1);
  463. if (dest == NULL)
  464. terminate (STATE_UNKNOWN, "failed malloc in strscat\n");
  465. strncpy (dest + len, src, l2);
  466. dest[len + l2] = '\0';
  467. return dest;
  468. }