utils.c 11 KB

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