diff options
author | sasha <sasha@97f52cf1-0a1b-0410-bd0e-c28be96e8082> | 2008-08-01 08:19:27 +0000 |
---|---|---|
committer | sasha <sasha@97f52cf1-0a1b-0410-bd0e-c28be96e8082> | 2008-08-01 08:19:27 +0000 |
commit | cca9b9f61d00f638b51ad9286da82865f63c4e08 (patch) | |
tree | f31f43477beebb3f1ad97a95d70e79584e05e0aa /src | |
parent | 4daf306cbcec1cc1eaf44c7b85b62e2e16f0e995 (diff) | |
download | zabbix-cca9b9f61d00f638b51ad9286da82865f63c4e08.tar.gz zabbix-cca9b9f61d00f638b51ad9286da82865f63c4e08.tar.xz zabbix-cca9b9f61d00f638b51ad9286da82865f63c4e08.zip |
- [DEV-195] added support of source ip address
git-svn-id: svn://svn.zabbix.com/trunk@5858 97f52cf1-0a1b-0410-bd0e-c28be96e8082
Diffstat (limited to 'src')
24 files changed, 264 insertions, 141 deletions
diff --git a/src/libs/zbxcomms/comms.c b/src/libs/zbxcomms/comms.c index 94479bbe..dfb40103 100644 --- a/src/libs/zbxcomms/comms.c +++ b/src/libs/zbxcomms/comms.c @@ -317,6 +317,7 @@ void zbx_tcp_init(zbx_sock_t *s, ZBX_SOCKET o) ******************************************************************************/ #if defined(HAVE_IPV6) int zbx_tcp_connect(zbx_sock_t *s, + const char *source_ip, const char *ip, unsigned short port, int timeout @@ -324,6 +325,7 @@ int zbx_tcp_connect(zbx_sock_t *s, { int ret = FAIL; struct addrinfo *ai = NULL, hints; + struct addrinfo *ai_bind = NULL; char service[MAX_STRING_LEN]; ZBX_TCP_START(); @@ -345,6 +347,29 @@ int zbx_tcp_connect(zbx_sock_t *s, goto out; } + if (NULL != source_ip) + { + memset(&hints, 0x00, sizeof(struct addrinfo)); + hints.ai_family = PF_UNSPEC; + hints.ai_socktype = SOCK_STREAM; + hints.ai_flags = AI_NUMERICHOST; + + if (0 != getaddrinfo(source_ip, NULL, &hints, &ai_bind)) + { + zbx_set_tcp_strerror("Invalid source IP address [%s]\n", + source_ip); + goto out; + } + + if (ZBX_TCP_ERROR == bind(s->socket, ai_bind->ai_addr, ai_bind->ai_addrlen)) + { + zbx_set_tcp_strerror("bind() failed with error %d: %s\n", + zbx_sock_last_error(), + strerror_from_system(zbx_sock_last_error())); + goto out; + } + } + if (0 != timeout) { s->timeout = timeout; #if defined(_WINDOWS) @@ -369,16 +394,21 @@ int zbx_tcp_connect(zbx_sock_t *s, out: if (NULL != ai) freeaddrinfo(ai); + + if (NULL != ai_bind) + freeaddrinfo(ai_bind); + return ret; } #else int zbx_tcp_connect(zbx_sock_t *s, + const char *source_ip, const char *ip, unsigned short port, int timeout ) { - ZBX_SOCKADDR servaddr_in; + ZBX_SOCKADDR servaddr_in, source_addr; struct hostent *hp; ZBX_TCP_START(); @@ -399,6 +429,21 @@ int zbx_tcp_connect(zbx_sock_t *s, return FAIL; } + if (NULL != source_ip) + { + source_addr.sin_family = AF_INET; + source_addr.sin_addr.s_addr = inet_addr(source_ip); + source_addr.sin_port = 0; + + if (ZBX_SOCK_ERROR == bind(s->socket, (struct sockaddr *)&source_addr, sizeof(ZBX_SOCKADDR)) ) + { + zbx_set_tcp_strerror("bind() failed with error %d: %s\n", + zbx_sock_last_error(), + strerror_from_system(zbx_sock_last_error())); + return FAIL; + } + } + if (0 != timeout) { s->timeout = timeout; #if defined(_WINDOWS) diff --git a/src/libs/zbxemail/email.c b/src/libs/zbxemail/email.c index af1d7103..9ff5841b 100644 --- a/src/libs/zbxemail/email.c +++ b/src/libs/zbxemail/email.c @@ -62,7 +62,7 @@ int send_email(char *smtp_server,char *smtp_helo,char *smtp_email,char *mailto,c zabbix_log( LOG_LEVEL_DEBUG, "In send_email[smtp_server:%s]", smtp_server); - if(FAIL == zbx_tcp_connect(&s, smtp_server, 25, 0)) + if(FAIL == zbx_tcp_connect(&s, CONFIG_SOURCE_IP, smtp_server, 25, 0)) { zbx_snprintf(error,max_error_len,"Cannot connect to SMTP server [%s] [%s]", smtp_server, zbx_tcp_strerror()); zabbix_log(LOG_LEVEL_DEBUG, "%s", error); diff --git a/src/libs/zbxicmpping/icmpping.c b/src/libs/zbxicmpping/icmpping.c index cbe62502..983bb16c 100644 --- a/src/libs/zbxicmpping/icmpping.c +++ b/src/libs/zbxicmpping/icmpping.c @@ -22,9 +22,10 @@ #include "log.h" #include "zlog.h" -extern char *CONFIG_FPING_LOCATION; +extern char *CONFIG_SOURCE_IP; +extern char *CONFIG_FPING_LOCATION; #ifdef HAVE_IPV6 -extern char *CONFIG_FPING6_LOCATION; +extern char *CONFIG_FPING6_LOCATION; #endif /* HAVE_IPV6 */ /****************************************************************************** @@ -48,9 +49,14 @@ int do_ping(ZBX_FPING_HOST *hosts, int hosts_count) FILE *f; char filename[MAX_STRING_LEN]; char tmp[MAX_STRING_LEN]; - int i; - char *c; + int i, res = FAIL; + char *c, source_ip[64]; ZBX_FPING_HOST *host; +#ifdef HAVE_IPV6 + struct addrinfo hints, *ai = NULL; + char *fping; + int e; +#endif zabbix_log(LOG_LEVEL_DEBUG, "In do_ping() [hosts_count:%d]", hosts_count); @@ -73,15 +79,51 @@ int do_ping(ZBX_FPING_HOST *hosts, int hosts_count) fclose(f); + if (NULL != CONFIG_SOURCE_IP) + zbx_snprintf(source_ip, sizeof(source_ip), "-S%s ", CONFIG_SOURCE_IP); + else + *source_ip = '\0'; + #ifdef HAVE_IPV6 - zbx_snprintf(tmp, sizeof(tmp), "%s -c3 2>/dev/null <%s;%s -c3 2>/dev/null <%s", - CONFIG_FPING_LOCATION, - filename, - CONFIG_FPING6_LOCATION, - filename); + if (NULL != CONFIG_SOURCE_IP) + { + memset(&hints, 0, sizeof(hints)); + hints.ai_family = PF_UNSPEC; + hints.ai_flags = AI_NUMERICHOST; + hints.ai_socktype = SOCK_STREAM; + + if (0 != (e = getaddrinfo(CONFIG_SOURCE_IP, NULL, &hints, &ai))) + { + zabbix_log(LOG_LEVEL_WARNING, "Cannot resolve address [%s], error %d: %s", + CONFIG_SOURCE_IP, e, gai_strerror(e)); + goto out; + } + + if (ai->ai_family == PF_INET) + fping = CONFIG_FPING_LOCATION; + else if (ai->ai_family == PF_INET6) + fping = CONFIG_FPING6_LOCATION; + else + { + zabbix_log(LOG_LEVEL_WARNING, "Unsupported address family [%s]", CONFIG_SOURCE_IP); + goto out; + } + + zbx_snprintf(tmp, sizeof(tmp), "%s %s-c3 2>/dev/null <%s", + fping, + source_ip, + filename); + } + else + zbx_snprintf(tmp, sizeof(tmp), "%s -c3 2>/dev/null <%s;%s -c3 2>/dev/null <%s", + CONFIG_FPING_LOCATION, + filename, + CONFIG_FPING6_LOCATION, + filename); #else /* HAVE_IPV6 */ - zbx_snprintf(tmp, sizeof(tmp), "%s -c3 2>/dev/null <%s", + zbx_snprintf(tmp, sizeof(tmp), "%s %s-c3 2>/dev/null <%s", CONFIG_FPING_LOCATION, + source_ip, filename); #endif /* HAVE_IPV6 */ @@ -92,7 +134,7 @@ int do_ping(ZBX_FPING_HOST *hosts, int hosts_count) zabbix_syslog("Cannot execute [%s] [%s]", CONFIG_FPING_LOCATION, strerror(errno)); - return FAIL; + goto out; } while (NULL != fgets(tmp, sizeof(tmp), f)) { @@ -123,10 +165,17 @@ int do_ping(ZBX_FPING_HOST *hosts, int hosts_count) } pclose(f); + res = SUCCEED; +out: unlink(filename); +#ifdef HAVE_IPV6 + if (NULL != ai) + freeaddrinfo(ai); +#endif + zabbix_log(LOG_LEVEL_DEBUG, "End of do_ping()"); - return SUCCEED; + return res; } diff --git a/src/libs/zbxsysinfo/common/http.c b/src/libs/zbxsysinfo/common/http.c index 29988756..dec67120 100644 --- a/src/libs/zbxsysinfo/common/http.c +++ b/src/libs/zbxsysinfo/common/http.c @@ -40,7 +40,7 @@ static int get_http_page(char *host, char *param, unsigned short port, char *buf assert(buffer); - if (SUCCEED == (ret = zbx_tcp_connect(&s, host, port, 0))) { + if (SUCCEED == (ret = zbx_tcp_connect(&s, CONFIG_SOURCE_IP, host, port, 0))) { zbx_snprintf(request, sizeof(request), "GET /%s HTTP/1.1\r\nHost: %s\r\nConnection: close\r\n\r\n", param, host); diff --git a/src/libs/zbxsysinfo/common/http.h b/src/libs/zbxsysinfo/common/http.h index 4f0fc118..efeac96b 100644 --- a/src/libs/zbxsysinfo/common/http.h +++ b/src/libs/zbxsysinfo/common/http.h @@ -21,6 +21,8 @@ #include "sysinfo.h" +extern char *CONFIG_SOURCE_IP; + int WEB_PAGE_GET(const char *cmd, const char *param, unsigned flags, AGENT_RESULT *result); int WEB_PAGE_PERF(const char *cmd, const char *param, unsigned flags, AGENT_RESULT *result); int WEB_PAGE_REGEXP(const char *cmd, const char *param, unsigned flags, AGENT_RESULT *result); diff --git a/src/libs/zbxsysinfo/common/net.c b/src/libs/zbxsysinfo/common/net.c index 0dd93dea..d3f336f2 100644 --- a/src/libs/zbxsysinfo/common/net.c +++ b/src/libs/zbxsysinfo/common/net.c @@ -31,12 +31,12 @@ * 1 - OK * */ int tcp_expect( - const char *host, + const char *host, unsigned short port, - const char *request, - const char *expect, - const char *sendtoclose, - int *value_int + const char *request, + const char *expect, + const char *sendtoclose, + int *value_int ) { zbx_sock_t s; @@ -47,7 +47,7 @@ int tcp_expect( *value_int = 0; - if (SUCCEED == (ret = zbx_tcp_connect(&s, host, port, 3/*alarm!!!*/))) { + if (SUCCEED == (ret = zbx_tcp_connect(&s, CONFIG_SOURCE_IP, host, port, 3/*alarm!!!*/))) { if( NULL == request ) { *value_int = 1; diff --git a/src/libs/zbxsysinfo/common/net.h b/src/libs/zbxsysinfo/common/net.h index 8de79d45..95a54b56 100644 --- a/src/libs/zbxsysinfo/common/net.h +++ b/src/libs/zbxsysinfo/common/net.h @@ -21,6 +21,8 @@ #include "sysinfo.h" +extern char *CONFIG_SOURCE_IP; + int tcp_expect(const char *host, unsigned short port, const char *request, const char *expect, const char *sendtoclose, int *value_int); int TCP_LISTEN(const char *cmd, const char *param, unsigned flags, AGENT_RESULT *result); int CHECK_PORT(const char *cmd, const char *param, unsigned flags, AGENT_RESULT *result); diff --git a/src/libs/zbxsysinfo/simple/ntp.c b/src/libs/zbxsysinfo/simple/ntp.c index 55c227a8..f1e7c082 100644 --- a/src/libs/zbxsysinfo/simple/ntp.c +++ b/src/libs/zbxsysinfo/simple/ntp.c @@ -22,6 +22,7 @@ #include "comms.h" #include "log.h" #include "cfg.h" +#include "ntp.h" #define NTP_SCALE 4294967296.0 /* 2^32, of course! */ @@ -190,7 +191,7 @@ int check_ntp(char *host, unsigned short port, int *value_int) *value_int = 0; - if (SUCCEED == (ret = zbx_tcp_connect(&s, host, port, 0))) { + if (SUCCEED == (ret = zbx_tcp_connect(&s, CONFIG_SOURCE_IP, host, port, 0))) { make_packet(&data); pack_ntp((unsigned char*)packet, sizeof(packet), &data); diff --git a/src/libs/zbxsysinfo/simple/ntp.h b/src/libs/zbxsysinfo/simple/ntp.h index 2805b36a..4d9b0ea2 100644 --- a/src/libs/zbxsysinfo/simple/ntp.h +++ b/src/libs/zbxsysinfo/simple/ntp.h @@ -19,6 +19,8 @@ #if !defined(SYSINFO_SYMPLE_NTP_H_INCLUDED) +extern char *CONFIG_SOURCE_IP; + int check_ntp(char *host, unsigned short port, int *value_int); #endif /* SYSINFO_SYMPLE_NTP_H_INCLUDED */ diff --git a/src/libs/zbxsysinfo/simple/simple.c b/src/libs/zbxsysinfo/simple/simple.c index 03bb05f7..b29b8e6f 100644 --- a/src/libs/zbxsysinfo/simple/simple.c +++ b/src/libs/zbxsysinfo/simple/simple.c @@ -118,7 +118,7 @@ static int check_ssh(const char *host, unsigned short port, int *value_int) assert(value_int); *value_int = 0; - if (SUCCEED == (ret = zbx_tcp_connect(&s, host, port, 0))) { + if (SUCCEED == (ret = zbx_tcp_connect(&s, CONFIG_SOURCE_IP, host, port, 0))) { if( SUCCEED == (ret = zbx_tcp_recv(&s, &recv_buf)) ) { if ( 0 == strncmp(recv_buf, "SSH", 3) ) diff --git a/src/libs/zbxsysinfo/simple/simple.h b/src/libs/zbxsysinfo/simple/simple.h index cc00e2ce..41f47443 100644 --- a/src/libs/zbxsysinfo/simple/simple.h +++ b/src/libs/zbxsysinfo/simple/simple.h @@ -21,6 +21,7 @@ #include "sysinfo.h" +extern char *CONFIG_SOURCE_IP; extern ZBX_METRIC parameters_simple[]; int CHECK_SERVICE_PERF(const char *cmd, const char *param, unsigned flags, AGENT_RESULT *result); diff --git a/src/zabbix_agent/active.c b/src/zabbix_agent/active.c index 9a76fbae..20d4cb60 100644 --- a/src/zabbix_agent/active.c +++ b/src/zabbix_agent/active.c @@ -309,7 +309,7 @@ static int refresh_active_checks( zbx_json_addstring(&json, ZBX_PROTO_TAG_REQUEST, ZBX_PROTO_VALUE_GET_ACTIVE_CHECKS, ZBX_JSON_TYPE_STRING); zbx_json_addstring(&json, ZBX_PROTO_TAG_HOST, CONFIG_HOSTNAME, ZBX_JSON_TYPE_STRING); - if (SUCCEED == (ret = zbx_tcp_connect(&s, host, port, CONFIG_TIMEOUT))) { + if (SUCCEED == (ret = zbx_tcp_connect(&s, CONFIG_SOURCE_IP, host, port, CONFIG_TIMEOUT))) { zabbix_log(LOG_LEVEL_DEBUG, "Sending [%s]", json.buffer); if( SUCCEED == (ret = zbx_tcp_send(&s, json.buffer)) ) @@ -468,7 +468,7 @@ static int send_buffer( zbx_json_adduint64(&json, ZBX_PROTO_TAG_CLOCK, (int)time(NULL)); - if (SUCCEED == (ret = zbx_tcp_connect(&s, host, port, MIN(buffer.count*CONFIG_TIMEOUT, 60)))) { + if (SUCCEED == (ret = zbx_tcp_connect(&s, CONFIG_SOURCE_IP, host, port, MIN(buffer.count*CONFIG_TIMEOUT, 60)))) { zabbix_log(LOG_LEVEL_DEBUG, "JSON before sending [%s]", json.buffer); diff --git a/src/zabbix_agent/active.h b/src/zabbix_agent/active.h index 19427197..2a297d6a 100644 --- a/src/zabbix_agent/active.h +++ b/src/zabbix_agent/active.h @@ -22,6 +22,7 @@ #include "threads.h" +extern char *CONFIG_SOURCE_IP; extern char *CONFIG_HOSTNAME; extern int CONFIG_REFRESH_ACTIVE_CHECKS; extern int CONFIG_BUFFER_SEND; diff --git a/src/zabbix_agent/zbxconf.c b/src/zabbix_agent/zbxconf.c index a55e3574..cdd1b157 100644 --- a/src/zabbix_agent/zbxconf.c +++ b/src/zabbix_agent/zbxconf.c @@ -52,6 +52,7 @@ int CONFIG_LISTEN_PORT = 10050; int CONFIG_SERVER_PORT = 10051; int CONFIG_REFRESH_ACTIVE_CHECKS = 120; char *CONFIG_LISTEN_IP = NULL; +char *CONFIG_SOURCE_IP = NULL; int CONFIG_LOG_LEVEL = LOG_LEVEL_INFORMATION; char CONFIG_LOG_UNRES_SYMB = 0; @@ -81,6 +82,7 @@ void load_config() {"ListenPort", &CONFIG_LISTEN_PORT, 0,TYPE_INT, PARM_OPT, 1024,32767}, {"ServerPort", &CONFIG_SERVER_PORT, 0,TYPE_INT, PARM_OPT, 1024,32767}, {"ListenIP", &CONFIG_LISTEN_IP, 0,TYPE_STRING, PARM_OPT, 0,0}, + {"SourceIP", &CONFIG_SOURCE_IP, 0,TYPE_STRING, PARM_OPT, 0,0}, {"DebugLevel", &CONFIG_LOG_LEVEL, 0,TYPE_INT, PARM_OPT, 0,5}, diff --git a/src/zabbix_get/zabbix_get.c b/src/zabbix_get/zabbix_get.c index a9086e1f..c537cccb 100644 --- a/src/zabbix_get/zabbix_get.c +++ b/src/zabbix_get/zabbix_get.c @@ -27,30 +27,36 @@ char *progname = NULL; char title_message[] = "ZABBIX get - Communicate with ZABBIX agent"; -char usage_message[] = "[-hV] -s<host name or IP> [-p<port>] -k<key>"; +char usage_message[] = "[-hV] -s<host name or IP> [-p<port>] [-I<ip address>] -k<key>"; #ifndef HAVE_GETOPT_LONG char *help_message[] = { "Options:", - " -p <port number> Specify port number of agent running on the host. Default is 10050.", - " -s <host name or IP> Specify host name or IP address of a host.", - " -k <key of metric> Specify metric name (key) we want to retrieve.", - " -h give this help", - " -V display version number", + " -s --host <host name or IP> Specify host name or IP address of a host.", + " -p --port <port number> Specify port number of agent running on the host. Default is 10050.", + " -I --source-address <ip address> Specify source IP address", "", - "Example: zabbix_get -s127.0.0.1 -p10050 -k\"system[procload]\"", + " -k --key <key of metric> Specify metric name (key) we want to retrieve.", + "", + " -h --help Give this help", + " -V --version Display version number", + "", + "Example: zabbix_get -s127.0.0.1 -p10050 -k\"system.cpu.load[all,avg1]\"", 0 /* end of text */ }; #else char *help_message[] = { "Options:", - " -p --port <port number> Specify port number of agent running on the host. Default is 10050.", - " -s --host <host name or IP> Specify host name or IP address of a host.", - " -k --key <key of metric> Specify metric name (key) we want to retrieve.", - " -h --help give this help", - " -V --version display version number", + " -s <host name or IP> Specify host name or IP address of a host.", + " -p <port number> Specify port number of agent running on the host. Default is 10050.", + " -I <ip address> Specify source IP address", + "", + " -k <key of metric> Specify metric name (key) we want to retrieve.", "", - "Example: zabbix_get -s127.0.0.1 -p10050 -k\"system[procload]\"", + " -h Give this help", + " -V Display version number", + "", + "Example: zabbix_get -s127.0.0.1 -p10050 -k\"system.cpu.load[all,avg1]\"", 0 /* end of text */ }; #endif @@ -60,17 +66,18 @@ char *help_message[] = { /* long options */ struct zbx_option longopts[] = { - {"port", 1, 0, 'p'}, - {"host", 1, 0, 's'}, - {"key", 1, 0, 'k'}, - {"help", 0, 0, 'h'}, - {"version", 0, 0, 'V'}, + {"host", 1, 0, 's'}, + {"port", 1, 0, 'p'}, + {"key", 1, 0, 'k'}, + {"source-address", 1, 0, 'I'}, + {"help", 0, 0, 'h'}, + {"version", 0, 0, 'V'}, {0,0,0,0} }; /* short options */ -static char shortopts[] = "k:p:s:hV"; +static char shortopts[] = "s:p:k:I:hV"; /* end of COMMAND LINE OPTIONS*/ @@ -129,6 +136,7 @@ void signal_handler( int sig ) * * ******************************************************************************/ static int get_value( + const char *source_ip, const char *host, unsigned short port, const char *key, @@ -144,7 +152,7 @@ static int get_value( *value = NULL; - if (SUCCEED == (ret = zbx_tcp_connect(&s, host, port, SENDER_TIMEOUT))) { + if (SUCCEED == (ret = zbx_tcp_connect(&s, source_ip, host, port, SENDER_TIMEOUT))) { zbx_snprintf(request, sizeof(request),"%s\n",key); if( SUCCEED == (ret = zbx_tcp_send(&s, request)) ) { @@ -185,6 +193,7 @@ int main(int argc, char **argv) char *value = NULL; char *host = NULL; char *key = NULL; + char *source_ip = NULL; char ch; progname = get_programm_name(argv[0]); @@ -201,6 +210,9 @@ int main(int argc, char **argv) case 's': host = strdup(zbx_optarg); break; + case 'I': + source_ip = strdup(zbx_optarg); + break; case 'h': help(); exit(-1); @@ -231,7 +243,7 @@ int main(int argc, char **argv) signal( SIGALRM, signal_handler ); #endif /* not WINDOWS */ - ret = get_value(host, port, key, &value); + ret = get_value(source_ip, host, port, key, &value); if(ret == SUCCEED) { diff --git a/src/zabbix_proxy/proxy.c b/src/zabbix_proxy/proxy.c index eb7bc161..da4587b7 100644 --- a/src/zabbix_proxy/proxy.c +++ b/src/zabbix_proxy/proxy.c @@ -118,6 +118,7 @@ int CONFIG_UNREACHABLE_POLLER_FORKS = 1; int CONFIG_LISTEN_PORT = 10051; char *CONFIG_LISTEN_IP = NULL; +char *CONFIG_SOURCE_IP = NULL; int CONFIG_TRAPPER_TIMEOUT = ZABBIX_TRAPPER_TIMEOUT; /**/ /*int CONFIG_NOTIMEWAIT = 0;*/ @@ -228,6 +229,7 @@ void init_config(void) {"UnavailableDelay",&CONFIG_UNAVAILABLE_DELAY,0,TYPE_INT,PARM_OPT,1,3600}, {"ListenIP",&CONFIG_LISTEN_IP,0,TYPE_STRING,PARM_OPT,0,0}, {"ListenPort",&CONFIG_LISTEN_PORT,0,TYPE_INT,PARM_OPT,1024,32768}, + {"SourceIP",&CONFIG_SOURCE_IP,0,TYPE_STRING,PARM_OPT,0,0}, /* {"NoTimeWait",&CONFIG_NOTIMEWAIT,0,TYPE_INT,PARM_OPT,0,1},*/ /* {"DisablePinger",&CONFIG_DISABLE_PINGER,0,TYPE_INT,PARM_OPT,0,1},*/ {"DebugLevel",&CONFIG_LOG_LEVEL,0,TYPE_INT,PARM_OPT,0,4}, diff --git a/src/zabbix_sender/zabbix_sender.c b/src/zabbix_sender/zabbix_sender.c index a76ccfcc..8224e203 100644 --- a/src/zabbix_sender/zabbix_sender.c +++ b/src/zabbix_sender/zabbix_sender.c @@ -30,16 +30,17 @@ char *progname = NULL; char title_message[] = "ZABBIX send"; -char usage_message[] = "[-Vhv] {[-zps] -ko | [-p] -z<ZABBIX server> -i <file>} [-c <file>]"; +char usage_message[] = "[-Vhv] {[-zpsI] -ko | [-zpI] -i <file>} [-c <file>]"; #ifdef HAVE_GETOPT_LONG char *help_message[] = { "Options:", - " -c --config <File> Specify configuration file" + " -c --config <File> Specify configuration file", "", " -z --zabbix-server <Server> Hostname or IP address of ZABBIX Server", " -p --port <Server port> Specify port number of server trapper running on the server. Default is 10051", " -s --host <Hostname> Specify host name. Host IP address and DNS name will not work.", + " -I --source-address <ip address> Specify source IP address", "", " -k --key <Key> Specify metric name (key) we want to send", " -o --value <Key value> Specify value of the key", @@ -57,11 +58,12 @@ char *help_message[] = { #else char *help_message[] = { "Options:", - " -c <File> Specify configuration file" + " -c <File> Specify configuration file", "", " -z <Server> Hostname or IP address of ZABBIX Server.", " -p <Server port> Specify port number of server trapper running on the server. Default is 10051.", " -s <Hostname> Specify hostname or IP address of a host.", + " -I <ip address> Specify source IP address", "", " -k <Key> Specify metric name (key) we want to send.", " -o <Key value> Specify value of the key.", @@ -88,6 +90,7 @@ static struct zbx_option longopts[] = {"zabbix-server", 1, NULL, 'z'}, {"port", 1, NULL, 'p'}, {"host", 1, NULL, 's'}, + {"source-address", 1, NULL, 'I'}, {"key", 1, NULL, 'k'}, {"value", 1, NULL, 'o'}, {"input-file", 1, NULL, 'i'}, @@ -99,7 +102,7 @@ static struct zbx_option longopts[] = /* short options */ -static char shortopts[] = "c:z:p:s:k:o:i:vhV"; +static char shortopts[] = "c:I:z:p:s:k:o:i:vhV"; /* end of COMMAND LINE OPTIONS*/ @@ -107,6 +110,7 @@ static int CONFIG_LOG_LEVEL = LOG_LEVEL_CRIT; static char* INPUT_FILE = NULL; +static char* CONFIG_SOURCE_IP = NULL; static char* ZABBIX_SERVER = NULL; unsigned short ZABBIX_SERVER_PORT = 0; static char* ZABBIX_HOSTNAME = NULL; @@ -134,9 +138,9 @@ static void send_signal_handler( int sig ) typedef struct zbx_active_metric_type { - char* server; + char *source_ip, *server; unsigned short port; - struct zbx_json json; + struct zbx_json json; } ZBX_THREAD_SENDVAL_ARGS; /****************************************************************************** @@ -213,8 +217,8 @@ static ZBX_THREAD_ENTRY(send_value, args) signal( SIGQUIT, send_signal_handler ); signal( SIGALRM, send_signal_handler ); #endif /* NOT _WINDOWS */ - - if (SUCCEED == (tcp_ret = zbx_tcp_connect(&sock, sentdval_args->server, sentdval_args->port, SENDER_TIMEOUT))) { + + if (SUCCEED == (tcp_ret = zbx_tcp_connect(&sock, CONFIG_SOURCE_IP, sentdval_args->server, sentdval_args->port, SENDER_TIMEOUT))) { tcp_ret = zbx_tcp_send(&sock, sentdval_args->json.buffer); if( SUCCEED == tcp_ret ) @@ -246,6 +250,7 @@ static ZBX_THREAD_ENTRY(send_value, args) static void init_config(const char* config_file) { + char* config_source_ip_from_conf = NULL; char* zabbix_server_from_conf = NULL; int zabbix_server_port_from_conf = 0; char* zabbix_hostname_from_conf = NULL; @@ -254,6 +259,7 @@ static void init_config(const char* config_file) struct cfg_line cfg[]= { /* PARAMETER ,VAR ,FUNC ,TYPE(0i,1s) ,MANDATORY ,MIN ,MAX */ + {"SourceIP" ,&config_source_ip_from_conf ,0 ,TYPE_STRING ,PARM_OPT ,0 ,0 }, {"Server" ,&zabbix_server_from_conf ,0 ,TYPE_STRING ,PARM_OPT ,0 ,0 }, {"ServerPort" ,&zabbix_server_port_from_conf ,0 ,TYPE_INT ,PARM_OPT ,MIN_ZABBIX_PORT ,MAX_ZABBIX_PORT}, {"Hostname" ,&zabbix_hostname_from_conf ,0 ,TYPE_STRING ,PARM_OPT ,0 ,0 }, @@ -264,6 +270,15 @@ static void init_config(const char* config_file) { parse_cfg_file(config_file, cfg); + if (NULL != config_source_ip_from_conf) + { + if (NULL == CONFIG_SOURCE_IP) /* apply parameter only if unsetted */ + { + CONFIG_SOURCE_IP = strdup(config_source_ip_from_conf); + } + zbx_free(config_source_ip_from_conf); + } + if( zabbix_server_from_conf ) { if( !ZABBIX_SERVER ) @@ -312,6 +327,9 @@ static zbx_task_t parse_commandline(int argc, char **argv) version(); exit(-1); break; + case 'I': + CONFIG_SOURCE_IP = strdup(zbx_optarg); + break; case 'z': ZABBIX_SERVER = strdup(zbx_optarg); break; @@ -376,44 +394,59 @@ int main(int argc, char **argv) zabbix_open_log(LOG_TYPE_UNDEFINED, CONFIG_LOG_LEVEL, NULL); + if (NULL == ZABBIX_SERVER) + { + zabbix_log(LOG_LEVEL_WARNING, "'Server' parameter required"); + goto exit; + } + if (0 == ZABBIX_SERVER_PORT) + ZABBIX_SERVER_PORT = 10051; + + if (MIN_ZABBIX_PORT > ZABBIX_SERVER_PORT) + { + zabbix_log(LOG_LEVEL_WARNING, "Incorrect port number [%d]. Allowed [%d:%d]", + (int)ZABBIX_SERVER_PORT, (int)MIN_ZABBIX_PORT, (int)MAX_ZABBIX_PORT); + goto exit; + } + + sentdval_args.server = ZABBIX_SERVER; + sentdval_args.port = ZABBIX_SERVER_PORT; + + zbx_json_init(&sentdval_args.json, 8*1024); + zbx_json_addstring(&sentdval_args.json, ZBX_PROTO_TAG_REQUEST, ZBX_PROTO_VALUE_SENDER_DATA, ZBX_JSON_TYPE_STRING); + zbx_json_addarray(&sentdval_args.json, ZBX_PROTO_TAG_DATA); + if( INPUT_FILE ) { - if( !(in = fopen(INPUT_FILE, "r")) ) + if (NULL == (in = fopen(INPUT_FILE, "r")) ) { - zabbix_log( LOG_LEVEL_WARNING, "Cannot open [%s] [%s]", INPUT_FILE, strerror(errno)); + zabbix_log(LOG_LEVEL_WARNING, "Cannot open [%s] [%s]", INPUT_FILE, strerror(errno)); return FAIL; } - zbx_json_init(&sentdval_args.json, 8*1024); - zbx_json_addstring(&sentdval_args.json, ZBX_PROTO_TAG_REQUEST, ZBX_PROTO_VALUE_SENDER_DATA, ZBX_JSON_TYPE_STRING); - zbx_json_addarray(&sentdval_args.json, ZBX_PROTO_TAG_DATA); - - while(fgets(in_line, sizeof(in_line), in) != NULL) - { /* <zabbix_server> <hostname> <port> <key> <value> */ + while (NULL != fgets(in_line, sizeof(in_line), in)) /* <hostname> <key> <value> */ + { total_count++; /* also used as inputline */ - hostname = in_line; - if( !(key = strchr(hostname, ' ')) ) + if (NULL == (key = strchr(hostname, ' '))) { - zabbix_log( LOG_LEVEL_WARNING, "[line %i] 'Key' required", total_count); + zabbix_log(LOG_LEVEL_WARNING, "[line %d] 'Key' required", total_count); continue; } - *key = '\0'; - key++; + *key++ = '\0'; - if( !(key_value = strchr(key, ' ')) ) + if (NULL == (key_value = strchr(key, ' '))) { - zabbix_log( LOG_LEVEL_WARNING, "[line %i] 'Key value' required", total_count); + zabbix_log(LOG_LEVEL_WARNING, "[line %d] 'Key value' required", total_count); continue; } - *key_value = '\0'; - key_value++; + *key_value++ = '\0'; - for(s = key_value; s && *s; s++) + for (s = key_value; s && *s; s++) { if(*s == '\r' || *s == '\n' ) { @@ -422,20 +455,6 @@ int main(int argc, char **argv) } } - if( ZABBIX_SERVER ) sentdval_args.server = ZABBIX_SERVER; - if( ZABBIX_HOSTNAME ) hostname = ZABBIX_HOSTNAME; - if( ZABBIX_KEY ) key = ZABBIX_KEY; - if( ZABBIX_KEY_VALUE ) key_value = ZABBIX_KEY_VALUE; - - if( ZABBIX_SERVER_PORT ) - sentdval_args.port = ZABBIX_SERVER_PORT; - - if( MIN_ZABBIX_PORT > sentdval_args.port /* || sentdval_args.port > MAX_ZABBIX_PORT (MAX_ZABBIX_PORT == max unsigned short) */) - { - zabbix_log( LOG_LEVEL_WARNING, "[line %i] Incorrect port number [%i]. Allowed [%i:%i]", - total_count, sentdval_args.port, MIN_ZABBIX_PORT, MAX_ZABBIX_PORT); - continue; - } zbx_json_addobject(&sentdval_args.json, NULL); zbx_json_addstring(&sentdval_args.json, ZBX_PROTO_TAG_HOST, hostname, ZBX_JSON_TYPE_STRING); zbx_json_addstring(&sentdval_args.json, ZBX_PROTO_TAG_KEY, key, ZBX_JSON_TYPE_STRING); @@ -445,26 +464,7 @@ int main(int argc, char **argv) succeed_count++; } - if(succeed_count > 0) - { - if( SUCCEED == zbx_thread_wait( - zbx_thread_start( - send_value, - &sentdval_args - ) - ) - ) - { - printf("sent: %i; skipped: %i; total: %i\n", succeed_count, (total_count - succeed_count), total_count); - } - else - { - printf("Sending failed. Use option -vv for more detailed output.\n"); - } - } - fclose(in); - zbx_json_free(&sentdval_args.json); } else { @@ -472,30 +472,26 @@ int main(int argc, char **argv) do /* try block simulation */ { - if( !ZABBIX_SERVER ) { zabbix_log( LOG_LEVEL_WARNING, "'Server' parameter required"); break; } - if( !ZABBIX_HOSTNAME ) { zabbix_log( LOG_LEVEL_WARNING, "'Hostname' parameter required"); break; } - if( !ZABBIX_KEY ) { zabbix_log( LOG_LEVEL_WARNING, "Key required"); break; } - if( !ZABBIX_KEY_VALUE ) { zabbix_log( LOG_LEVEL_WARNING, "Key value required"); break; } - - if( !ZABBIX_SERVER_PORT ) ZABBIX_SERVER_PORT = 10051; - - if( MIN_ZABBIX_PORT > ZABBIX_SERVER_PORT /* || ZABBIX_SERVER_PORT > MAX_ZABBIX_PORT (MAX_ZABBIX_PORT == max unsigned short) */) + if (NULL == ZABBIX_HOSTNAME) + { + zabbix_log(LOG_LEVEL_WARNING, "'Hostname' parameter required"); + break; + } + if (NULL == ZABBIX_KEY) + { + zabbix_log(LOG_LEVEL_WARNING, "Key required"); + break; + } + if (NULL == ZABBIX_KEY_VALUE) { - zabbix_log( LOG_LEVEL_WARNING, "Incorrect port number [%i]. Allowed [%i:%i]", - ZABBIX_SERVER_PORT, MIN_ZABBIX_PORT, MAX_ZABBIX_PORT); + zabbix_log(LOG_LEVEL_WARNING, "Key value required"); break; } - sentdval_args.server = ZABBIX_SERVER; - sentdval_args.port = ZABBIX_SERVER_PORT; hostname = ZABBIX_HOSTNAME; key = ZABBIX_KEY; key_value = ZABBIX_KEY_VALUE; - zbx_json_init(&sentdval_args.json, 8*1024); - zbx_json_addstring(&sentdval_args.json, ZBX_PROTO_TAG_REQUEST, ZBX_PROTO_VALUE_SENDER_DATA, ZBX_JSON_TYPE_STRING); - zbx_json_addarray(&sentdval_args.json, ZBX_PROTO_TAG_DATA); - zbx_json_addobject(&sentdval_args.json, NULL); zbx_json_addstring(&sentdval_args.json, ZBX_PROTO_TAG_HOST, hostname, ZBX_JSON_TYPE_STRING); zbx_json_addstring(&sentdval_args.json, ZBX_PROTO_TAG_KEY, key, ZBX_JSON_TYPE_STRING); @@ -503,27 +499,24 @@ int main(int argc, char **argv) zbx_json_close(&sentdval_args.json); succeed_count++; - - if( SUCCEED == zbx_thread_wait( - zbx_thread_start( - send_value, - &sentdval_args - ) - ) - ) - { - printf("sent: %i; skipped: %i; total: %i\n", succeed_count, (total_count - succeed_count), total_count); - } - else - { - printf("Sending failed. Use option -vv for more detailed outputs.\n"); - } - zbx_json_free(&sentdval_args.json); } while(0); /* try block simulation */ } + if (succeed_count > 0) + { + if (SUCCEED == zbx_thread_wait(zbx_thread_start(send_value, &sentdval_args))) + { + printf("sent: %d; skipped: %d; total: %d\n", succeed_count, (total_count - succeed_count), total_count); + } + else + { + printf("Sending failed. Use option -vv for more detailed output.\n"); + } + } + zbx_json_free(&sentdval_args.json); +exit: zabbix_close_log(); return ret; diff --git a/src/zabbix_server/nodewatcher/nodecomms.c b/src/zabbix_server/nodewatcher/nodecomms.c index 7f87befc..17e8151d 100644 --- a/src/zabbix_server/nodewatcher/nodecomms.c +++ b/src/zabbix_server/nodewatcher/nodecomms.c @@ -42,7 +42,7 @@ int connect_to_node(int nodeid, zbx_sock_t *sock) if (NULL != (row = DBfetch(result))) { port = (unsigned short)atoi(row[1]); - if (SUCCEED == zbx_tcp_connect(sock, row[0], port, 0)) + if (SUCCEED == zbx_tcp_connect(sock, CONFIG_SOURCE_IP, row[0], port, 0)) res = SUCCEED; else zabbix_log(LOG_LEVEL_ERR, "NODE %d: Unable to connect to Node [%d] error: %s", diff --git a/src/zabbix_server/nodewatcher/nodecomms.h b/src/zabbix_server/nodewatcher/nodecomms.h index 88303cf1..5ece09c1 100644 --- a/src/zabbix_server/nodewatcher/nodecomms.h +++ b/src/zabbix_server/nodewatcher/nodecomms.h @@ -22,6 +22,8 @@ #include "comms.h" +extern char *CONFIG_SOURCE_IP; + int connect_to_node(int nodeid, zbx_sock_t *sock); int send_data_to_node(int nodeid, zbx_sock_t *sock, const char *data); int recv_data_from_node(int nodeid, zbx_sock_t *sock, char **data); diff --git a/src/zabbix_server/poller/checks_agent.c b/src/zabbix_server/poller/checks_agent.c index b6238336..4f77afcb 100644 --- a/src/zabbix_server/poller/checks_agent.c +++ b/src/zabbix_server/poller/checks_agent.c @@ -61,7 +61,7 @@ int get_value_agent(DB_ITEM *item, AGENT_RESULT *result) addr, item->key); - if (SUCCEED == (ret = zbx_tcp_connect(&s, addr, item->port, 0))) { + if (SUCCEED == (ret = zbx_tcp_connect(&s, CONFIG_SOURCE_IP, addr, item->port, 0))) { zbx_snprintf(packet, sizeof(packet), "%s\n",item->key); zabbix_log(LOG_LEVEL_DEBUG, "Sending [%s]", packet); diff --git a/src/zabbix_server/poller/checks_agent.h b/src/zabbix_server/poller/checks_agent.h index efb3d915..3ad8c004 100644 --- a/src/zabbix_server/poller/checks_agent.h +++ b/src/zabbix_server/poller/checks_agent.h @@ -23,6 +23,8 @@ #include "db.h" #include "sysinfo.h" -extern int get_value_agent(DB_ITEM *item, AGENT_RESULT *result); +extern char *CONFIG_SOURCE_IP; + +int get_value_agent(DB_ITEM *item, AGENT_RESULT *result); #endif diff --git a/src/zabbix_server/server.c b/src/zabbix_server/server.c index 89982cf7..7a052b51 100644 --- a/src/zabbix_server/server.c +++ b/src/zabbix_server/server.c @@ -131,6 +131,7 @@ int CONFIG_ESCALATOR_FORKS = 1; int CONFIG_LISTEN_PORT = 10051; char *CONFIG_LISTEN_IP = NULL; +char *CONFIG_SOURCE_IP = NULL; int CONFIG_TRAPPER_TIMEOUT = ZABBIX_TRAPPER_TIMEOUT; /**/ /*int CONFIG_NOTIMEWAIT =0;*/ @@ -216,6 +217,7 @@ void init_config(void) {"UnavailableDelay",&CONFIG_UNAVAILABLE_DELAY,0,TYPE_INT,PARM_OPT,1,3600}, {"ListenIP",&CONFIG_LISTEN_IP,0,TYPE_STRING,PARM_OPT,0,0}, {"ListenPort",&CONFIG_LISTEN_PORT,0,TYPE_INT,PARM_OPT,1024,32768}, + {"SourceIP",&CONFIG_SOURCE_IP,0,TYPE_STRING,PARM_OPT,0,0}, /* {"NoTimeWait",&CONFIG_NOTIMEWAIT,0,TYPE_INT,PARM_OPT,0,1},*/ /* {"DisablePinger",&CONFIG_DISABLE_PINGER,0,TYPE_INT,PARM_OPT,0,1},*/ {"DisableHousekeeping",&CONFIG_DISABLE_HOUSEKEEPING,0,TYPE_INT,PARM_OPT,0,1}, diff --git a/src/zabbix_server/trapper/nodecommand.c b/src/zabbix_server/trapper/nodecommand.c index 7fbc645e..a815552c 100644 --- a/src/zabbix_server/trapper/nodecommand.c +++ b/src/zabbix_server/trapper/nodecommand.c @@ -33,6 +33,7 @@ #include <sys/socket.h> #include <errno.h> +#include "nodecommand.h" #include "comms.h" #include "common.h" #include "db.h" @@ -122,7 +123,7 @@ void send_script(int nodeid, const char *data, char **result, int *result_alloca nodeid); if (NULL != (dbrow = DBfetch(dbresult))) { - if (SUCCEED == zbx_tcp_connect(&sock, dbrow[0], atoi(dbrow[1]), 0)) { + if (SUCCEED == zbx_tcp_connect(&sock, CONFIG_SOURCE_IP, dbrow[0], atoi(dbrow[1]), 0)) { if (FAIL == zbx_tcp_send(&sock, data)) { zbx_snprintf_alloc(result, result_allocated, &result_offset, 128, "%d%cNODE %d: Error while sending data to Node [%d] error: %s", diff --git a/src/zabbix_server/trapper/nodecommand.h b/src/zabbix_server/trapper/nodecommand.h index 0c92e05b..bab783ac 100644 --- a/src/zabbix_server/trapper/nodecommand.h +++ b/src/zabbix_server/trapper/nodecommand.h @@ -21,6 +21,10 @@ #ifndef ZABBIX_NODECOMMAND_H #define ZABBIX_NODECOMMAND_H +#include "comms.h" + +extern char *CONFIG_SOURCE_IP; + int node_process_command(zbx_sock_t *sock, const char *data); #endif |