utils.c 11 KB

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