diff options
Diffstat (limited to 'src/zabbix_server')
-rw-r--r-- | src/zabbix_server/actions.c | 14 | ||||
-rw-r--r-- | src/zabbix_server/discoverer/discoverer.c | 326 | ||||
-rw-r--r-- | src/zabbix_server/discoverer/discoverer.h | 8 | ||||
-rw-r--r-- | src/zabbix_server/trapper/proxydiscovery.c | 66 |
4 files changed, 240 insertions, 174 deletions
diff --git a/src/zabbix_server/actions.c b/src/zabbix_server/actions.c index a846bcf4..91b8cf40 100644 --- a/src/zabbix_server/actions.c +++ b/src/zabbix_server/actions.c @@ -415,7 +415,7 @@ static int check_action_condition(DB_EVENT *event, DB_CONDITION *condition) event->value, condition->value); value_int = atoi(condition->value); - if(event->object == EVENT_OBJECT_DHOST) +/* if(event->object == EVENT_OBJECT_DHOST) { result = DBselect("select status from dhosts where dhostid=" ZBX_FS_UI64, event->objectid); @@ -427,14 +427,16 @@ static int check_action_condition(DB_EVENT *event, DB_CONDITION *condition) } row = DBfetch(result); if(row && DBis_null(row[0]) != SUCCEED) - { + {*/ if(condition->operator == CONDITION_OPERATOR_EQUAL) { - if(value_int == atoi(row[0])) ret = SUCCEED; +/* if(value_int == atoi(row[0])) ret = SUCCEED;*/ + if(value_int == event->value) ret = SUCCEED; } else if(condition->operator == CONDITION_OPERATOR_NOT_EQUAL) { - if(atoi(row[0]) != value_int) ret = SUCCEED; +/* if(atoi(row[0]) != value_int) ret = SUCCEED;*/ + if(value_int != event->value) ret = SUCCEED; } else { @@ -442,8 +444,8 @@ static int check_action_condition(DB_EVENT *event, DB_CONDITION *condition) condition->operator, condition->conditionid); } - } - DBfree_result(result); +/* } + DBfree_result(result);*/ } else if(event->source == EVENT_SOURCE_DISCOVERY && (event->object == EVENT_OBJECT_DHOST || event->object == EVENT_OBJECT_DSERVICE) && diff --git a/src/zabbix_server/discoverer/discoverer.c b/src/zabbix_server/discoverer/discoverer.c index b06b7a15..beefd736 100644 --- a/src/zabbix_server/discoverer/discoverer.c +++ b/src/zabbix_server/discoverer/discoverer.c @@ -55,8 +55,6 @@ static void add_event(int object, zbx_uint64_t objectid, int value) { DB_EVENT event; - zabbix_log(LOG_LEVEL_DEBUG, "In add_event()"); - memset(&event, 0, sizeof(DB_EVENT)); event.eventid = 0; @@ -68,8 +66,6 @@ static void add_event(int object, zbx_uint64_t objectid, int value) event.acknowledged = 0; process_event(&event); - - zabbix_log(LOG_LEVEL_DEBUG, "End add_event()"); } /****************************************************************************** @@ -87,7 +83,7 @@ static void add_event(int object, zbx_uint64_t objectid, int value) * Comments: * * * ******************************************************************************/ -void update_dservice(DB_DSERVICE *service) +static void update_dservice(DB_DSERVICE *service) { char value_esc[MAX_STRING_LEN]; @@ -133,7 +129,7 @@ static void update_dservice_value(DB_DSERVICE *service) /****************************************************************************** * * - * Function: update_host * + * Function: update_dhost * * * * Purpose: update descovered host details * * * @@ -146,7 +142,7 @@ static void update_dservice_value(DB_DSERVICE *service) * Comments: * * * ******************************************************************************/ -void update_dhost(DB_DHOST *dhost) +static void update_dhost(DB_DHOST *dhost) { assert(dhost); @@ -172,48 +168,42 @@ void update_dhost(DB_DHOST *dhost) * Comments: * * * ******************************************************************************/ -void register_service(DB_DSERVICE *service, DB_DCHECK *check, const char *ip, int port) +static void register_service(DB_DSERVICE *service, const char *ip, int port, int status) { DB_RESULT result; DB_ROW row; char key_esc[MAX_STRING_LEN]; assert(service); - assert(check); assert(ip); zabbix_log(LOG_LEVEL_DEBUG, "In register_service(ip:%s,port:%d,key:%s)", ip, port, - check->key_); + service->key_); - DBescape_string(check->key_, key_esc, sizeof(key_esc)); + DBescape_string(service->key_, key_esc, sizeof(key_esc)); - result = DBselect("select dserviceid,status,lastup,lastdown,value,key_" + result = DBselect("select dserviceid,status,lastup,lastdown,value" " from dservices where dhostid=" ZBX_FS_UI64 " and type=%d and port=%d and key_='%s'", service->dhostid, - check->type, + service->type, port, key_esc); if (NULL == (row = DBfetch(result)) || DBis_null(row[0]) == SUCCEED) { /* Add host only if service is up */ - if (check->status == DOBJECT_STATUS_UP) { + if (status == DOBJECT_STATUS_UP) { zabbix_log(LOG_LEVEL_DEBUG, "New service discovered on port %d", port); service->dserviceid = DBget_maxid("dservices","dserviceid"); - service->type = check->type; service->port = port; service->status = DOBJECT_STATUS_DOWN; - service->lastup = 0; - service->lastdown = 0; - *service->value = '\0'; - strscpy(service->key_, check->key_); DBexecute("insert into dservices (dhostid,dserviceid,type,port,status,key_) values (" ZBX_FS_UI64 "," ZBX_FS_UI64 ",%d,%d,%d,'%s')", service->dhostid, service->dserviceid, - check->type, + service->type, service->port, service->status, key_esc); @@ -222,13 +212,11 @@ void register_service(DB_DSERVICE *service, DB_DCHECK *check, const char *ip, in zabbix_log(LOG_LEVEL_DEBUG, "Service is already in database"); service->dserviceid = zbx_atoui64(row[0]); - service->type = check->type; service->port = port; service->status = atoi(row[1]); service->lastup = atoi(row[2]); service->lastdown = atoi(row[3]); strscpy(service->value, row[4]); - strscpy(service->key_, row[5]); } DBfree_result(result); @@ -250,14 +238,13 @@ void register_service(DB_DSERVICE *service, DB_DCHECK *check, const char *ip, in * Comments: * * * ******************************************************************************/ -void register_host(DB_DHOST *dhost, DB_DCHECK *check, const char *ip) +void register_host(DB_DHOST *dhost, const char *ip, int status) { DB_RESULT result; DB_ROW row; char ip_esc[MAX_STRING_LEN]; assert(dhost); - assert(check); assert(ip); zabbix_log(LOG_LEVEL_DEBUG, "In register_host(ip:%s)", @@ -267,18 +254,17 @@ void register_host(DB_DHOST *dhost, DB_DCHECK *check, const char *ip) result = DBselect("select dhostid,ip,status,lastup,lastdown from dhosts" " where druleid=" ZBX_FS_UI64 " and ip='%s'" DB_NODE, - check->druleid, + dhost->druleid, ip_esc, DBnode_local("dhostid")); if (NULL == (row = DBfetch(result)) || DBis_null(row[0]) == SUCCEED) { /* Add host only if service is up */ - if (check->status == DOBJECT_STATUS_UP) { + if (status == DOBJECT_STATUS_UP) { zabbix_log(LOG_LEVEL_DEBUG, "New host discovered at %s", ip); dhost->dhostid = DBget_maxid("dhosts", "dhostid"); - dhost->druleid = check->druleid; dhost->status = DOBJECT_STATUS_DOWN; strscpy(dhost->ip, ip); @@ -292,7 +278,6 @@ void register_host(DB_DHOST *dhost, DB_DCHECK *check, const char *ip) ip); dhost->dhostid = zbx_atoui64(row[0]); - dhost->druleid = check->druleid; dhost->status = atoi(row[2]); dhost->lastup = atoi(row[3]); dhost->lastdown = atoi(row[4]); @@ -303,26 +288,98 @@ void register_host(DB_DHOST *dhost, DB_DCHECK *check, const char *ip) zabbix_log(LOG_LEVEL_DEBUG, "End register_host()"); } -static void proxy_resigter_dhistory(int now, zbx_uint64_t druleid, int type, const char *ip, int port, const char *key, const char *value, int status) +/****************************************************************************** + * * + * Function: update_service_status * + * * + * Purpose: process new service status * + * * + * Parameters: * + * * + * Return value: * + * * + * Author: Aleksander Vladishev * + * * + * Comments: * + * * + ******************************************************************************/ +static void update_service_status(DB_DSERVICE *service, DB_DCHECK *check, int now) { - char ip_esc[MAX_STRING_LEN], - key_esc[MAX_STRING_LEN], - value_esc[MAX_STRING_LEN]; + assert(service); + assert(check); - DBescape_string(ip, ip_esc, sizeof(ip_esc)); - DBescape_string(key, key_esc, sizeof(key_esc)); - DBescape_string(value, value_esc, sizeof(value_esc)); + /* Update service status */ + if (check->status == DOBJECT_STATUS_UP) { + if (service->status == DOBJECT_STATUS_DOWN || service->lastup == 0) { + service->status = check->status; + service->lastdown = 0; + service->lastup = now; + strcpy(service->value, check->value); - DBexecute("insert into proxy_dhistory (clock,druleid,type,ip,port,key_,value,status)" - " values (%d," ZBX_FS_UI64 ",%d,'%s',%d,'%s','%s',%d)", - now, - druleid, - type, - ip_esc, - port, - key_esc, - value_esc, - status); + update_dservice(service); + + add_event(EVENT_OBJECT_DSERVICE, service->dserviceid, DOBJECT_STATUS_DISCOVER); + } else if (0 != strcmp(service->value, check->value)) { + strcpy(service->value, check->value); + + update_dservice_value(service); + } + } else { /* DOBJECT_STATUS_DOWN */ + if (service->status == DOBJECT_STATUS_UP || service->lastdown == 0) { + service->status = check->status; + service->lastdown = now; + service->lastup = 0; + + update_dservice(service); + + add_event(EVENT_OBJECT_DSERVICE, service->dserviceid, DOBJECT_STATUS_LOST); + } + } + add_event(EVENT_OBJECT_DSERVICE, service->dserviceid, check->status); +} + +/****************************************************************************** + * * + * Function: update_host_status * + * * + * Purpose: update new host status * + * * + * Parameters: * + * * + * Return value: * + * * + * Author: Aleksander Vladishev * + * * + * Comments: * + * * + ******************************************************************************/ +void update_host_status(DB_DHOST *dhost, int status, int now) +{ + assert(dhost); + + /* Update host status */ + if (status == DOBJECT_STATUS_UP) { + if (dhost->status == DOBJECT_STATUS_DOWN || dhost->lastup == 0) { + dhost->status = status; + dhost->lastdown = 0; + dhost->lastup = now; + + update_dhost(dhost); + + add_event(EVENT_OBJECT_DHOST, dhost->dhostid, DOBJECT_STATUS_DISCOVER); + } + } else { /* DOBJECT_STATUS_DOWN */ + if (dhost->status == DOBJECT_STATUS_UP || dhost->lastdown == 0) { + dhost->status = status; + dhost->lastdown = now; + dhost->lastup = 0; + + update_dhost(dhost); + + add_event(EVENT_OBJECT_DHOST, dhost->dhostid, DOBJECT_STATUS_LOST); + } + } + add_event(EVENT_OBJECT_DHOST, dhost->dhostid, status); } /****************************************************************************** @@ -340,14 +397,11 @@ static void proxy_resigter_dhistory(int now, zbx_uint64_t druleid, int type, con * Comments: * * * ******************************************************************************/ -static void update_service(DB_DRULE *rule, DB_DHOST *dhost, int *host_status, DB_DCHECK *check, char *ip, int port) +void update_service(DB_DHOST *dhost, DB_DCHECK *check, char *ip, int port, int now) { - int now; DB_DSERVICE service; - assert(rule); assert(dhost); - assert(host_status); assert(check); assert(ip); @@ -356,59 +410,98 @@ static void update_service(DB_DRULE *rule, DB_DHOST *dhost, int *host_status, DB port, (check->status == DOBJECT_STATUS_UP ? "up" : "down")); - now = time(NULL); + memset(&service, 0, sizeof(service)); + + /* Register host if is not registered yet */ + if (dhost->dhostid == 0) + register_host(dhost, ip, check->status); - if (zbx_process == ZBX_PROCESS_SERVER) { - memset(&service, 0, sizeof(service)); + /* Register service if is not registered yet */ + if (dhost->dhostid > 0) { + service.dhostid = dhost->dhostid; + service.type = check->type; + strscpy(service.key_, check->key_); + register_service(&service, ip, port, check->status); + } - /* Register host if is not registered yet */ - if (dhost->dhostid == 0) - register_host(dhost, check, ip); + /* Service wasn't registered because we do not add down service */ + if (service.dserviceid == 0) + return; - /* Register service if is not registered yet */ - if (dhost->dhostid > 0) { - service.dhostid = dhost->dhostid; - register_service(&service, check, ip, port); - } + update_service_status(&service, check, now); - /* Service wasn't registered because we do not add down service */ - if (service.dserviceid == 0) - return; + zabbix_log(LOG_LEVEL_DEBUG, "End update_service()"); +} - /* Update host status */ - if (*host_status == -1 || check->status == DOBJECT_STATUS_UP) - *host_status = check->status; +/****************************************************************************** + * * + * Function: proxy_update_service * + * * + * Purpose: process new service status * + * * + * Parameters: service - service info * + * * + * Return value: * + * * + * Author: Aleksander Vladishev * + * * + * Comments: * + * * + ******************************************************************************/ +static void proxy_update_service(DB_DCHECK *check, char *ip, int port, int now) +{ + char ip_esc[MAX_STRING_LEN], + key_esc[MAX_STRING_LEN], + value_esc[MAX_STRING_LEN]; - /* Update service status */ - if (check->status == DOBJECT_STATUS_UP) { - if (service.status == DOBJECT_STATUS_DOWN || service.lastup == 0) { - service.status = DOBJECT_STATUS_UP; - service.lastdown = 0; - service.lastup = now; - strcpy(service.value, check->value); + assert(check); + assert(ip); - update_dservice(&service); + DBescape_string(ip, ip_esc, sizeof(ip_esc)); + DBescape_string(check->key_, key_esc, sizeof(key_esc)); + DBescape_string(check->value, value_esc, sizeof(value_esc)); - add_event(EVENT_OBJECT_DSERVICE, service.dserviceid, DOBJECT_STATUS_DISCOVER); - } else if (0 != strcmp(service.value, check->value)) { - strcpy(service.value, check->value); + DBexecute("insert into proxy_dhistory (clock,druleid,type,ip,port,key_,value,status)" + " values (%d," ZBX_FS_UI64 ",%d,'%s',%d,'%s','%s',%d)", + now, + check->druleid, + check->type, + ip_esc, + port, + key_esc, + value_esc, + check->status); +} - update_dservice_value(&service); - } - } else { /* DOBJECT_STATUS_DOWN */ - if (service.status == DOBJECT_STATUS_UP || service.lastdown == 0) { - service.status = DOBJECT_STATUS_DOWN; - service.lastdown = now; - service.lastup = 0; +/****************************************************************************** + * * + * Function: proxy_update_host * + * * + * Purpose: process new service status * + * * + * Parameters: service - service info * + * * + * Return value: * + * * + * Author: Aleksander Vladishev * + * * + * Comments: * + * * + ******************************************************************************/ +static void proxy_update_host(zbx_uint64_t druleid, char *ip, int status, int now) +{ + char ip_esc[MAX_STRING_LEN]; - update_dservice(&service); + assert(ip); - add_event(EVENT_OBJECT_DSERVICE, service.dserviceid, DOBJECT_STATUS_LOST); - } - } - add_event(EVENT_OBJECT_DSERVICE, service.dserviceid, check->status); - } else if (zbx_process == ZBX_PROCESS_PROXY) - proxy_resigter_dhistory(now, rule->druleid, check->type, ip, port, check->key_, check->value, check->status); + DBescape_string(ip, ip_esc, sizeof(ip_esc)); + + DBexecute("insert into proxy_dhistory (clock,druleid,type,ip,status)" + " values (%d," ZBX_FS_UI64 ",-1,'%s',%d)", + now, + druleid, + ip_esc, + status); } /****************************************************************************** @@ -607,7 +700,8 @@ static void process_check(DB_DRULE *rule, DB_DHOST *dhost, int *host_status, DB_ { int port, first, - last; + last, + now; char *curr_range = NULL, *next_range = NULL, *last_port = NULL; @@ -650,7 +744,21 @@ static void process_check(DB_DRULE *rule, DB_DHOST *dhost, int *host_status, DB_ for (port = first; port <= last; port++) { check->status = SUCCEED == discover_service(check, ip, port) ? DOBJECT_STATUS_UP : DOBJECT_STATUS_DOWN; - update_service(rule, dhost, host_status, check, ip, port); + + /* Update host status */ + if (*host_status == -1 || check->status == DOBJECT_STATUS_UP) + *host_status = check->status; + + now = time(NULL); + + switch (zbx_process) { + case ZBX_PROCESS_SERVER : + update_service(dhost, check, ip, port, now); + break; + case ZBX_PROCESS_PROXY : + proxy_update_service(check, ip, port, now); + break; + } } } @@ -812,30 +920,16 @@ static void process_rule(DB_DRULE *rule) } DBfree_result(result); - if (dhost.dhostid > 0) { - /* Update host status */ - if (host_status == DOBJECT_STATUS_UP) { - if (dhost.status == DOBJECT_STATUS_DOWN || dhost.lastup == 0) { - dhost.status = host_status; - dhost.lastdown = 0; - dhost.lastup = now; - - update_dhost(&dhost); - - add_event(EVENT_OBJECT_DHOST, dhost.dhostid, DOBJECT_STATUS_DISCOVER); - } - } else { /* DOBJECT_STATUS_DOWN */ - if (dhost.status == DOBJECT_STATUS_UP || dhost.lastdown == 0) { - dhost.status = host_status; - dhost.lastdown = now; - dhost.lastup = 0; - - update_dhost(&dhost); + switch (zbx_process) { + case ZBX_PROCESS_SERVER : + if (dhost.dhostid == 0) + break; - add_event(EVENT_OBJECT_DHOST, dhost.dhostid, DOBJECT_STATUS_LOST); - } - } - add_event(EVENT_OBJECT_DHOST, dhost.dhostid, host_status); + update_host_status(&dhost, host_status, now); + break; + case ZBX_PROCESS_PROXY : + proxy_update_host(rule->druleid, ip, host_status, now); + break; } } } diff --git a/src/zabbix_server/discoverer/discoverer.h b/src/zabbix_server/discoverer/discoverer.h index 8380ca50..a4de0af6 100644 --- a/src/zabbix_server/discoverer/discoverer.h +++ b/src/zabbix_server/discoverer/discoverer.h @@ -24,11 +24,9 @@ extern int CONFIG_DISCOVERER_FORKS; -void update_dhost(DB_DHOST *host); -void update_dservice(DB_DSERVICE *service); - -void register_host(DB_DHOST *host, DB_DCHECK *check, const char *ip); -void register_service(DB_DSERVICE *service, DB_DCHECK *check, const char *ip, int port); +void register_host(DB_DHOST *dhost, const char *ip, int status); +void update_host_status(DB_DHOST *dhost, int status, int now); +void update_service(DB_DHOST *dhost, DB_DCHECK *check, char *ip, int port, int now); void main_discoverer_loop(zbx_process_t p, int num); diff --git a/src/zabbix_server/trapper/proxydiscovery.c b/src/zabbix_server/trapper/proxydiscovery.c index eff1e0ce..2151432d 100644 --- a/src/zabbix_server/trapper/proxydiscovery.c +++ b/src/zabbix_server/trapper/proxydiscovery.c @@ -46,9 +46,8 @@ int process_discovery_data(zbx_sock_t *sock, struct zbx_json_parse *jp) { char tmp[MAX_STRING_LEN]; - DB_DCHECK check; - DB_DHOST host; - DB_DSERVICE service; + DB_DCHECK dcheck; + DB_DHOST dhost; struct zbx_json_parse jp_data, jp_row; int res = SUCCEED; @@ -78,14 +77,14 @@ int process_discovery_data(zbx_sock_t *sock, struct zbx_json_parse *jp) hosttime = atoi(tmp); if (SUCCEED == zbx_json_brackets_by_name(jp, ZBX_PROTO_TAG_DATA, &jp_data)) { + memset(&dhost, 0, sizeof(dhost)); + p = NULL; while (NULL != (p = zbx_json_next(&jp_data, p)) && SUCCEED == res) { if (FAIL == (res = zbx_json_brackets_open(p, &jp_row))) break; - memset(&host, 0, sizeof(host)); - memset(&check, 0, sizeof(check)); - memset(&service, 0, sizeof(service)); + memset(&dcheck, 0, sizeof(dcheck)); if (FAIL == zbx_json_value_by_name(&jp_row, ZBX_PROTO_TAG_CLOCK, tmp, sizeof(tmp))) goto json_parse_error; @@ -93,11 +92,11 @@ int process_discovery_data(zbx_sock_t *sock, struct zbx_json_parse *jp) if (FAIL == zbx_json_value_by_name(&jp_row, ZBX_PROTO_TAG_DRULE, tmp, sizeof(tmp))) goto json_parse_error; - check.druleid = zbx_atoui64(tmp); + dcheck.druleid = zbx_atoui64(tmp); if (FAIL == zbx_json_value_by_name(&jp_row, ZBX_PROTO_TAG_TYPE, tmp, sizeof(tmp))) goto json_parse_error; - check.type = atoi(tmp); + dcheck.type = atoi(tmp); if (FAIL == zbx_json_value_by_name(&jp_row, ZBX_PROTO_TAG_IP, ip, sizeof(ip))) goto json_parse_error; @@ -108,54 +107,27 @@ int process_discovery_data(zbx_sock_t *sock, struct zbx_json_parse *jp) if (FAIL == zbx_json_value_by_name(&jp_row, ZBX_PROTO_TAG_KEY, key_, sizeof(key_))) goto json_parse_error; - check.key_ = key_; + dcheck.key_ = key_; - if (FAIL == zbx_json_value_by_name(&jp_row, ZBX_PROTO_TAG_VALUE, check.value, sizeof(check.value))) + if (FAIL == zbx_json_value_by_name(&jp_row, ZBX_PROTO_TAG_VALUE, dcheck.value, sizeof(dcheck.value))) goto json_parse_error; if (FAIL == zbx_json_value_by_name(&jp_row, ZBX_PROTO_TAG_STATUS, tmp, sizeof(tmp))) goto json_parse_error; - check.status = atoi(tmp); + dcheck.status = atoi(tmp); - register_host(&host, &check, ip); + if (dcheck.type == -1) { + memset(&dhost, 0, sizeof(dhost)); + dhost.druleid = dcheck.druleid; - if (host.dhostid > 0) { - service.dhostid = host.dhostid; - register_service(&service, &check, ip, port); - } + register_host(&dhost, ip, dcheck.status); + update_host_status(&dhost, dcheck.status, itemtime); + } else { + memset(&dhost, 0, sizeof(dhost)); + dhost.druleid = dcheck.druleid; - if (service.dserviceid == 0) - continue; - - host.status = check.status; - service.status = check.status; - if (check.status == DOBJECT_STATUS_UP) { - /* Update host status */ - if (host.status == DOBJECT_STATUS_DOWN || host.lastup == 0) { - host.lastdown = 0; - host.lastup = itemtime; - update_dhost(&host); - } - /* Update service status */ - if (service.status == DOBJECT_STATUS_DOWN || service.lastup == 0) { - service.lastdown = 0; - service.lastup = itemtime; - update_dservice(&service); - } - } else { /* DOBJECT_STATUS_DOWN */ - if (host.status == DOBJECT_STATUS_UP || host.lastdown == 0) { - host.lastdown = itemtime; - host.lastup = 0; - update_dhost(&host); - } - /* Update service status */ - if (service.status == DOBJECT_STATUS_UP || service.lastdown == 0) { - service.lastdown = itemtime; - service.lastup = 0; - update_dservice(&service); - } + update_service(&dhost, &dcheck, ip, port, itemtime); } -/* add_service_event(&service);*/ continue; json_parse_error: |