utils.c 11 KB

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