4
0

utils_base.h 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. #ifndef NAGIOS_UTILS_BASE_H_INCLUDED
  2. #define NAGIOS_UTILS_BASE_H_INCLUDED
  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. char *warning_string;
  25. char *critical_string;
  26. } thresholds;
  27. #define NP_STATE_FORMAT_VERSION 1
  28. typedef struct state_data_struct {
  29. time_t time;
  30. void *data;
  31. int length; /* Of binary data */
  32. } state_data;
  33. typedef struct state_key_struct {
  34. char *name;
  35. char *plugin_name;
  36. int data_version;
  37. char *_filename;
  38. state_data *state_data;
  39. } state_key;
  40. typedef struct np_struct {
  41. char *plugin_name;
  42. state_key *state;
  43. int argc;
  44. char **argv;
  45. } nagios_plugin;
  46. range *parse_range_string (char *);
  47. int _set_thresholds(thresholds **, char *, char *);
  48. void set_thresholds(thresholds **, char *, char *);
  49. void print_thresholds(const char *, thresholds *);
  50. int check_range(double, range *);
  51. int get_status(double, thresholds *);
  52. /* All possible characters in a threshold range */
  53. #define NP_THRESHOLDS_CHARS "-0123456789.:@~"
  54. char *np_escaped_string (const char *);
  55. void die (int, const char *, ...) __attribute__((noreturn,format(printf, 2, 3)));
  56. /* Return codes for _set_thresholds */
  57. #define NP_RANGE_UNPARSEABLE 1
  58. #define NP_WARN_WITHIN_CRIT 2
  59. /* a simple check to see if we're running as root.
  60. * returns zero on failure, nonzero on success */
  61. int np_check_if_root(void);
  62. /* and a helpful wrapper around that. it returns the same status
  63. * code from the above function, in case it's helpful for testing */
  64. int np_warn_if_not_root(void);
  65. /*
  66. * Read a string representing a state (ok, warning... or numeric: 0, 1) and
  67. * return the corresponding NP_STATE or ERROR)
  68. */
  69. int translate_state (char *);
  70. /*
  71. * Extract the value from key/value pairs, or return NULL. The value returned
  72. * can be free()ed.
  73. * This function can be used to parse NTP control packet data and performance
  74. * data strings.
  75. */
  76. char *np_extract_value(const char*, const char*, char);
  77. /*
  78. * Same as np_extract_value with separator suitable for NTP control packet
  79. * payloads (comma)
  80. */
  81. #define np_extract_ntpvar(l, n) np_extract_value(l, n, ',')
  82. void np_enable_state(char *, int);
  83. state_data *np_state_read();
  84. void np_state_write_string(time_t, char *);
  85. void np_init(char *, int argc, char **argv);
  86. void np_set_args(int argc, char **argv);
  87. void np_cleanup();
  88. /* np_suid() returns true if the real and effective uids differs, such as when
  89. + * running a suid plugin */
  90. #define np_suid() (getuid() != geteuid())
  91. #endif /* NAGIOS_UTILS_BASE_H_INCLUDED */