utils.c 10 KB

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