diff options
author | RincewindsHat <12514511+RincewindsHat@users.noreply.github.com> | 2021-12-02 15:11:21 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-12-02 15:11:21 +0100 |
commit | df1e9e975f69c8f7965bab91684870fe509739c6 (patch) | |
tree | 526b0c77c524b2219d5a48fd78b8302f055a4a5b /lib | |
parent | 6246b8f1830e8a375bc656deafd28aab715b4fd9 (diff) | |
parent | 1a3a715702a5b6709fed2a1eb53ec644c9968057 (diff) | |
download | monitoring-plugins-df1e9e975f69c8f7965bab91684870fe509739c6.tar.gz |
Merge branch 'master' into mailq-add-config-dir
Diffstat (limited to 'lib')
-rw-r--r-- | lib/utils_base.c | 32 | ||||
-rw-r--r-- | lib/utils_base.h | 6 | ||||
-rw-r--r-- | lib/utils_cmd.c | 42 | ||||
-rw-r--r-- | lib/utils_cmd.h | 13 | ||||
-rw-r--r-- | lib/utils_disk.c | 2 | ||||
-rw-r--r-- | lib/utils_disk.h | 5 |
6 files changed, 68 insertions, 32 deletions
diff --git a/lib/utils_base.c b/lib/utils_base.c index 3822bcf1..08fa215c 100644 --- a/lib/utils_base.c +++ b/lib/utils_base.c | |||
@@ -37,6 +37,9 @@ | |||
37 | 37 | ||
38 | monitoring_plugin *this_monitoring_plugin=NULL; | 38 | monitoring_plugin *this_monitoring_plugin=NULL; |
39 | 39 | ||
40 | unsigned int timeout_state = STATE_CRITICAL; | ||
41 | unsigned int timeout_interval = DEFAULT_SOCKET_TIMEOUT; | ||
42 | |||
40 | int _np_state_read_file(FILE *); | 43 | int _np_state_read_file(FILE *); |
41 | 44 | ||
42 | void np_init( char *plugin_name, int argc, char **argv ) { | 45 | void np_init( char *plugin_name, int argc, char **argv ) { |
@@ -87,10 +90,13 @@ void _get_monitoring_plugin( monitoring_plugin **pointer ){ | |||
87 | void | 90 | void |
88 | die (int result, const char *fmt, ...) | 91 | die (int result, const char *fmt, ...) |
89 | { | 92 | { |
90 | va_list ap; | 93 | if(fmt!=NULL) { |
91 | va_start (ap, fmt); | 94 | va_list ap; |
92 | vprintf (fmt, ap); | 95 | va_start (ap, fmt); |
93 | va_end (ap); | 96 | vprintf (fmt, ap); |
97 | va_end (ap); | ||
98 | } | ||
99 | |||
94 | if(this_monitoring_plugin!=NULL) { | 100 | if(this_monitoring_plugin!=NULL) { |
95 | np_cleanup(); | 101 | np_cleanup(); |
96 | } | 102 | } |
@@ -122,6 +128,7 @@ range | |||
122 | temp_range->end = 0; | 128 | temp_range->end = 0; |
123 | temp_range->end_infinity = TRUE; | 129 | temp_range->end_infinity = TRUE; |
124 | temp_range->alert_on = OUTSIDE; | 130 | temp_range->alert_on = OUTSIDE; |
131 | temp_range->text = strdup(str); | ||
125 | 132 | ||
126 | if (str[0] == '@') { | 133 | if (str[0] == '@') { |
127 | temp_range->alert_on = INSIDE; | 134 | temp_range->alert_on = INSIDE; |
@@ -356,6 +363,22 @@ char *np_extract_value(const char *varlist, const char *name, char sep) { | |||
356 | return value; | 363 | return value; |
357 | } | 364 | } |
358 | 365 | ||
366 | const char * | ||
367 | state_text (int result) | ||
368 | { | ||
369 | switch (result) { | ||
370 | case STATE_OK: | ||
371 | return "OK"; | ||
372 | case STATE_WARNING: | ||
373 | return "WARNING"; | ||
374 | case STATE_CRITICAL: | ||
375 | return "CRITICAL"; | ||
376 | case STATE_DEPENDENT: | ||
377 | return "DEPENDENT"; | ||
378 | default: | ||
379 | return "UNKNOWN"; | ||
380 | } | ||
381 | } | ||
359 | 382 | ||
360 | /* | 383 | /* |
361 | * Read a string representing a state (ok, warning... or numeric: 0, 1) and | 384 | * Read a string representing a state (ok, warning... or numeric: 0, 1) and |
@@ -684,4 +707,3 @@ void np_state_write_string(time_t data_time, char *data_string) { | |||
684 | 707 | ||
685 | np_free(temp_file); | 708 | np_free(temp_file); |
686 | } | 709 | } |
687 | |||
diff --git a/lib/utils_base.h b/lib/utils_base.h index 42ae0c09..9482f23b 100644 --- a/lib/utils_base.h +++ b/lib/utils_base.h | |||
@@ -23,6 +23,7 @@ typedef struct range_struct { | |||
23 | double end; | 23 | double end; |
24 | int end_infinity; | 24 | int end_infinity; |
25 | int alert_on; /* OUTSIDE (default) or INSIDE */ | 25 | int alert_on; /* OUTSIDE (default) or INSIDE */ |
26 | char* text; /* original unparsed text input */ | ||
26 | } range; | 27 | } range; |
27 | 28 | ||
28 | typedef struct thresholds_struct { | 29 | typedef struct thresholds_struct { |
@@ -61,6 +62,10 @@ void print_thresholds(const char *, thresholds *); | |||
61 | int check_range(double, range *); | 62 | int check_range(double, range *); |
62 | int get_status(double, thresholds *); | 63 | int get_status(double, thresholds *); |
63 | 64 | ||
65 | /* Handle timeouts */ | ||
66 | extern unsigned int timeout_state; | ||
67 | extern unsigned int timeout_interval; | ||
68 | |||
64 | /* All possible characters in a threshold range */ | 69 | /* All possible characters in a threshold range */ |
65 | #define NP_THRESHOLDS_CHARS "-0123456789.:@~" | 70 | #define NP_THRESHOLDS_CHARS "-0123456789.:@~" |
66 | 71 | ||
@@ -107,5 +112,6 @@ void np_state_write_string(time_t, char *); | |||
107 | void np_init(char *, int argc, char **argv); | 112 | void np_init(char *, int argc, char **argv); |
108 | void np_set_args(int argc, char **argv); | 113 | void np_set_args(int argc, char **argv); |
109 | void np_cleanup(); | 114 | void np_cleanup(); |
115 | const char *state_text (int); | ||
110 | 116 | ||
111 | #endif /* _UTILS_BASE_ */ | 117 | #endif /* _UTILS_BASE_ */ |
diff --git a/lib/utils_cmd.c b/lib/utils_cmd.c index 7eb9a3a0..795840d3 100644 --- a/lib/utils_cmd.c +++ b/lib/utils_cmd.c | |||
@@ -40,6 +40,7 @@ | |||
40 | 40 | ||
41 | /** includes **/ | 41 | /** includes **/ |
42 | #include "common.h" | 42 | #include "common.h" |
43 | #include "utils.h" | ||
43 | #include "utils_cmd.h" | 44 | #include "utils_cmd.h" |
44 | #include "utils_base.h" | 45 | #include "utils_base.h" |
45 | #include <fcntl.h> | 46 | #include <fcntl.h> |
@@ -65,31 +66,6 @@ extern char **environ; | |||
65 | # define SIG_ERR ((Sigfunc *)-1) | 66 | # define SIG_ERR ((Sigfunc *)-1) |
66 | #endif | 67 | #endif |
67 | 68 | ||
68 | /* This variable must be global, since there's no way the caller | ||
69 | * can forcibly slay a dead or ungainly running program otherwise. | ||
70 | * Multithreading apps and plugins can initialize it (via CMD_INIT) | ||
71 | * in an async safe manner PRIOR to calling cmd_run() or cmd_run_array() | ||
72 | * for the first time. | ||
73 | * | ||
74 | * The check for initialized values is atomic and can | ||
75 | * occur in any number of threads simultaneously. */ | ||
76 | static pid_t *_cmd_pids = NULL; | ||
77 | |||
78 | /* Try sysconf(_SC_OPEN_MAX) first, as it can be higher than OPEN_MAX. | ||
79 | * If that fails and the macro isn't defined, we fall back to an educated | ||
80 | * guess. There's no guarantee that our guess is adequate and the program | ||
81 | * will die with SIGSEGV if it isn't and the upper boundary is breached. */ | ||
82 | #define DEFAULT_MAXFD 256 /* fallback value if no max open files value is set */ | ||
83 | #define MAXFD_LIMIT 8192 /* upper limit of open files */ | ||
84 | #ifdef _SC_OPEN_MAX | ||
85 | static long maxfd = 0; | ||
86 | #elif defined(OPEN_MAX) | ||
87 | # define maxfd OPEN_MAX | ||
88 | #else /* sysconf macro unavailable, so guess (may be wildly inaccurate) */ | ||
89 | # define maxfd DEFAULT_MAXFD | ||
90 | #endif | ||
91 | |||
92 | |||
93 | /** prototypes **/ | 69 | /** prototypes **/ |
94 | static int _cmd_open (char *const *, int *, int *) | 70 | static int _cmd_open (char *const *, int *, int *) |
95 | __attribute__ ((__nonnull__ (1, 2, 3))); | 71 | __attribute__ ((__nonnull__ (1, 2, 3))); |
@@ -406,3 +382,19 @@ cmd_file_read ( char *filename, output *out, int flags) | |||
406 | 382 | ||
407 | return 0; | 383 | return 0; |
408 | } | 384 | } |
385 | |||
386 | void | ||
387 | timeout_alarm_handler (int signo) | ||
388 | { | ||
389 | size_t i; | ||
390 | if (signo == SIGALRM) { | ||
391 | printf (_("%s - Plugin timed out after %d seconds\n"), | ||
392 | state_text(timeout_state), timeout_interval); | ||
393 | |||
394 | if(_cmd_pids) for(i = 0; i < maxfd; i++) { | ||
395 | if(_cmd_pids[i] != 0) kill(_cmd_pids[i], SIGKILL); | ||
396 | } | ||
397 | |||
398 | exit (timeout_state); | ||
399 | } | ||
400 | } | ||
diff --git a/lib/utils_cmd.h b/lib/utils_cmd.h index ebaf15be..6f3aeb81 100644 --- a/lib/utils_cmd.h +++ b/lib/utils_cmd.h | |||
@@ -32,4 +32,17 @@ void cmd_init (void); | |||
32 | #define CMD_NO_ARRAYS 0x01 /* don't populate arrays at all */ | 32 | #define CMD_NO_ARRAYS 0x01 /* don't populate arrays at all */ |
33 | #define CMD_NO_ASSOC 0x02 /* output.line won't point to buf */ | 33 | #define CMD_NO_ASSOC 0x02 /* output.line won't point to buf */ |
34 | 34 | ||
35 | /* This variable must be global, since there's no way the caller | ||
36 | * can forcibly slay a dead or ungainly running program otherwise. | ||
37 | * Multithreading apps and plugins can initialize it (via CMD_INIT) | ||
38 | * in an async safe manner PRIOR to calling cmd_run() or cmd_run_array() | ||
39 | * for the first time. | ||
40 | * | ||
41 | * The check for initialized values is atomic and can | ||
42 | * occur in any number of threads simultaneously. */ | ||
43 | static pid_t *_cmd_pids = NULL; | ||
44 | |||
45 | RETSIGTYPE timeout_alarm_handler (int); | ||
46 | |||
47 | |||
35 | #endif /* _UTILS_CMD_ */ | 48 | #endif /* _UTILS_CMD_ */ |
diff --git a/lib/utils_disk.c b/lib/utils_disk.c index efe35fc5..c7c9126e 100644 --- a/lib/utils_disk.c +++ b/lib/utils_disk.c | |||
@@ -69,6 +69,8 @@ np_add_parameter(struct parameter_list **list, const char *name) | |||
69 | new_path->dtotal_units = 0; | 69 | new_path->dtotal_units = 0; |
70 | new_path->inodes_total = 0; | 70 | new_path->inodes_total = 0; |
71 | new_path->inodes_free = 0; | 71 | new_path->inodes_free = 0; |
72 | new_path->inodes_free_to_root = 0; | ||
73 | new_path->inodes_used = 0; | ||
72 | new_path->dused_inodes_percent = 0; | 74 | new_path->dused_inodes_percent = 0; |
73 | new_path->dfree_inodes_percent = 0; | 75 | new_path->dfree_inodes_percent = 0; |
74 | 76 | ||
diff --git a/lib/utils_disk.h b/lib/utils_disk.h index 83a37639..bf52e4ce 100644 --- a/lib/utils_disk.h +++ b/lib/utils_disk.h | |||
@@ -24,9 +24,10 @@ struct parameter_list | |||
24 | char *group; | 24 | char *group; |
25 | struct mount_entry *best_match; | 25 | struct mount_entry *best_match; |
26 | struct parameter_list *name_next; | 26 | struct parameter_list *name_next; |
27 | uintmax_t total, available, available_to_root, used, inodes_free, inodes_total; | 27 | uintmax_t total, available, available_to_root, used, |
28 | inodes_free, inodes_free_to_root, inodes_used, inodes_total; | ||
28 | double dfree_pct, dused_pct; | 29 | double dfree_pct, dused_pct; |
29 | double dused_units, dfree_units, dtotal_units; | 30 | uint64_t dused_units, dfree_units, dtotal_units; |
30 | double dused_inodes_percent, dfree_inodes_percent; | 31 | double dused_inodes_percent, dfree_inodes_percent; |
31 | }; | 32 | }; |
32 | 33 | ||