utils.c 9.4 KB

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