diff options
| author | osmiy <osmiy@97f52cf1-0a1b-0410-bd0e-c28be96e8082> | 2006-07-13 12:00:27 +0000 |
|---|---|---|
| committer | osmiy <osmiy@97f52cf1-0a1b-0410-bd0e-c28be96e8082> | 2006-07-13 12:00:27 +0000 |
| commit | bae7d0f66be396db7f5ff3eefc3dc21c34e131c9 (patch) | |
| tree | 9a8bb8af899b1b91602cb39461b1df278ef3aeda /src | |
| parent | 697332fe6532254a7c9125aefdfd3a68dda2a6f1 (diff) | |
| download | zabbix-bae7d0f66be396db7f5ff3eefc3dc21c34e131c9.tar.gz zabbix-bae7d0f66be396db7f5ff3eefc3dc21c34e131c9.tar.xz zabbix-bae7d0f66be396db7f5ff3eefc3dc21c34e131c9.zip | |
fixed NTP protocol monitoring
git-svn-id: svn://svn.zabbix.com/trunk@3043 97f52cf1-0a1b-0410-bd0e-c28be96e8082
Diffstat (limited to 'src')
| -rw-r--r-- | src/libs/zbxlog/log.c | 2 | ||||
| -rw-r--r-- | src/libs/zbxsysinfo/common/common.c | 2 | ||||
| -rw-r--r-- | src/libs/zbxsysinfo/common/ntp.c | 291 | ||||
| -rw-r--r-- | src/zabbix_agent/active.c | 51 | ||||
| -rw-r--r-- | src/zabbix_agent/zabbix_agentd.c | 13 |
5 files changed, 203 insertions, 156 deletions
diff --git a/src/libs/zbxlog/log.c b/src/libs/zbxlog/log.c index 18a2e910..1cbf1de3 100644 --- a/src/libs/zbxlog/log.c +++ b/src/libs/zbxlog/log.c @@ -27,7 +27,7 @@ static char log_filename[MAX_STRING_LEN]; static int log_type = LOG_TYPE_UNDEFINED; -static int log_level = LOG_LEVEL_ERR; +static int log_level = LOG_LEVEL_DEBUG; static ZBX_MUTEX log_file_access; diff --git a/src/libs/zbxsysinfo/common/common.c b/src/libs/zbxsysinfo/common/common.c index a9b61b1e..d54179f3 100644 --- a/src/libs/zbxsysinfo/common/common.c +++ b/src/libs/zbxsysinfo/common/common.c @@ -1859,7 +1859,7 @@ int CHECK_SERVICE_PERF(const char *cmd, const char *param, unsigned flags, AGENT /* check_service[ssh,127.0.0.1,ssh] */ int CHECK_SERVICE(const char *cmd, const char *param, unsigned flags, AGENT_RESULT *result) { - short port=0; + unsigned short port=0; char service[MAX_STRING_LEN]; char ip[MAX_STRING_LEN]; char str_port[MAX_STRING_LEN]; diff --git a/src/libs/zbxsysinfo/common/ntp.c b/src/libs/zbxsysinfo/common/ntp.c index 187bec0c..63990347 100644 --- a/src/libs/zbxsysinfo/common/ntp.c +++ b/src/libs/zbxsysinfo/common/ntp.c @@ -20,6 +20,8 @@ #include "common.h" #include "sysinfo.h" #include "zbxsock.h" +#include "log.h" +#include "cfg.h" #define JAN_1970 2208988800.0 /* 1970 - 1900 in seconds */ #define NTP_SCALE 4294967296.0 /* 2^32, of course! */ @@ -50,54 +52,68 @@ #define RESET_MIN 15 /* Minimum period between resets */ #define ABSCISSA 3.0 /* Scale factor for standard errors */ -typedef struct NTP_DATA { - unsigned char status, version, mode, stratum, polling, precision; - double dispersion, reference, originate, receive, transmit, current; +typedef struct ntp_data_s { + + unsigned char + status, + version, + mode, + stratum, + polling, + precision; + double + dispersion, + reference, + originate, + receive, + transmit, + current; + } ntp_data; -double current_time (double offset) +static double current_time (double offset) { -#if !defined(WIN32) || (defined(WIN32) && defined(TODO)) - /* Get the current UTC time in seconds since the Epoch plus an offset (usually the time from the beginning of the century to the Epoch!) */ - struct timeval current; +#if defined(WIN32) - errno = 0; -#ifdef WIN32 -# error "Replace <gettimeofday> function" -#endif /* WIN32 */ - - if (gettimeofday(¤t,NULL)) - { - /* No processing of error condition here */ - } - return offset+current.tv_sec+1.0e-6*current.tv_usec;\ + struct _timeb current; -#else + _ftime(¤t); + + return (offset + ((double)current.time) + 1.0e-6 * ((double)current.millitm)); + +#else /* not WIN32 */ - return 0.; + struct timeval current; -#endif /* TODO */ + gettimeofday(¤t,NULL); + + return (offset + ((double)current.tv_sec) + 1.0e-6 * ((double)current.tv_usec)); + +#endif /* WIN32 */ + + return 0.; } -void make_packet (ntp_data *data) +static void make_packet (ntp_data *data) { - data->status = NTP_LI_FUDGE<<6; - data->stratum = NTP_STRATUM; + data->status = NTP_LI_FUDGE<<6; + data->stratum = NTP_STRATUM; data->reference = data->dispersion = 0.0; - data->version = NTP_VERSION; - data->mode = 1; - data->polling = NTP_POLLING; - data->precision = NTP_PRECISION; - data->receive = data->originate = 0.0; - data->current = data->transmit = current_time(JAN_1970); + data->version = NTP_VERSION; + data->mode = 1; + data->polling = NTP_POLLING; + data->precision = NTP_PRECISION; + data->receive = data->originate = 0.0; + data->current = data->transmit = current_time(JAN_1970); } -void pack_ntp (unsigned char *packet, int length, ntp_data *data) { +static void pack_ntp (unsigned char *packet, int length, ntp_data *data) +{ /* Pack the essential data into an NTP packet, bypassing struct layout and endian problems. Note that it ignores fields irrelevant to SNTP. */ @@ -105,32 +121,38 @@ endian problems. Note that it ignores fields irrelevant to SNTP. */ int i, k; double d; + assert(length >= (NTP_TRANSMIT + 8)); + memset(packet,0,(size_t)length); - packet[0] = (data->status<<6)|(data->version<<3)|data->mode; + + packet[0] = (data->status << 6) | (data->version << 3) | data->mode; packet[1] = data->stratum; packet[2] = data->polling; packet[3] = data->precision; - d = data->originate/NTP_SCALE; + + d = data->originate / NTP_SCALE; for (i = 0; i < 8; ++i) { if ((k = (int)(d *= 256.0)) >= 256) k = 255; - packet[NTP_ORIGINATE+i] = k; + packet[NTP_ORIGINATE + i] = k; d -= k; } - d = data->receive/NTP_SCALE; + + d = data->receive / NTP_SCALE; for (i = 0; i < 8; ++i) { if ((k = (int)(d *= 256.0)) >= 256) k = 255; - packet[NTP_RECEIVE+i] = k; + packet[NTP_RECEIVE + i] = k; d -= k; } - d = data->transmit/NTP_SCALE; + + d = data->transmit / NTP_SCALE; for (i = 0; i < 8; ++i) { if ((k = (int)(d *= 256.0)) >= 256) k = 255; - packet[NTP_TRANSMIT+i] = k; + packet[NTP_TRANSMIT + i] = k; d -= k; } } -void unpack_ntp (ntp_data *data, unsigned char *packet, int length) { +static void unpack_ntp (ntp_data *data, unsigned char *packet, int length) { /* Unpack the essential data from an NTP packet, bypassing struct layout and endian problems. Note that it ignores fields irrelevant to SNTP. */ @@ -138,43 +160,50 @@ endian problems. Note that it ignores fields irrelevant to SNTP. */ int i; double d; - data->current = current_time(JAN_1970); /* Best to come first */ - data->status = (packet[0] >> 6); - data->version = (packet[0] >> 3)&0x07; - data->mode = packet[0]&0x07; - data->stratum = packet[1]; - data->polling = packet[2]; - data->precision = packet[3]; + memset(data, 0, sizeof(ntp_data)); + + if(length == 0) + return; + + assert(length >= (NTP_TRANSMIT + 8)); + + data->current = current_time(JAN_1970); /* Best to come first */ + data->status = (packet[0] >> 6); + data->version = (packet[0] >> 3) & 0x07; + data->mode = packet[0] & 0x07; + data->stratum = packet[1]; + data->polling = packet[2]; + data->precision = packet[3]; + d = 0.0; - for (i = 0; i < 4; ++i) d = 256.0*d+packet[NTP_DISP_FIELD+i]; - data->dispersion = d/65536.0; + for (i = 0; i < 4; ++i) d = 256.0 * d + packet[NTP_DISP_FIELD + i]; + data->dispersion = d / 65536.0; d = 0.0; - for (i = 0; i < 8; ++i) d = 256.0*d+packet[NTP_REFERENCE+i]; - data->reference = d/NTP_SCALE; + for (i = 0; i < 8; ++i) d = 256.0 * d + packet[NTP_REFERENCE + i]; + data->reference = d / NTP_SCALE; d = 0.0; - for (i = 0; i < 8; ++i) d = 256.0*d+packet[NTP_ORIGINATE+i]; - data->originate = d/NTP_SCALE; + for (i = 0; i < 8; ++i) d = 256.0 * d + packet[NTP_ORIGINATE + i]; + data->originate = d / NTP_SCALE; d = 0.0; - for (i = 0; i < 8; ++i) d = 256.0*d+packet[NTP_RECEIVE+i]; - data->receive = d/NTP_SCALE; + for (i = 0; i < 8; ++i) d = 256.0 * d + packet[NTP_RECEIVE + i]; + data->receive = d / NTP_SCALE; d = 0.0; - for (i = 0; i < 8; ++i) d = 256.0*d+packet[NTP_TRANSMIT+i]; - data->transmit = d/NTP_SCALE; + for (i = 0; i < 8; ++i) d = 256.0 * d + packet[NTP_TRANSMIT + i]; + data->transmit = d / NTP_SCALE; } -/* -void display_data (ntp_data *data) { +static void display_data (ntp_data *data) { - printf("sta=%d ver=%d mod=%d str=%d pol=%d dis=%.6f ref=%.6f\n", + printf("sta = %d ver = %d mod = %d str = %d pol = %d dis = %.6f ref = %.6f\n", data->status,data->version,data->mode,data->stratum,data->polling, data->dispersion,data->reference); - printf("ori=%.6f rec=%.6f\n",data->originate,data->receive); - printf("tra=%.6f cur=%.6f\n",data->transmit,data->current); + printf("ori = %.6f rec = %.6f\n",data->originate, data->receive); + printf("tra = %.6f cur = %.6f\n",data->transmit, data->current); } -*/ +#if OFF -time_t convert_time (double value, int *millisecs) { +static time_t convert_time (double value, int *millisecs) { /* Convert the time to the ANSI C form. */ @@ -187,8 +216,15 @@ time_t convert_time (double value, int *millisecs) { return result; } -int format_time (char *text, int length, double offset, double error, - double drift, double drifterr) { +/* !!! damaged function !!! for using correct tham !!! */ +static int format_time ( + char *text, + int length, + double offset, + double error, /* not USED */ + double drift, /* not USED */ + double drifterr /* not USED */ + ) { /* Format the current time into a string, with the extra information as requested. Note that the rest of the program uses the correction needed, which @@ -196,10 +232,15 @@ is what is printed for diagnostics, but this formats the error in the local system for display to users. So the results from this are the negation of those printed by the verbose options. */ - int milli, len; - time_t now; - struct tm *gmt; - static const char *months[] = { + int + milli, + len; + time_t + now; + struct tm + *gmt; + static const char + *months[] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" }; @@ -210,15 +251,16 @@ systems do not set the return value from (s)printf. */ now = convert_time(current_time(offset),&milli); errno = 0; + if ((gmt = localtime(&now)) == NULL) { - printf("unable to work out local time"); + zbx_error("unable to work out local time"); return -1; } len = 24; if (length <= len) { - printf("internal error calling format_time"); + zbx_error("internal error calling format_time"); return -1; } @@ -230,119 +272,111 @@ systems do not set the return value from (s)printf. */ return now; } -int check_ntp(char *host, int port, int *value_int) -{ +#endif /* OFF */ -#if !defined(WIN32) || (defined(WIN32) && defined(TODO)) +int check_ntp(char *host, unsigned short port, int *value_int) +{ ZBX_SOCKET s; ZBX_SOCKADDR servaddr_in; - int len; - unsigned char c[MAX_STRING_LEN]; + int len; + unsigned char buf[MAX_STRING_LEN]; struct hostent *hp; - char text[50]; - - ntp_data data; + ntp_data data; unsigned char packet[NTP_PACKET_MIN]; *value_int = 0; - make_packet(&data); - - servaddr_in.sin_family=AF_INET; - hp=gethostbyname(host); - - if(hp==NULL) + if(NULL == (hp = gethostbyname(host)) ) { -/* zbx_error("gethostbyname(%s) failed [%s]", host, hstrerror(h_errno));*/ +#ifdef HAVE_HSTRERROR + zabbix_log( LOG_LEVEL_DEBUG, "gethostbyname() failed for NTP server [%d]", (char*)hstrerror((int)h_errno)); +#else + zabbix_log( LOG_LEVEL_DEBUG, "gethostbyname() failed for NTP server [%s]", strerror_from_system(h_errno)); +#endif return SYSINFO_RET_OK; } - servaddr_in.sin_addr.s_addr=((struct in_addr *)(hp->h_addr))->s_addr; - - servaddr_in.sin_port=htons(port); + servaddr_in.sin_family = AF_INET; + servaddr_in.sin_addr.s_addr = ((struct in_addr *)(hp->h_addr))->s_addr; + servaddr_in.sin_port = htons(port); - s=socket(AF_INET,SOCK_DGRAM,0); - - if(s == -1) + if( SOCKET_ERROR == (s = socket(AF_INET,SOCK_DGRAM,0)) ) { -/* zbx_error("Cannot create socket [%s]", strerror(errno));*/ + zabbix_log(LOG_LEVEL_DEBUG, "Cannot create socket for NTP server. [%s]", strerror_from_system(errno)); return SYSINFO_RET_OK; } - if( connect(s,(struct sockaddr *)&servaddr_in,sizeof(struct sockaddr_in)) == -1 ) + if(SOCKET_ERROR == connect(s, (struct sockaddr *)&servaddr_in, sizeof(ZBX_SOCKADDR)) ) { - /* useless code switch (errno) { case EINTR: + zabbix_log(LOG_LEVEL_DEBUG, "Timeout while connecting to NTP server."); break; case EHOSTUNREACH: + zabbix_log(LOG_LEVEL_DEBUG, "No route to NTP server."); break; default: + zabbix_log(LOG_LEVEL_DEBUG, "Cannot connect to NTP server. [%s]", strerror(errno)); break; - } - */ -/* zbx_error("Cannot connect [%s]", strerror(errno));*/ + } goto lbl_error; } - pack_ntp(packet,NTP_PACKET_MIN,&data); + make_packet(&data); + +#if OFF + display_data(&data); +#endif /* OFF */ -#ifdef WIN32 -# error "TIDO replace <write> function" -#endif /* WIN32 */ - if( write(s,&packet,NTP_PACKET_MIN) == -1 ) + pack_ntp(packet, sizeof(packet), &data); + + if(SOCKET_ERROR == zbx_sock_write(s, packet, sizeof(packet))) { - /* useless code switch (errno) { case EINTR: + zabbix_log(LOG_LEVEL_DEBUG, "Timeout while sending data to NTP server."); break; default: + zabbix_log(LOG_LEVEL_DEBUG, "Error while sending data to NTP server. [%s]", strerror(errno)); break; } - */ -/* zbx_error("Cannot write [%s]", strerror(errno));*/ goto lbl_error; } - memset(c,0,MAX_STRING_LEN); + memset(buf, 0, sizeof(buf)); -#ifdef WIN32 -# error "TIDO replace <read> function" -#endif /* WIN32 */ - - len = read(s,c,MAX_STRING_LEN); - - if(len == -1) + if( SOCKET_ERROR == (len = zbx_sock_read(s, buf, sizeof(buf), CONFIG_TIMEOUT))) { - /* useless code switch (errno) { case EINTR: - break; + zabbix_log( LOG_LEVEL_DEBUG,"Timeout while receiving data from NTP server"); + break; case ECONNRESET: - break; + zabbix_log( LOG_LEVEL_DEBUG,"Connection to NTP server reseted by peer."); + break; default: - break; + zabbix_log( LOG_LEVEL_DEBUG,"Error while receiving data from NTP server [%s]", strerror(errno)); + break; } - */ -/* zbx_error("Cannot read0 [%d]", errno);*/ goto lbl_error; } - zbx_sock_close(s); - unpack_ntp(&data,c,len); + unpack_ntp(&data, buf, len); -/* display_data(&data); */ + zbx_sock_close(s); - zbx_snprintf(text, sizeof(text), "%d",0); +#if OFF + display_data(&data); +#endif /* OFF */ -/* format_time(text,75,offset,error,0.0,-1.0);*/ +/* format_time(text,sizeof(text),offset,error,0.0,-1.0);*/ /* if (dispersion < data->dispersion) dispersion = data->dispersion; x = data->receive-data->originate; @@ -352,14 +386,19 @@ int check_ntp(char *host, int port, int *value_int) x = data->current-data->originate; if (0.5*x > *err) *err = 0.5*x; */ - *value_int = format_time(text,75,0,0,0.0,-1.0); +/* *value_int = format_time(text,sizeof(text),0,0,0.0,-1.0); */ + +#if OFF + *value_int = time(NULL); /* local time */ +#else + *value_int = (data.receive > 0) ? (int)(data.receive - JAN_1970) : 0; /* server time */ +#endif return SYSINFO_RET_OK; lbl_error: zbx_sock_close(s); -#endif /* TODO */ return SYSINFO_RET_OK; } diff --git a/src/zabbix_agent/active.c b/src/zabbix_agent/active.c index e838e41f..1db29967 100644 --- a/src/zabbix_agent/active.c +++ b/src/zabbix_agent/active.c @@ -249,9 +249,7 @@ static int get_active_checks(char *server, unsigned short port, char *error, int zabbix_log( LOG_LEVEL_DEBUG, "get_active_checks('%s',%u)", server, port); servaddr_in.sin_family = AF_INET; - hp = gethostbyname(server); - - if(hp==NULL) + if(NULL == (hp = gethostbyname(server)) ) { #ifdef HAVE_HSTRERROR zbx_snprintf(error, max_error_len,"gethostbyname() failed for server '%s' [%s]", server, (char*)hstrerror((int)h_errno)); @@ -273,7 +271,7 @@ static int get_active_checks(char *server, unsigned short port, char *error, int return FAIL; } - if(SOCKET_ERROR == connect(s,(struct sockaddr *)&servaddr_in,sizeof(struct sockaddr_in))) + if(SOCKET_ERROR == connect(s,(struct sockaddr *)&servaddr_in,sizeof(ZBX_SOCKADDR))) { switch (errno) { @@ -321,24 +319,24 @@ static int get_active_checks(char *server, unsigned short port, char *error, int { len = zbx_sock_read(s, buf + amount_read, (sizeof(buf)-1) - amount_read, CONFIG_TIMEOUT); - if(SOCKET_ERROR == len) - { - switch (errno) + if(SOCKET_ERROR == len) { - case EINTR: - zbx_snprintf(error,max_error_len,"Timeout while receiving data from [%s:%u]",server,port); - break; - case ECONNRESET: - zbx_snprintf(error,max_error_len,"Connection reset by peer."); - break; - default: - zbx_snprintf(error,max_error_len,"Error while receiving data from [%s:%u] [%s]",server,port,strerror(errno)); - break; - } - zabbix_log( LOG_LEVEL_WARNING, error); - zbx_sock_close(s); - return FAIL; - } + switch (errno) + { + case EINTR: + zbx_snprintf(error,max_error_len,"Timeout while receiving data from [%s:%u]",server,port); + break; + case ECONNRESET: + zbx_snprintf(error,max_error_len,"Connection reset by peer."); + break; + default: + zbx_snprintf(error,max_error_len,"Error while receiving data from [%s:%u] [%s]",server,port,strerror(errno)); + break; + } + zabbix_log( LOG_LEVEL_WARNING, error); + zbx_sock_close(s); + return FAIL; + } amount_read += len; } @@ -364,10 +362,7 @@ static int send_value(char *server,unsigned short port,char *host, char *key,cha zabbix_log( LOG_LEVEL_DEBUG, "In send_value('%s',%u,'%s','%s','%s')", server, port, host, key, lastlogsize); - servaddr_in.sin_family=AF_INET; - hp = gethostbyname(server); - - if(hp==NULL) + if( NULL == (hp = gethostbyname(server)) ) { #ifdef HAVE_HSTRERROR zabbix_log( LOG_LEVEL_WARNING, "gethostbyname() failed for server '%s' [%d]", server, (char*)hstrerror((int)h_errno)); @@ -377,9 +372,9 @@ static int send_value(char *server,unsigned short port,char *host, char *key,cha return FAIL; } - servaddr_in.sin_addr.s_addr=((struct in_addr *)(hp->h_addr))->s_addr; - - servaddr_in.sin_port = htons(port); + servaddr_in.sin_family = AF_INET; + servaddr_in.sin_addr.s_addr = ((struct in_addr *)(hp->h_addr))->s_addr; + servaddr_in.sin_port = htons(port); if(INVALID_SOCKET == (s = socket(AF_INET,SOCK_STREAM,0))) { diff --git a/src/zabbix_agent/zabbix_agentd.c b/src/zabbix_agent/zabbix_agentd.c index ec212d2c..0efa0643 100644 --- a/src/zabbix_agent/zabbix_agentd.c +++ b/src/zabbix_agent/zabbix_agentd.c @@ -437,8 +437,21 @@ int main(int argc, char **argv) int main() { #if ON + int res, val; + WSADATA sockInfo; + + WSAStartup(0x0002,&sockInfo); + + res = check_ntp("142.3.100.15",123,&val); + + zbx_error("check_ntp result '%i' value '%i'", res, val); + +#elif OFF + zbx_error("%s",strerror_from_module(MSG_ZABBIX_MESSAGE, NULL)); + #elif OFF + char buffer[100*1024]; get_http_page("www.zabbix.com", "", 80, buffer, 100*1024); |
