diff options
Diffstat (limited to 'plugins/check_ntp_peer.c')
| -rw-r--r-- | plugins/check_ntp_peer.c | 636 | 
1 files changed, 321 insertions, 315 deletions
| diff --git a/plugins/check_ntp_peer.c b/plugins/check_ntp_peer.c index 464a9e10..f99e5032 100644 --- a/plugins/check_ntp_peer.c +++ b/plugins/check_ntp_peer.c | |||
| @@ -1,88 +1,88 @@ | |||
| 1 | /***************************************************************************** | 1 | /***************************************************************************** | 
| 2 | * | 2 | * | 
| 3 | * Monitoring check_ntp_peer plugin | 3 | * Monitoring check_ntp_peer plugin | 
| 4 | * | 4 | * | 
| 5 | * License: GPL | 5 | * License: GPL | 
| 6 | * Copyright (c) 2006 Sean Finney <seanius@seanius.net> | 6 | * Copyright (c) 2006 Sean Finney <seanius@seanius.net> | 
| 7 | * Copyright (c) 2006-2008 Monitoring Plugins Development Team | 7 | * Copyright (c) 2006-2024 Monitoring Plugins Development Team | 
| 8 | * | 8 | * | 
| 9 | * Description: | 9 | * Description: | 
| 10 | * | 10 | * | 
| 11 | * This file contains the check_ntp_peer plugin | 11 | * This file contains the check_ntp_peer plugin | 
| 12 | * | 12 | * | 
| 13 | * This plugin checks an NTP server independent of any commandline | 13 | * This plugin checks an NTP server independent of any commandline | 
| 14 | * programs or external libraries. | 14 | * programs or external libraries. | 
| 15 | * | 15 | * | 
| 16 | * Use this plugin to check the health of an NTP server. It supports | 16 | * Use this plugin to check the health of an NTP server. It supports | 
| 17 | * checking the offset with the sync peer, the jitter and stratum. This | 17 | * checking the offset with the sync peer, the jitter and stratum. This | 
| 18 | * plugin will not check the clock offset between the local host and NTP | 18 | * plugin will not check the clock offset between the local host and NTP | 
| 19 | * server; please use check_ntp_time for that purpose. | 19 | * server; please use check_ntp_time for that purpose. | 
| 20 | * | 20 | * | 
| 21 | * | 21 | * | 
| 22 | * This program is free software: you can redistribute it and/or modify | 22 | * This program is free software: you can redistribute it and/or modify | 
| 23 | * it under the terms of the GNU General Public License as published by | 23 | * it under the terms of the GNU General Public License as published by | 
| 24 | * the Free Software Foundation, either version 3 of the License, or | 24 | * the Free Software Foundation, either version 3 of the License, or | 
| 25 | * (at your option) any later version. | 25 | * (at your option) any later version. | 
| 26 | * | 26 | * | 
| 27 | * This program is distributed in the hope that it will be useful, | 27 | * This program is distributed in the hope that it will be useful, | 
| 28 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | 28 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | 
| 29 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | 29 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | 
| 30 | * GNU General Public License for more details. | 30 | * GNU General Public License for more details. | 
| 31 | * | 31 | * | 
| 32 | * You should have received a copy of the GNU General Public License | 32 | * You should have received a copy of the GNU General Public License | 
| 33 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | 33 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | 
| 34 | * | 34 | * | 
| 35 | * | 35 | * | 
| 36 | *****************************************************************************/ | 36 | *****************************************************************************/ | 
| 37 | 37 | ||
| 38 | const char *progname = "check_ntp_peer"; | 38 | const char *progname = "check_ntp_peer"; | 
| 39 | const char *copyright = "2006-2008"; | 39 | const char *copyright = "2006-2024"; | 
| 40 | const char *email = "devel@monitoring-plugins.org"; | 40 | const char *email = "devel@monitoring-plugins.org"; | 
| 41 | 41 | ||
| 42 | #include "common.h" | 42 | #include "common.h" | 
| 43 | #include "netutils.h" | 43 | #include "netutils.h" | 
| 44 | #include "utils.h" | 44 | #include "utils.h" | 
| 45 | 45 | ||
| 46 | static char *server_address=NULL; | 46 | static char *server_address = NULL; | 
| 47 | static int port=123; | 47 | static int port = 123; | 
| 48 | static int verbose=0; | 48 | static int verbose = 0; | 
| 49 | static bool quiet = false; | 49 | static bool quiet = false; | 
| 50 | static char *owarn="60"; | 50 | static char *owarn = "60"; | 
| 51 | static char *ocrit="120"; | 51 | static char *ocrit = "120"; | 
| 52 | static bool do_stratum = false; | 52 | static bool do_stratum = false; | 
| 53 | static char *swarn="-1:16"; | 53 | static char *swarn = "-1:16"; | 
| 54 | static char *scrit="-1:16"; | 54 | static char *scrit = "-1:16"; | 
| 55 | static bool do_jitter = false; | 55 | static bool do_jitter = false; | 
| 56 | static char *jwarn="-1:5000"; | 56 | static char *jwarn = "-1:5000"; | 
| 57 | static char *jcrit="-1:10000"; | 57 | static char *jcrit = "-1:10000"; | 
| 58 | static bool do_truechimers = false; | 58 | static bool do_truechimers = false; | 
| 59 | static char *twarn="0:"; | 59 | static char *twarn = "0:"; | 
| 60 | static char *tcrit="0:"; | 60 | static char *tcrit = "0:"; | 
| 61 | static bool syncsource_found = false; | 61 | static bool syncsource_found = false; | 
| 62 | static bool li_alarm = false; | 62 | static bool li_alarm = false; | 
| 63 | 63 | ||
| 64 | int process_arguments (int, char **); | 64 | static int process_arguments(int /*argc*/, char ** /*argv*/); | 
| 65 | thresholds *offset_thresholds = NULL; | 65 | static thresholds *offset_thresholds = NULL; | 
| 66 | thresholds *jitter_thresholds = NULL; | 66 | static thresholds *jitter_thresholds = NULL; | 
| 67 | thresholds *stratum_thresholds = NULL; | 67 | static thresholds *stratum_thresholds = NULL; | 
| 68 | thresholds *truechimer_thresholds = NULL; | 68 | static thresholds *truechimer_thresholds = NULL; | 
| 69 | void print_help (void); | 69 | static void print_help(void); | 
| 70 | void print_usage (void); | 70 | void print_usage(void); | 
| 71 | 71 | ||
| 72 | /* max size of control message data */ | 72 | /* max size of control message data */ | 
| 73 | #define MAX_CM_SIZE 468 | 73 | #define MAX_CM_SIZE 468 | 
| 74 | 74 | ||
| 75 | /* this structure holds everything in an ntp control message as per rfc1305 */ | 75 | /* this structure holds everything in an ntp control message as per rfc1305 */ | 
| 76 | typedef struct { | 76 | typedef struct { | 
| 77 | uint8_t flags; /* byte with leapindicator,vers,mode. see macros */ | 77 | uint8_t flags; /* byte with leapindicator,vers,mode. see macros */ | 
| 78 | uint8_t op; /* R,E,M bits and Opcode */ | 78 | uint8_t op; /* R,E,M bits and Opcode */ | 
| 79 | uint16_t seq; /* Packet sequence */ | 79 | uint16_t seq; /* Packet sequence */ | 
| 80 | uint16_t status; /* Clock status */ | 80 | uint16_t status; /* Clock status */ | 
| 81 | uint16_t assoc; /* Association */ | 81 | uint16_t assoc; /* Association */ | 
| 82 | uint16_t offset; /* Similar to TCP sequence # */ | 82 | uint16_t offset; /* Similar to TCP sequence # */ | 
| 83 | uint16_t count; /* # bytes of data */ | 83 | uint16_t count; /* # bytes of data */ | 
| 84 | char data[MAX_CM_SIZE]; /* ASCII data of the request */ | 84 | char data[MAX_CM_SIZE]; /* ASCII data of the request */ | 
| 85 | /* NB: not necessarily NULL terminated! */ | 85 | /* NB: not necessarily NULL terminated! */ | 
| 86 | } ntp_control_message; | 86 | } ntp_control_message; | 
| 87 | 87 | ||
| 88 | /* this is an association/status-word pair found in control packet responses */ | 88 | /* this is an association/status-word pair found in control packet responses */ | 
| @@ -93,82 +93,96 @@ typedef struct { | |||
| 93 | 93 | ||
| 94 | /* bits 1,2 are the leap indicator */ | 94 | /* bits 1,2 are the leap indicator */ | 
| 95 | #define LI_MASK 0xc0 | 95 | #define LI_MASK 0xc0 | 
| 96 | #define LI(x) ((x&LI_MASK)>>6) | 96 | #define LI(x) ((x & LI_MASK) >> 6) | 
| 97 | #define LI_SET(x,y) do{ x |= ((y<<6)&LI_MASK); }while(0) | 97 | #define LI_SET(x, y) \ | 
| 98 | do { \ | ||
| 99 | x |= ((y << 6) & LI_MASK); \ | ||
| 100 | } while (0) | ||
| 98 | /* and these are the values of the leap indicator */ | 101 | /* and these are the values of the leap indicator */ | 
| 99 | #define LI_NOWARNING 0x00 | 102 | #define LI_NOWARNING 0x00 | 
| 100 | #define LI_EXTRASEC 0x01 | 103 | #define LI_EXTRASEC 0x01 | 
| 101 | #define LI_MISSINGSEC 0x02 | 104 | #define LI_MISSINGSEC 0x02 | 
| 102 | #define LI_ALARM 0x03 | 105 | #define LI_ALARM 0x03 | 
| 103 | /* bits 3,4,5 are the ntp version */ | 106 | /* bits 3,4,5 are the ntp version */ | 
| 104 | #define VN_MASK 0x38 | 107 | #define VN_MASK 0x38 | 
| 105 | #define VN(x) ((x&VN_MASK)>>3) | 108 | #define VN(x) ((x & VN_MASK) >> 3) | 
| 106 | #define VN_SET(x,y) do{ x |= ((y<<3)&VN_MASK); }while(0) | 109 | #define VN_SET(x, y) \ | 
| 110 | do { \ | ||
| 111 | x |= ((y << 3) & VN_MASK); \ | ||
| 112 | } while (0) | ||
| 107 | #define VN_RESERVED 0x02 | 113 | #define VN_RESERVED 0x02 | 
| 108 | /* bits 6,7,8 are the ntp mode */ | 114 | /* bits 6,7,8 are the ntp mode */ | 
| 109 | #define MODE_MASK 0x07 | 115 | #define MODE_MASK 0x07 | 
| 110 | #define MODE(x) (x&MODE_MASK) | 116 | #define MODE(x) (x & MODE_MASK) | 
| 111 | #define MODE_SET(x,y) do{ x |= (y&MODE_MASK); }while(0) | 117 | #define MODE_SET(x, y) \ | 
| 118 | do { \ | ||
| 119 | x |= (y & MODE_MASK); \ | ||
| 120 | } while (0) | ||
| 112 | /* here are some values */ | 121 | /* here are some values */ | 
| 113 | #define MODE_CLIENT 0x03 | 122 | #define MODE_CLIENT 0x03 | 
| 114 | #define MODE_CONTROLMSG 0x06 | 123 | #define MODE_CONTROLMSG 0x06 | 
| 115 | /* In control message, bits 8-10 are R,E,M bits */ | 124 | /* In control message, bits 8-10 are R,E,M bits */ | 
| 116 | #define REM_MASK 0xe0 | 125 | #define REM_MASK 0xe0 | 
| 117 | #define REM_RESP 0x80 | 126 | #define REM_RESP 0x80 | 
| 118 | #define REM_ERROR 0x40 | 127 | #define REM_ERROR 0x40 | 
| 119 | #define REM_MORE 0x20 | 128 | #define REM_MORE 0x20 | 
| 120 | /* In control message, bits 11 - 15 are opcode */ | 129 | /* In control message, bits 11 - 15 are opcode */ | 
| 121 | #define OP_MASK 0x1f | 130 | #define OP_MASK 0x1f | 
| 122 | #define OP_SET(x,y) do{ x |= (y&OP_MASK); }while(0) | 131 | #define OP_SET(x, y) \ | 
| 132 | do { \ | ||
| 133 | x |= (y & OP_MASK); \ | ||
| 134 | } while (0) | ||
| 123 | #define OP_READSTAT 0x01 | 135 | #define OP_READSTAT 0x01 | 
| 124 | #define OP_READVAR 0x02 | 136 | #define OP_READVAR 0x02 | 
| 125 | /* In peer status bytes, bits 6,7,8 determine clock selection status */ | 137 | /* In peer status bytes, bits 6,7,8 determine clock selection status */ | 
| 126 | #define PEER_SEL(x) ((ntohs(x)>>8)&0x07) | 138 | #define PEER_SEL(x) ((ntohs(x) >> 8) & 0x07) | 
| 127 | #define PEER_TRUECHIMER 0x02 | 139 | #define PEER_TRUECHIMER 0x02 | 
| 128 | #define PEER_INCLUDED 0x04 | 140 | #define PEER_INCLUDED 0x04 | 
| 129 | #define PEER_SYNCSOURCE 0x06 | 141 | #define PEER_SYNCSOURCE 0x06 | 
| 130 | 142 | ||
| 131 | /* NTP control message header is 12 bytes, plus any data in the data | 143 | /* NTP control message header is 12 bytes, plus any data in the data | 
| 132 | * field, plus null padding to the nearest 32-bit boundary per rfc. | 144 | * field, plus null padding to the nearest 32-bit boundary per rfc. | 
| 133 | */ | 145 | */ | 
| 134 | #define SIZEOF_NTPCM(m) (12+ntohs(m.count)+((ntohs(m.count)%4)?4-(ntohs(m.count)%4):0)) | 146 | #define SIZEOF_NTPCM(m) (12 + ntohs(m.count) + ((ntohs(m.count) % 4) ? 4 - (ntohs(m.count) % 4) : 0)) | 
| 135 | 147 | ||
| 136 | /* finally, a little helper or two for debugging: */ | 148 | /* finally, a little helper or two for debugging: */ | 
| 137 | #define DBG(x) do{if(verbose>1){ x; }}while(0); | 149 | #define DBG(x) \ | 
| 138 | #define PRINTSOCKADDR(x) \ | 150 | do { \ | 
| 139 | do{ \ | 151 | if (verbose > 1) { \ | 
| 140 | printf("%u.%u.%u.%u", (x>>24)&0xff, (x>>16)&0xff, (x>>8)&0xff, x&0xff);\ | 152 | x; \ | 
| 141 | }while(0); | 153 | } \ | 
| 142 | 154 | } while (0); | |
| 143 | void print_ntp_control_message(const ntp_control_message *p){ | 155 | #define PRINTSOCKADDR(x) \ | 
| 144 | int i=0, numpeers=0; | 156 | do { \ | 
| 145 | const ntp_assoc_status_pair *peer=NULL; | 157 | printf("%u.%u.%u.%u", (x >> 24) & 0xff, (x >> 16) & 0xff, (x >> 8) & 0xff, x & 0xff); \ | 
| 146 | 158 | } while (0); | |
| 159 | |||
| 160 | void print_ntp_control_message(const ntp_control_message *p) { | ||
| 147 | printf("control packet contents:\n"); | 161 | printf("control packet contents:\n"); | 
| 148 | printf("\tflags: 0x%.2x , 0x%.2x\n", p->flags, p->op); | 162 | printf("\tflags: 0x%.2x , 0x%.2x\n", p->flags, p->op); | 
| 149 | printf("\t li=%d (0x%.2x)\n", LI(p->flags), p->flags&LI_MASK); | 163 | printf("\t li=%d (0x%.2x)\n", LI(p->flags), p->flags & LI_MASK); | 
| 150 | printf("\t vn=%d (0x%.2x)\n", VN(p->flags), p->flags&VN_MASK); | 164 | printf("\t vn=%d (0x%.2x)\n", VN(p->flags), p->flags & VN_MASK); | 
| 151 | printf("\t mode=%d (0x%.2x)\n", MODE(p->flags), p->flags&MODE_MASK); | 165 | printf("\t mode=%d (0x%.2x)\n", MODE(p->flags), p->flags & MODE_MASK); | 
| 152 | printf("\t response=%d (0x%.2x)\n", (p->op&REM_RESP)>0, p->op&REM_RESP); | 166 | printf("\t response=%d (0x%.2x)\n", (p->op & REM_RESP) > 0, p->op & REM_RESP); | 
| 153 | printf("\t more=%d (0x%.2x)\n", (p->op&REM_MORE)>0, p->op&REM_MORE); | 167 | printf("\t more=%d (0x%.2x)\n", (p->op & REM_MORE) > 0, p->op & REM_MORE); | 
| 154 | printf("\t error=%d (0x%.2x)\n", (p->op&REM_ERROR)>0, p->op&REM_ERROR); | 168 | printf("\t error=%d (0x%.2x)\n", (p->op & REM_ERROR) > 0, p->op & REM_ERROR); | 
| 155 | printf("\t op=%d (0x%.2x)\n", p->op&OP_MASK, p->op&OP_MASK); | 169 | printf("\t op=%d (0x%.2x)\n", p->op & OP_MASK, p->op & OP_MASK); | 
| 156 | printf("\tsequence: %d (0x%.2x)\n", ntohs(p->seq), ntohs(p->seq)); | 170 | printf("\tsequence: %d (0x%.2x)\n", ntohs(p->seq), ntohs(p->seq)); | 
| 157 | printf("\tstatus: %d (0x%.2x)\n", ntohs(p->status), ntohs(p->status)); | 171 | printf("\tstatus: %d (0x%.2x)\n", ntohs(p->status), ntohs(p->status)); | 
| 158 | printf("\tassoc: %d (0x%.2x)\n", ntohs(p->assoc), ntohs(p->assoc)); | 172 | printf("\tassoc: %d (0x%.2x)\n", ntohs(p->assoc), ntohs(p->assoc)); | 
| 159 | printf("\toffset: %d (0x%.2x)\n", ntohs(p->offset), ntohs(p->offset)); | 173 | printf("\toffset: %d (0x%.2x)\n", ntohs(p->offset), ntohs(p->offset)); | 
| 160 | printf("\tcount: %d (0x%.2x)\n", ntohs(p->count), ntohs(p->count)); | 174 | printf("\tcount: %d (0x%.2x)\n", ntohs(p->count), ntohs(p->count)); | 
| 161 | numpeers=ntohs(p->count)/(sizeof(ntp_assoc_status_pair)); | 175 | |
| 162 | if(p->op&REM_RESP && p->op&OP_READSTAT){ | 176 | int numpeers = ntohs(p->count) / (sizeof(ntp_assoc_status_pair)); | 
| 163 | peer=(ntp_assoc_status_pair*)p->data; | 177 | if (p->op & REM_RESP && p->op & OP_READSTAT) { | 
| 164 | for(i=0;i<numpeers;i++){ | 178 | const ntp_assoc_status_pair *peer = (ntp_assoc_status_pair *)p->data; | 
| 165 | printf("\tpeer id %.2x status %.2x", | 179 | for (int i = 0; i < numpeers; i++) { | 
| 166 | ntohs(peer[i].assoc), ntohs(peer[i].status)); | 180 | printf("\tpeer id %.2x status %.2x", ntohs(peer[i].assoc), ntohs(peer[i].status)); | 
| 167 | if(PEER_SEL(peer[i].status) >= PEER_SYNCSOURCE){ | 181 | if (PEER_SEL(peer[i].status) >= PEER_SYNCSOURCE) { | 
| 168 | printf(" <-- current sync source"); | 182 | printf(" <-- current sync source"); | 
| 169 | } else if(PEER_SEL(peer[i].status) >= PEER_INCLUDED){ | 183 | } else if (PEER_SEL(peer[i].status) >= PEER_INCLUDED) { | 
| 170 | printf(" <-- current sync candidate"); | 184 | printf(" <-- current sync candidate"); | 
| 171 | } else if(PEER_SEL(peer[i].status) >= PEER_TRUECHIMER){ | 185 | } else if (PEER_SEL(peer[i].status) >= PEER_TRUECHIMER) { | 
| 172 | printf(" <-- outlyer, but truechimer"); | 186 | printf(" <-- outlyer, but truechimer"); | 
| 173 | } | 187 | } | 
| 174 | printf("\n"); | 188 | printf("\n"); | 
| @@ -176,8 +190,7 @@ void print_ntp_control_message(const ntp_control_message *p){ | |||
| 176 | } | 190 | } | 
| 177 | } | 191 | } | 
| 178 | 192 | ||
| 179 | void | 193 | void setup_control_request(ntp_control_message *p, uint8_t opcode, uint16_t seq) { | 
| 180 | setup_control_request(ntp_control_message *p, uint8_t opcode, uint16_t seq){ | ||
| 181 | memset(p, 0, sizeof(ntp_control_message)); | 194 | memset(p, 0, sizeof(ntp_control_message)); | 
| 182 | LI_SET(p->flags, LI_NOWARNING); | 195 | LI_SET(p->flags, LI_NOWARNING); | 
| 183 | VN_SET(p->flags, VN_RESERVED); | 196 | VN_SET(p->flags, VN_RESERVED); | 
| @@ -198,19 +211,7 @@ setup_control_request(ntp_control_message *p, uint8_t opcode, uint16_t seq){ | |||
| 198 | * status is pretty much useless as syncsource_found is a global variable | 211 | * status is pretty much useless as syncsource_found is a global variable | 
| 199 | * used later in main to check is the server was synchronized. It works | 212 | * used later in main to check is the server was synchronized. It works | 
| 200 | * so I left it alone */ | 213 | * so I left it alone */ | 
| 201 | int ntp_request(double *offset, int *offset_result, double *jitter, int *stratum, int *num_truechimers){ | 214 | int ntp_request(double *offset, int *offset_result, double *jitter, int *stratum, int *num_truechimers) { | 
| 202 | int conn=-1, i, npeers=0, num_candidates=0; | ||
| 203 | double tmp_offset = 0; | ||
| 204 | int min_peer_sel=PEER_INCLUDED; | ||
| 205 | int peers_size=0, peer_offset=0; | ||
| 206 | int status; | ||
| 207 | ntp_assoc_status_pair *peers=NULL; | ||
| 208 | ntp_control_message req; | ||
| 209 | const char *getvar = "stratum,offset,jitter"; | ||
| 210 | char *data, *value, *nptr; | ||
| 211 | void *tmp; | ||
| 212 | |||
| 213 | status = STATE_OK; | ||
| 214 | *offset_result = STATE_UNKNOWN; | 215 | *offset_result = STATE_UNKNOWN; | 
| 215 | *jitter = *stratum = -1; | 216 | *jitter = *stratum = -1; | 
| 216 | *num_truechimers = 0; | 217 | *num_truechimers = 0; | 
| @@ -231,11 +232,20 @@ int ntp_request(double *offset, int *offset_result, double *jitter, int *stratum | |||
| 231 | * 4) Extract the offset, jitter and stratum value from the data[] | 232 | * 4) Extract the offset, jitter and stratum value from the data[] | 
| 232 | * (it's ASCII) | 233 | * (it's ASCII) | 
| 233 | */ | 234 | */ | 
| 235 | int min_peer_sel = PEER_INCLUDED; | ||
| 236 | int num_candidates = 0; | ||
| 237 | void *tmp; | ||
| 238 | ntp_assoc_status_pair *peers = NULL; | ||
| 239 | int peer_offset = 0; | ||
| 240 | int peers_size = 0; | ||
| 241 | int npeers = 0; | ||
| 242 | int conn = -1; | ||
| 234 | my_udp_connect(server_address, port, &conn); | 243 | my_udp_connect(server_address, port, &conn); | 
| 235 | 244 | ||
| 236 | /* keep sending requests until the server stops setting the | 245 | /* keep sending requests until the server stops setting the | 
| 237 | * REM_MORE bit, though usually this is only 1 packet. */ | 246 | * REM_MORE bit, though usually this is only 1 packet. */ | 
| 238 | do{ | 247 | ntp_control_message req; | 
| 248 | do { | ||
| 239 | setup_control_request(&req, OP_READSTAT, 1); | 249 | setup_control_request(&req, OP_READSTAT, 1); | 
| 240 | DBG(printf("sending READSTAT request")); | 250 | DBG(printf("sending READSTAT request")); | 
| 241 | write(conn, &req, SIZEOF_NTPCM(req)); | 251 | write(conn, &req, SIZEOF_NTPCM(req)); | 
| @@ -243,63 +253,73 @@ int ntp_request(double *offset, int *offset_result, double *jitter, int *stratum | |||
| 243 | 253 | ||
| 244 | do { | 254 | do { | 
| 245 | /* Attempt to read the largest size packet possible */ | 255 | /* Attempt to read the largest size packet possible */ | 
| 246 | req.count=htons(MAX_CM_SIZE); | 256 | req.count = htons(MAX_CM_SIZE); | 
| 247 | DBG(printf("receiving READSTAT response")) | 257 | DBG(printf("receiving READSTAT response")) | 
| 248 | if(read(conn, &req, SIZEOF_NTPCM(req)) == -1) | 258 | if (read(conn, &req, SIZEOF_NTPCM(req)) == -1) | 
| 249 | die(STATE_CRITICAL, "NTP CRITICAL: No response from NTP server\n"); | 259 | die(STATE_CRITICAL, "NTP CRITICAL: No response from NTP server\n"); | 
| 250 | DBG(print_ntp_control_message(&req)); | 260 | DBG(print_ntp_control_message(&req)); | 
| 251 | /* discard obviously invalid packets */ | 261 | /* discard obviously invalid packets */ | 
| 252 | if (ntohs(req.count) > MAX_CM_SIZE) | 262 | if (ntohs(req.count) > MAX_CM_SIZE) | 
| 253 | die(STATE_CRITICAL, "NTP CRITICAL: Invalid packet received from NTP server\n"); | 263 | die(STATE_CRITICAL, "NTP CRITICAL: Invalid packet received from NTP server\n"); | 
| 254 | } while (!(req.op&OP_READSTAT && ntohs(req.seq) == 1)); | 264 | } while (!(req.op & OP_READSTAT && ntohs(req.seq) == 1)); | 
| 255 | 265 | ||
| 256 | if (LI(req.flags) == LI_ALARM) li_alarm = true; | 266 | if (LI(req.flags) == LI_ALARM) | 
| 267 | li_alarm = true; | ||
| 257 | /* Each peer identifier is 4 bytes in the data section, which | 268 | /* Each peer identifier is 4 bytes in the data section, which | 
| 258 | * we represent as a ntp_assoc_status_pair datatype. | 269 | * we represent as a ntp_assoc_status_pair datatype. | 
| 259 | */ | 270 | */ | 
| 260 | peers_size+=ntohs(req.count); | 271 | peers_size += ntohs(req.count); | 
| 261 | if((tmp=realloc(peers, peers_size)) == NULL) | 272 | if ((tmp = realloc(peers, peers_size)) == NULL) | 
| 262 | free(peers), die(STATE_UNKNOWN, "can not (re)allocate 'peers' buffer\n"); | 273 | free(peers), die(STATE_UNKNOWN, "can not (re)allocate 'peers' buffer\n"); | 
| 263 | peers=tmp; | 274 | peers = tmp; | 
| 264 | memcpy((void*)((ptrdiff_t)peers+peer_offset), (void*)req.data, ntohs(req.count)); | 275 | memcpy((void *)((ptrdiff_t)peers + peer_offset), (void *)req.data, ntohs(req.count)); | 
| 265 | npeers=peers_size/sizeof(ntp_assoc_status_pair); | 276 | npeers = peers_size / sizeof(ntp_assoc_status_pair); | 
| 266 | peer_offset+=ntohs(req.count); | 277 | peer_offset += ntohs(req.count); | 
| 267 | } while(req.op&REM_MORE); | 278 | } while (req.op & REM_MORE); | 
| 268 | 279 | ||
| 269 | /* first, let's find out if we have a sync source, or if there are | 280 | /* first, let's find out if we have a sync source, or if there are | 
| 270 | * at least some candidates. In the latter case we'll issue | 281 | * at least some candidates. In the latter case we'll issue | 
| 271 | * a warning but go ahead with the check on them. */ | 282 | * a warning but go ahead with the check on them. */ | 
| 272 | for (i = 0; i < npeers; i++){ | 283 | for (int i = 0; i < npeers; i++) { | 
| 273 | if(PEER_SEL(peers[i].status) >= PEER_TRUECHIMER){ | 284 | if (PEER_SEL(peers[i].status) >= PEER_TRUECHIMER) { | 
| 274 | (*num_truechimers)++; | 285 | (*num_truechimers)++; | 
| 275 | if(PEER_SEL(peers[i].status) >= PEER_INCLUDED){ | 286 | if (PEER_SEL(peers[i].status) >= PEER_INCLUDED) { | 
| 276 | num_candidates++; | 287 | num_candidates++; | 
| 277 | if(PEER_SEL(peers[i].status) >= PEER_SYNCSOURCE){ | 288 | if (PEER_SEL(peers[i].status) >= PEER_SYNCSOURCE) { | 
| 278 | syncsource_found = true; | 289 | syncsource_found = true; | 
| 279 | min_peer_sel=PEER_SYNCSOURCE; | 290 | min_peer_sel = PEER_SYNCSOURCE; | 
| 280 | } | 291 | } | 
| 281 | } | 292 | } | 
| 282 | } | 293 | } | 
| 283 | } | 294 | } | 
| 284 | if(verbose) printf("%d candidate peers available\n", num_candidates); | 295 | |
| 285 | if(verbose && syncsource_found) printf("synchronization source found\n"); | 296 | if (verbose) | 
| 286 | if(! syncsource_found){ | 297 | printf("%d candidate peers available\n", num_candidates); | 
| 298 | if (verbose && syncsource_found) | ||
| 299 | printf("synchronization source found\n"); | ||
| 300 | |||
| 301 | int status = STATE_OK; | ||
| 302 | if (!syncsource_found) { | ||
| 287 | status = STATE_WARNING; | 303 | status = STATE_WARNING; | 
| 288 | if(verbose) printf("warning: no synchronization source found\n"); | 304 | if (verbose) | 
| 305 | printf("warning: no synchronization source found\n"); | ||
| 289 | } | 306 | } | 
| 290 | if(li_alarm){ | 307 | if (li_alarm) { | 
| 291 | status = STATE_WARNING; | 308 | status = STATE_WARNING; | 
| 292 | if(verbose) printf("warning: LI_ALARM bit is set\n"); | 309 | if (verbose) | 
| 310 | printf("warning: LI_ALARM bit is set\n"); | ||
| 293 | } | 311 | } | 
| 294 | 312 | ||
| 295 | 313 | const char *getvar = "stratum,offset,jitter"; | |
| 296 | for (i = 0; i < npeers; i++){ | 314 | char *data; | 
| 315 | for (int i = 0; i < npeers; i++) { | ||
| 297 | /* Only query this server if it is the current sync source */ | 316 | /* Only query this server if it is the current sync source */ | 
| 298 | /* If there's no sync.peer, query all candidates and use the best one */ | 317 | /* If there's no sync.peer, query all candidates and use the best one */ | 
| 299 | if (PEER_SEL(peers[i].status) >= min_peer_sel){ | 318 | if (PEER_SEL(peers[i].status) >= min_peer_sel) { | 
| 300 | if(verbose) printf("Getting offset, jitter and stratum for peer %.2x\n", ntohs(peers[i].assoc)); | 319 | if (verbose) | 
| 320 | printf("Getting offset, jitter and stratum for peer %.2x\n", ntohs(peers[i].assoc)); | ||
| 301 | xasprintf(&data, ""); | 321 | xasprintf(&data, ""); | 
| 302 | do{ | 322 | do { | 
| 303 | setup_control_request(&req, OP_READVAR, 2); | 323 | setup_control_request(&req, OP_READVAR, 2); | 
| 304 | req.assoc = peers[i].assoc; | 324 | req.assoc = peers[i].assoc; | 
| 305 | /* Putting the wanted variable names in the request | 325 | /* Putting the wanted variable names in the request | 
| @@ -309,7 +329,7 @@ int ntp_request(double *offset, int *offset_result, double *jitter, int *stratum | |||
| 309 | */ | 329 | */ | 
| 310 | /* Older servers doesn't know what jitter is, so if we get an | 330 | /* Older servers doesn't know what jitter is, so if we get an | 
| 311 | * error on the first pass we redo it with "dispersion" */ | 331 | * error on the first pass we redo it with "dispersion" */ | 
| 312 | strncpy(req.data, getvar, MAX_CM_SIZE-1); | 332 | strncpy(req.data, getvar, MAX_CM_SIZE - 1); | 
| 313 | req.count = htons(strlen(getvar)); | 333 | req.count = htons(strlen(getvar)); | 
| 314 | DBG(printf("sending READVAR request...\n")); | 334 | DBG(printf("sending READVAR request...\n")); | 
| 315 | write(conn, &req, SIZEOF_NTPCM(req)); | 335 | write(conn, &req, SIZEOF_NTPCM(req)); | 
| @@ -320,44 +340,53 @@ int ntp_request(double *offset, int *offset_result, double *jitter, int *stratum | |||
| 320 | DBG(printf("receiving READVAR response...\n")); | 340 | DBG(printf("receiving READVAR response...\n")); | 
| 321 | read(conn, &req, SIZEOF_NTPCM(req)); | 341 | read(conn, &req, SIZEOF_NTPCM(req)); | 
| 322 | DBG(print_ntp_control_message(&req)); | 342 | DBG(print_ntp_control_message(&req)); | 
| 323 | } while (!(req.op&OP_READVAR && ntohs(req.seq) == 2)); | 343 | } while (!(req.op & OP_READVAR && ntohs(req.seq) == 2)); | 
| 324 | 344 | ||
| 325 | if(!(req.op&REM_ERROR)) | 345 | if (!(req.op & REM_ERROR)) | 
| 326 | xasprintf(&data, "%s%s", data, req.data); | 346 | xasprintf(&data, "%s%s", data, req.data); | 
| 327 | } while(req.op&REM_MORE); | 347 | } while (req.op & REM_MORE); | 
| 328 | 348 | ||
| 329 | if(req.op&REM_ERROR) { | 349 | if (req.op & REM_ERROR) { | 
| 330 | if(strstr(getvar, "jitter")) { | 350 | if (strstr(getvar, "jitter")) { | 
| 331 | if(verbose) printf("The command failed. This is usually caused by servers refusing the 'jitter'\nvariable. Restarting with 'dispersion'...\n"); | 351 | if (verbose) | 
| 352 | printf("The command failed. This is usually caused by servers refusing the 'jitter'\nvariable. Restarting with " | ||
| 353 | "'dispersion'...\n"); | ||
| 332 | getvar = "stratum,offset,dispersion"; | 354 | getvar = "stratum,offset,dispersion"; | 
| 333 | i--; | 355 | i--; | 
| 334 | continue; | 356 | continue; | 
| 335 | } else if(strlen(getvar)) { | 357 | } | 
| 336 | if(verbose) printf("Server didn't like dispersion either; will retrieve everything\n"); | 358 | if (strlen(getvar)) { | 
| 359 | if (verbose) | ||
| 360 | printf("Server didn't like dispersion either; will retrieve everything\n"); | ||
| 337 | getvar = ""; | 361 | getvar = ""; | 
| 338 | i--; | 362 | i--; | 
| 339 | continue; | 363 | continue; | 
| 340 | } | 364 | } | 
| 341 | } | 365 | } | 
| 342 | 366 | ||
| 343 | if(verbose > 1) | 367 | if (verbose > 1) | 
| 344 | printf("Server responded: >>>%s<<<\n", data); | 368 | printf("Server responded: >>>%s<<<\n", data); | 
| 345 | 369 | ||
| 370 | double tmp_offset = 0; | ||
| 371 | char *value; | ||
| 372 | char *nptr; | ||
| 346 | /* get the offset */ | 373 | /* get the offset */ | 
| 347 | if(verbose) | 374 | if (verbose) | 
| 348 | printf("parsing offset from peer %.2x: ", ntohs(peers[i].assoc)); | 375 | printf("parsing offset from peer %.2x: ", ntohs(peers[i].assoc)); | 
| 349 | 376 | ||
| 350 | value = np_extract_ntpvar(data, "offset"); | 377 | value = np_extract_ntpvar(data, "offset"); | 
| 351 | nptr=NULL; | 378 | nptr = NULL; | 
| 352 | /* Convert the value if we have one */ | 379 | /* Convert the value if we have one */ | 
| 353 | if(value != NULL) | 380 | if (value != NULL) | 
| 354 | tmp_offset = strtod(value, &nptr) / 1000; | 381 | tmp_offset = strtod(value, &nptr) / 1000; | 
| 355 | /* If value is null or no conversion was performed */ | 382 | /* If value is null or no conversion was performed */ | 
| 356 | if(value == NULL || value==nptr) { | 383 | if (value == NULL || value == nptr) { | 
| 357 | if(verbose) printf("error: unable to read server offset response.\n"); | 384 | if (verbose) | 
| 385 | printf("error: unable to read server offset response.\n"); | ||
| 358 | } else { | 386 | } else { | 
| 359 | if(verbose) printf("%.10g\n", tmp_offset); | 387 | if (verbose) | 
| 360 | if(*offset_result == STATE_UNKNOWN || fabs(tmp_offset) < fabs(*offset)) { | 388 | printf("%.10g\n", tmp_offset); | 
| 389 | if (*offset_result == STATE_UNKNOWN || fabs(tmp_offset) < fabs(*offset)) { | ||
| 361 | *offset = tmp_offset; | 390 | *offset = tmp_offset; | 
| 362 | *offset_result = STATE_OK; | 391 | *offset_result = STATE_OK; | 
| 363 | } else { | 392 | } else { | 
| @@ -366,85 +395,75 @@ int ntp_request(double *offset, int *offset_result, double *jitter, int *stratum | |||
| 366 | } | 395 | } | 
| 367 | } | 396 | } | 
| 368 | 397 | ||
| 369 | if(do_jitter) { | 398 | if (do_jitter) { | 
| 370 | /* get the jitter */ | 399 | /* get the jitter */ | 
| 371 | if(verbose) { | 400 | if (verbose) { | 
| 372 | printf("parsing %s from peer %.2x: ", strstr(getvar, "dispersion") != NULL ? "dispersion" : "jitter", ntohs(peers[i].assoc)); | 401 | printf("parsing %s from peer %.2x: ", strstr(getvar, "dispersion") != NULL ? "dispersion" : "jitter", | 
| 402 | ntohs(peers[i].assoc)); | ||
| 373 | } | 403 | } | 
| 374 | value = np_extract_ntpvar(data, strstr(getvar, "dispersion") != NULL ? "dispersion" : "jitter"); | 404 | value = np_extract_ntpvar(data, strstr(getvar, "dispersion") != NULL ? "dispersion" : "jitter"); | 
| 375 | nptr=NULL; | 405 | nptr = NULL; | 
| 376 | /* Convert the value if we have one */ | 406 | /* Convert the value if we have one */ | 
| 377 | if(value != NULL) | 407 | if (value != NULL) | 
| 378 | *jitter = strtod(value, &nptr); | 408 | *jitter = strtod(value, &nptr); | 
| 379 | /* If value is null or no conversion was performed */ | 409 | /* If value is null or no conversion was performed */ | 
| 380 | if(value == NULL || value==nptr) { | 410 | if (value == NULL || value == nptr) { | 
| 381 | if(verbose) printf("error: unable to read server jitter/dispersion response.\n"); | 411 | if (verbose) | 
| 412 | printf("error: unable to read server jitter/dispersion response.\n"); | ||
| 382 | *jitter = -1; | 413 | *jitter = -1; | 
| 383 | } else if(verbose) { | 414 | } else if (verbose) { | 
| 384 | printf("%.10g\n", *jitter); | 415 | printf("%.10g\n", *jitter); | 
| 385 | } | 416 | } | 
| 386 | } | 417 | } | 
| 387 | 418 | ||
| 388 | if(do_stratum) { | 419 | if (do_stratum) { | 
| 389 | /* get the stratum */ | 420 | /* get the stratum */ | 
| 390 | if(verbose) { | 421 | if (verbose) { | 
| 391 | printf("parsing stratum from peer %.2x: ", ntohs(peers[i].assoc)); | 422 | printf("parsing stratum from peer %.2x: ", ntohs(peers[i].assoc)); | 
| 392 | } | 423 | } | 
| 393 | value = np_extract_ntpvar(data, "stratum"); | 424 | value = np_extract_ntpvar(data, "stratum"); | 
| 394 | nptr=NULL; | 425 | nptr = NULL; | 
| 395 | /* Convert the value if we have one */ | 426 | /* Convert the value if we have one */ | 
| 396 | if(value != NULL) | 427 | if (value != NULL) | 
| 397 | *stratum = strtol(value, &nptr, 10); | 428 | *stratum = strtol(value, &nptr, 10); | 
| 398 | if(value == NULL || value==nptr) { | 429 | if (value == NULL || value == nptr) { | 
| 399 | if(verbose) printf("error: unable to read server stratum response.\n"); | 430 | if (verbose) | 
| 431 | printf("error: unable to read server stratum response.\n"); | ||
| 400 | *stratum = -1; | 432 | *stratum = -1; | 
| 401 | } else { | 433 | } else { | 
| 402 | if(verbose) printf("%i\n", *stratum); | 434 | if (verbose) | 
| 435 | printf("%i\n", *stratum); | ||
| 403 | } | 436 | } | 
| 404 | } | 437 | } | 
| 405 | } /* if (PEER_SEL(peers[i].status) >= min_peer_sel) */ | 438 | } /* if (PEER_SEL(peers[i].status) >= min_peer_sel) */ | 
| 406 | } /* for (i = 0; i < npeers; i++) */ | 439 | } /* for (i = 0; i < npeers; i++) */ | 
| 407 | 440 | ||
| 408 | close(conn); | 441 | close(conn); | 
| 409 | if(peers!=NULL) free(peers); | 442 | if (peers != NULL) | 
| 443 | free(peers); | ||
| 410 | 444 | ||
| 411 | return status; | 445 | return status; | 
| 412 | } | 446 | } | 
| 413 | 447 | ||
| 414 | int process_arguments(int argc, char **argv){ | 448 | int process_arguments(int argc, char **argv) { | 
| 415 | int c; | ||
| 416 | int option=0; | ||
| 417 | static struct option longopts[] = { | 449 | static struct option longopts[] = { | 
| 418 | {"version", no_argument, 0, 'V'}, | 450 | {"version", no_argument, 0, 'V'}, {"help", no_argument, 0, 'h'}, {"verbose", no_argument, 0, 'v'}, | 
| 419 | {"help", no_argument, 0, 'h'}, | 451 | {"use-ipv4", no_argument, 0, '4'}, {"use-ipv6", no_argument, 0, '6'}, {"quiet", no_argument, 0, 'q'}, | 
| 420 | {"verbose", no_argument, 0, 'v'}, | 452 | {"warning", required_argument, 0, 'w'}, {"critical", required_argument, 0, 'c'}, {"swarn", required_argument, 0, 'W'}, | 
| 421 | {"use-ipv4", no_argument, 0, '4'}, | 453 | {"scrit", required_argument, 0, 'C'}, {"jwarn", required_argument, 0, 'j'}, {"jcrit", required_argument, 0, 'k'}, | 
| 422 | {"use-ipv6", no_argument, 0, '6'}, | 454 | {"twarn", required_argument, 0, 'm'}, {"tcrit", required_argument, 0, 'n'}, {"timeout", required_argument, 0, 't'}, | 
| 423 | {"quiet", no_argument, 0, 'q'}, | 455 | {"hostname", required_argument, 0, 'H'}, {"port", required_argument, 0, 'p'}, {0, 0, 0, 0}}; | 
| 424 | {"warning", required_argument, 0, 'w'}, | ||
| 425 | {"critical", required_argument, 0, 'c'}, | ||
| 426 | {"swarn", required_argument, 0, 'W'}, | ||
| 427 | {"scrit", required_argument, 0, 'C'}, | ||
| 428 | {"jwarn", required_argument, 0, 'j'}, | ||
| 429 | {"jcrit", required_argument, 0, 'k'}, | ||
| 430 | {"twarn", required_argument, 0, 'm'}, | ||
| 431 | {"tcrit", required_argument, 0, 'n'}, | ||
| 432 | {"timeout", required_argument, 0, 't'}, | ||
| 433 | {"hostname", required_argument, 0, 'H'}, | ||
| 434 | {"port", required_argument, 0, 'p'}, | ||
| 435 | {0, 0, 0, 0} | ||
| 436 | }; | ||
| 437 | |||
| 438 | 456 | ||
| 439 | if (argc < 2) | 457 | if (argc < 2) | 
| 440 | usage ("\n"); | 458 | usage("\n"); | 
| 441 | 459 | ||
| 442 | while (true) { | 460 | while (true) { | 
| 443 | c = getopt_long (argc, argv, "Vhv46qw:c:W:C:j:k:m:n:t:H:p:", longopts, &option); | 461 | int option = 0; | 
| 444 | if (c == -1 || c == EOF || c == 1) | 462 | 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) | ||
| 445 | break; | 464 | break; | 
| 446 | 465 | ||
| 447 | switch (c) { | 466 | switch (option_char) { | 
| 448 | case 'h': | 467 | case 'h': | 
| 449 | print_help(); | 468 | print_help(); | 
| 450 | exit(STATE_UNKNOWN); | 469 | exit(STATE_UNKNOWN); | 
| @@ -490,15 +509,15 @@ int process_arguments(int argc, char **argv){ | |||
| 490 | tcrit = optarg; | 509 | tcrit = optarg; | 
| 491 | break; | 510 | break; | 
| 492 | case 'H': | 511 | case 'H': | 
| 493 | if(!is_host(optarg)) | 512 | if (!is_host(optarg)) | 
| 494 | usage2(_("Invalid hostname/address"), optarg); | 513 | usage2(_("Invalid hostname/address"), optarg); | 
| 495 | server_address = strdup(optarg); | 514 | server_address = strdup(optarg); | 
| 496 | break; | 515 | break; | 
| 497 | case 'p': | 516 | case 'p': | 
| 498 | port=atoi(optarg); | 517 | port = atoi(optarg); | 
| 499 | break; | 518 | break; | 
| 500 | case 't': | 519 | case 't': | 
| 501 | socket_timeout=atoi(optarg); | 520 | socket_timeout = atoi(optarg); | 
| 502 | break; | 521 | break; | 
| 503 | case '4': | 522 | case '4': | 
| 504 | address_family = AF_INET; | 523 | address_family = AF_INET; | 
| @@ -507,69 +526,53 @@ int process_arguments(int argc, char **argv){ | |||
| 507 | #ifdef USE_IPV6 | 526 | #ifdef USE_IPV6 | 
| 508 | address_family = AF_INET6; | 527 | address_family = AF_INET6; | 
| 509 | #else | 528 | #else | 
| 510 | usage4 (_("IPv6 support not available")); | 529 | usage4(_("IPv6 support not available")); | 
| 511 | #endif | 530 | #endif | 
| 512 | break; | 531 | break; | 
| 513 | case '?': | 532 | case '?': | 
| 514 | /* print short usage statement if args not parsable */ | 533 | /* print short usage statement if args not parsable */ | 
| 515 | usage5 (); | 534 | usage5(); | 
| 516 | break; | 535 | break; | 
| 517 | } | 536 | } | 
| 518 | } | 537 | } | 
| 519 | 538 | ||
| 520 | if(server_address == NULL){ | 539 | if (server_address == NULL) { | 
| 521 | usage4(_("Hostname was not supplied")); | 540 | usage4(_("Hostname was not supplied")); | 
| 522 | } | 541 | } | 
| 523 | 542 | ||
| 524 | return 0; | 543 | return 0; | 
| 525 | } | 544 | } | 
| 526 | 545 | ||
| 527 | char *perfd_offset (double offset) | 546 | char *perfd_offset(double offset) { | 
| 528 | { | 547 | return fperfdata("offset", offset, "s", true, offset_thresholds->warning->end, true, offset_thresholds->critical->end, false, 0, false, | 
| 529 | return fperfdata ("offset", offset, "s", | 548 | 0); | 
| 530 | true, offset_thresholds->warning->end, | ||
| 531 | true, offset_thresholds->critical->end, | ||
| 532 | false, 0, false, 0); | ||
| 533 | } | 549 | } | 
| 534 | 550 | ||
| 535 | char *perfd_jitter (double jitter) | 551 | char *perfd_jitter(double jitter) { | 
| 536 | { | 552 | return fperfdata("jitter", jitter, "", do_jitter, jitter_thresholds->warning->end, do_jitter, jitter_thresholds->critical->end, true, 0, | 
| 537 | return fperfdata ("jitter", jitter, "", | 553 | false, 0); | 
| 538 | do_jitter, jitter_thresholds->warning->end, | ||
| 539 | do_jitter, jitter_thresholds->critical->end, | ||
| 540 | true, 0, false, 0); | ||
| 541 | } | 554 | } | 
| 542 | 555 | ||
| 543 | char *perfd_stratum (int stratum) | 556 | char *perfd_stratum(int stratum) { | 
| 544 | { | 557 | return perfdata("stratum", stratum, "", do_stratum, (int)stratum_thresholds->warning->end, do_stratum, | 
| 545 | return perfdata ("stratum", stratum, "", | 558 | (int)stratum_thresholds->critical->end, true, 0, true, 16); | 
| 546 | do_stratum, (int)stratum_thresholds->warning->end, | ||
| 547 | do_stratum, (int)stratum_thresholds->critical->end, | ||
| 548 | true, 0, true, 16); | ||
| 549 | } | 559 | } | 
| 550 | 560 | ||
| 551 | char *perfd_truechimers (int num_truechimers) | 561 | char *perfd_truechimers(int num_truechimers) { | 
| 552 | { | 562 | return perfdata("truechimers", num_truechimers, "", do_truechimers, (int)truechimer_thresholds->warning->end, do_truechimers, | 
| 553 | return perfdata ("truechimers", num_truechimers, "", | 563 | (int)truechimer_thresholds->critical->end, true, 0, false, 0); | 
| 554 | do_truechimers, (int)truechimer_thresholds->warning->end, | ||
| 555 | do_truechimers, (int)truechimer_thresholds->critical->end, | ||
| 556 | true, 0, false, 0); | ||
| 557 | } | 564 | } | 
| 558 | 565 | ||
| 559 | int main(int argc, char *argv[]){ | 566 | int main(int argc, char *argv[]) { | 
| 560 | int result, offset_result, stratum, num_truechimers; | 567 | setlocale(LC_ALL, ""); | 
| 561 | double offset=0, jitter=0; | 568 | bindtextdomain(PACKAGE, LOCALEDIR); | 
| 562 | char *result_line, *perfdata_line; | 569 | textdomain(PACKAGE); | 
| 563 | |||
| 564 | setlocale (LC_ALL, ""); | ||
| 565 | bindtextdomain (PACKAGE, LOCALEDIR); | ||
| 566 | textdomain (PACKAGE); | ||
| 567 | 570 | ||
| 568 | /* Parse extra opts if any */ | 571 | /* Parse extra opts if any */ | 
| 569 | argv=np_extra_opts (&argc, argv, progname); | 572 | argv = np_extra_opts(&argc, argv, progname); | 
| 570 | 573 | ||
| 571 | if (process_arguments (argc, argv) == ERROR) | 574 | if (process_arguments(argc, argv) == ERROR) | 
| 572 | usage4 (_("Could not parse arguments")); | 575 | usage4(_("Could not parse arguments")); | 
| 573 | 576 | ||
| 574 | set_thresholds(&offset_thresholds, owarn, ocrit); | 577 | set_thresholds(&offset_thresholds, owarn, ocrit); | 
| 575 | set_thresholds(&jitter_thresholds, jwarn, jcrit); | 578 | set_thresholds(&jitter_thresholds, jwarn, jcrit); | 
| @@ -577,15 +580,20 @@ int main(int argc, char *argv[]){ | |||
| 577 | set_thresholds(&truechimer_thresholds, twarn, tcrit); | 580 | set_thresholds(&truechimer_thresholds, twarn, tcrit); | 
| 578 | 581 | ||
| 579 | /* initialize alarm signal handling */ | 582 | /* initialize alarm signal handling */ | 
| 580 | signal (SIGALRM, socket_timeout_alarm_handler); | 583 | signal(SIGALRM, socket_timeout_alarm_handler); | 
| 581 | 584 | ||
| 582 | /* set socket timeout */ | 585 | /* set socket timeout */ | 
| 583 | alarm (socket_timeout); | 586 | alarm(socket_timeout); | 
| 584 | 587 | ||
| 588 | int offset_result; | ||
| 589 | int stratum; | ||
| 590 | int num_truechimers; | ||
| 591 | double offset = 0; | ||
| 592 | double jitter = 0; | ||
| 585 | /* This returns either OK or WARNING (See comment preceding ntp_request) */ | 593 | /* This returns either OK or WARNING (See comment preceding ntp_request) */ | 
| 586 | result = ntp_request(&offset, &offset_result, &jitter, &stratum, &num_truechimers); | 594 | int result = ntp_request(&offset, &offset_result, &jitter, &stratum, &num_truechimers); | 
| 587 | 595 | ||
| 588 | if(offset_result == STATE_UNKNOWN) { | 596 | if (offset_result == STATE_UNKNOWN) { | 
| 589 | /* if there's no sync peer (this overrides ntp_request output): */ | 597 | /* if there's no sync peer (this overrides ntp_request output): */ | 
| 590 | result = (quiet ? STATE_UNKNOWN : STATE_CRITICAL); | 598 | result = (quiet ? STATE_UNKNOWN : STATE_CRITICAL); | 
| 591 | } else { | 599 | } else { | 
| @@ -597,50 +605,49 @@ int main(int argc, char *argv[]){ | |||
| 597 | 605 | ||
| 598 | int oresult = result; | 606 | int oresult = result; | 
| 599 | 607 | ||
| 600 | |||
| 601 | int tresult = STATE_UNKNOWN; | 608 | int tresult = STATE_UNKNOWN; | 
| 602 | 609 | ||
| 603 | if(do_truechimers) { | 610 | if (do_truechimers) { | 
| 604 | tresult = get_status(num_truechimers, truechimer_thresholds); | 611 | tresult = get_status(num_truechimers, truechimer_thresholds); | 
| 605 | result = max_state_alt(result, tresult); | 612 | result = max_state_alt(result, tresult); | 
| 606 | } | 613 | } | 
| 607 | 614 | ||
| 608 | |||
| 609 | int sresult = STATE_UNKNOWN; | 615 | int sresult = STATE_UNKNOWN; | 
| 610 | 616 | ||
| 611 | if(do_stratum) { | 617 | if (do_stratum) { | 
| 612 | sresult = get_status(stratum, stratum_thresholds); | 618 | sresult = get_status(stratum, stratum_thresholds); | 
| 613 | result = max_state_alt(result, sresult); | 619 | result = max_state_alt(result, sresult); | 
| 614 | } | 620 | } | 
| 615 | 621 | ||
| 616 | |||
| 617 | int jresult = STATE_UNKNOWN; | 622 | int jresult = STATE_UNKNOWN; | 
| 618 | 623 | ||
| 619 | if(do_jitter) { | 624 | if (do_jitter) { | 
| 620 | jresult = get_status(jitter, jitter_thresholds); | 625 | jresult = get_status(jitter, jitter_thresholds); | 
| 621 | result = max_state_alt(result, jresult); | 626 | result = max_state_alt(result, jresult); | 
| 622 | } | 627 | } | 
| 623 | 628 | ||
| 629 | char *result_line; | ||
| 624 | switch (result) { | 630 | switch (result) { | 
| 625 | case STATE_CRITICAL : | 631 | case STATE_CRITICAL: | 
| 626 | xasprintf(&result_line, _("NTP CRITICAL:")); | 632 | xasprintf(&result_line, _("NTP CRITICAL:")); | 
| 627 | break; | 633 | break; | 
| 628 | case STATE_WARNING : | 634 | case STATE_WARNING: | 
| 629 | xasprintf(&result_line, _("NTP WARNING:")); | 635 | xasprintf(&result_line, _("NTP WARNING:")); | 
| 630 | break; | 636 | break; | 
| 631 | case STATE_OK : | 637 | case STATE_OK: | 
| 632 | xasprintf(&result_line, _("NTP OK:")); | 638 | xasprintf(&result_line, _("NTP OK:")); | 
| 633 | break; | 639 | break; | 
| 634 | default : | 640 | default: | 
| 635 | xasprintf(&result_line, _("NTP UNKNOWN:")); | 641 | xasprintf(&result_line, _("NTP UNKNOWN:")); | 
| 636 | break; | 642 | break; | 
| 637 | } | 643 | } | 
| 638 | if(!syncsource_found) | 644 | if (!syncsource_found) | 
| 639 | xasprintf(&result_line, "%s %s,", result_line, _("Server not synchronized")); | 645 | xasprintf(&result_line, "%s %s,", result_line, _("Server not synchronized")); | 
| 640 | else if(li_alarm) | 646 | else if (li_alarm) | 
| 641 | xasprintf(&result_line, "%s %s,", result_line, _("Server has the LI_ALARM bit set")); | 647 | xasprintf(&result_line, "%s %s,", result_line, _("Server has the LI_ALARM bit set")); | 
| 642 | 648 | ||
| 643 | if(offset_result == STATE_UNKNOWN){ | 649 | char *perfdata_line; | 
| 650 | if (offset_result == STATE_UNKNOWN) { | ||
| 644 | xasprintf(&result_line, "%s %s", result_line, _("Offset unknown")); | 651 | xasprintf(&result_line, "%s %s", result_line, _("Offset unknown")); | 
| 645 | xasprintf(&perfdata_line, ""); | 652 | xasprintf(&perfdata_line, ""); | 
| 646 | } else if (oresult == STATE_WARNING) { | 653 | } else if (oresult == STATE_WARNING) { | 
| @@ -649,9 +656,9 @@ int main(int argc, char *argv[]){ | |||
| 649 | xasprintf(&result_line, "%s %s %.10g secs (CRITICAL)", result_line, _("Offset"), offset); | 656 | xasprintf(&result_line, "%s %s %.10g secs (CRITICAL)", result_line, _("Offset"), offset); | 
| 650 | } else { | 657 | } else { | 
| 651 | xasprintf(&result_line, "%s %s %.10g secs", result_line, _("Offset"), offset); | 658 | xasprintf(&result_line, "%s %s %.10g secs", result_line, _("Offset"), offset); | 
| 652 | } | 659 | } | 
| 653 | xasprintf(&perfdata_line, "%s", perfd_offset(offset)); | 660 | xasprintf(&perfdata_line, "%s", perfd_offset(offset)); | 
| 654 | 661 | ||
| 655 | if (do_jitter) { | 662 | if (do_jitter) { | 
| 656 | if (jresult == STATE_WARNING) { | 663 | if (jresult == STATE_WARNING) { | 
| 657 | xasprintf(&result_line, "%s, jitter=%f (WARNING)", result_line, jitter); | 664 | xasprintf(&result_line, "%s, jitter=%f (WARNING)", result_line, jitter); | 
| @@ -684,45 +691,46 @@ int main(int argc, char *argv[]){ | |||
| 684 | } | 691 | } | 
| 685 | printf("%s|%s\n", result_line, perfdata_line); | 692 | printf("%s|%s\n", result_line, perfdata_line); | 
| 686 | 693 | ||
| 687 | if(server_address!=NULL) free(server_address); | 694 | if (server_address != NULL) | 
| 695 | free(server_address); | ||
| 688 | return result; | 696 | return result; | 
| 689 | } | 697 | } | 
| 690 | 698 | ||
| 691 | void print_help(void){ | 699 | void print_help(void) { | 
| 692 | print_revision(progname, NP_VERSION); | 700 | print_revision(progname, NP_VERSION); | 
| 693 | 701 | ||
| 694 | printf ("Copyright (c) 2006 Sean Finney\n"); | 702 | printf("Copyright (c) 2006 Sean Finney\n"); | 
| 695 | printf (COPYRIGHT, copyright, email); | 703 | printf(COPYRIGHT, copyright, email); | 
| 696 | 704 | ||
| 697 | printf ("%s\n", _("This plugin checks the selected ntp server")); | 705 | printf("%s\n", _("This plugin checks the selected ntp server")); | 
| 698 | 706 | ||
| 699 | printf ("\n\n"); | 707 | printf("\n\n"); | 
| 700 | 708 | ||
| 701 | print_usage(); | 709 | print_usage(); | 
| 702 | printf (UT_HELP_VRSN); | 710 | printf(UT_HELP_VRSN); | 
| 703 | printf (UT_EXTRA_OPTS); | 711 | printf(UT_EXTRA_OPTS); | 
| 704 | printf (UT_IPv46); | 712 | printf(UT_IPv46); | 
| 705 | printf (UT_HOST_PORT, 'p', "123"); | 713 | printf(UT_HOST_PORT, 'p', "123"); | 
| 706 | printf (" %s\n", "-q, --quiet"); | 714 | printf(" %s\n", "-q, --quiet"); | 
| 707 | printf (" %s\n", _("Returns UNKNOWN instead of CRITICAL or WARNING if server isn't synchronized")); | 715 | printf(" %s\n", _("Returns UNKNOWN instead of CRITICAL or WARNING if server isn't synchronized")); | 
| 708 | printf (" %s\n", "-w, --warning=THRESHOLD"); | 716 | printf(" %s\n", "-w, --warning=THRESHOLD"); | 
| 709 | printf (" %s\n", _("Offset to result in warning status (seconds)")); | 717 | printf(" %s\n", _("Offset to result in warning status (seconds)")); | 
| 710 | printf (" %s\n", "-c, --critical=THRESHOLD"); | 718 | printf(" %s\n", "-c, --critical=THRESHOLD"); | 
| 711 | printf (" %s\n", _("Offset to result in critical status (seconds)")); | 719 | printf(" %s\n", _("Offset to result in critical status (seconds)")); | 
| 712 | printf (" %s\n", "-W, --swarn=THRESHOLD"); | 720 | printf(" %s\n", "-W, --swarn=THRESHOLD"); | 
| 713 | printf (" %s\n", _("Warning threshold for stratum of server's synchronization peer")); | 721 | printf(" %s\n", _("Warning threshold for stratum of server's synchronization peer")); | 
| 714 | printf (" %s\n", "-C, --scrit=THRESHOLD"); | 722 | printf(" %s\n", "-C, --scrit=THRESHOLD"); | 
| 715 | printf (" %s\n", _("Critical threshold for stratum of server's synchronization peer")); | 723 | printf(" %s\n", _("Critical threshold for stratum of server's synchronization peer")); | 
| 716 | printf (" %s\n", "-j, --jwarn=THRESHOLD"); | 724 | printf(" %s\n", "-j, --jwarn=THRESHOLD"); | 
| 717 | printf (" %s\n", _("Warning threshold for jitter")); | 725 | printf(" %s\n", _("Warning threshold for jitter")); | 
| 718 | printf (" %s\n", "-k, --jcrit=THRESHOLD"); | 726 | printf(" %s\n", "-k, --jcrit=THRESHOLD"); | 
| 719 | printf (" %s\n", _("Critical threshold for jitter")); | 727 | printf(" %s\n", _("Critical threshold for jitter")); | 
| 720 | printf (" %s\n", "-m, --twarn=THRESHOLD"); | 728 | printf(" %s\n", "-m, --twarn=THRESHOLD"); | 
| 721 | printf (" %s\n", _("Warning threshold for number of usable time sources (\"truechimers\")")); | 729 | printf(" %s\n", _("Warning threshold for number of usable time sources (\"truechimers\")")); | 
| 722 | printf (" %s\n", "-n, --tcrit=THRESHOLD"); | 730 | printf(" %s\n", "-n, --tcrit=THRESHOLD"); | 
| 723 | printf (" %s\n", _("Critical threshold for number of usable time sources (\"truechimers\")")); | 731 | printf(" %s\n", _("Critical threshold for number of usable time sources (\"truechimers\")")); | 
| 724 | printf (UT_CONN_TIMEOUT, DEFAULT_SOCKET_TIMEOUT); | 732 | printf(UT_CONN_TIMEOUT, DEFAULT_SOCKET_TIMEOUT); | 
| 725 | printf (UT_VERBOSE); | 733 | printf(UT_VERBOSE); | 
| 726 | 734 | ||
| 727 | printf("\n"); | 735 | printf("\n"); | 
| 728 | printf("%s\n", _("This plugin checks an NTP server independent of any commandline")); | 736 | printf("%s\n", _("This plugin checks an NTP server independent of any commandline")); | 
| @@ -751,13 +759,11 @@ void print_help(void){ | |||
| 751 | printf(" %s\n", _("Check only stratum:")); | 759 | printf(" %s\n", _("Check only stratum:")); | 
| 752 | printf(" %s\n", ("./check_ntp_peer -H ntpserv -W 4 -C 6")); | 760 | printf(" %s\n", ("./check_ntp_peer -H ntpserv -W 4 -C 6")); | 
| 753 | 761 | ||
| 754 | printf (UT_SUPPORT); | 762 | printf(UT_SUPPORT); | 
| 755 | } | 763 | } | 
| 756 | 764 | ||
| 757 | void | 765 | void print_usage(void) { | 
| 758 | print_usage(void) | 766 | printf("%s\n", _("Usage:")); | 
| 759 | { | ||
| 760 | printf ("%s\n", _("Usage:")); | ||
| 761 | printf(" %s -H <host> [-4|-6] [-w <warn>] [-c <crit>] [-W <warn>] [-C <crit>]\n", progname); | 767 | printf(" %s -H <host> [-4|-6] [-w <warn>] [-c <crit>] [-W <warn>] [-C <crit>]\n", progname); | 
| 762 | printf(" [-j <warn>] [-k <crit>] [-v verbose]\n"); | 768 | printf(" [-j <warn>] [-k <crit>] [-v verbose]\n"); | 
| 763 | } | 769 | } | 
