4
0

utils_base.h 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. #ifndef _UTILS_BASE_
  2. #define _UTILS_BASE_
  3. /* Header file for nagios plugins utils_base.c */
  4. #include "sha1.h"
  5. /* This file holds header information for thresholds - use this in preference to
  6. individual plugin logic */
  7. /* This has not been merged with utils.h because of problems with
  8. timeout_interval when other utils_*.h files use utils.h */
  9. /* Long term, add new functions to utils_base.h for common routines
  10. and utils_*.h for specific to plugin routines. If routines are
  11. placed in utils_*.h, then these can be tested with libtap */
  12. #define OUTSIDE 0
  13. #define INSIDE 1
  14. typedef struct range_struct {
  15. double start;
  16. int start_infinity; /* FALSE (default) or TRUE */
  17. double end;
  18. int end_infinity;
  19. int alert_on; /* OUTSIDE (default) or INSIDE */
  20. } range;
  21. typedef struct thresholds_struct {
  22. range *warning;
  23. range *critical;
  24. } thresholds;
  25. #define NP_STATE_FORMAT_VERSION 1
  26. typedef struct state_data_struct {
  27. time_t time;
  28. void *data;
  29. int length; /* Of binary data */
  30. } state_data;
  31. typedef struct state_key_struct {
  32. char *name;
  33. char *plugin_name;
  34. int data_version;
  35. char *_filename;
  36. state_data *state_data;
  37. } state_key;
  38. typedef struct np_struct {
  39. char *plugin_name;
  40. state_key *state;
  41. int argc;
  42. char **argv;
  43. } nagios_plugin;
  44. range *parse_range_string (char *);
  45. int _set_thresholds(thresholds **, char *, char *);
  46. void set_thresholds(thresholds **, char *, char *);
  47. void print_thresholds(const char *, thresholds *);
  48. int check_range(double, range *);
  49. int get_status(double, thresholds *);
  50. /* All possible characters in a threshold range */
  51. #define NP_THRESHOLDS_CHARS "-0123456789.:@~"
  52. char *np_escaped_string (const char *);
  53. void die (int, const char *, ...) __attribute__((noreturn,format(printf, 2, 3)));
  54. /* Return codes for _set_thresholds */
  55. #define NP_RANGE_UNPARSEABLE 1
  56. #define NP_WARN_WITHIN_CRIT 2
  57. /* a simple check to see if we're running as root.
  58. * returns zero on failure, nonzero on success */
  59. int np_check_if_root(void);
  60. /* and a helpful wrapper around that. it returns the same status
  61. * code from the above function, in case it's helpful for testing */
  62. int np_warn_if_not_root(void);
  63. /*
  64. * Read a string representing a state (ok, warning... or numeric: 0, 1) and
  65. * return the corresponding NP_STATE or ERROR)
  66. */
  67. int translate_state (char *);
  68. /*
  69. * Extract the value from key/value pairs, or return NULL. The value returned
  70. * can be free()ed.
  71. * This function can be used to parse NTP control packet data and performance
  72. * data strings.
  73. */
  74. char *np_extract_value(const char*, const char*, char);
  75. /*
  76. * Same as np_extract_value with separator suitable for NTP control packet
  77. * payloads (comma)
  78. */
  79. #define np_extract_ntpvar(l, n) np_extract_value(l, n, ',')
  80. void np_enable_state(char *, int);
  81. state_data *np_state_read();
  82. void np_state_write_string(time_t, char *);
  83. void np_init(char *, int argc, char **argv);
  84. void np_set_args(int argc, char **argv);
  85. void np_cleanup();
  86. /* np_suid() returns true if the real and effective uids differs, such as when
  87. + * running a suid plugin */
  88. #define np_suid() (getuid() != geteuid())
  89. #endif /* _UTILS_BASE_ */