diff options
| -rw-r--r-- | .github/monitoring-plugins.spec | 14 | ||||
| -rw-r--r-- | .gitignore | 1 | ||||
| -rw-r--r-- | plugins/Makefile.am | 5 | ||||
| -rw-r--r-- | plugins/check_ntp_peer.c | 385 | ||||
| -rw-r--r-- | plugins/check_ntp_peer.d/config.h | 67 | ||||
| -rw-r--r-- | plugins/check_overcr.c | 417 | ||||
| -rw-r--r-- | plugins/check_radius.c | 541 | ||||
| -rw-r--r-- | plugins/check_radius.d/config.h | 42 |
8 files changed, 599 insertions, 873 deletions
diff --git a/.github/monitoring-plugins.spec b/.github/monitoring-plugins.spec index 5cae3e59..64ee34f2 100644 --- a/.github/monitoring-plugins.spec +++ b/.github/monitoring-plugins.spec | |||
| @@ -193,7 +193,6 @@ Requires: %{name}-ntp_peer | |||
| 193 | Requires: %{name}-ntp_time | 193 | Requires: %{name}-ntp_time |
| 194 | Requires: %{name}-nwstat | 194 | Requires: %{name}-nwstat |
| 195 | Requires: %{name}-oracle | 195 | Requires: %{name}-oracle |
| 196 | Requires: %{name}-overcr | ||
| 197 | Requires: %{name}-pgsql | 196 | Requires: %{name}-pgsql |
| 198 | Requires: %{name}-ping | 197 | Requires: %{name}-ping |
| 199 | Requires: %{name}-procs | 198 | Requires: %{name}-procs |
| @@ -729,19 +728,6 @@ Provides check_oracle of the Monitoring Plugins. | |||
| 729 | 728 | ||
| 730 | 729 | ||
| 731 | 730 | ||
| 732 | # check_overcr | ||
| 733 | %package overcr | ||
| 734 | Summary: Monitoring Plugins - check_overcr | ||
| 735 | Requires: %{name} = %{version}-%{release} | ||
| 736 | |||
| 737 | %description overcr | ||
| 738 | Provides check_overcr of the Monitoring Plugins. | ||
| 739 | |||
| 740 | %files overcr | ||
| 741 | %{plugindir}/check_overcr | ||
| 742 | |||
| 743 | |||
| 744 | |||
| 745 | # check_pgsql | 731 | # check_pgsql |
| 746 | %package pgsql | 732 | %package pgsql |
| 747 | Summary: Monitoring Plugins - check_pgsql | 733 | Summary: Monitoring Plugins - check_pgsql |
| @@ -178,7 +178,6 @@ NP-VERSION-FILE | |||
| 178 | /plugins/check_ntp_peer | 178 | /plugins/check_ntp_peer |
| 179 | /plugins/check_ntp_time | 179 | /plugins/check_ntp_time |
| 180 | /plugins/check_nwstat | 180 | /plugins/check_nwstat |
| 181 | /plugins/check_overcr | ||
| 182 | /plugins/check_pgsql | 181 | /plugins/check_pgsql |
| 183 | /plugins/check_ping | 182 | /plugins/check_ping |
| 184 | /plugins/check_pop | 183 | /plugins/check_pop |
diff --git a/plugins/Makefile.am b/plugins/Makefile.am index 10a12168..a1a8103d 100644 --- a/plugins/Makefile.am +++ b/plugins/Makefile.am | |||
| @@ -27,7 +27,7 @@ MATHLIBS = @MATHLIBS@ | |||
| 27 | #AM_CFLAGS = -Wall | 27 | #AM_CFLAGS = -Wall |
| 28 | 28 | ||
| 29 | libexec_PROGRAMS = check_apt check_cluster check_disk check_dummy check_http check_load \ | 29 | libexec_PROGRAMS = check_apt check_cluster check_disk check_dummy check_http check_load \ |
| 30 | check_mrtg check_mrtgtraf check_ntp check_ntp_peer check_nwstat check_overcr check_ping \ | 30 | check_mrtg check_mrtgtraf check_ntp check_ntp_peer check_nwstat check_ping \ |
| 31 | check_real check_smtp check_ssh check_tcp check_time check_ntp_time \ | 31 | check_real check_smtp check_ssh check_tcp check_time check_ntp_time \ |
| 32 | check_ups check_users negate \ | 32 | check_ups check_users negate \ |
| 33 | urlize @EXTRAS@ | 33 | urlize @EXTRAS@ |
| @@ -53,6 +53,7 @@ EXTRA_DIST = t \ | |||
| 53 | check_ldap.d \ | 53 | check_ldap.d \ |
| 54 | check_hpjd.d \ | 54 | check_hpjd.d \ |
| 55 | check_game.d \ | 55 | check_game.d \ |
| 56 | check_radius.d \ | ||
| 56 | check_nagios.d \ | 57 | check_nagios.d \ |
| 57 | check_dbi.d \ | 58 | check_dbi.d \ |
| 58 | check_ssh.d \ | 59 | check_ssh.d \ |
| @@ -61,6 +62,7 @@ EXTRA_DIST = t \ | |||
| 61 | check_mrtgtraf.d \ | 62 | check_mrtgtraf.d \ |
| 62 | check_mysql_query.d \ | 63 | check_mysql_query.d \ |
| 63 | check_mrtg.d \ | 64 | check_mrtg.d \ |
| 65 | check_ntp_peer.d \ | ||
| 64 | check_apt.d \ | 66 | check_apt.d \ |
| 65 | check_pgsql.d \ | 67 | check_pgsql.d \ |
| 66 | check_ping.d \ | 68 | check_ping.d \ |
| @@ -133,7 +135,6 @@ check_nt_LDADD = $(NETLIBS) | |||
| 133 | check_ntp_LDADD = $(NETLIBS) $(MATHLIBS) | 135 | check_ntp_LDADD = $(NETLIBS) $(MATHLIBS) |
| 134 | check_ntp_peer_LDADD = $(NETLIBS) $(MATHLIBS) | 136 | check_ntp_peer_LDADD = $(NETLIBS) $(MATHLIBS) |
| 135 | check_nwstat_LDADD = $(NETLIBS) | 137 | check_nwstat_LDADD = $(NETLIBS) |
| 136 | check_overcr_LDADD = $(NETLIBS) | ||
| 137 | check_pgsql_LDADD = $(NETLIBS) $(PGLIBS) | 138 | check_pgsql_LDADD = $(NETLIBS) $(PGLIBS) |
| 138 | check_ping_LDADD = $(NETLIBS) | 139 | check_ping_LDADD = $(NETLIBS) |
| 139 | check_procs_LDADD = $(BASEOBJS) | 140 | check_procs_LDADD = $(BASEOBJS) |
diff --git a/plugins/check_ntp_peer.c b/plugins/check_ntp_peer.c index f99e5032..6e76bf23 100644 --- a/plugins/check_ntp_peer.c +++ b/plugins/check_ntp_peer.c | |||
| @@ -35,6 +35,7 @@ | |||
| 35 | * | 35 | * |
| 36 | *****************************************************************************/ | 36 | *****************************************************************************/ |
| 37 | 37 | ||
| 38 | #include "thresholds.h" | ||
| 38 | const char *progname = "check_ntp_peer"; | 39 | const char *progname = "check_ntp_peer"; |
| 39 | const char *copyright = "2006-2024"; | 40 | const char *copyright = "2006-2024"; |
| 40 | const char *email = "devel@monitoring-plugins.org"; | 41 | const char *email = "devel@monitoring-plugins.org"; |
| @@ -42,30 +43,18 @@ const char *email = "devel@monitoring-plugins.org"; | |||
| 42 | #include "common.h" | 43 | #include "common.h" |
| 43 | #include "netutils.h" | 44 | #include "netutils.h" |
| 44 | #include "utils.h" | 45 | #include "utils.h" |
| 46 | #include "../lib/states.h" | ||
| 47 | #include "check_ntp_peer.d/config.h" | ||
| 45 | 48 | ||
| 46 | static char *server_address = NULL; | ||
| 47 | static int port = 123; | ||
| 48 | static int verbose = 0; | 49 | static int verbose = 0; |
| 49 | static bool quiet = false; | ||
| 50 | static char *owarn = "60"; | ||
| 51 | static char *ocrit = "120"; | ||
| 52 | static bool do_stratum = false; | ||
| 53 | static char *swarn = "-1:16"; | ||
| 54 | static char *scrit = "-1:16"; | ||
| 55 | static bool do_jitter = false; | ||
| 56 | static char *jwarn = "-1:5000"; | ||
| 57 | static char *jcrit = "-1:10000"; | ||
| 58 | static bool do_truechimers = false; | ||
| 59 | static char *twarn = "0:"; | ||
| 60 | static char *tcrit = "0:"; | ||
| 61 | static bool syncsource_found = false; | 50 | static bool syncsource_found = false; |
| 62 | static bool li_alarm = false; | 51 | static bool li_alarm = false; |
| 63 | 52 | ||
| 64 | static int process_arguments(int /*argc*/, char ** /*argv*/); | 53 | typedef struct { |
| 65 | static thresholds *offset_thresholds = NULL; | 54 | int errorcode; |
| 66 | static thresholds *jitter_thresholds = NULL; | 55 | check_ntp_peer_config config; |
| 67 | static thresholds *stratum_thresholds = NULL; | 56 | } check_ntp_peer_config_wrapper; |
| 68 | static thresholds *truechimer_thresholds = NULL; | 57 | static check_ntp_peer_config_wrapper process_arguments(int /*argc*/, char ** /*argv*/); |
| 69 | static void print_help(void); | 58 | static void print_help(void); |
| 70 | void print_usage(void); | 59 | void print_usage(void); |
| 71 | 60 | ||
| @@ -157,25 +146,25 @@ typedef struct { | |||
| 157 | printf("%u.%u.%u.%u", (x >> 24) & 0xff, (x >> 16) & 0xff, (x >> 8) & 0xff, x & 0xff); \ | 146 | printf("%u.%u.%u.%u", (x >> 24) & 0xff, (x >> 16) & 0xff, (x >> 8) & 0xff, x & 0xff); \ |
| 158 | } while (0); | 147 | } while (0); |
| 159 | 148 | ||
| 160 | void print_ntp_control_message(const ntp_control_message *p) { | 149 | void print_ntp_control_message(const ntp_control_message *message) { |
| 161 | printf("control packet contents:\n"); | 150 | printf("control packet contents:\n"); |
| 162 | printf("\tflags: 0x%.2x , 0x%.2x\n", p->flags, p->op); | 151 | printf("\tflags: 0x%.2x , 0x%.2x\n", message->flags, message->op); |
| 163 | printf("\t li=%d (0x%.2x)\n", LI(p->flags), p->flags & LI_MASK); | 152 | printf("\t li=%d (0x%.2x)\n", LI(message->flags), message->flags & LI_MASK); |
| 164 | printf("\t vn=%d (0x%.2x)\n", VN(p->flags), p->flags & VN_MASK); | 153 | printf("\t vn=%d (0x%.2x)\n", VN(message->flags), message->flags & VN_MASK); |
| 165 | printf("\t mode=%d (0x%.2x)\n", MODE(p->flags), p->flags & MODE_MASK); | 154 | printf("\t mode=%d (0x%.2x)\n", MODE(message->flags), message->flags & MODE_MASK); |
| 166 | printf("\t response=%d (0x%.2x)\n", (p->op & REM_RESP) > 0, p->op & REM_RESP); | 155 | printf("\t response=%d (0x%.2x)\n", (message->op & REM_RESP) > 0, message->op & REM_RESP); |
| 167 | printf("\t more=%d (0x%.2x)\n", (p->op & REM_MORE) > 0, p->op & REM_MORE); | 156 | printf("\t more=%d (0x%.2x)\n", (message->op & REM_MORE) > 0, message->op & REM_MORE); |
| 168 | printf("\t error=%d (0x%.2x)\n", (p->op & REM_ERROR) > 0, p->op & REM_ERROR); | 157 | printf("\t error=%d (0x%.2x)\n", (message->op & REM_ERROR) > 0, message->op & REM_ERROR); |
| 169 | printf("\t op=%d (0x%.2x)\n", p->op & OP_MASK, p->op & OP_MASK); | 158 | printf("\t op=%d (0x%.2x)\n", message->op & OP_MASK, message->op & OP_MASK); |
| 170 | printf("\tsequence: %d (0x%.2x)\n", ntohs(p->seq), ntohs(p->seq)); | 159 | printf("\tsequence: %d (0x%.2x)\n", ntohs(message->seq), ntohs(message->seq)); |
| 171 | printf("\tstatus: %d (0x%.2x)\n", ntohs(p->status), ntohs(p->status)); | 160 | printf("\tstatus: %d (0x%.2x)\n", ntohs(message->status), ntohs(message->status)); |
| 172 | printf("\tassoc: %d (0x%.2x)\n", ntohs(p->assoc), ntohs(p->assoc)); | 161 | printf("\tassoc: %d (0x%.2x)\n", ntohs(message->assoc), ntohs(message->assoc)); |
| 173 | printf("\toffset: %d (0x%.2x)\n", ntohs(p->offset), ntohs(p->offset)); | 162 | printf("\toffset: %d (0x%.2x)\n", ntohs(message->offset), ntohs(message->offset)); |
| 174 | printf("\tcount: %d (0x%.2x)\n", ntohs(p->count), ntohs(p->count)); | 163 | printf("\tcount: %d (0x%.2x)\n", ntohs(message->count), ntohs(message->count)); |
| 175 | 164 | ||
| 176 | int numpeers = ntohs(p->count) / (sizeof(ntp_assoc_status_pair)); | 165 | int numpeers = ntohs(message->count) / (sizeof(ntp_assoc_status_pair)); |
| 177 | if (p->op & REM_RESP && p->op & OP_READSTAT) { | 166 | if (message->op & REM_RESP && message->op & OP_READSTAT) { |
| 178 | const ntp_assoc_status_pair *peer = (ntp_assoc_status_pair *)p->data; | 167 | const ntp_assoc_status_pair *peer = (ntp_assoc_status_pair *)message->data; |
| 179 | for (int i = 0; i < numpeers; i++) { | 168 | for (int i = 0; i < numpeers; i++) { |
| 180 | printf("\tpeer id %.2x status %.2x", ntohs(peer[i].assoc), ntohs(peer[i].status)); | 169 | printf("\tpeer id %.2x status %.2x", ntohs(peer[i].assoc), ntohs(peer[i].status)); |
| 181 | if (PEER_SEL(peer[i].status) >= PEER_SYNCSOURCE) { | 170 | if (PEER_SEL(peer[i].status) >= PEER_SYNCSOURCE) { |
| @@ -190,13 +179,13 @@ void print_ntp_control_message(const ntp_control_message *p) { | |||
| 190 | } | 179 | } |
| 191 | } | 180 | } |
| 192 | 181 | ||
| 193 | void setup_control_request(ntp_control_message *p, uint8_t opcode, uint16_t seq) { | 182 | void setup_control_request(ntp_control_message *message, uint8_t opcode, uint16_t seq) { |
| 194 | memset(p, 0, sizeof(ntp_control_message)); | 183 | memset(message, 0, sizeof(ntp_control_message)); |
| 195 | LI_SET(p->flags, LI_NOWARNING); | 184 | LI_SET(message->flags, LI_NOWARNING); |
| 196 | VN_SET(p->flags, VN_RESERVED); | 185 | VN_SET(message->flags, VN_RESERVED); |
| 197 | MODE_SET(p->flags, MODE_CONTROLMSG); | 186 | MODE_SET(message->flags, MODE_CONTROLMSG); |
| 198 | OP_SET(p->op, opcode); | 187 | OP_SET(message->op, opcode); |
| 199 | p->seq = htons(seq); | 188 | message->seq = htons(seq); |
| 200 | /* Remaining fields are zero for requests */ | 189 | /* Remaining fields are zero for requests */ |
| 201 | } | 190 | } |
| 202 | 191 | ||
| @@ -211,10 +200,23 @@ void setup_control_request(ntp_control_message *p, uint8_t opcode, uint16_t seq) | |||
| 211 | * status is pretty much useless as syncsource_found is a global variable | 200 | * status is pretty much useless as syncsource_found is a global variable |
| 212 | * used later in main to check is the server was synchronized. It works | 201 | * used later in main to check is the server was synchronized. It works |
| 213 | * so I left it alone */ | 202 | * so I left it alone */ |
| 214 | int ntp_request(double *offset, int *offset_result, double *jitter, int *stratum, int *num_truechimers) { | 203 | typedef struct { |
| 215 | *offset_result = STATE_UNKNOWN; | 204 | mp_state_enum state; |
| 216 | *jitter = *stratum = -1; | 205 | mp_state_enum offset_result; |
| 217 | *num_truechimers = 0; | 206 | double offset; |
| 207 | double jitter; | ||
| 208 | long stratum; | ||
| 209 | int num_truechimers; | ||
| 210 | } ntp_request_result; | ||
| 211 | ntp_request_result ntp_request(const check_ntp_peer_config config) { | ||
| 212 | |||
| 213 | ntp_request_result result = { | ||
| 214 | .state = STATE_OK, | ||
| 215 | .offset_result = STATE_UNKNOWN, | ||
| 216 | .jitter = -1, | ||
| 217 | .stratum = -1, | ||
| 218 | .num_truechimers = 0, | ||
| 219 | }; | ||
| 218 | 220 | ||
| 219 | /* Long-winded explanation: | 221 | /* Long-winded explanation: |
| 220 | * Getting the sync peer offset, jitter and stratum requires a number of | 222 | * Getting the sync peer offset, jitter and stratum requires a number of |
| @@ -237,10 +239,10 @@ int ntp_request(double *offset, int *offset_result, double *jitter, int *stratum | |||
| 237 | void *tmp; | 239 | void *tmp; |
| 238 | ntp_assoc_status_pair *peers = NULL; | 240 | ntp_assoc_status_pair *peers = NULL; |
| 239 | int peer_offset = 0; | 241 | int peer_offset = 0; |
| 240 | int peers_size = 0; | 242 | size_t peers_size = 0; |
| 241 | int npeers = 0; | 243 | size_t npeers = 0; |
| 242 | int conn = -1; | 244 | int conn = -1; |
| 243 | my_udp_connect(server_address, port, &conn); | 245 | my_udp_connect(config.server_address, config.port, &conn); |
| 244 | 246 | ||
| 245 | /* keep sending requests until the server stops setting the | 247 | /* keep sending requests until the server stops setting the |
| 246 | * REM_MORE bit, though usually this is only 1 packet. */ | 248 | * REM_MORE bit, though usually this is only 1 packet. */ |
| @@ -255,24 +257,28 @@ int ntp_request(double *offset, int *offset_result, double *jitter, int *stratum | |||
| 255 | /* Attempt to read the largest size packet possible */ | 257 | /* Attempt to read the largest size packet possible */ |
| 256 | req.count = htons(MAX_CM_SIZE); | 258 | req.count = htons(MAX_CM_SIZE); |
| 257 | DBG(printf("receiving READSTAT response")) | 259 | DBG(printf("receiving READSTAT response")) |
| 258 | if (read(conn, &req, SIZEOF_NTPCM(req)) == -1) | 260 | if (read(conn, &req, SIZEOF_NTPCM(req)) == -1) { |
| 259 | die(STATE_CRITICAL, "NTP CRITICAL: No response from NTP server\n"); | 261 | die(STATE_CRITICAL, "NTP CRITICAL: No response from NTP server\n"); |
| 262 | } | ||
| 260 | DBG(print_ntp_control_message(&req)); | 263 | DBG(print_ntp_control_message(&req)); |
| 261 | /* discard obviously invalid packets */ | 264 | /* discard obviously invalid packets */ |
| 262 | if (ntohs(req.count) > MAX_CM_SIZE) | 265 | if (ntohs(req.count) > MAX_CM_SIZE) { |
| 263 | die(STATE_CRITICAL, "NTP CRITICAL: Invalid packet received from NTP server\n"); | 266 | die(STATE_CRITICAL, "NTP CRITICAL: Invalid packet received from NTP server\n"); |
| 267 | } | ||
| 264 | } while (!(req.op & OP_READSTAT && ntohs(req.seq) == 1)); | 268 | } while (!(req.op & OP_READSTAT && ntohs(req.seq) == 1)); |
| 265 | 269 | ||
| 266 | if (LI(req.flags) == LI_ALARM) | 270 | if (LI(req.flags) == LI_ALARM) { |
| 267 | li_alarm = true; | 271 | li_alarm = true; |
| 272 | } | ||
| 268 | /* Each peer identifier is 4 bytes in the data section, which | 273 | /* Each peer identifier is 4 bytes in the data section, which |
| 269 | * we represent as a ntp_assoc_status_pair datatype. | 274 | * we represent as a ntp_assoc_status_pair datatype. |
| 270 | */ | 275 | */ |
| 271 | peers_size += ntohs(req.count); | 276 | peers_size += ntohs(req.count); |
| 272 | if ((tmp = realloc(peers, peers_size)) == NULL) | 277 | if ((tmp = realloc(peers, peers_size)) == NULL) { |
| 273 | free(peers), die(STATE_UNKNOWN, "can not (re)allocate 'peers' buffer\n"); | 278 | free(peers), die(STATE_UNKNOWN, "can not (re)allocate 'peers' buffer\n"); |
| 279 | } | ||
| 274 | peers = tmp; | 280 | peers = tmp; |
| 275 | memcpy((void *)((ptrdiff_t)peers + peer_offset), (void *)req.data, ntohs(req.count)); | 281 | memcpy((peers + peer_offset), (void *)req.data, ntohs(req.count)); |
| 276 | npeers = peers_size / sizeof(ntp_assoc_status_pair); | 282 | npeers = peers_size / sizeof(ntp_assoc_status_pair); |
| 277 | peer_offset += ntohs(req.count); | 283 | peer_offset += ntohs(req.count); |
| 278 | } while (req.op & REM_MORE); | 284 | } while (req.op & REM_MORE); |
| @@ -280,9 +286,9 @@ int ntp_request(double *offset, int *offset_result, double *jitter, int *stratum | |||
| 280 | /* first, let's find out if we have a sync source, or if there are | 286 | /* first, let's find out if we have a sync source, or if there are |
| 281 | * at least some candidates. In the latter case we'll issue | 287 | * at least some candidates. In the latter case we'll issue |
| 282 | * a warning but go ahead with the check on them. */ | 288 | * a warning but go ahead with the check on them. */ |
| 283 | for (int i = 0; i < npeers; i++) { | 289 | for (size_t i = 0; i < npeers; i++) { |
| 284 | if (PEER_SEL(peers[i].status) >= PEER_TRUECHIMER) { | 290 | if (PEER_SEL(peers[i].status) >= PEER_TRUECHIMER) { |
| 285 | (*num_truechimers)++; | 291 | result.num_truechimers++; |
| 286 | if (PEER_SEL(peers[i].status) >= PEER_INCLUDED) { | 292 | if (PEER_SEL(peers[i].status) >= PEER_INCLUDED) { |
| 287 | num_candidates++; | 293 | num_candidates++; |
| 288 | if (PEER_SEL(peers[i].status) >= PEER_SYNCSOURCE) { | 294 | if (PEER_SEL(peers[i].status) >= PEER_SYNCSOURCE) { |
| @@ -293,31 +299,35 @@ int ntp_request(double *offset, int *offset_result, double *jitter, int *stratum | |||
| 293 | } | 299 | } |
| 294 | } | 300 | } |
| 295 | 301 | ||
| 296 | if (verbose) | 302 | if (verbose) { |
| 297 | printf("%d candidate peers available\n", num_candidates); | 303 | printf("%d candidate peers available\n", num_candidates); |
| 298 | if (verbose && syncsource_found) | 304 | } |
| 305 | if (verbose && syncsource_found) { | ||
| 299 | printf("synchronization source found\n"); | 306 | printf("synchronization source found\n"); |
| 307 | } | ||
| 300 | 308 | ||
| 301 | int status = STATE_OK; | ||
| 302 | if (!syncsource_found) { | 309 | if (!syncsource_found) { |
| 303 | status = STATE_WARNING; | 310 | result.state = STATE_WARNING; |
| 304 | if (verbose) | 311 | if (verbose) { |
| 305 | printf("warning: no synchronization source found\n"); | 312 | printf("warning: no synchronization source found\n"); |
| 313 | } | ||
| 306 | } | 314 | } |
| 307 | if (li_alarm) { | 315 | if (li_alarm) { |
| 308 | status = STATE_WARNING; | 316 | result.state = STATE_WARNING; |
| 309 | if (verbose) | 317 | if (verbose) { |
| 310 | printf("warning: LI_ALARM bit is set\n"); | 318 | printf("warning: LI_ALARM bit is set\n"); |
| 319 | } | ||
| 311 | } | 320 | } |
| 312 | 321 | ||
| 313 | const char *getvar = "stratum,offset,jitter"; | 322 | const char *getvar = "stratum,offset,jitter"; |
| 314 | char *data; | 323 | char *data; |
| 315 | for (int i = 0; i < npeers; i++) { | 324 | for (size_t i = 0; i < npeers; i++) { |
| 316 | /* Only query this server if it is the current sync source */ | 325 | /* Only query this server if it is the current sync source */ |
| 317 | /* If there's no sync.peer, query all candidates and use the best one */ | 326 | /* If there's no sync.peer, query all candidates and use the best one */ |
| 318 | if (PEER_SEL(peers[i].status) >= min_peer_sel) { | 327 | if (PEER_SEL(peers[i].status) >= min_peer_sel) { |
| 319 | if (verbose) | 328 | if (verbose) { |
| 320 | printf("Getting offset, jitter and stratum for peer %.2x\n", ntohs(peers[i].assoc)); | 329 | printf("Getting offset, jitter and stratum for peer %.2x\n", ntohs(peers[i].assoc)); |
| 330 | } | ||
| 321 | xasprintf(&data, ""); | 331 | xasprintf(&data, ""); |
| 322 | do { | 332 | do { |
| 323 | setup_control_request(&req, OP_READVAR, 2); | 333 | setup_control_request(&req, OP_READVAR, 2); |
| @@ -342,60 +352,68 @@ int ntp_request(double *offset, int *offset_result, double *jitter, int *stratum | |||
| 342 | DBG(print_ntp_control_message(&req)); | 352 | DBG(print_ntp_control_message(&req)); |
| 343 | } while (!(req.op & OP_READVAR && ntohs(req.seq) == 2)); | 353 | } while (!(req.op & OP_READVAR && ntohs(req.seq) == 2)); |
| 344 | 354 | ||
| 345 | if (!(req.op & REM_ERROR)) | 355 | if (!(req.op & REM_ERROR)) { |
| 346 | xasprintf(&data, "%s%s", data, req.data); | 356 | xasprintf(&data, "%s%s", data, req.data); |
| 357 | } | ||
| 347 | } while (req.op & REM_MORE); | 358 | } while (req.op & REM_MORE); |
| 348 | 359 | ||
| 349 | if (req.op & REM_ERROR) { | 360 | if (req.op & REM_ERROR) { |
| 350 | if (strstr(getvar, "jitter")) { | 361 | if (strstr(getvar, "jitter")) { |
| 351 | if (verbose) | 362 | if (verbose) { |
| 352 | printf("The command failed. This is usually caused by servers refusing the 'jitter'\nvariable. Restarting with " | 363 | printf("The command failed. This is usually caused by servers refusing the 'jitter'\nvariable. Restarting with " |
| 353 | "'dispersion'...\n"); | 364 | "'dispersion'...\n"); |
| 365 | } | ||
| 354 | getvar = "stratum,offset,dispersion"; | 366 | getvar = "stratum,offset,dispersion"; |
| 355 | i--; | 367 | i--; |
| 356 | continue; | 368 | continue; |
| 357 | } | 369 | } |
| 358 | if (strlen(getvar)) { | 370 | if (strlen(getvar)) { |
| 359 | if (verbose) | 371 | if (verbose) { |
| 360 | printf("Server didn't like dispersion either; will retrieve everything\n"); | 372 | printf("Server didn't like dispersion either; will retrieve everything\n"); |
| 373 | } | ||
| 361 | getvar = ""; | 374 | getvar = ""; |
| 362 | i--; | 375 | i--; |
| 363 | continue; | 376 | continue; |
| 364 | } | 377 | } |
| 365 | } | 378 | } |
| 366 | 379 | ||
| 367 | if (verbose > 1) | 380 | if (verbose > 1) { |
| 368 | printf("Server responded: >>>%s<<<\n", data); | 381 | printf("Server responded: >>>%s<<<\n", data); |
| 382 | } | ||
| 369 | 383 | ||
| 370 | double tmp_offset = 0; | 384 | double tmp_offset = 0; |
| 371 | char *value; | 385 | char *value; |
| 372 | char *nptr; | 386 | char *nptr; |
| 373 | /* get the offset */ | 387 | /* get the offset */ |
| 374 | if (verbose) | 388 | if (verbose) { |
| 375 | printf("parsing offset from peer %.2x: ", ntohs(peers[i].assoc)); | 389 | printf("parsing offset from peer %.2x: ", ntohs(peers[i].assoc)); |
| 390 | } | ||
| 376 | 391 | ||
| 377 | value = np_extract_ntpvar(data, "offset"); | 392 | value = np_extract_ntpvar(data, "offset"); |
| 378 | nptr = NULL; | 393 | nptr = NULL; |
| 379 | /* Convert the value if we have one */ | 394 | /* Convert the value if we have one */ |
| 380 | if (value != NULL) | 395 | if (value != NULL) { |
| 381 | tmp_offset = strtod(value, &nptr) / 1000; | 396 | tmp_offset = strtod(value, &nptr) / 1000; |
| 397 | } | ||
| 382 | /* If value is null or no conversion was performed */ | 398 | /* If value is null or no conversion was performed */ |
| 383 | if (value == NULL || value == nptr) { | 399 | if (value == NULL || value == nptr) { |
| 384 | if (verbose) | 400 | if (verbose) { |
| 385 | printf("error: unable to read server offset response.\n"); | 401 | printf("error: unable to read server offset response.\n"); |
| 402 | } | ||
| 386 | } else { | 403 | } else { |
| 387 | if (verbose) | 404 | if (verbose) { |
| 388 | printf("%.10g\n", tmp_offset); | 405 | printf("%.10g\n", tmp_offset); |
| 389 | if (*offset_result == STATE_UNKNOWN || fabs(tmp_offset) < fabs(*offset)) { | 406 | } |
| 390 | *offset = tmp_offset; | 407 | if (result.offset_result == STATE_UNKNOWN || fabs(tmp_offset) < fabs(result.offset)) { |
| 391 | *offset_result = STATE_OK; | 408 | result.offset = tmp_offset; |
| 409 | result.offset_result = STATE_OK; | ||
| 392 | } else { | 410 | } else { |
| 393 | /* Skip this one; move to the next */ | 411 | /* Skip this one; move to the next */ |
| 394 | continue; | 412 | continue; |
| 395 | } | 413 | } |
| 396 | } | 414 | } |
| 397 | 415 | ||
| 398 | if (do_jitter) { | 416 | if (config.do_jitter) { |
| 399 | /* get the jitter */ | 417 | /* get the jitter */ |
| 400 | if (verbose) { | 418 | if (verbose) { |
| 401 | printf("parsing %s from peer %.2x: ", strstr(getvar, "dispersion") != NULL ? "dispersion" : "jitter", | 419 | printf("parsing %s from peer %.2x: ", strstr(getvar, "dispersion") != NULL ? "dispersion" : "jitter", |
| @@ -404,19 +422,21 @@ int ntp_request(double *offset, int *offset_result, double *jitter, int *stratum | |||
| 404 | value = np_extract_ntpvar(data, strstr(getvar, "dispersion") != NULL ? "dispersion" : "jitter"); | 422 | value = np_extract_ntpvar(data, strstr(getvar, "dispersion") != NULL ? "dispersion" : "jitter"); |
| 405 | nptr = NULL; | 423 | nptr = NULL; |
| 406 | /* Convert the value if we have one */ | 424 | /* Convert the value if we have one */ |
| 407 | if (value != NULL) | 425 | if (value != NULL) { |
| 408 | *jitter = strtod(value, &nptr); | 426 | result.jitter = strtod(value, &nptr); |
| 427 | } | ||
| 409 | /* If value is null or no conversion was performed */ | 428 | /* If value is null or no conversion was performed */ |
| 410 | if (value == NULL || value == nptr) { | 429 | if (value == NULL || value == nptr) { |
| 411 | if (verbose) | 430 | if (verbose) { |
| 412 | printf("error: unable to read server jitter/dispersion response.\n"); | 431 | printf("error: unable to read server jitter/dispersion response.\n"); |
| 413 | *jitter = -1; | 432 | } |
| 433 | result.jitter = -1; | ||
| 414 | } else if (verbose) { | 434 | } else if (verbose) { |
| 415 | printf("%.10g\n", *jitter); | 435 | printf("%.10g\n", result.jitter); |
| 416 | } | 436 | } |
| 417 | } | 437 | } |
| 418 | 438 | ||
| 419 | if (do_stratum) { | 439 | if (config.do_stratum) { |
| 420 | /* get the stratum */ | 440 | /* get the stratum */ |
| 421 | if (verbose) { | 441 | if (verbose) { |
| 422 | printf("parsing stratum from peer %.2x: ", ntohs(peers[i].assoc)); | 442 | printf("parsing stratum from peer %.2x: ", ntohs(peers[i].assoc)); |
| @@ -424,28 +444,32 @@ int ntp_request(double *offset, int *offset_result, double *jitter, int *stratum | |||
| 424 | value = np_extract_ntpvar(data, "stratum"); | 444 | value = np_extract_ntpvar(data, "stratum"); |
| 425 | nptr = NULL; | 445 | nptr = NULL; |
| 426 | /* Convert the value if we have one */ | 446 | /* Convert the value if we have one */ |
| 427 | if (value != NULL) | 447 | if (value != NULL) { |
| 428 | *stratum = strtol(value, &nptr, 10); | 448 | result.stratum = strtol(value, &nptr, 10); |
| 449 | } | ||
| 429 | if (value == NULL || value == nptr) { | 450 | if (value == NULL || value == nptr) { |
| 430 | if (verbose) | 451 | if (verbose) { |
| 431 | printf("error: unable to read server stratum response.\n"); | 452 | printf("error: unable to read server stratum response.\n"); |
| 432 | *stratum = -1; | 453 | } |
| 454 | result.stratum = -1; | ||
| 433 | } else { | 455 | } else { |
| 434 | if (verbose) | 456 | if (verbose) { |
| 435 | printf("%i\n", *stratum); | 457 | printf("%li\n", result.stratum); |
| 458 | } | ||
| 436 | } | 459 | } |
| 437 | } | 460 | } |
| 438 | } /* if (PEER_SEL(peers[i].status) >= min_peer_sel) */ | 461 | } /* if (PEER_SEL(peers[i].status) >= min_peer_sel) */ |
| 439 | } /* for (i = 0; i < npeers; i++) */ | 462 | } /* for (i = 0; i < npeers; i++) */ |
| 440 | 463 | ||
| 441 | close(conn); | 464 | close(conn); |
| 442 | if (peers != NULL) | 465 | if (peers != NULL) { |
| 443 | free(peers); | 466 | free(peers); |
| 467 | } | ||
| 444 | 468 | ||
| 445 | return status; | 469 | return result; |
| 446 | } | 470 | } |
| 447 | 471 | ||
| 448 | int process_arguments(int argc, char **argv) { | 472 | check_ntp_peer_config_wrapper process_arguments(int argc, char **argv) { |
| 449 | static struct option longopts[] = { | 473 | static struct option longopts[] = { |
| 450 | {"version", no_argument, 0, 'V'}, {"help", no_argument, 0, 'h'}, {"verbose", no_argument, 0, 'v'}, | 474 | {"version", no_argument, 0, 'V'}, {"help", no_argument, 0, 'h'}, {"verbose", no_argument, 0, 'v'}, |
| 451 | {"use-ipv4", no_argument, 0, '4'}, {"use-ipv6", no_argument, 0, '6'}, {"quiet", no_argument, 0, 'q'}, | 475 | {"use-ipv4", no_argument, 0, '4'}, {"use-ipv6", no_argument, 0, '6'}, {"quiet", no_argument, 0, 'q'}, |
| @@ -454,14 +478,21 @@ int process_arguments(int argc, char **argv) { | |||
| 454 | {"twarn", required_argument, 0, 'm'}, {"tcrit", required_argument, 0, 'n'}, {"timeout", required_argument, 0, 't'}, | 478 | {"twarn", required_argument, 0, 'm'}, {"tcrit", required_argument, 0, 'n'}, {"timeout", required_argument, 0, 't'}, |
| 455 | {"hostname", required_argument, 0, 'H'}, {"port", required_argument, 0, 'p'}, {0, 0, 0, 0}}; | 479 | {"hostname", required_argument, 0, 'H'}, {"port", required_argument, 0, 'p'}, {0, 0, 0, 0}}; |
| 456 | 480 | ||
| 457 | if (argc < 2) | 481 | if (argc < 2) { |
| 458 | usage("\n"); | 482 | usage("\n"); |
| 483 | } | ||
| 484 | |||
| 485 | check_ntp_peer_config_wrapper result = { | ||
| 486 | .errorcode = OK, | ||
| 487 | .config = check_ntp_peer_config_init(), | ||
| 488 | }; | ||
| 459 | 489 | ||
| 460 | while (true) { | 490 | while (true) { |
| 461 | int option = 0; | 491 | int option = 0; |
| 462 | int option_char = getopt_long(argc, argv, "Vhv46qw:c:W:C:j:k:m:n:t:H:p:", longopts, &option); | 492 | int option_char = getopt_long(argc, argv, "Vhv46qw:c:W:C:j:k:m:n:t:H:p:", longopts, &option); |
| 463 | if (option_char == -1 || option_char == EOF || option_char == 1) | 493 | if (option_char == -1 || option_char == EOF || option_char == 1) { |
| 464 | break; | 494 | break; |
| 495 | } | ||
| 465 | 496 | ||
| 466 | switch (option_char) { | 497 | switch (option_char) { |
| 467 | case 'h': | 498 | case 'h': |
| @@ -476,45 +507,46 @@ int process_arguments(int argc, char **argv) { | |||
| 476 | verbose++; | 507 | verbose++; |
| 477 | break; | 508 | break; |
| 478 | case 'q': | 509 | case 'q': |
| 479 | quiet = true; | 510 | result.config.quiet = true; |
| 480 | break; | 511 | break; |
| 481 | case 'w': | 512 | case 'w': |
| 482 | owarn = optarg; | 513 | result.config.owarn = optarg; |
| 483 | break; | 514 | break; |
| 484 | case 'c': | 515 | case 'c': |
| 485 | ocrit = optarg; | 516 | result.config.ocrit = optarg; |
| 486 | break; | 517 | break; |
| 487 | case 'W': | 518 | case 'W': |
| 488 | do_stratum = true; | 519 | result.config.do_stratum = true; |
| 489 | swarn = optarg; | 520 | result.config.swarn = optarg; |
| 490 | break; | 521 | break; |
| 491 | case 'C': | 522 | case 'C': |
| 492 | do_stratum = true; | 523 | result.config.do_stratum = true; |
| 493 | scrit = optarg; | 524 | result.config.scrit = optarg; |
| 494 | break; | 525 | break; |
| 495 | case 'j': | 526 | case 'j': |
| 496 | do_jitter = true; | 527 | result.config.do_jitter = true; |
| 497 | jwarn = optarg; | 528 | result.config.jwarn = optarg; |
| 498 | break; | 529 | break; |
| 499 | case 'k': | 530 | case 'k': |
| 500 | do_jitter = true; | 531 | result.config.do_jitter = true; |
| 501 | jcrit = optarg; | 532 | result.config.jcrit = optarg; |
| 502 | break; | 533 | break; |
| 503 | case 'm': | 534 | case 'm': |
| 504 | do_truechimers = true; | 535 | result.config.do_truechimers = true; |
| 505 | twarn = optarg; | 536 | result.config.twarn = optarg; |
| 506 | break; | 537 | break; |
| 507 | case 'n': | 538 | case 'n': |
| 508 | do_truechimers = true; | 539 | result.config.do_truechimers = true; |
| 509 | tcrit = optarg; | 540 | result.config.tcrit = optarg; |
| 510 | break; | 541 | break; |
| 511 | case 'H': | 542 | case 'H': |
| 512 | if (!is_host(optarg)) | 543 | if (!is_host(optarg)) { |
| 513 | usage2(_("Invalid hostname/address"), optarg); | 544 | usage2(_("Invalid hostname/address"), optarg); |
| 514 | server_address = strdup(optarg); | 545 | } |
| 546 | result.config.server_address = strdup(optarg); | ||
| 515 | break; | 547 | break; |
| 516 | case 'p': | 548 | case 'p': |
| 517 | port = atoi(optarg); | 549 | result.config.port = atoi(optarg); |
| 518 | break; | 550 | break; |
| 519 | case 't': | 551 | case 't': |
| 520 | socket_timeout = atoi(optarg); | 552 | socket_timeout = atoi(optarg); |
| @@ -536,29 +568,34 @@ int process_arguments(int argc, char **argv) { | |||
| 536 | } | 568 | } |
| 537 | } | 569 | } |
| 538 | 570 | ||
| 539 | if (server_address == NULL) { | 571 | if (result.config.server_address == NULL) { |
| 540 | usage4(_("Hostname was not supplied")); | 572 | usage4(_("Hostname was not supplied")); |
| 541 | } | 573 | } |
| 542 | 574 | ||
| 543 | return 0; | 575 | set_thresholds(&result.config.offset_thresholds, result.config.owarn, result.config.ocrit); |
| 576 | set_thresholds(&result.config.jitter_thresholds, result.config.jwarn, result.config.jcrit); | ||
| 577 | set_thresholds(&result.config.stratum_thresholds, result.config.swarn, result.config.scrit); | ||
| 578 | set_thresholds(&result.config.truechimer_thresholds, result.config.twarn, result.config.tcrit); | ||
| 579 | |||
| 580 | return result; | ||
| 544 | } | 581 | } |
| 545 | 582 | ||
| 546 | char *perfd_offset(double offset) { | 583 | char *perfd_offset(double offset, thresholds *offset_thresholds) { |
| 547 | return fperfdata("offset", offset, "s", true, offset_thresholds->warning->end, true, offset_thresholds->critical->end, false, 0, false, | 584 | return fperfdata("offset", offset, "s", true, offset_thresholds->warning->end, true, offset_thresholds->critical->end, false, 0, false, |
| 548 | 0); | 585 | 0); |
| 549 | } | 586 | } |
| 550 | 587 | ||
| 551 | char *perfd_jitter(double jitter) { | 588 | char *perfd_jitter(double jitter, bool do_jitter, thresholds *jitter_thresholds) { |
| 552 | return fperfdata("jitter", jitter, "", do_jitter, jitter_thresholds->warning->end, do_jitter, jitter_thresholds->critical->end, true, 0, | 589 | return fperfdata("jitter", jitter, "", do_jitter, jitter_thresholds->warning->end, do_jitter, jitter_thresholds->critical->end, true, 0, |
| 553 | false, 0); | 590 | false, 0); |
| 554 | } | 591 | } |
| 555 | 592 | ||
| 556 | char *perfd_stratum(int stratum) { | 593 | char *perfd_stratum(int stratum, bool do_stratum, thresholds *stratum_thresholds) { |
| 557 | return perfdata("stratum", stratum, "", do_stratum, (int)stratum_thresholds->warning->end, do_stratum, | 594 | return perfdata("stratum", stratum, "", do_stratum, (int)stratum_thresholds->warning->end, do_stratum, |
| 558 | (int)stratum_thresholds->critical->end, true, 0, true, 16); | 595 | (int)stratum_thresholds->critical->end, true, 0, true, 16); |
| 559 | } | 596 | } |
| 560 | 597 | ||
| 561 | char *perfd_truechimers(int num_truechimers) { | 598 | char *perfd_truechimers(int num_truechimers, const bool do_truechimers, thresholds *truechimer_thresholds) { |
| 562 | return perfdata("truechimers", num_truechimers, "", do_truechimers, (int)truechimer_thresholds->warning->end, do_truechimers, | 599 | return perfdata("truechimers", num_truechimers, "", do_truechimers, (int)truechimer_thresholds->warning->end, do_truechimers, |
| 563 | (int)truechimer_thresholds->critical->end, true, 0, false, 0); | 600 | (int)truechimer_thresholds->critical->end, true, 0, false, 0); |
| 564 | } | 601 | } |
| @@ -571,13 +608,13 @@ int main(int argc, char *argv[]) { | |||
| 571 | /* Parse extra opts if any */ | 608 | /* Parse extra opts if any */ |
| 572 | argv = np_extra_opts(&argc, argv, progname); | 609 | argv = np_extra_opts(&argc, argv, progname); |
| 573 | 610 | ||
| 574 | if (process_arguments(argc, argv) == ERROR) | 611 | check_ntp_peer_config_wrapper tmp_config = process_arguments(argc, argv); |
| 612 | |||
| 613 | if (tmp_config.errorcode == ERROR) { | ||
| 575 | usage4(_("Could not parse arguments")); | 614 | usage4(_("Could not parse arguments")); |
| 615 | } | ||
| 576 | 616 | ||
| 577 | set_thresholds(&offset_thresholds, owarn, ocrit); | 617 | const check_ntp_peer_config config = tmp_config.config; |
| 578 | set_thresholds(&jitter_thresholds, jwarn, jcrit); | ||
| 579 | set_thresholds(&stratum_thresholds, swarn, scrit); | ||
| 580 | set_thresholds(&truechimer_thresholds, twarn, tcrit); | ||
| 581 | 618 | ||
| 582 | /* initialize alarm signal handling */ | 619 | /* initialize alarm signal handling */ |
| 583 | signal(SIGALRM, socket_timeout_alarm_handler); | 620 | signal(SIGALRM, socket_timeout_alarm_handler); |
| @@ -585,44 +622,40 @@ int main(int argc, char *argv[]) { | |||
| 585 | /* set socket timeout */ | 622 | /* set socket timeout */ |
| 586 | alarm(socket_timeout); | 623 | alarm(socket_timeout); |
| 587 | 624 | ||
| 588 | int offset_result; | ||
| 589 | int stratum; | ||
| 590 | int num_truechimers; | ||
| 591 | double offset = 0; | ||
| 592 | double jitter = 0; | ||
| 593 | /* This returns either OK or WARNING (See comment preceding ntp_request) */ | 625 | /* This returns either OK or WARNING (See comment preceding ntp_request) */ |
| 594 | int result = ntp_request(&offset, &offset_result, &jitter, &stratum, &num_truechimers); | 626 | ntp_request_result ntp_res = ntp_request(config); |
| 627 | mp_state_enum result = STATE_UNKNOWN; | ||
| 595 | 628 | ||
| 596 | if (offset_result == STATE_UNKNOWN) { | 629 | if (ntp_res.offset_result == STATE_UNKNOWN) { |
| 597 | /* if there's no sync peer (this overrides ntp_request output): */ | 630 | /* if there's no sync peer (this overrides ntp_request output): */ |
| 598 | result = (quiet ? STATE_UNKNOWN : STATE_CRITICAL); | 631 | result = (config.quiet ? STATE_UNKNOWN : STATE_CRITICAL); |
| 599 | } else { | 632 | } else { |
| 600 | /* Be quiet if there's no candidates either */ | 633 | /* Be quiet if there's no candidates either */ |
| 601 | if (quiet && result == STATE_WARNING) | 634 | if (config.quiet && result == STATE_WARNING) { |
| 602 | result = STATE_UNKNOWN; | 635 | result = STATE_UNKNOWN; |
| 603 | result = max_state_alt(result, get_status(fabs(offset), offset_thresholds)); | 636 | } |
| 637 | result = max_state_alt(result, get_status(fabs(ntp_res.offset), config.offset_thresholds)); | ||
| 604 | } | 638 | } |
| 605 | 639 | ||
| 606 | int oresult = result; | 640 | mp_state_enum oresult = result; |
| 607 | 641 | mp_state_enum tresult = STATE_UNKNOWN; | |
| 608 | int tresult = STATE_UNKNOWN; | ||
| 609 | 642 | ||
| 610 | if (do_truechimers) { | 643 | if (config.do_truechimers) { |
| 611 | tresult = get_status(num_truechimers, truechimer_thresholds); | 644 | tresult = get_status(ntp_res.num_truechimers, config.truechimer_thresholds); |
| 612 | result = max_state_alt(result, tresult); | 645 | result = max_state_alt(result, tresult); |
| 613 | } | 646 | } |
| 614 | 647 | ||
| 615 | int sresult = STATE_UNKNOWN; | 648 | mp_state_enum sresult = STATE_UNKNOWN; |
| 616 | 649 | ||
| 617 | if (do_stratum) { | 650 | if (config.do_stratum) { |
| 618 | sresult = get_status(stratum, stratum_thresholds); | 651 | sresult = get_status((double)ntp_res.stratum, config.stratum_thresholds); |
| 619 | result = max_state_alt(result, sresult); | 652 | result = max_state_alt(result, sresult); |
| 620 | } | 653 | } |
| 621 | 654 | ||
| 622 | int jresult = STATE_UNKNOWN; | 655 | mp_state_enum jresult = STATE_UNKNOWN; |
| 623 | 656 | ||
| 624 | if (do_jitter) { | 657 | if (config.do_jitter) { |
| 625 | jresult = get_status(jitter, jitter_thresholds); | 658 | jresult = get_status(ntp_res.jitter, config.jitter_thresholds); |
| 626 | result = max_state_alt(result, jresult); | 659 | result = max_state_alt(result, jresult); |
| 627 | } | 660 | } |
| 628 | 661 | ||
| @@ -641,59 +674,67 @@ int main(int argc, char *argv[]) { | |||
| 641 | xasprintf(&result_line, _("NTP UNKNOWN:")); | 674 | xasprintf(&result_line, _("NTP UNKNOWN:")); |
| 642 | break; | 675 | break; |
| 643 | } | 676 | } |
| 644 | if (!syncsource_found) | 677 | |
| 678 | if (!syncsource_found) { | ||
| 645 | xasprintf(&result_line, "%s %s,", result_line, _("Server not synchronized")); | 679 | xasprintf(&result_line, "%s %s,", result_line, _("Server not synchronized")); |
| 646 | else if (li_alarm) | 680 | } else if (li_alarm) { |
| 647 | xasprintf(&result_line, "%s %s,", result_line, _("Server has the LI_ALARM bit set")); | 681 | xasprintf(&result_line, "%s %s,", result_line, _("Server has the LI_ALARM bit set")); |
| 682 | } | ||
| 648 | 683 | ||
| 649 | char *perfdata_line; | 684 | char *perfdata_line; |
| 650 | if (offset_result == STATE_UNKNOWN) { | 685 | if (ntp_res.offset_result == STATE_UNKNOWN) { |
| 651 | xasprintf(&result_line, "%s %s", result_line, _("Offset unknown")); | 686 | xasprintf(&result_line, "%s %s", result_line, _("Offset unknown")); |
| 652 | xasprintf(&perfdata_line, ""); | 687 | xasprintf(&perfdata_line, ""); |
| 653 | } else if (oresult == STATE_WARNING) { | 688 | } else if (oresult == STATE_WARNING) { |
| 654 | xasprintf(&result_line, "%s %s %.10g secs (WARNING)", result_line, _("Offset"), offset); | 689 | xasprintf(&result_line, "%s %s %.10g secs (WARNING)", result_line, _("Offset"), ntp_res.offset); |
| 655 | } else if (oresult == STATE_CRITICAL) { | 690 | } else if (oresult == STATE_CRITICAL) { |
| 656 | xasprintf(&result_line, "%s %s %.10g secs (CRITICAL)", result_line, _("Offset"), offset); | 691 | xasprintf(&result_line, "%s %s %.10g secs (CRITICAL)", result_line, _("Offset"), ntp_res.offset); |
| 657 | } else { | 692 | } else { |
| 658 | xasprintf(&result_line, "%s %s %.10g secs", result_line, _("Offset"), offset); | 693 | xasprintf(&result_line, "%s %s %.10g secs", result_line, _("Offset"), ntp_res.offset); |
| 659 | } | 694 | } |
| 660 | xasprintf(&perfdata_line, "%s", perfd_offset(offset)); | 695 | xasprintf(&perfdata_line, "%s", perfd_offset(ntp_res.offset, config.offset_thresholds)); |
| 661 | 696 | ||
| 662 | if (do_jitter) { | 697 | if (config.do_jitter) { |
| 663 | if (jresult == STATE_WARNING) { | 698 | if (jresult == STATE_WARNING) { |
| 664 | xasprintf(&result_line, "%s, jitter=%f (WARNING)", result_line, jitter); | 699 | xasprintf(&result_line, "%s, jitter=%f (WARNING)", result_line, ntp_res.jitter); |
| 665 | } else if (jresult == STATE_CRITICAL) { | 700 | } else if (jresult == STATE_CRITICAL) { |
| 666 | xasprintf(&result_line, "%s, jitter=%f (CRITICAL)", result_line, jitter); | 701 | xasprintf(&result_line, "%s, jitter=%f (CRITICAL)", result_line, ntp_res.jitter); |
| 667 | } else { | 702 | } else { |
| 668 | xasprintf(&result_line, "%s, jitter=%f", result_line, jitter); | 703 | xasprintf(&result_line, "%s, jitter=%f", result_line, ntp_res.jitter); |
| 669 | } | 704 | } |
| 670 | xasprintf(&perfdata_line, "%s %s", perfdata_line, perfd_jitter(jitter)); | 705 | xasprintf(&perfdata_line, "%s %s", perfdata_line, perfd_jitter(ntp_res.jitter, config.do_jitter, config.jitter_thresholds)); |
| 671 | } | 706 | } |
| 672 | if (do_stratum) { | 707 | |
| 708 | if (config.do_stratum) { | ||
| 673 | if (sresult == STATE_WARNING) { | 709 | if (sresult == STATE_WARNING) { |
| 674 | xasprintf(&result_line, "%s, stratum=%i (WARNING)", result_line, stratum); | 710 | xasprintf(&result_line, "%s, stratum=%l (WARNING)", result_line, ntp_res.stratum); |
| 675 | } else if (sresult == STATE_CRITICAL) { | 711 | } else if (sresult == STATE_CRITICAL) { |
| 676 | xasprintf(&result_line, "%s, stratum=%i (CRITICAL)", result_line, stratum); | 712 | xasprintf(&result_line, "%s, stratum=%l (CRITICAL)", result_line, ntp_res.stratum); |
| 677 | } else { | 713 | } else { |
| 678 | xasprintf(&result_line, "%s, stratum=%i", result_line, stratum); | 714 | xasprintf(&result_line, "%s, stratum=%l", result_line, ntp_res.stratum); |
| 679 | } | 715 | } |
| 680 | xasprintf(&perfdata_line, "%s %s", perfdata_line, perfd_stratum(stratum)); | 716 | xasprintf(&perfdata_line, "%s %s", perfdata_line, perfd_stratum(ntp_res.stratum, config.do_stratum, config.stratum_thresholds)); |
| 681 | } | 717 | } |
| 682 | if (do_truechimers) { | 718 | |
| 719 | if (config.do_truechimers) { | ||
| 683 | if (tresult == STATE_WARNING) { | 720 | if (tresult == STATE_WARNING) { |
| 684 | xasprintf(&result_line, "%s, truechimers=%i (WARNING)", result_line, num_truechimers); | 721 | xasprintf(&result_line, "%s, truechimers=%i (WARNING)", result_line, ntp_res.num_truechimers); |
| 685 | } else if (tresult == STATE_CRITICAL) { | 722 | } else if (tresult == STATE_CRITICAL) { |
| 686 | xasprintf(&result_line, "%s, truechimers=%i (CRITICAL)", result_line, num_truechimers); | 723 | xasprintf(&result_line, "%s, truechimers=%i (CRITICAL)", result_line, ntp_res.num_truechimers); |
| 687 | } else { | 724 | } else { |
| 688 | xasprintf(&result_line, "%s, truechimers=%i", result_line, num_truechimers); | 725 | xasprintf(&result_line, "%s, truechimers=%i", result_line, ntp_res.num_truechimers); |
| 689 | } | 726 | } |
| 690 | xasprintf(&perfdata_line, "%s %s", perfdata_line, perfd_truechimers(num_truechimers)); | 727 | xasprintf(&perfdata_line, "%s %s", perfdata_line, |
| 728 | perfd_truechimers(ntp_res.num_truechimers, config.do_truechimers, config.truechimer_thresholds)); | ||
| 691 | } | 729 | } |
| 730 | |||
| 692 | printf("%s|%s\n", result_line, perfdata_line); | 731 | printf("%s|%s\n", result_line, perfdata_line); |
| 693 | 732 | ||
| 694 | if (server_address != NULL) | 733 | if (config.server_address != NULL) { |
| 695 | free(server_address); | 734 | free(config.server_address); |
| 696 | return result; | 735 | } |
| 736 | |||
| 737 | exit(result); | ||
| 697 | } | 738 | } |
| 698 | 739 | ||
| 699 | void print_help(void) { | 740 | void print_help(void) { |
diff --git a/plugins/check_ntp_peer.d/config.h b/plugins/check_ntp_peer.d/config.h new file mode 100644 index 00000000..00e6b05d --- /dev/null +++ b/plugins/check_ntp_peer.d/config.h | |||
| @@ -0,0 +1,67 @@ | |||
| 1 | #pragma once | ||
| 2 | |||
| 3 | #include "../../config.h" | ||
| 4 | #include "thresholds.h" | ||
| 5 | #include <stddef.h> | ||
| 6 | |||
| 7 | enum { | ||
| 8 | DEFAULT_NTP_PORT = 123, | ||
| 9 | }; | ||
| 10 | |||
| 11 | typedef struct { | ||
| 12 | char *server_address; | ||
| 13 | int port; | ||
| 14 | |||
| 15 | bool quiet; | ||
| 16 | |||
| 17 | // truechimer stuff | ||
| 18 | bool do_truechimers; | ||
| 19 | char *twarn; | ||
| 20 | char *tcrit; | ||
| 21 | thresholds *truechimer_thresholds; | ||
| 22 | |||
| 23 | char *owarn; | ||
| 24 | char *ocrit; | ||
| 25 | thresholds *offset_thresholds; | ||
| 26 | |||
| 27 | // stratum stuff | ||
| 28 | bool do_stratum; | ||
| 29 | char *swarn; | ||
| 30 | char *scrit; | ||
| 31 | thresholds *stratum_thresholds; | ||
| 32 | |||
| 33 | // jitter stuff | ||
| 34 | bool do_jitter; | ||
| 35 | char *jwarn; | ||
| 36 | char *jcrit; | ||
| 37 | thresholds *jitter_thresholds; | ||
| 38 | |||
| 39 | } check_ntp_peer_config; | ||
| 40 | |||
| 41 | check_ntp_peer_config check_ntp_peer_config_init() { | ||
| 42 | check_ntp_peer_config tmp = { | ||
| 43 | .server_address = NULL, | ||
| 44 | .port = DEFAULT_NTP_PORT, | ||
| 45 | |||
| 46 | .quiet = false, | ||
| 47 | .do_truechimers = false, | ||
| 48 | .twarn = "0:", | ||
| 49 | .tcrit = "0:", | ||
| 50 | .truechimer_thresholds = NULL, | ||
| 51 | |||
| 52 | .owarn = "60", | ||
| 53 | .ocrit = "120", | ||
| 54 | .offset_thresholds = NULL, | ||
| 55 | |||
| 56 | .do_stratum = false, | ||
| 57 | .swarn = "-1:16", | ||
| 58 | .scrit = "-1:16", | ||
| 59 | .stratum_thresholds = NULL, | ||
| 60 | |||
| 61 | .do_jitter = false, | ||
| 62 | .jwarn = "-1:5000", | ||
| 63 | .jcrit = "-1:10000", | ||
| 64 | .jitter_thresholds = NULL, | ||
| 65 | }; | ||
| 66 | return tmp; | ||
| 67 | } | ||
diff --git a/plugins/check_overcr.c b/plugins/check_overcr.c deleted file mode 100644 index 599540b7..00000000 --- a/plugins/check_overcr.c +++ /dev/null | |||
| @@ -1,417 +0,0 @@ | |||
| 1 | /***************************************************************************** | ||
| 2 | * | ||
| 3 | * Monitoring check_overcr plugin | ||
| 4 | * | ||
| 5 | * License: GPL | ||
| 6 | * Copyright (c) 2000-2024 Monitoring Plugins Development Team | ||
| 7 | * | ||
| 8 | * Description: | ||
| 9 | * | ||
| 10 | * This file contains the check_overcr plugin | ||
| 11 | * | ||
| 12 | * This plugin attempts to contact the Over-CR collector daemon running on the | ||
| 13 | * remote UNIX server in order to gather the requested system information. | ||
| 14 | * | ||
| 15 | * | ||
| 16 | * This program is free software: you can redistribute it and/or modify | ||
| 17 | * it under the terms of the GNU General Public License as published by | ||
| 18 | * the Free Software Foundation, either version 3 of the License, or | ||
| 19 | * (at your option) any later version. | ||
| 20 | * | ||
| 21 | * This program is distributed in the hope that it will be useful, | ||
| 22 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 23 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| 24 | * GNU General Public License for more details. | ||
| 25 | * | ||
| 26 | * You should have received a copy of the GNU General Public License | ||
| 27 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
| 28 | * | ||
| 29 | * | ||
| 30 | *****************************************************************************/ | ||
| 31 | |||
| 32 | const char *progname = "check_overcr"; | ||
| 33 | const char *copyright = "2000-2024"; | ||
| 34 | const char *email = "devel@monitoring-plugins.org"; | ||
| 35 | |||
| 36 | #include "common.h" | ||
| 37 | #include "netutils.h" | ||
| 38 | #include "utils.h" | ||
| 39 | |||
| 40 | enum checkvar { | ||
| 41 | NONE, | ||
| 42 | LOAD1, | ||
| 43 | LOAD5, | ||
| 44 | LOAD15, | ||
| 45 | DPU, | ||
| 46 | PROCS, | ||
| 47 | NETSTAT, | ||
| 48 | UPTIME | ||
| 49 | }; | ||
| 50 | |||
| 51 | enum { | ||
| 52 | PORT = 2000 | ||
| 53 | }; | ||
| 54 | |||
| 55 | static char *server_address = NULL; | ||
| 56 | static int server_port = PORT; | ||
| 57 | static double warning_value = 0L; | ||
| 58 | static double critical_value = 0L; | ||
| 59 | static bool check_warning_value = false; | ||
| 60 | static bool check_critical_value = false; | ||
| 61 | static enum checkvar vars_to_check = NONE; | ||
| 62 | |||
| 63 | static int netstat_port = 0; | ||
| 64 | static char *disk_name = NULL; | ||
| 65 | static char *process_name = NULL; | ||
| 66 | static char send_buffer[MAX_INPUT_BUFFER]; | ||
| 67 | |||
| 68 | static int process_arguments(int, char **); | ||
| 69 | void print_usage(void); | ||
| 70 | static void print_help(void); | ||
| 71 | |||
| 72 | int main(int argc, char **argv) { | ||
| 73 | int result = STATE_UNKNOWN; | ||
| 74 | char recv_buffer[MAX_INPUT_BUFFER]; | ||
| 75 | char temp_buffer[MAX_INPUT_BUFFER]; | ||
| 76 | char *temp_ptr = NULL; | ||
| 77 | bool found_disk = false; | ||
| 78 | unsigned long percent_used_disk_space = 100; | ||
| 79 | double load; | ||
| 80 | double load_1min; | ||
| 81 | double load_5min; | ||
| 82 | double load_15min; | ||
| 83 | int port_connections = 0; | ||
| 84 | int processes = 0; | ||
| 85 | double uptime_raw_hours; | ||
| 86 | int uptime_raw_minutes = 0; | ||
| 87 | int uptime_days = 0; | ||
| 88 | int uptime_hours = 0; | ||
| 89 | int uptime_minutes = 0; | ||
| 90 | |||
| 91 | setlocale(LC_ALL, ""); | ||
| 92 | bindtextdomain(PACKAGE, LOCALEDIR); | ||
| 93 | textdomain(PACKAGE); | ||
| 94 | |||
| 95 | /* Parse extra opts if any */ | ||
| 96 | argv = np_extra_opts(&argc, argv, progname); | ||
| 97 | |||
| 98 | if (process_arguments(argc, argv) == ERROR) | ||
| 99 | usage4(_("Could not parse arguments")); | ||
| 100 | |||
| 101 | /* initialize alarm signal handling */ | ||
| 102 | signal(SIGALRM, socket_timeout_alarm_handler); | ||
| 103 | |||
| 104 | /* set socket timeout */ | ||
| 105 | alarm(socket_timeout); | ||
| 106 | |||
| 107 | result = process_tcp_request2(server_address, server_port, send_buffer, recv_buffer, sizeof(recv_buffer)); | ||
| 108 | |||
| 109 | switch (vars_to_check) { | ||
| 110 | |||
| 111 | case LOAD1: | ||
| 112 | case LOAD5: | ||
| 113 | case LOAD15: | ||
| 114 | |||
| 115 | if (result != STATE_OK) | ||
| 116 | die(result, _("Unknown error fetching load data\n")); | ||
| 117 | |||
| 118 | temp_ptr = (char *)strtok(recv_buffer, "\r\n"); | ||
| 119 | if (temp_ptr == NULL) | ||
| 120 | die(STATE_CRITICAL, _("Invalid response from server - no load information\n")); | ||
| 121 | else | ||
| 122 | load_1min = strtod(temp_ptr, NULL); | ||
| 123 | |||
| 124 | temp_ptr = (char *)strtok(NULL, "\r\n"); | ||
| 125 | if (temp_ptr == NULL) | ||
| 126 | die(STATE_CRITICAL, _("Invalid response from server after load 1\n")); | ||
| 127 | else | ||
| 128 | load_5min = strtod(temp_ptr, NULL); | ||
| 129 | |||
| 130 | temp_ptr = (char *)strtok(NULL, "\r\n"); | ||
| 131 | if (temp_ptr == NULL) | ||
| 132 | die(STATE_CRITICAL, _("Invalid response from server after load 5\n")); | ||
| 133 | else | ||
| 134 | load_15min = strtod(temp_ptr, NULL); | ||
| 135 | |||
| 136 | switch (vars_to_check) { | ||
| 137 | case LOAD1: | ||
| 138 | strcpy(temp_buffer, "1"); | ||
| 139 | load = load_1min; | ||
| 140 | break; | ||
| 141 | case LOAD5: | ||
| 142 | strcpy(temp_buffer, "5"); | ||
| 143 | load = load_5min; | ||
| 144 | break; | ||
| 145 | default: | ||
| 146 | strcpy(temp_buffer, "15"); | ||
| 147 | load = load_15min; | ||
| 148 | break; | ||
| 149 | } | ||
| 150 | |||
| 151 | if (check_critical_value && (load >= critical_value)) | ||
| 152 | result = STATE_CRITICAL; | ||
| 153 | else if (check_warning_value && (load >= warning_value)) | ||
| 154 | result = STATE_WARNING; | ||
| 155 | |||
| 156 | die(result, _("Load %s - %s-min load average = %0.2f"), state_text(result), temp_buffer, load); | ||
| 157 | |||
| 158 | break; | ||
| 159 | |||
| 160 | case DPU: | ||
| 161 | |||
| 162 | if (result != STATE_OK) | ||
| 163 | die(result, _("Unknown error fetching disk data\n")); | ||
| 164 | |||
| 165 | for (temp_ptr = (char *)strtok(recv_buffer, " "); temp_ptr != NULL; temp_ptr = (char *)strtok(NULL, " ")) { | ||
| 166 | |||
| 167 | if (!strcmp(temp_ptr, disk_name)) { | ||
| 168 | found_disk = true; | ||
| 169 | temp_ptr = (char *)strtok(NULL, "%"); | ||
| 170 | if (temp_ptr == NULL) | ||
| 171 | die(STATE_CRITICAL, _("Invalid response from server\n")); | ||
| 172 | else | ||
| 173 | percent_used_disk_space = strtoul(temp_ptr, NULL, 10); | ||
| 174 | break; | ||
| 175 | } | ||
| 176 | |||
| 177 | temp_ptr = (char *)strtok(NULL, "\r\n"); | ||
| 178 | } | ||
| 179 | |||
| 180 | /* error if we couldn't find the info for the disk */ | ||
| 181 | if (!found_disk) | ||
| 182 | die(STATE_CRITICAL, "CRITICAL - Disk '%s' non-existent or not mounted", disk_name); | ||
| 183 | |||
| 184 | if (check_critical_value && (percent_used_disk_space >= critical_value)) | ||
| 185 | result = STATE_CRITICAL; | ||
| 186 | else if (check_warning_value && (percent_used_disk_space >= warning_value)) | ||
| 187 | result = STATE_WARNING; | ||
| 188 | |||
| 189 | die(result, "Disk %s - %lu%% used on %s", state_text(result), percent_used_disk_space, disk_name); | ||
| 190 | |||
| 191 | break; | ||
| 192 | |||
| 193 | case NETSTAT: | ||
| 194 | |||
| 195 | if (result != STATE_OK) | ||
| 196 | die(result, _("Unknown error fetching network status\n")); | ||
| 197 | else | ||
| 198 | port_connections = strtod(recv_buffer, NULL); | ||
| 199 | |||
| 200 | if (check_critical_value && (port_connections >= critical_value)) | ||
| 201 | result = STATE_CRITICAL; | ||
| 202 | else if (check_warning_value && (port_connections >= warning_value)) | ||
| 203 | result = STATE_WARNING; | ||
| 204 | |||
| 205 | die(result, _("Net %s - %d connection%s on port %d"), state_text(result), port_connections, (port_connections == 1) ? "" : "s", | ||
| 206 | netstat_port); | ||
| 207 | |||
| 208 | break; | ||
| 209 | |||
| 210 | case PROCS: | ||
| 211 | |||
| 212 | if (result != STATE_OK) | ||
| 213 | die(result, _("Unknown error fetching process status\n")); | ||
| 214 | |||
| 215 | temp_ptr = (char *)strtok(recv_buffer, "("); | ||
| 216 | if (temp_ptr == NULL) | ||
| 217 | die(STATE_CRITICAL, _("Invalid response from server\n")); | ||
| 218 | |||
| 219 | temp_ptr = (char *)strtok(NULL, ")"); | ||
| 220 | if (temp_ptr == NULL) | ||
| 221 | die(STATE_CRITICAL, _("Invalid response from server\n")); | ||
| 222 | else | ||
| 223 | processes = strtod(temp_ptr, NULL); | ||
| 224 | |||
| 225 | if (check_critical_value && (processes >= critical_value)) | ||
| 226 | result = STATE_CRITICAL; | ||
| 227 | else if (check_warning_value && (processes >= warning_value)) | ||
| 228 | result = STATE_WARNING; | ||
| 229 | |||
| 230 | die(result, _("Process %s - %d instance%s of %s running"), state_text(result), processes, (processes == 1) ? "" : "s", | ||
| 231 | process_name); | ||
| 232 | break; | ||
| 233 | |||
| 234 | case UPTIME: | ||
| 235 | |||
| 236 | if (result != STATE_OK) | ||
| 237 | return result; | ||
| 238 | |||
| 239 | uptime_raw_hours = strtod(recv_buffer, NULL); | ||
| 240 | uptime_raw_minutes = (unsigned long)(uptime_raw_hours * 60.0); | ||
| 241 | |||
| 242 | if (check_critical_value && (uptime_raw_minutes <= critical_value)) | ||
| 243 | result = STATE_CRITICAL; | ||
| 244 | else if (check_warning_value && (uptime_raw_minutes <= warning_value)) | ||
| 245 | result = STATE_WARNING; | ||
| 246 | |||
| 247 | uptime_days = uptime_raw_minutes / 1440; | ||
| 248 | uptime_raw_minutes %= 1440; | ||
| 249 | uptime_hours = uptime_raw_minutes / 60; | ||
| 250 | uptime_raw_minutes %= 60; | ||
| 251 | uptime_minutes = uptime_raw_minutes; | ||
| 252 | |||
| 253 | die(result, _("Uptime %s - Up %d days %d hours %d minutes"), state_text(result), uptime_days, uptime_hours, uptime_minutes); | ||
| 254 | break; | ||
| 255 | |||
| 256 | default: | ||
| 257 | die(STATE_UNKNOWN, _("Nothing to check!\n")); | ||
| 258 | break; | ||
| 259 | } | ||
| 260 | } | ||
| 261 | |||
| 262 | /* process command-line arguments */ | ||
| 263 | int process_arguments(int argc, char **argv) { | ||
| 264 | int c; | ||
| 265 | |||
| 266 | int option = 0; | ||
| 267 | static struct option longopts[] = { | ||
| 268 | {"port", required_argument, 0, 'p'}, {"timeout", required_argument, 0, 't'}, {"critical", required_argument, 0, 'c'}, | ||
| 269 | {"warning", required_argument, 0, 'w'}, {"variable", required_argument, 0, 'v'}, {"hostname", required_argument, 0, 'H'}, | ||
| 270 | {"version", no_argument, 0, 'V'}, {"help", no_argument, 0, 'h'}, {0, 0, 0, 0}}; | ||
| 271 | |||
| 272 | /* no options were supplied */ | ||
| 273 | if (argc < 2) | ||
| 274 | return ERROR; | ||
| 275 | |||
| 276 | /* backwards compatibility */ | ||
| 277 | if (!is_option(argv[1])) { | ||
| 278 | server_address = argv[1]; | ||
| 279 | argv[1] = argv[0]; | ||
| 280 | argv = &argv[1]; | ||
| 281 | argc--; | ||
| 282 | } | ||
| 283 | |||
| 284 | for (c = 1; c < argc; c++) { | ||
| 285 | if (strcmp("-to", argv[c]) == 0) | ||
| 286 | strcpy(argv[c], "-t"); | ||
| 287 | else if (strcmp("-wv", argv[c]) == 0) | ||
| 288 | strcpy(argv[c], "-w"); | ||
| 289 | else if (strcmp("-cv", argv[c]) == 0) | ||
| 290 | strcpy(argv[c], "-c"); | ||
| 291 | } | ||
| 292 | |||
| 293 | while (1) { | ||
| 294 | c = getopt_long(argc, argv, "+hVH:t:c:w:p:v:", longopts, &option); | ||
| 295 | |||
| 296 | if (c == -1 || c == EOF || c == 1) | ||
| 297 | break; | ||
| 298 | |||
| 299 | switch (c) { | ||
| 300 | case '?': /* print short usage statement if args not parsable */ | ||
| 301 | usage5(); | ||
| 302 | case 'h': /* help */ | ||
| 303 | print_help(); | ||
| 304 | exit(STATE_UNKNOWN); | ||
| 305 | case 'V': /* version */ | ||
| 306 | print_revision(progname, NP_VERSION); | ||
| 307 | exit(STATE_UNKNOWN); | ||
| 308 | case 'H': /* hostname */ | ||
| 309 | server_address = optarg; | ||
| 310 | break; | ||
| 311 | case 'p': /* port */ | ||
| 312 | if (is_intnonneg(optarg)) | ||
| 313 | server_port = atoi(optarg); | ||
| 314 | else | ||
| 315 | die(STATE_UNKNOWN, _("Server port an integer\n")); | ||
| 316 | break; | ||
| 317 | case 'v': /* variable */ | ||
| 318 | if (strcmp(optarg, "LOAD") == 0) { | ||
| 319 | strcpy(send_buffer, "LOAD\r\nQUIT\r\n"); | ||
| 320 | if (strcmp(optarg, "LOAD1") == 0) | ||
| 321 | vars_to_check = LOAD1; | ||
| 322 | else if (strcmp(optarg, "LOAD5") == 0) | ||
| 323 | vars_to_check = LOAD5; | ||
| 324 | else if (strcmp(optarg, "LOAD15") == 0) | ||
| 325 | vars_to_check = LOAD15; | ||
| 326 | } else if (strcmp(optarg, "UPTIME") == 0) { | ||
| 327 | vars_to_check = UPTIME; | ||
| 328 | strcpy(send_buffer, "UPTIME\r\n"); | ||
| 329 | } else if (strstr(optarg, "PROC") == optarg) { | ||
| 330 | vars_to_check = PROCS; | ||
| 331 | process_name = strscpy(process_name, optarg + 4); | ||
| 332 | sprintf(send_buffer, "PROCESS %s\r\n", process_name); | ||
| 333 | } else if (strstr(optarg, "NET") == optarg) { | ||
| 334 | vars_to_check = NETSTAT; | ||
| 335 | netstat_port = atoi(optarg + 3); | ||
| 336 | sprintf(send_buffer, "NETSTAT %d\r\n", netstat_port); | ||
| 337 | } else if (strstr(optarg, "DPU") == optarg) { | ||
| 338 | vars_to_check = DPU; | ||
| 339 | strcpy(send_buffer, "DISKSPACE\r\n"); | ||
| 340 | disk_name = strscpy(disk_name, optarg + 3); | ||
| 341 | } else | ||
| 342 | return ERROR; | ||
| 343 | break; | ||
| 344 | case 'w': /* warning threshold */ | ||
| 345 | warning_value = strtoul(optarg, NULL, 10); | ||
| 346 | check_warning_value = true; | ||
| 347 | break; | ||
| 348 | case 'c': /* critical threshold */ | ||
| 349 | critical_value = strtoul(optarg, NULL, 10); | ||
| 350 | check_critical_value = true; | ||
| 351 | break; | ||
| 352 | case 't': /* timeout */ | ||
| 353 | socket_timeout = atoi(optarg); | ||
| 354 | if (socket_timeout <= 0) | ||
| 355 | return ERROR; | ||
| 356 | } | ||
| 357 | } | ||
| 358 | return OK; | ||
| 359 | } | ||
| 360 | |||
| 361 | void print_help(void) { | ||
| 362 | char *myport; | ||
| 363 | xasprintf(&myport, "%d", PORT); | ||
| 364 | |||
| 365 | print_revision(progname, NP_VERSION); | ||
| 366 | |||
| 367 | printf("Copyright (c) 1999 Ethan Galstad <nagios@nagios.org>\n"); | ||
| 368 | printf(COPYRIGHT, copyright, email); | ||
| 369 | |||
| 370 | printf("%s\n", _("This plugin attempts to contact the Over-CR collector daemon running on the")); | ||
| 371 | printf("%s\n", _("remote UNIX server in order to gather the requested system information.")); | ||
| 372 | |||
| 373 | printf("\n\n"); | ||
| 374 | |||
| 375 | print_usage(); | ||
| 376 | |||
| 377 | printf(UT_HELP_VRSN); | ||
| 378 | printf(UT_EXTRA_OPTS); | ||
| 379 | |||
| 380 | printf(UT_HOST_PORT, 'p', myport); | ||
| 381 | |||
| 382 | printf(" %s\n", "-w, --warning=INTEGER"); | ||
| 383 | printf(" %s\n", _("Threshold which will result in a warning status")); | ||
| 384 | printf(" %s\n", "-c, --critical=INTEGER"); | ||
| 385 | printf(" %s\n", _("Threshold which will result in a critical status")); | ||
| 386 | printf(" %s\n", "-v, --variable=STRING"); | ||
| 387 | printf(" %s\n", _("Variable to check. Valid variables include:")); | ||
| 388 | printf(" %s\n", _("LOAD1 = 1 minute average CPU load")); | ||
| 389 | printf(" %s\n", _("LOAD5 = 5 minute average CPU load")); | ||
| 390 | printf(" %s\n", _("LOAD15 = 15 minute average CPU load")); | ||
| 391 | printf(" %s\n", _("DPU<filesys> = percent used disk space on filesystem <filesys>")); | ||
| 392 | printf(" %s\n", _("PROC<process> = number of running processes with name <process>")); | ||
| 393 | printf(" %s\n", _("NET<port> = number of active connections on TCP port <port>")); | ||
| 394 | printf(" %s\n", _("UPTIME = system uptime in seconds")); | ||
| 395 | |||
| 396 | printf(UT_CONN_TIMEOUT, DEFAULT_SOCKET_TIMEOUT); | ||
| 397 | |||
| 398 | printf(UT_VERBOSE); | ||
| 399 | |||
| 400 | printf("\n"); | ||
| 401 | printf("%s\n", _("This plugin requires that Eric Molitors' Over-CR collector daemon be")); | ||
| 402 | printf("%s\n", _("running on the remote server.")); | ||
| 403 | printf("%s\n", _("Over-CR can be downloaded from http://www.molitor.org/overcr")); | ||
| 404 | printf("%s\n", _("This plugin was tested with version 0.99.53 of the Over-CR collector")); | ||
| 405 | |||
| 406 | printf("\n"); | ||
| 407 | printf("%s\n", _("Notes:")); | ||
| 408 | printf(" %s\n", _("For the available options, the critical threshold value should always be")); | ||
| 409 | printf(" %s\n", _("higher than the warning threshold value, EXCEPT with the uptime variable")); | ||
| 410 | |||
| 411 | printf(UT_SUPPORT); | ||
| 412 | } | ||
| 413 | |||
| 414 | void print_usage(void) { | ||
| 415 | printf("%s\n", _("Usage:")); | ||
| 416 | printf("%s -H host [-p port] [-v variable] [-w warning] [-c critical] [-t timeout]\n", progname); | ||
| 417 | } | ||
diff --git a/plugins/check_radius.c b/plugins/check_radius.c index d9ff8fa7..cc846709 100644 --- a/plugins/check_radius.c +++ b/plugins/check_radius.c | |||
| @@ -1,32 +1,32 @@ | |||
| 1 | /***************************************************************************** | 1 | /***************************************************************************** |
| 2 | * | 2 | * |
| 3 | * Monitoring check_radius plugin | 3 | * Monitoring check_radius plugin |
| 4 | * | 4 | * |
| 5 | * License: GPL | 5 | * License: GPL |
| 6 | * Copyright (c) 1999-2024 Monitoring Plugins Development Team | 6 | * Copyright (c) 1999-2024 Monitoring Plugins Development Team |
| 7 | * | 7 | * |
| 8 | * Description: | 8 | * Description: |
| 9 | * | 9 | * |
| 10 | * This file contains the check_radius plugin | 10 | * This file contains the check_radius plugin |
| 11 | * | 11 | * |
| 12 | * Tests to see if a radius server is accepting connections. | 12 | * Tests to see if a radius server is accepting connections. |
| 13 | * | 13 | * |
| 14 | * | 14 | * |
| 15 | * This program is free software: you can redistribute it and/or modify | 15 | * This program is free software: you can redistribute it and/or modify |
| 16 | * it under the terms of the GNU General Public License as published by | 16 | * it under the terms of the GNU General Public License as published by |
| 17 | * the Free Software Foundation, either version 3 of the License, or | 17 | * the Free Software Foundation, either version 3 of the License, or |
| 18 | * (at your option) any later version. | 18 | * (at your option) any later version. |
| 19 | * | 19 | * |
| 20 | * This program is distributed in the hope that it will be useful, | 20 | * This program is distributed in the hope that it will be useful, |
| 21 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | 21 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 22 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | 22 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 23 | * GNU General Public License for more details. | 23 | * GNU General Public License for more details. |
| 24 | * | 24 | * |
| 25 | * You should have received a copy of the GNU General Public License | 25 | * You should have received a copy of the GNU General Public License |
| 26 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | 26 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 27 | * | 27 | * |
| 28 | * | 28 | * |
| 29 | *****************************************************************************/ | 29 | *****************************************************************************/ |
| 30 | 30 | ||
| 31 | const char *progname = "check_radius"; | 31 | const char *progname = "check_radius"; |
| 32 | const char *copyright = "2000-2024"; | 32 | const char *copyright = "2000-2024"; |
| @@ -35,64 +35,57 @@ const char *email = "devel@monitoring-plugins.org"; | |||
| 35 | #include "common.h" | 35 | #include "common.h" |
| 36 | #include "utils.h" | 36 | #include "utils.h" |
| 37 | #include "netutils.h" | 37 | #include "netutils.h" |
| 38 | #include "states.h" | ||
| 39 | #include "check_radius.d/config.h" | ||
| 38 | 40 | ||
| 39 | #if defined(HAVE_LIBRADCLI) | 41 | #if defined(HAVE_LIBRADCLI) |
| 40 | #include <radcli/radcli.h> | 42 | # include <radcli/radcli.h> |
| 41 | #elif defined(HAVE_LIBFREERADIUS_CLIENT) | 43 | #elif defined(HAVE_LIBFREERADIUS_CLIENT) |
| 42 | #include <freeradius-client.h> | 44 | # include <freeradius-client.h> |
| 43 | #elif defined(HAVE_LIBRADIUSCLIENT_NG) | 45 | #elif defined(HAVE_LIBRADIUSCLIENT_NG) |
| 44 | #include <radiusclient-ng.h> | 46 | # include <radiusclient-ng.h> |
| 45 | #else | 47 | #else |
| 46 | #include <radiusclient.h> | 48 | # include <radiusclient.h> |
| 47 | #endif | 49 | #endif |
| 48 | 50 | ||
| 49 | static int process_arguments (int /*argc*/, char ** /*argv*/); | 51 | typedef struct { |
| 50 | static void print_help (void); | 52 | int errorcode; |
| 51 | void print_usage (void); | 53 | check_radius_config config; |
| 54 | } check_radius_config_wrapper; | ||
| 55 | static check_radius_config_wrapper process_arguments(int /*argc*/, char ** /*argv*/); | ||
| 56 | static void print_help(void); | ||
| 57 | void print_usage(void); | ||
| 52 | 58 | ||
| 53 | #if defined(HAVE_LIBFREERADIUS_CLIENT) || defined(HAVE_LIBRADIUSCLIENT_NG) || defined(HAVE_LIBRADCLI) | 59 | #if defined(HAVE_LIBFREERADIUS_CLIENT) || defined(HAVE_LIBRADIUSCLIENT_NG) || defined(HAVE_LIBRADCLI) |
| 54 | #define my_rc_conf_str(a) rc_conf_str(rch,a) | 60 | # define my_rc_conf_str(a) rc_conf_str(rch, a) |
| 55 | #if defined(HAVE_LIBRADCLI) | 61 | # if defined(HAVE_LIBRADCLI) |
| 56 | #define my_rc_send_server(a,b) rc_send_server(rch,a,b,AUTH) | 62 | # define my_rc_send_server(a, b) rc_send_server(rch, a, b, AUTH) |
| 57 | #else | 63 | # else |
| 58 | #define my_rc_send_server(a,b) rc_send_server(rch,a,b) | 64 | # define my_rc_send_server(a, b) rc_send_server(rch, a, b) |
| 59 | #endif | 65 | # endif |
| 60 | #if defined(HAVE_LIBFREERADIUS_CLIENT) || defined(HAVE_LIBRADCLI) | 66 | # if defined(HAVE_LIBFREERADIUS_CLIENT) || defined(HAVE_LIBRADCLI) |
| 61 | #define my_rc_buildreq(a,b,c,d,e,f) rc_buildreq(rch,a,b,c,d,(a)->secret,e,f) | 67 | # define my_rc_buildreq(a, b, c, d, e, f) rc_buildreq(rch, a, b, c, d, (a)->secret, e, f) |
| 62 | #else | 68 | # else |
| 63 | #define my_rc_buildreq(a,b,c,d,e,f) rc_buildreq(rch,a,b,c,d,e,f) | 69 | # define my_rc_buildreq(a, b, c, d, e, f) rc_buildreq(rch, a, b, c, d, e, f) |
| 64 | #endif | 70 | # endif |
| 65 | #define my_rc_avpair_add(a,b,c,d) rc_avpair_add(rch,a,b,c,-1,d) | 71 | # define my_rc_avpair_add(a, b, c, d) rc_avpair_add(rch, a, b, c, -1, d) |
| 66 | #define my_rc_read_dictionary(a) rc_read_dictionary(rch, a) | 72 | # define my_rc_read_dictionary(a) rc_read_dictionary(rch, a) |
| 67 | #else | 73 | #else |
| 68 | #define my_rc_conf_str(a) rc_conf_str(a) | 74 | # define my_rc_conf_str(a) rc_conf_str(a) |
| 69 | #define my_rc_send_server(a,b) rc_send_server(a, b) | 75 | # define my_rc_send_server(a, b) rc_send_server(a, b) |
| 70 | #define my_rc_buildreq(a,b,c,d,e,f) rc_buildreq(a,b,c,d,e,f) | 76 | # define my_rc_buildreq(a, b, c, d, e, f) rc_buildreq(a, b, c, d, e, f) |
| 71 | #define my_rc_avpair_add(a,b,c,d) rc_avpair_add(a, b, c, d) | 77 | # define my_rc_avpair_add(a, b, c, d) rc_avpair_add(a, b, c, d) |
| 72 | #define my_rc_read_dictionary(a) rc_read_dictionary(a) | 78 | # define my_rc_read_dictionary(a) rc_read_dictionary(a) |
| 73 | #endif | 79 | #endif |
| 74 | 80 | ||
| 75 | /* REJECT_RC is only defined in some version of radiusclient. It has | 81 | /* REJECT_RC is only defined in some version of radiusclient. It has |
| 76 | * been reported from radiusclient-ng 0.5.6 on FreeBSD 7.2-RELEASE */ | 82 | * been reported from radiusclient-ng 0.5.6 on FreeBSD 7.2-RELEASE */ |
| 77 | #ifndef REJECT_RC | 83 | #ifndef REJECT_RC |
| 78 | #define REJECT_RC BADRESP_RC | 84 | # define REJECT_RC BADRESP_RC |
| 79 | #endif | 85 | #endif |
| 80 | 86 | ||
| 81 | static int my_rc_read_config(char * /*a*/); | 87 | static int my_rc_read_config(char * /*a*/, rc_handle ** /*rch*/); |
| 82 | |||
| 83 | #if defined(HAVE_LIBFREERADIUS_CLIENT) || defined(HAVE_LIBRADIUSCLIENT_NG) || defined(HAVE_LIBRADCLI) | ||
| 84 | static rc_handle *rch = NULL; | ||
| 85 | #endif | ||
| 86 | 88 | ||
| 87 | static char *server = NULL; | ||
| 88 | static char *username = NULL; | ||
| 89 | static char *password = NULL; | ||
| 90 | static char *nasid = NULL; | ||
| 91 | static char *nasipaddress = NULL; | ||
| 92 | static char *expect = NULL; | ||
| 93 | static char *config_file = NULL; | ||
| 94 | static unsigned short port = PW_AUTH_UDP_PORT; | ||
| 95 | static int retries = 1; | ||
| 96 | static bool verbose = false; | 89 | static bool verbose = false; |
| 97 | 90 | ||
| 98 | /****************************************************************************** | 91 | /****************************************************************************** |
| @@ -148,149 +141,167 @@ Please note that all tags must be lowercase to use the DocBook XML DTD. | |||
| 148 | -@@ | 141 | -@@ |
| 149 | ******************************************************************************/ | 142 | ******************************************************************************/ |
| 150 | 143 | ||
| 144 | int main(int argc, char **argv) { | ||
| 145 | setlocale(LC_ALL, ""); | ||
| 146 | bindtextdomain(PACKAGE, LOCALEDIR); | ||
| 147 | textdomain(PACKAGE); | ||
| 151 | 148 | ||
| 149 | /* Parse extra opts if any */ | ||
| 150 | argv = np_extra_opts(&argc, argv, progname); | ||
| 151 | |||
| 152 | check_radius_config_wrapper tmp_config = process_arguments(argc, argv); | ||
| 153 | |||
| 154 | if (tmp_config.errorcode == ERROR) { | ||
| 155 | usage4(_("Could not parse arguments")); | ||
| 156 | } | ||
| 157 | |||
| 158 | check_radius_config config = tmp_config.config; | ||
| 159 | |||
| 160 | #if defined(HAVE_LIBFREERADIUS_CLIENT) || defined(HAVE_LIBRADIUSCLIENT_NG) || defined(HAVE_LIBRADCLI) | ||
| 161 | rc_handle *rch = NULL; | ||
| 162 | #endif | ||
| 163 | |||
| 164 | char *str = strdup("dictionary"); | ||
| 165 | if ((config.config_file && my_rc_read_config(config.config_file, &rch)) || my_rc_read_dictionary(my_rc_conf_str(str))) { | ||
| 166 | die(STATE_UNKNOWN, _("Config file error\n")); | ||
| 167 | } | ||
| 168 | |||
| 169 | uint32_t service = PW_AUTHENTICATE_ONLY; | ||
| 170 | |||
| 171 | SEND_DATA data; | ||
| 172 | memset(&data, 0, sizeof(data)); | ||
| 173 | if (!(my_rc_avpair_add(&data.send_pairs, PW_SERVICE_TYPE, &service, 0) && | ||
| 174 | my_rc_avpair_add(&data.send_pairs, PW_USER_NAME, config.username, 0) && | ||
| 175 | my_rc_avpair_add(&data.send_pairs, PW_USER_PASSWORD, config.password, 0))) { | ||
| 176 | die(STATE_UNKNOWN, _("Out of Memory?\n")); | ||
| 177 | } | ||
| 178 | |||
| 179 | if (config.nas_id != NULL) { | ||
| 180 | if (!(my_rc_avpair_add(&data.send_pairs, PW_NAS_IDENTIFIER, config.nas_id, 0))) { | ||
| 181 | die(STATE_UNKNOWN, _("Invalid NAS-Identifier\n")); | ||
| 182 | } | ||
| 183 | } | ||
| 152 | 184 | ||
| 153 | int | ||
| 154 | main (int argc, char **argv) | ||
| 155 | { | ||
| 156 | struct sockaddr_storage ss; | ||
| 157 | char name[HOST_NAME_MAX]; | 185 | char name[HOST_NAME_MAX]; |
| 186 | if (config.nas_ip_address == NULL) { | ||
| 187 | if (gethostname(name, sizeof(name)) != 0) { | ||
| 188 | die(STATE_UNKNOWN, _("gethostname() failed!\n")); | ||
| 189 | } | ||
| 190 | config.nas_ip_address = name; | ||
| 191 | } | ||
| 192 | |||
| 193 | struct sockaddr_storage radius_server_socket; | ||
| 194 | if (!dns_lookup(config.nas_ip_address, &radius_server_socket, AF_UNSPEC)) { | ||
| 195 | die(STATE_UNKNOWN, _("Invalid NAS-IP-Address\n")); | ||
| 196 | } | ||
| 197 | |||
| 198 | uint32_t client_id = ntohl(((struct sockaddr_in *)&radius_server_socket)->sin_addr.s_addr); | ||
| 199 | if (my_rc_avpair_add(&(data.send_pairs), PW_NAS_IP_ADDRESS, &client_id, 0) == NULL) { | ||
| 200 | die(STATE_UNKNOWN, _("Invalid NAS-IP-Address\n")); | ||
| 201 | } | ||
| 202 | |||
| 203 | my_rc_buildreq(&data, PW_ACCESS_REQUEST, config.server, config.port, (int)timeout_interval, config.retries); | ||
| 204 | |||
| 158 | #ifdef RC_BUFFER_LEN | 205 | #ifdef RC_BUFFER_LEN |
| 159 | char msg[RC_BUFFER_LEN]; | 206 | char msg[RC_BUFFER_LEN]; |
| 160 | #else | 207 | #else |
| 161 | char msg[BUFFER_LEN]; | 208 | char msg[BUFFER_LEN]; |
| 162 | #endif | 209 | #endif |
| 163 | SEND_DATA data; | ||
| 164 | int result = STATE_UNKNOWN; | ||
| 165 | uint32_t client_id, service; | ||
| 166 | char *str; | ||
| 167 | |||
| 168 | setlocale (LC_ALL, ""); | ||
| 169 | bindtextdomain (PACKAGE, LOCALEDIR); | ||
| 170 | textdomain (PACKAGE); | ||
| 171 | 210 | ||
| 172 | /* Parse extra opts if any */ | 211 | int result = my_rc_send_server(&data, msg); |
| 173 | argv=np_extra_opts (&argc, argv, progname); | 212 | rc_avpair_free(data.send_pairs); |
| 213 | if (data.receive_pairs) { | ||
| 214 | rc_avpair_free(data.receive_pairs); | ||
| 215 | } | ||
| 174 | 216 | ||
| 175 | if (process_arguments (argc, argv) == ERROR) | 217 | if (result == TIMEOUT_RC) { |
| 176 | usage4 (_("Could not parse arguments")); | 218 | printf("Timeout\n"); |
| 219 | exit(STATE_CRITICAL); | ||
| 220 | } | ||
| 177 | 221 | ||
| 178 | str = strdup ("dictionary"); | 222 | if (result == ERROR_RC) { |
| 179 | if ((config_file && my_rc_read_config (config_file)) || | 223 | printf(_("Auth Error\n")); |
| 180 | my_rc_read_dictionary (my_rc_conf_str (str))) | 224 | exit(STATE_CRITICAL); |
| 181 | die (STATE_UNKNOWN, _("Config file error\n")); | 225 | } |
| 182 | 226 | ||
| 183 | service = PW_AUTHENTICATE_ONLY; | 227 | if (result == REJECT_RC) { |
| 228 | printf(_("Auth Failed\n")); | ||
| 229 | exit(STATE_WARNING); | ||
| 230 | } | ||
| 184 | 231 | ||
| 185 | memset (&data, 0, sizeof(data)); | 232 | if (result == BADRESP_RC) { |
| 186 | if (!(my_rc_avpair_add (&data.send_pairs, PW_SERVICE_TYPE, &service, 0) && | 233 | printf(_("Bad Response\n")); |
| 187 | my_rc_avpair_add (&data.send_pairs, PW_USER_NAME, username, 0) && | 234 | exit(STATE_WARNING); |
| 188 | my_rc_avpair_add (&data.send_pairs, PW_USER_PASSWORD, password, 0) | 235 | } |
| 189 | )) | ||
| 190 | die (STATE_UNKNOWN, _("Out of Memory?\n")); | ||
| 191 | 236 | ||
| 192 | if (nasid != NULL) { | 237 | if (config.expect && !strstr(msg, config.expect)) { |
| 193 | if (!(my_rc_avpair_add (&data.send_pairs, PW_NAS_IDENTIFIER, nasid, 0))) | 238 | printf("%s\n", msg); |
| 194 | die (STATE_UNKNOWN, _("Invalid NAS-Identifier\n")); | 239 | exit(STATE_WARNING); |
| 195 | } | 240 | } |
| 196 | 241 | ||
| 197 | if (nasipaddress == NULL) { | 242 | if (result == OK_RC) { |
| 198 | if (gethostname (name, sizeof(name)) != 0) | 243 | printf(_("Auth OK\n")); |
| 199 | die (STATE_UNKNOWN, _("gethostname() failed!\n")); | 244 | exit(STATE_OK); |
| 200 | nasipaddress = name; | ||
| 201 | } | 245 | } |
| 202 | if (!dns_lookup (nasipaddress, &ss, AF_INET)) /* TODO: Support IPv6. */ | 246 | |
| 203 | die (STATE_UNKNOWN, _("Invalid NAS-IP-Address\n")); | ||
| 204 | client_id = ntohl (((struct sockaddr_in *)&ss)->sin_addr.s_addr); | ||
| 205 | if (my_rc_avpair_add (&(data.send_pairs), PW_NAS_IP_ADDRESS, &client_id, 0) == NULL) | ||
| 206 | die (STATE_UNKNOWN, _("Invalid NAS-IP-Address\n")); | ||
| 207 | |||
| 208 | my_rc_buildreq (&data, PW_ACCESS_REQUEST, server, port, (int)timeout_interval, | ||
| 209 | retries); | ||
| 210 | |||
| 211 | result = my_rc_send_server (&data, msg); | ||
| 212 | rc_avpair_free (data.send_pairs); | ||
| 213 | if (data.receive_pairs) | ||
| 214 | rc_avpair_free (data.receive_pairs); | ||
| 215 | |||
| 216 | if (result == TIMEOUT_RC) | ||
| 217 | die (STATE_CRITICAL, _("Timeout\n")); | ||
| 218 | if (result == ERROR_RC) | ||
| 219 | die (STATE_CRITICAL, _("Auth Error\n")); | ||
| 220 | if (result == REJECT_RC) | ||
| 221 | die (STATE_WARNING, _("Auth Failed\n")); | ||
| 222 | if (result == BADRESP_RC) | ||
| 223 | die (STATE_WARNING, _("Bad Response\n")); | ||
| 224 | if (expect && !strstr (msg, expect)) | ||
| 225 | die (STATE_WARNING, "%s\n", msg); | ||
| 226 | if (result == OK_RC) | ||
| 227 | die (STATE_OK, _("Auth OK\n")); | ||
| 228 | (void)snprintf(msg, sizeof(msg), _("Unexpected result code %d"), result); | 247 | (void)snprintf(msg, sizeof(msg), _("Unexpected result code %d"), result); |
| 229 | die (STATE_UNKNOWN, "%s\n", msg); | 248 | printf("%s\n", msg); |
| 249 | exit(STATE_UNKNOWN); | ||
| 230 | } | 250 | } |
| 231 | 251 | ||
| 232 | |||
| 233 | |||
| 234 | /* process command-line arguments */ | 252 | /* process command-line arguments */ |
| 235 | int | 253 | check_radius_config_wrapper process_arguments(int argc, char **argv) { |
| 236 | process_arguments (int argc, char **argv) | 254 | static struct option longopts[] = {{"hostname", required_argument, 0, 'H'}, {"port", required_argument, 0, 'P'}, |
| 237 | { | 255 | {"username", required_argument, 0, 'u'}, {"password", required_argument, 0, 'p'}, |
| 238 | int c; | 256 | {"nas-id", required_argument, 0, 'n'}, {"nas-ip-address", required_argument, 0, 'N'}, |
| 239 | 257 | {"filename", required_argument, 0, 'F'}, {"expect", required_argument, 0, 'e'}, | |
| 240 | int option = 0; | 258 | {"retries", required_argument, 0, 'r'}, {"timeout", required_argument, 0, 't'}, |
| 241 | static struct option longopts[] = { | 259 | {"verbose", no_argument, 0, 'v'}, {"version", no_argument, 0, 'V'}, |
| 242 | {"hostname", required_argument, 0, 'H'}, | 260 | {"help", no_argument, 0, 'h'}, {0, 0, 0, 0}}; |
| 243 | {"port", required_argument, 0, 'P'}, | 261 | |
| 244 | {"username", required_argument, 0, 'u'}, | 262 | check_radius_config_wrapper result = { |
| 245 | {"password", required_argument, 0, 'p'}, | 263 | .errorcode = OK, |
| 246 | {"nas-id", required_argument, 0, 'n'}, | 264 | .config = check_radius_config_init(), |
| 247 | {"nas-ip-address", required_argument, 0, 'N'}, | ||
| 248 | {"filename", required_argument, 0, 'F'}, | ||
| 249 | {"expect", required_argument, 0, 'e'}, | ||
| 250 | {"retries", required_argument, 0, 'r'}, | ||
| 251 | {"timeout", required_argument, 0, 't'}, | ||
| 252 | {"verbose", no_argument, 0, 'v'}, | ||
| 253 | {"version", no_argument, 0, 'V'}, | ||
| 254 | {"help", no_argument, 0, 'h'}, | ||
| 255 | {0, 0, 0, 0} | ||
| 256 | }; | 265 | }; |
| 257 | 266 | ||
| 258 | while (1) { | 267 | while (true) { |
| 259 | c = getopt_long (argc, argv, "+hVvH:P:F:u:p:n:N:t:r:e:", longopts, | 268 | int option = 0; |
| 260 | &option); | 269 | int option_index = getopt_long(argc, argv, "+hVvH:P:F:u:p:n:N:t:r:e:", longopts, &option); |
| 261 | 270 | ||
| 262 | if (c == -1 || c == EOF || c == 1) | 271 | if (option_index == -1 || option_index == EOF || option_index == 1) { |
| 263 | break; | 272 | break; |
| 273 | } | ||
| 264 | 274 | ||
| 265 | switch (c) { | 275 | switch (option_index) { |
| 266 | case '?': /* print short usage statement if args not parsable */ | 276 | case '?': /* print short usage statement if args not parsable */ |
| 267 | usage5 (); | 277 | usage5(); |
| 268 | case 'h': /* help */ | 278 | case 'h': /* help */ |
| 269 | print_help (); | 279 | print_help(); |
| 270 | exit (STATE_UNKNOWN); | 280 | exit(STATE_UNKNOWN); |
| 271 | case 'V': /* version */ | 281 | case 'V': /* version */ |
| 272 | print_revision (progname, NP_VERSION); | 282 | print_revision(progname, NP_VERSION); |
| 273 | exit (STATE_UNKNOWN); | 283 | exit(STATE_UNKNOWN); |
| 274 | case 'v': /* verbose mode */ | 284 | case 'v': /* verbose mode */ |
| 275 | verbose = true; | 285 | verbose = true; |
| 276 | break; | 286 | break; |
| 277 | case 'H': /* hostname */ | 287 | case 'H': /* hostname */ |
| 278 | if (!is_host (optarg)) { | 288 | if (!is_host(optarg)) { |
| 279 | usage2 (_("Invalid hostname/address"), optarg); | 289 | usage2(_("Invalid hostname/address"), optarg); |
| 280 | } | 290 | } |
| 281 | server = optarg; | 291 | result.config.server = optarg; |
| 282 | break; | 292 | break; |
| 283 | case 'P': /* port */ | 293 | case 'P': /* port */ |
| 284 | if (is_intnonneg (optarg)) | 294 | if (is_intnonneg(optarg)) { |
| 285 | port = (unsigned short)atoi (optarg); | 295 | result.config.port = (unsigned short)atoi(optarg); |
| 286 | else | 296 | } else { |
| 287 | usage4 (_("Port must be a positive integer")); | 297 | usage4(_("Port must be a positive integer")); |
| 298 | } | ||
| 288 | break; | 299 | break; |
| 289 | case 'u': /* username */ | 300 | case 'u': /* username */ |
| 290 | username = optarg; | 301 | result.config.username = optarg; |
| 291 | break; | 302 | break; |
| 292 | case 'p': /* password */ | 303 | case 'p': /* password */ |
| 293 | password = strdup(optarg); | 304 | result.config.password = strdup(optarg); |
| 294 | 305 | ||
| 295 | /* Delete the password from process list */ | 306 | /* Delete the password from process list */ |
| 296 | while (*optarg != '\0') { | 307 | while (*optarg != '\0') { |
| @@ -298,119 +309,115 @@ process_arguments (int argc, char **argv) | |||
| 298 | optarg++; | 309 | optarg++; |
| 299 | } | 310 | } |
| 300 | break; | 311 | break; |
| 301 | case 'n': /* nas id */ | 312 | case 'n': /* nas id */ |
| 302 | nasid = optarg; | 313 | result.config.nas_id = optarg; |
| 303 | break; | 314 | break; |
| 304 | case 'N': /* nas ip address */ | 315 | case 'N': /* nas ip address */ |
| 305 | nasipaddress = optarg; | 316 | result.config.nas_ip_address = optarg; |
| 306 | break; | 317 | break; |
| 307 | case 'F': /* configuration file */ | 318 | case 'F': /* configuration file */ |
| 308 | config_file = optarg; | 319 | result.config.config_file = optarg; |
| 309 | break; | 320 | break; |
| 310 | case 'e': /* expect */ | 321 | case 'e': /* expect */ |
| 311 | expect = optarg; | 322 | result.config.expect = optarg; |
| 312 | break; | 323 | break; |
| 313 | case 'r': /* retries */ | 324 | case 'r': /* retries */ |
| 314 | if (is_intpos (optarg)) | 325 | if (is_intpos(optarg)) { |
| 315 | retries = atoi (optarg); | 326 | result.config.retries = atoi(optarg); |
| 316 | else | 327 | } else { |
| 317 | usage4 (_("Number of retries must be a positive integer")); | 328 | usage4(_("Number of retries must be a positive integer")); |
| 329 | } | ||
| 318 | break; | 330 | break; |
| 319 | case 't': /* timeout */ | 331 | case 't': /* timeout */ |
| 320 | if (is_intpos (optarg)) | 332 | if (is_intpos(optarg)) { |
| 321 | timeout_interval = (unsigned)atoi (optarg); | 333 | timeout_interval = (unsigned)atoi(optarg); |
| 322 | else | 334 | } else { |
| 323 | usage2 (_("Timeout interval must be a positive integer"), optarg); | 335 | usage2(_("Timeout interval must be a positive integer"), optarg); |
| 336 | } | ||
| 324 | break; | 337 | break; |
| 325 | } | 338 | } |
| 326 | } | 339 | } |
| 327 | 340 | ||
| 328 | if (server == NULL) | 341 | if (result.config.server == NULL) { |
| 329 | usage4 (_("Hostname was not supplied")); | 342 | usage4(_("Hostname was not supplied")); |
| 330 | if (username == NULL) | 343 | } |
| 331 | usage4 (_("User not specified")); | 344 | if (result.config.username == NULL) { |
| 332 | if (password == NULL) | 345 | usage4(_("User not specified")); |
| 333 | usage4 (_("Password not specified")); | 346 | } |
| 334 | if (config_file == NULL) | 347 | if (result.config.password == NULL) { |
| 335 | usage4 (_("Configuration file not specified")); | 348 | usage4(_("Password not specified")); |
| 349 | } | ||
| 350 | if (result.config.config_file == NULL) { | ||
| 351 | usage4(_("Configuration file not specified")); | ||
| 352 | } | ||
| 336 | 353 | ||
| 337 | return OK; | 354 | return result; |
| 338 | } | 355 | } |
| 339 | 356 | ||
| 340 | 357 | void print_help(void) { | |
| 341 | |||
| 342 | void | ||
| 343 | print_help (void) | ||
| 344 | { | ||
| 345 | char *myport; | 358 | char *myport; |
| 346 | xasprintf (&myport, "%d", PW_AUTH_UDP_PORT); | 359 | xasprintf(&myport, "%d", PW_AUTH_UDP_PORT); |
| 347 | 360 | ||
| 348 | print_revision (progname, NP_VERSION); | 361 | print_revision(progname, NP_VERSION); |
| 349 | 362 | ||
| 350 | printf ("Copyright (c) 1999 Robert August Vincent II\n"); | 363 | printf("Copyright (c) 1999 Robert August Vincent II\n"); |
| 351 | printf (COPYRIGHT, copyright, email); | 364 | printf(COPYRIGHT, copyright, email); |
| 352 | 365 | ||
| 353 | printf("%s\n", _("Tests to see if a RADIUS server is accepting connections.")); | 366 | printf("%s\n", _("Tests to see if a RADIUS server is accepting connections.")); |
| 354 | 367 | ||
| 355 | printf ("\n\n"); | 368 | printf("\n\n"); |
| 356 | 369 | ||
| 357 | print_usage (); | 370 | print_usage(); |
| 358 | 371 | ||
| 359 | printf (UT_HELP_VRSN); | 372 | printf(UT_HELP_VRSN); |
| 360 | printf (UT_EXTRA_OPTS); | 373 | printf(UT_EXTRA_OPTS); |
| 361 | 374 | ||
| 362 | printf (UT_HOST_PORT, 'P', myport); | 375 | printf(UT_HOST_PORT, 'P', myport); |
| 363 | 376 | ||
| 364 | printf (" %s\n", "-u, --username=STRING"); | 377 | printf(" %s\n", "-u, --username=STRING"); |
| 365 | printf (" %s\n", _("The user to authenticate")); | 378 | printf(" %s\n", _("The user to authenticate")); |
| 366 | printf (" %s\n", "-p, --password=STRING"); | 379 | printf(" %s\n", "-p, --password=STRING"); |
| 367 | printf (" %s\n", _("Password for authentication (SECURITY RISK)")); | 380 | printf(" %s\n", _("Password for authentication (SECURITY RISK)")); |
| 368 | printf (" %s\n", "-n, --nas-id=STRING"); | 381 | printf(" %s\n", "-n, --nas-id=STRING"); |
| 369 | printf (" %s\n", _("NAS identifier")); | 382 | printf(" %s\n", _("NAS identifier")); |
| 370 | printf (" %s\n", "-N, --nas-ip-address=STRING"); | 383 | printf(" %s\n", "-N, --nas-ip-address=STRING"); |
| 371 | printf (" %s\n", _("NAS IP Address")); | 384 | printf(" %s\n", _("NAS IP Address")); |
| 372 | printf (" %s\n", "-F, --filename=STRING"); | 385 | printf(" %s\n", "-F, --filename=STRING"); |
| 373 | printf (" %s\n", _("Configuration file")); | 386 | printf(" %s\n", _("Configuration file")); |
| 374 | printf (" %s\n", "-e, --expect=STRING"); | 387 | printf(" %s\n", "-e, --expect=STRING"); |
| 375 | printf (" %s\n", _("Response string to expect from the server")); | 388 | printf(" %s\n", _("Response string to expect from the server")); |
| 376 | printf (" %s\n", "-r, --retries=INTEGER"); | 389 | printf(" %s\n", "-r, --retries=INTEGER"); |
| 377 | printf (" %s\n", _("Number of times to retry a failed connection")); | 390 | printf(" %s\n", _("Number of times to retry a failed connection")); |
| 378 | 391 | ||
| 379 | printf (UT_CONN_TIMEOUT, timeout_interval); | 392 | printf(UT_CONN_TIMEOUT, timeout_interval); |
| 380 | 393 | ||
| 381 | printf ("\n"); | 394 | printf("\n"); |
| 382 | printf ("%s\n", _("This plugin tests a RADIUS server to see if it is accepting connections.")); | 395 | printf("%s\n", _("This plugin tests a RADIUS server to see if it is accepting connections.")); |
| 383 | printf ("%s\n", _("The server to test must be specified in the invocation, as well as a user")); | 396 | printf("%s\n", _("The server to test must be specified in the invocation, as well as a user")); |
| 384 | printf ("%s\n", _("name and password. A configuration file must be present. The format of")); | 397 | printf("%s\n", _("name and password. A configuration file must be present. The format of")); |
| 385 | printf ("%s\n", _("the configuration file is described in the radiusclient library sources.")); | 398 | printf("%s\n", _("the configuration file is described in the radiusclient library sources.")); |
| 386 | printf ("%s\n", _("The password option presents a substantial security issue because the")); | 399 | printf("%s\n", _("The password option presents a substantial security issue because the")); |
| 387 | printf ("%s\n", _("password can possibly be determined by careful watching of the command line")); | 400 | printf("%s\n", _("password can possibly be determined by careful watching of the command line")); |
| 388 | printf ("%s\n", _("in a process listing. This risk is exacerbated because the plugin will")); | 401 | printf("%s\n", _("in a process listing. This risk is exacerbated because the plugin will")); |
| 389 | printf ("%s\n", _("typically be executed at regular predictable intervals. Please be sure that")); | 402 | printf("%s\n", _("typically be executed at regular predictable intervals. Please be sure that")); |
| 390 | printf ("%s\n", _("the password used does not allow access to sensitive system resources.")); | 403 | printf("%s\n", _("the password used does not allow access to sensitive system resources.")); |
| 391 | 404 | ||
| 392 | printf (UT_SUPPORT); | 405 | printf(UT_SUPPORT); |
| 393 | } | 406 | } |
| 394 | 407 | ||
| 395 | 408 | void print_usage(void) { | |
| 396 | 409 | printf("%s\n", _("Usage:")); | |
| 397 | void | 410 | printf("%s -H host -F config_file -u username -p password\n\ |
| 398 | print_usage (void) | ||
| 399 | { | ||
| 400 | printf ("%s\n", _("Usage:")); | ||
| 401 | printf ("%s -H host -F config_file -u username -p password\n\ | ||
| 402 | [-P port] [-t timeout] [-r retries] [-e expect]\n\ | 411 | [-P port] [-t timeout] [-r retries] [-e expect]\n\ |
| 403 | [-n nas-id] [-N nas-ip-addr]\n", progname); | 412 | [-n nas-id] [-N nas-ip-addr]\n", |
| 413 | progname); | ||
| 404 | } | 414 | } |
| 405 | 415 | ||
| 406 | 416 | int my_rc_read_config(char *config_file_name, rc_handle **rch) { | |
| 407 | |||
| 408 | int my_rc_read_config(char * a) | ||
| 409 | { | ||
| 410 | #if defined(HAVE_LIBFREERADIUS_CLIENT) || defined(HAVE_LIBRADIUSCLIENT_NG) || defined(HAVE_LIBRADCLI) | 417 | #if defined(HAVE_LIBFREERADIUS_CLIENT) || defined(HAVE_LIBRADIUSCLIENT_NG) || defined(HAVE_LIBRADCLI) |
| 411 | rch = rc_read_config(a); | 418 | *rch = rc_read_config(config_file_name); |
| 412 | return (rch == NULL) ? 1 : 0; | 419 | return (rch == NULL) ? 1 : 0; |
| 413 | #else | 420 | #else |
| 414 | return rc_read_config(a); | 421 | return rc_read_config(config_file_name); |
| 415 | #endif | 422 | #endif |
| 416 | } | 423 | } |
diff --git a/plugins/check_radius.d/config.h b/plugins/check_radius.d/config.h new file mode 100644 index 00000000..b27d31e7 --- /dev/null +++ b/plugins/check_radius.d/config.h | |||
| @@ -0,0 +1,42 @@ | |||
| 1 | #pragma once | ||
| 2 | |||
| 3 | #include "../../config.h" | ||
| 4 | #include <stddef.h> | ||
| 5 | #if defined(HAVE_LIBRADCLI) | ||
| 6 | # include <radcli/radcli.h> | ||
| 7 | #elif defined(HAVE_LIBFREERADIUS_CLIENT) | ||
| 8 | # include <freeradius-client.h> | ||
| 9 | #elif defined(HAVE_LIBRADIUSCLIENT_NG) | ||
| 10 | # include <radiusclient-ng.h> | ||
| 11 | #else | ||
| 12 | # include <radiusclient.h> | ||
| 13 | #endif | ||
| 14 | |||
| 15 | typedef struct { | ||
| 16 | char *server; | ||
| 17 | char *username; | ||
| 18 | char *password; | ||
| 19 | char *config_file; | ||
| 20 | char *nas_id; | ||
| 21 | char *nas_ip_address; | ||
| 22 | int retries; | ||
| 23 | unsigned short port; | ||
| 24 | |||
| 25 | char *expect; | ||
| 26 | } check_radius_config; | ||
| 27 | |||
| 28 | check_radius_config check_radius_config_init() { | ||
| 29 | check_radius_config tmp = { | ||
| 30 | .server = NULL, | ||
| 31 | .username = NULL, | ||
| 32 | .password = NULL, | ||
| 33 | .config_file = NULL, | ||
| 34 | .nas_id = NULL, | ||
| 35 | .nas_ip_address = NULL, | ||
| 36 | .retries = 1, | ||
| 37 | .port = PW_AUTH_UDP_PORT, | ||
| 38 | |||
| 39 | .expect = NULL, | ||
| 40 | }; | ||
| 41 | return tmp; | ||
| 42 | } | ||
