utils.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500
  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 ("%s", 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, int 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. const 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. va_start (ap, fmt);
  117. vprintf (fmt, ap);
  118. va_end (ap);
  119. exit (result);
  120. }
  121. void
  122. timeout_alarm_handler (int signo)
  123. {
  124. if (signo == SIGALRM) {
  125. printf ("CRITICAL - Plugin timed out after %d seconds\n",
  126. timeout_interval);
  127. exit (STATE_CRITICAL);
  128. }
  129. }
  130. int
  131. is_numeric (char *number)
  132. {
  133. char tmp[1];
  134. float x;
  135. if (!number)
  136. return FALSE;
  137. else if (sscanf (number, "%f%c", &x, tmp) == 1)
  138. return TRUE;
  139. else
  140. return FALSE;
  141. }
  142. int
  143. is_positive (char *number)
  144. {
  145. if (is_numeric (number) && atof (number) > 0.0)
  146. return TRUE;
  147. else
  148. return FALSE;
  149. }
  150. int
  151. is_negative (char *number)
  152. {
  153. if (is_numeric (number) && atof (number) < 0.0)
  154. return TRUE;
  155. else
  156. return FALSE;
  157. }
  158. int
  159. is_nonnegative (char *number)
  160. {
  161. if (is_numeric (number) && atof (number) >= 0.0)
  162. return TRUE;
  163. else
  164. return FALSE;
  165. }
  166. int
  167. is_percentage (char *number)
  168. {
  169. int x;
  170. if (is_numeric (number) && (x = atof (number)) >= 0 && x <= 100)
  171. return TRUE;
  172. else
  173. return FALSE;
  174. }
  175. int
  176. is_integer (char *number)
  177. {
  178. long int n;
  179. if (!number || (strspn (number, "-0123456789 ") != strlen (number)))
  180. return FALSE;
  181. n = strtol (number, NULL, 10);
  182. if (errno != ERANGE && n >= INT_MIN && n <= INT_MAX)
  183. return TRUE;
  184. else
  185. return FALSE;
  186. }
  187. int
  188. is_intpos (char *number)
  189. {
  190. if (is_integer (number) && atoi (number) > 0)
  191. return TRUE;
  192. else
  193. return FALSE;
  194. }
  195. int
  196. is_intneg (char *number)
  197. {
  198. if (is_integer (number) && atoi (number) < 0)
  199. return TRUE;
  200. else
  201. return FALSE;
  202. }
  203. int
  204. is_intnonneg (char *number)
  205. {
  206. if (is_integer (number) && atoi (number) >= 0)
  207. return TRUE;
  208. else
  209. return FALSE;
  210. }
  211. int
  212. is_intpercent (char *number)
  213. {
  214. int i;
  215. if (is_integer (number) && (i = atoi (number)) >= 0 && i <= 100)
  216. return TRUE;
  217. else
  218. return FALSE;
  219. }
  220. int
  221. is_option (char *str)
  222. {
  223. if (!str)
  224. return FALSE;
  225. else if (strspn (str, "-") == 1 || strspn (str, "-") == 2)
  226. return TRUE;
  227. else
  228. return FALSE;
  229. }
  230. #ifdef NEED_GETTIMEOFDAY
  231. int
  232. gettimeofday (struct timeval *tv, struct timezone *tz)
  233. {
  234. tv->tv_usec = 0;
  235. tv->tv_sec = (long) time ((time_t) 0);
  236. }
  237. #endif
  238. double
  239. delta_time (struct timeval tv)
  240. {
  241. struct timeval now;
  242. gettimeofday (&now, NULL);
  243. return ((double)(now.tv_sec - tv.tv_sec) + (double)(now.tv_usec - tv.tv_usec) / (double)1000000);
  244. }
  245. long
  246. deltime (struct timeval tv)
  247. {
  248. struct timeval now;
  249. gettimeofday (&now, NULL);
  250. return (now.tv_sec - tv.tv_sec)*1000000 + now.tv_usec - tv.tv_usec;
  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. * Returns a pointer to the next line of a multiline string buffer
  289. *
  290. * Given a pointer string, find the text following the next sequence
  291. * of \r and \n characters. This has the effect of skipping blank
  292. * lines as well
  293. *
  294. * Example:
  295. *
  296. * Given text as follows:
  297. *
  298. * ==============================
  299. * This
  300. * is
  301. * a
  302. *
  303. * multiline string buffer
  304. * ==============================
  305. *
  306. * int i=0;
  307. * char *str=NULL;
  308. * char *ptr=NULL;
  309. * str = strscpy(str,"This\nis\r\na\n\nmultiline string buffer\n");
  310. * ptr = str;
  311. * while (ptr) {
  312. * printf("%d %s",i++,firstword(ptr));
  313. * ptr = strnl(ptr);
  314. * }
  315. *
  316. * Produces the following:
  317. *
  318. * 1 This
  319. * 2 is
  320. * 3 a
  321. * 4 multiline
  322. *
  323. * NOTE: The 'firstword()' function is conceptual only and does not
  324. * exist in this package.
  325. *
  326. * NOTE: Although the second 'ptr' variable is not strictly needed in
  327. * this example, it is good practice with these utilities. Once
  328. * the * pointer is advance in this manner, it may no longer be
  329. * handled with * realloc(). So at the end of the code fragment
  330. * above, * strscpy(str,"foo") work perfectly fine, but
  331. * strscpy(ptr,"foo") will * cause the the program to crash with
  332. * a segmentation fault.
  333. *
  334. *****************************************************************************/
  335. char *
  336. strnl (char *str)
  337. {
  338. size_t len;
  339. if (str == NULL)
  340. return NULL;
  341. str = strpbrk (str, "\r\n");
  342. if (str == NULL)
  343. return NULL;
  344. len = strspn (str, "\r\n");
  345. if (str[len] == '\0')
  346. return NULL;
  347. str += len;
  348. if (strlen (str) == 0)
  349. return NULL;
  350. return str;
  351. }
  352. /******************************************************************************
  353. *
  354. * Like strscpy, except only the portion of the source string up to
  355. * the provided delimiter is copied.
  356. *
  357. * Example:
  358. *
  359. * str = strpcpy(str,"This is a line of text with no trailing newline","x");
  360. * printf("%s\n",str);
  361. *
  362. * Produces:
  363. *
  364. *This is a line of te
  365. *
  366. *****************************************************************************/
  367. char *
  368. strpcpy (char *dest, const char *src, const char *str)
  369. {
  370. size_t len;
  371. if (src)
  372. len = strcspn (src, str);
  373. else
  374. return NULL;
  375. if (dest == NULL || strlen (dest) < len)
  376. dest = realloc (dest, len + 1);
  377. if (dest == NULL)
  378. die (STATE_UNKNOWN, "failed realloc in strpcpy\n");
  379. strncpy (dest, src, len);
  380. dest[len] = '\0';
  381. return dest;
  382. }
  383. /******************************************************************************
  384. *
  385. * Like strscat, except only the portion of the source string up to
  386. * the provided delimiter is copied.
  387. *
  388. * str = strpcpy(str,"This is a line of text with no trailing newline","x");
  389. * str = strpcat(str,"This is a line of text with no trailing newline","x");
  390. * printf("%s\n",str);
  391. *
  392. *This is a line of texThis is a line of tex
  393. *
  394. *****************************************************************************/
  395. char *
  396. strpcat (char *dest, const char *src, const char *str)
  397. {
  398. size_t len, l2;
  399. if (dest)
  400. len = strlen (dest);
  401. else
  402. len = 0;
  403. if (src) {
  404. l2 = strcspn (src, str);
  405. }
  406. else {
  407. return dest;
  408. }
  409. dest = realloc (dest, len + l2 + 1);
  410. if (dest == NULL)
  411. die (STATE_UNKNOWN, "failed malloc in strscat\n");
  412. strncpy (dest + len, src, l2);
  413. dest[len + l2] = '\0';
  414. return dest;
  415. }