diff options
Diffstat (limited to 'src/zabbix_proxy')
-rw-r--r-- | src/zabbix_proxy/datasender/datasender.c | 128 | ||||
-rw-r--r-- | src/zabbix_proxy/events.c | 4 | ||||
-rw-r--r-- | src/zabbix_proxy/proxy.c | 40 | ||||
-rw-r--r-- | src/zabbix_proxy/servercomms.c | 26 | ||||
-rw-r--r-- | src/zabbix_proxy/servercomms.h | 6 |
5 files changed, 145 insertions, 59 deletions
diff --git a/src/zabbix_proxy/datasender/datasender.c b/src/zabbix_proxy/datasender/datasender.c index 55c7afdb..e6fbce22 100644 --- a/src/zabbix_proxy/datasender/datasender.c +++ b/src/zabbix_proxy/datasender/datasender.c @@ -40,7 +40,12 @@ struct history_table_t { ZBX_HISTORY_FIELD fields[ZBX_MAX_FIELDS]; }; -ZBX_HISTORY_TABLE ht[]={ +struct last_ids { + const char *lastfieldname; + zbx_uint64_t lastid; +}; + +static ZBX_HISTORY_TABLE ht[]={ {"history_sync", "history_lastid", { {"clock", ZBX_PROTO_TAG_CLOCK}, @@ -82,7 +87,7 @@ ZBX_HISTORY_TABLE ht[]={ {NULL} }; -ZBX_HISTORY_TABLE dht[]={ +static ZBX_HISTORY_TABLE dht[]={ {"proxy_dhistory", "dhistory_lastid", { {"clock", ZBX_PROTO_TAG_CLOCK}, @@ -99,11 +104,6 @@ ZBX_HISTORY_TABLE dht[]={ {NULL} }; -struct last_ids { - const char *lastfieldname; - zbx_uint64_t lastid; -}; - #define ZBX_SENDER_TABLE_COUNT 6 /****************************************************************************** @@ -214,6 +214,8 @@ static int get_history_data(struct zbx_json *j, const ZBX_HISTORY_TABLE *ht, zbx zbx_json_close(j); } + DBfree_result(result); + return records; } @@ -292,12 +294,14 @@ static int get_dhistory_data(struct zbx_json *j, const ZBX_HISTORY_TABLE *ht, zb zbx_json_close(j); } + DBfree_result(result); + return records; } /****************************************************************************** * * - * Function: main_datasender * + * Function: history_sender * * * * Purpose: * * * @@ -310,28 +314,27 @@ static int get_dhistory_data(struct zbx_json *j, const ZBX_HISTORY_TABLE *ht, zb * Comments: never returns * * * ******************************************************************************/ -static int main_datasender() +static int history_sender(struct zbx_json *j) { - struct zbx_json j; int i, r, records = 0; zbx_sock_t sock; - char *answer, buf[11]; /* strlen("4294967296") + 1 */ + char buf[11]; /* strlen("4294967296") + 1 */ zbx_uint64_t lastid; struct last_ids li[ZBX_SENDER_TABLE_COUNT]; int li_no = 0; - zabbix_log(LOG_LEVEL_DEBUG, "In main_datasender()"); + zabbix_log(LOG_LEVEL_DEBUG, "In history_sender()"); - if (FAIL == connect_to_server(&sock, 60)) /* alarm !!! */ + if (FAIL == connect_to_server(&sock, 600)) /* alarm !!! */ return FAIL; - zbx_json_init(&j, 512*1024); - zbx_json_addstring(&j, ZBX_PROTO_TAG_REQUEST, ZBX_PROTO_VALUE_SENDER_DATA, ZBX_JSON_TYPE_STRING); - zbx_json_addstring(&j, ZBX_PROTO_TAG_PROXY, CONFIG_HOSTNAME, ZBX_JSON_TYPE_STRING); + zbx_json_clean(j); + zbx_json_addstring(j, ZBX_PROTO_TAG_REQUEST, ZBX_PROTO_VALUE_HISTORY_DATA, ZBX_JSON_TYPE_STRING); + zbx_json_addstring(j, ZBX_PROTO_TAG_PROXY, CONFIG_HOSTNAME, ZBX_JSON_TYPE_STRING); - zbx_json_addarray(&j, ZBX_PROTO_TAG_DATA); + zbx_json_addarray(j, ZBX_PROTO_TAG_DATA); for (i = 0; ht[i].table != NULL; i ++) { - if (0 == (r = get_history_data(&j, &ht[i], &lastid))) + if (0 == (r = get_history_data(j, &ht[i], &lastid))) continue; li[li_no].lastfieldname = ht[i].lastfieldname; @@ -340,11 +343,64 @@ static int main_datasender() records += r; } - zbx_json_close(&j); /* array */ + zbx_json_close(j); + + zbx_snprintf(buf, sizeof(buf), "%d", (int)time(NULL)); + + zbx_json_addstring(j, ZBX_PROTO_TAG_CLOCK, buf, ZBX_JSON_TYPE_INT); + + if (SUCCEED == put_data_to_server(&sock, j)) { + DBbegin(); + + for (i = 0; i < li_no; i++) { + DBexecute("update proxies set %s=" ZBX_FS_UI64, + li[i].lastfieldname, + li[i].lastid); + } + DBcommit(); + } + + disconnect_server(&sock); + + return records; +} + +/****************************************************************************** + * * + * Function: dhistory_sender * + * * + * Purpose: * + * * + * Parameters: * + * * + * Return value: * + * * + * Author: Aleksander Vladishev * + * * + * Comments: never returns * + * * + ******************************************************************************/ +static int dhistory_sender(struct zbx_json *j) +{ + int i, r, records = 0; + zbx_sock_t sock; + char buf[11]; /* strlen("4294967296") + 1 */ + zbx_uint64_t lastid; + struct last_ids li[ZBX_SENDER_TABLE_COUNT]; + int li_no = 0; - zbx_json_addarray(&j, ZBX_PROTO_TAG_DISCOVERY_DATA); + zabbix_log(LOG_LEVEL_DEBUG, "In dhistory_sender()"); + + if (FAIL == connect_to_server(&sock, 600)) /* alarm !!! */ + return FAIL; + + zbx_json_clean(j); + zbx_json_addstring(j, ZBX_PROTO_TAG_REQUEST, ZBX_PROTO_VALUE_DISCOVERY_DATA, ZBX_JSON_TYPE_STRING); + zbx_json_addstring(j, ZBX_PROTO_TAG_PROXY, CONFIG_HOSTNAME, ZBX_JSON_TYPE_STRING); + + zbx_json_addarray(j, ZBX_PROTO_TAG_DATA); for (i = 0; dht[i].table != NULL; i ++) { - if (0 == (r = get_dhistory_data(&j, &dht[i], &lastid))) + if (0 == (r = get_dhistory_data(j, &dht[i], &lastid))) continue; li[li_no].lastfieldname = dht[i].lastfieldname; @@ -353,13 +409,13 @@ static int main_datasender() records += r; } - zbx_json_close(&j); /* array */ + zbx_json_close(j); zbx_snprintf(buf, sizeof(buf), "%d", (int)time(NULL)); - zbx_json_addstring(&j, ZBX_PROTO_TAG_CLOCK, buf, ZBX_JSON_TYPE_INT); + zbx_json_addstring(j, ZBX_PROTO_TAG_CLOCK, buf, ZBX_JSON_TYPE_INT); - if (SUCCEED == put_data_to_server(&sock, &j, &answer)) { + if (SUCCEED == put_data_to_server(&sock, j)) { DBbegin(); for (i = 0; i < li_no; i++) { @@ -367,12 +423,9 @@ static int main_datasender() li[i].lastfieldname, li[i].lastid); } - DBcommit(); } - zbx_json_free(&j); - disconnect_server(&sock); return records; @@ -399,6 +452,7 @@ int main_datasender_loop() int now, sleeptime, records; double sec; + struct zbx_json j; zabbix_log(LOG_LEVEL_DEBUG, "In main_datasender_loop()"); @@ -407,24 +461,26 @@ int main_datasender_loop() phan.sa_flags = 0; sigaction(SIGALRM, &phan, NULL); + zbx_setproctitle("data sender [connecting to the database]"); + + DBconnect(ZBX_DB_CONNECT_NORMAL); + + zbx_json_init(&j, 16*1024); + for (;;) { now = time(NULL); sec = zbx_time(); - zbx_setproctitle("data sender [connecting to the database]"); - - DBconnect(ZBX_DB_CONNECT_NORMAL); - zbx_setproctitle("data sender [sending data]"); - records = main_datasender(); + records = 0; + records += history_sender(&j); + records += dhistory_sender(&j); zabbix_log(LOG_LEVEL_DEBUG, "Datasender spent " ZBX_FS_DBL " seconds while processing %3d values.", zbx_time() - sec, records); - DBclose(); - sleeptime = CONFIG_DATASENDER_FREQUENCY - (time(NULL) - now); if (sleeptime > 0) { @@ -435,4 +491,8 @@ int main_datasender_loop() sleep(sleeptime); } } + + zbx_json_free(&j); + + DBclose(); } diff --git a/src/zabbix_proxy/events.c b/src/zabbix_proxy/events.c index 20c2c90a..febaa962 100644 --- a/src/zabbix_proxy/events.c +++ b/src/zabbix_proxy/events.c @@ -39,7 +39,7 @@ ******************************************************************************/ int process_event(DB_EVENT *event) { - zabbix_log(LOG_LEVEL_DEBUG,"In [proxy]process_event(eventid:" ZBX_FS_UI64 ",object:%d,objectid:" ZBX_FS_UI64 ")", +/* zabbix_log(LOG_LEVEL_DEBUG,"In [proxy]process_event(eventid:" ZBX_FS_UI64 ",object:%d,objectid:" ZBX_FS_UI64 ")", event->eventid, event->object, event->objectid); @@ -57,6 +57,6 @@ int process_event(DB_EVENT *event) event->value); zabbix_log(LOG_LEVEL_DEBUG, "End of [proxy]process_event()"); - +*/ return SUCCEED; } diff --git a/src/zabbix_proxy/proxy.c b/src/zabbix_proxy/proxy.c index 080a8b77..d3d1ba95 100644 --- a/src/zabbix_proxy/proxy.c +++ b/src/zabbix_proxy/proxy.c @@ -128,8 +128,8 @@ int CONFIG_TRAPPER_TIMEOUT = TRAPPER_TIMEOUT; /*int CONFIG_NOTIMEWAIT = 0;*/ int CONFIG_DISABLE_HOUSEKEEPING = 0; int CONFIG_HOUSEKEEPING_FREQUENCY = 1; /* h */ -int CONFIG_PROXY_LOCAL_BUFFER = 24; /* 24h */ -int CONFIG_PROXY_OFFLINE_BUFFER = 720; /* 720h */ +int CONFIG_PROXY_LOCAL_BUFFER = 0; /* 24h */ +int CONFIG_PROXY_OFFLINE_BUFFER = 1; /* 720h */ int CONFIG_PROXYCONFIG_FREQUENCY = 120; @@ -157,7 +157,7 @@ int CONFIG_DBPORT = 0; int CONFIG_ENABLE_REMOTE_COMMANDS = 0; char *CONFIG_SERVER = NULL; -int CONFIG_SERVER_PORT = 10050; +int CONFIG_SERVER_PORT = 10051; char *CONFIG_HOSTNAME = NULL; int CONFIG_NODEID = -1; int CONFIG_MASTER_NODEID = 0; @@ -192,12 +192,15 @@ ZBX_MUTEX node_sync_access; ******************************************************************************/ void init_config(void) { + AGENT_RESULT result; + char **value = NULL; + static struct cfg_line cfg[]= { /* PARAMETER ,VAR ,FUNC, TYPE(0i,1s),MANDATORY,MIN,MAX */ {"Server",&CONFIG_SERVER,0,TYPE_STRING,PARM_MAND,0,0}, - {"ServerPort",&CONFIG_SERVER_PORT,0,TYPE_INT,PARM_MAND,1024,32768}, - {"Hostname",&CONFIG_HOSTNAME,0,TYPE_STRING,PARM_MAND,0,0}, + {"ServerPort",&CONFIG_SERVER_PORT,0,TYPE_INT,PARM_OPT,1024,32768}, + {"Hostname",&CONFIG_HOSTNAME,0,TYPE_STRING,PARM_OPT,0,0}, {"StartDiscoverers",&CONFIG_DISCOVERER_FORKS,0,TYPE_INT,PARM_OPT,0,255}, {"StartHTTPPollers",&CONFIG_HTTPPOLLER_FORKS,0,TYPE_INT,PARM_OPT,0,255}, @@ -206,12 +209,13 @@ void init_config(void) {"StartPollersUnreachable",&CONFIG_UNREACHABLE_POLLER_FORKS,0,TYPE_INT,PARM_OPT,0,255}, {"StartTrappers",&CONFIG_TRAPPERD_FORKS,0,TYPE_INT,PARM_OPT,0,255}, - {"DisableHousekeeping",&CONFIG_DISABLE_HOUSEKEEPING,0,TYPE_INT,PARM_OPT,0,1}, {"HousekeepingFrequency",&CONFIG_HOUSEKEEPING_FREQUENCY,0,TYPE_INT,PARM_OPT,1,24}, - {"ProxyLocalBuffer",&CONFIG_PROXY_LOCAL_BUFFER,0,TYPE_INT,PARM_OPT,1,720}, + {"ProxyLocalBuffer",&CONFIG_PROXY_LOCAL_BUFFER,0,TYPE_INT,PARM_OPT,0,720}, {"ProxyOfflineBuffer",&CONFIG_PROXY_OFFLINE_BUFFER,0,TYPE_INT,PARM_OPT,1,720}, - {"SenderFrequency",&CONFIG_SENDER_FREQUENCY,0,TYPE_INT,PARM_OPT,5,3600}, + {"DataSenderFrequency",&CONFIG_DATASENDER_FREQUENCY,0,TYPE_INT,PARM_OPT,1,3600}, + +/* {"SenderFrequency",&CONFIG_SENDER_FREQUENCY,0,TYPE_INT,PARM_OPT,5,3600},*/ {"PingerFrequency",&CONFIG_PINGER_FREQUENCY,0,TYPE_INT,PARM_OPT,1,3600}, {"FpingLocation",&CONFIG_FPING_LOCATION,0,TYPE_STRING,PARM_OPT,0,0}, #ifdef HAVE_IPV6 @@ -230,7 +234,7 @@ void init_config(void) {"PidFile",&APP_PID_FILE,0,TYPE_STRING,PARM_OPT,0,0}, {"LogFile",&CONFIG_LOG_FILE,0,TYPE_STRING,PARM_OPT,0,0}, {"LogFileSize",&CONFIG_LOG_FILE_SIZE,0,TYPE_INT,PARM_OPT,0,1024}, - {"AlertScriptsPath",&CONFIG_ALERT_SCRIPTS_PATH,0,TYPE_STRING,PARM_OPT,0,0}, +/* {"AlertScriptsPath",&CONFIG_ALERT_SCRIPTS_PATH,0,TYPE_STRING,PARM_OPT,0,0},*/ {"ExternalScripts",&CONFIG_EXTERNALSCRIPTS,0,TYPE_STRING,PARM_OPT,0,0}, {"DBHost",&CONFIG_DBHOST,0,TYPE_STRING,PARM_OPT,0,0}, {"DBName",&CONFIG_DBNAME,0,TYPE_STRING,PARM_MAND,0,0}, @@ -243,9 +247,25 @@ void init_config(void) CONFIG_SERVER_STARTUP_TIME = time(NULL); - parse_cfg_file(CONFIG_FILE,cfg); + if(CONFIG_HOSTNAME == NULL) + { + if(SUCCEED == process("system.hostname", 0, &result)) + { + if( NULL != (value = GET_STR_RESULT(&result)) ) + { + CONFIG_HOSTNAME = strdup(*value); + } + } + free_result(&result); + + if(CONFIG_HOSTNAME == NULL) + { + zabbix_log( LOG_LEVEL_CRIT, "Hostname is not defined"); + exit(1); + } + } if(CONFIG_DBNAME == NULL) { zabbix_log( LOG_LEVEL_CRIT, "DBName not in config file"); diff --git a/src/zabbix_proxy/servercomms.c b/src/zabbix_proxy/servercomms.c index 2adee6fa..e0651bfa 100644 --- a/src/zabbix_proxy/servercomms.c +++ b/src/zabbix_proxy/servercomms.c @@ -95,7 +95,7 @@ void disconnect_server(zbx_sock_t *sock) * Comments: * * * ******************************************************************************/ -int get_data_from_server(zbx_sock_t *sock, /*const char *name, */const char *request, char **data) +int get_data_from_server(zbx_sock_t *sock, const char *request, char **data) { int ret = FAIL; struct zbx_json j; @@ -113,9 +113,6 @@ int get_data_from_server(zbx_sock_t *sock, /*const char *name, */const char *req if (FAIL == recv_data_from_server(sock, data)) goto exit; -/* zabbix_log (LOG_LEVEL_WARNING, "Received %s from server", - name);*/ - ret = SUCCEED; exit: zbx_json_free(&j); @@ -139,9 +136,11 @@ exit: * Comments: * * * ******************************************************************************/ -int put_data_to_server(zbx_sock_t *sock, /*const char *name, */struct zbx_json *j, char **answer) +int put_data_to_server(zbx_sock_t *sock, struct zbx_json *j) { - int ret = FAIL; + struct zbx_json_parse jp; + int ret = FAIL; + char *answer, value[MAX_STRING_LEN]; zabbix_log(LOG_LEVEL_DEBUG, "In put_data_to_server() [datalen:%zd]", j->buffer_size); @@ -149,13 +148,22 @@ int put_data_to_server(zbx_sock_t *sock, /*const char *name, */struct zbx_json * if (FAIL == send_data_to_server(sock, j->buffer)) goto exit; - if (FAIL == recv_data_from_server(sock, answer)) + if (FAIL == recv_data_from_server(sock, &answer)) + goto exit; + + if (FAIL == zbx_json_open(answer, &jp)) goto exit; -/* zabbix_log(LOG_LEVEL_WARNING, "Received %s from server", - name);*/ + if (FAIL == zbx_json_value_by_name(&jp, ZBX_PROTO_TAG_RESPONSE, value, sizeof(value))) + goto exit; + + if (0 != strcmp(value, ZBX_PROTO_VALUE_SUCCESS)) + goto exit; ret = SUCCEED; exit: + zabbix_log(LOG_LEVEL_DEBUG, "End of put_data_to_server():%s", + ret == SUCCEED ? "SUCCEED" : "FAIL"); + return ret; } diff --git a/src/zabbix_proxy/servercomms.h b/src/zabbix_proxy/servercomms.h index 04cc72f1..10974fb5 100644 --- a/src/zabbix_proxy/servercomms.h +++ b/src/zabbix_proxy/servercomms.h @@ -27,11 +27,9 @@ extern char *CONFIG_HOSTNAME; #include "comms.h" int connect_to_server(zbx_sock_t *sock, int timeout); -/*int send_data_to_server(zbx_sock_t *sock, const char *data); -int recv_data_from_server(zbx_sock_t *sock, char **data);*/ void disconnect_server(zbx_sock_t *sock); -int get_data_from_server(zbx_sock_t *sock, /*const char *name, */const char *request, char **data); -int put_data_to_server(zbx_sock_t *sock, /*const char *name, */struct zbx_json *j, char **answer); +int get_data_from_server(zbx_sock_t *sock, const char *request, char **data); +int put_data_to_server(zbx_sock_t *sock, struct zbx_json *j); #endif |