diff options
| author | sasha <sasha@97f52cf1-0a1b-0410-bd0e-c28be96e8082> | 2008-03-30 19:41:18 +0000 |
|---|---|---|
| committer | sasha <sasha@97f52cf1-0a1b-0410-bd0e-c28be96e8082> | 2008-03-30 19:41:18 +0000 |
| commit | 8e3e630441eaf134f276b3dcd67dce2f0e044387 (patch) | |
| tree | 627860a10232ebe1caf4b496a132f52da553a9b1 /src | |
| parent | 5d1e6e58ae6245c3c2034ed7e35f95d45a73055a (diff) | |
| download | zabbix-8e3e630441eaf134f276b3dcd67dce2f0e044387.tar.gz zabbix-8e3e630441eaf134f276b3dcd67dce2f0e044387.tar.xz zabbix-8e3e630441eaf134f276b3dcd67dce2f0e044387.zip | |
- [DEV-15] autodiscovery by ICMP pings
git-svn-id: svn://svn.zabbix.com/trunk@5559 97f52cf1-0a1b-0410-bd0e-c28be96e8082
Diffstat (limited to 'src')
| -rw-r--r-- | src/libs/Makefile.am | 9 | ||||
| -rw-r--r-- | src/libs/zbxicmpping/Makefile.am | 6 | ||||
| -rw-r--r-- | src/libs/zbxicmpping/icmpping.c | 132 | ||||
| -rw-r--r-- | src/zabbix_proxy/Makefile.am | 1 | ||||
| -rw-r--r-- | src/zabbix_server/Makefile.am | 1 | ||||
| -rw-r--r-- | src/zabbix_server/discoverer/discoverer.c | 14 | ||||
| -rw-r--r-- | src/zabbix_server/pinger/pinger.c | 181 | ||||
| -rw-r--r-- | src/zabbix_server/pinger/pinger.h | 4 |
8 files changed, 205 insertions, 143 deletions
diff --git a/src/libs/Makefile.am b/src/libs/Makefile.am index 37be0906..69e297b3 100644 --- a/src/libs/Makefile.am +++ b/src/libs/Makefile.am @@ -17,14 +17,16 @@ DIST_SUBDIRS = \ zbxsys \ zbxjabber \ zbxjson \ - zbxserver + zbxserver \ + zbxicmpping if PROXY PROXY_SUBDIRS = \ zbxdb \ zbxdbcache \ zbxdbhigh \ - zbxserver + zbxserver \ + zbxicmpping endif if SERVER @@ -34,7 +36,8 @@ SERVER_SUBDIRS = \ zbxdbhigh \ zbxemail \ zbxsms \ - zbxserver + zbxserver \ + zbxicmpping endif if JABBER diff --git a/src/libs/zbxicmpping/Makefile.am b/src/libs/zbxicmpping/Makefile.am new file mode 100644 index 00000000..303c6d29 --- /dev/null +++ b/src/libs/zbxicmpping/Makefile.am @@ -0,0 +1,6 @@ +## Process this file with automake to produce Makefile.in + +noinst_LIBRARIES = libzbxicmpping.a + +libzbxicmpping_a_SOURCES = \ + icmpping.c diff --git a/src/libs/zbxicmpping/icmpping.c b/src/libs/zbxicmpping/icmpping.c new file mode 100644 index 00000000..cbe62502 --- /dev/null +++ b/src/libs/zbxicmpping/icmpping.c @@ -0,0 +1,132 @@ +/* +** ZABBIX +** Copyright (C) 2000-2005 SIA Zabbix +** +** This program is free software; you can redistribute it and/or modify +** it under the terms of the GNU General Public License as published by +** the Free Software Foundation; either version 2 of the License, or +** (at your option) any later version. +** +** This program is distributed in the hope that it will be useful, +** but WITHOUT ANY WARRANTY; without even the implied warranty of +** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +** GNU General Public License for more details. +** +** You should have received a copy of the GNU General Public License +** along with this program; if not, write to the Free Software +** Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. +**/ + +#include "zbxicmpping.h" +#include "threads.h" +#include "log.h" +#include "zlog.h" + +extern char *CONFIG_FPING_LOCATION; +#ifdef HAVE_IPV6 +extern char *CONFIG_FPING6_LOCATION; +#endif /* HAVE_IPV6 */ + +/****************************************************************************** + * * + * Function: do_ping * + * * + * Purpose: ping hosts listed in the host files * + * * + * Parameters: * + * * + * Return value: => 0 - successfully processed items * + * FAIL - otherwise * + * * + * Author: Alexei Vladishev * + * * + * Comments: use external binary 'fping' to avoid superuser priviledges * + * * + ******************************************************************************/ +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; + ZBX_FPING_HOST *host; + + zabbix_log(LOG_LEVEL_DEBUG, "In do_ping() [hosts_count:%d]", + hosts_count); + + zbx_snprintf(filename, sizeof(filename), "/tmp/zabbix_server_%li.pinger", + zbx_get_thread_id()); + + if (NULL == (f = fopen(filename, "w"))) { + zabbix_log(LOG_LEVEL_ERR, "Cannot open file [%s] [%s]", + filename, + strerror(errno)); + zabbix_syslog("Cannot open file [%s] [%s]", + filename, + strerror(errno)); + return FAIL; + } + + for (i = 0; i < hosts_count; i++) + fprintf(f, "%s\n", hosts[i].addr); + + fclose(f); + +#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); +#else /* HAVE_IPV6 */ + zbx_snprintf(tmp, sizeof(tmp), "%s -c3 2>/dev/null <%s", + CONFIG_FPING_LOCATION, + filename); +#endif /* HAVE_IPV6 */ + + if (0 == (f = popen(tmp, "r"))) { + zabbix_log(LOG_LEVEL_ERR, "Cannot execute [%s] [%s]", + CONFIG_FPING_LOCATION, + strerror(errno)); + zabbix_syslog("Cannot execute [%s] [%s]", + CONFIG_FPING_LOCATION, + strerror(errno)); + return FAIL; + } + + while (NULL != fgets(tmp, sizeof(tmp), f)) { + zabbix_log(LOG_LEVEL_DEBUG, "Update IP [%s]", + tmp); + + /* 12fc::21 : [0], 76 bytes, 0.39 ms (0.39 avg, 0% loss) */ + + host = NULL; + + if (NULL != (c = strchr(tmp, ' '))) { + *c = '\0'; + for (i = 0; i < hosts_count; i++) + if (0 == strcmp(tmp, hosts[i].addr)) { + host = &hosts[i]; + break; + } + } + + if (NULL != host) { + c++; + if (NULL != (c = strchr(c, '('))) { + c++; + host->alive = 1; + host->sec = atof(c)/1000; + } + } + } + pclose(f); + + unlink(filename); + + zabbix_log(LOG_LEVEL_DEBUG, "End of do_ping()"); + + return SUCCEED; +} + diff --git a/src/zabbix_proxy/Makefile.am b/src/zabbix_proxy/Makefile.am index 57b72d6d..1752a58c 100644 --- a/src/zabbix_proxy/Makefile.am +++ b/src/zabbix_proxy/Makefile.am @@ -49,6 +49,7 @@ zabbix_proxy_LDADD = \ $(top_srcdir)/src/libs/zbxcomms/libzbxcomms.a \ $(top_srcdir)/src/libs/zbxjson/libzbxjson.a \ $(top_srcdir)/src/libs/zbxserver/libzbxserver.a \ + $(top_srcdir)/src/libs/zbxicmpping/libzbxicmpping.a \ @PROXY_LIBS@ zabbix_proxy_LDFLAGS = \ diff --git a/src/zabbix_server/Makefile.am b/src/zabbix_server/Makefile.am index e5adeee5..9e2874fb 100644 --- a/src/zabbix_server/Makefile.am +++ b/src/zabbix_server/Makefile.am @@ -58,6 +58,7 @@ zabbix_server_LDADD = \ $(top_srcdir)/src/libs/zbxcomms/libzbxcomms.a \ $(top_srcdir)/src/libs/zbxjson/libzbxjson.a \ $(top_srcdir)/src/libs/zbxserver/libzbxserver.a \ + $(top_srcdir)/src/libs/zbxicmpping/libzbxicmpping.a \ $(JABBER_LDADD) \ @SERVER_LIBS@ diff --git a/src/zabbix_server/discoverer/discoverer.c b/src/zabbix_server/discoverer/discoverer.c index 57872b1e..20dfef88 100644 --- a/src/zabbix_server/discoverer/discoverer.c +++ b/src/zabbix_server/discoverer/discoverer.c @@ -24,6 +24,7 @@ #include "db.h" #include "log.h" #include "sysinfo.h" +#include "zbxicmpping.h" #include "daemon.h" #include "discoverer.h" @@ -525,6 +526,7 @@ static int discover_service(DB_DCHECK *check, char *ip, int port) char key[MAX_STRING_LEN]; AGENT_RESULT value; DB_ITEM item; + ZBX_FPING_HOST host; assert(check); assert(ip); @@ -585,6 +587,7 @@ static int discover_service(DB_DCHECK *check, char *ip, int port) case SVC_AGENT: case SVC_SNMPv1: case SVC_SNMPv2c: + case SVC_ICMPPING: break; default: ret = FAIL; @@ -659,6 +662,17 @@ static int discover_service(DB_DCHECK *check, char *ip, int port) } #endif break; + case SVC_ICMPPING: + memset(&host, 0, sizeof(host)); + strscpy(host.addr, ip); + host.useip = 1; + + if (SUCCEED == do_ping(&host, 1)) { + if (0 == host.alive) + ret = FAIL; + } else + ret = FAIL; + break; /* Simple checks */ default: if(process(key, 0, &value) == SUCCEED) diff --git a/src/zabbix_server/pinger/pinger.c b/src/zabbix_server/pinger/pinger.c index 0fd91fea..2b98cb48 100644 --- a/src/zabbix_server/pinger/pinger.c +++ b/src/zabbix_server/pinger/pinger.c @@ -24,19 +24,11 @@ #include "log.h" #include "zlog.h" #include "sysinfo.h" -#include "threads.h" #include "zbxserver.h" +#include "zbxicmpping.h" #include "pinger.h" -#define ZBX_FPING_HOST struct zbx_fipng_host -ZBX_FPING_HOST -{ - char addr[HOST_ADDR_LEN_MAX]; - int alive, useip; - double mseconds; -}; - static zbx_process_t zbx_process; static int pinger_num; @@ -58,7 +50,7 @@ static int pinger_num; * Comments: can be done in process_data() * * * ******************************************************************************/ -static int process_value(char *key, ZBX_FPING_HOST *host, AGENT_RESULT *value, int now, int *items) +static void process_value(char *key, ZBX_FPING_HOST *host, AGENT_RESULT *value, int now, int *items) { DB_RESULT result; DB_ROW row; @@ -102,8 +94,48 @@ static int process_value(char *key, ZBX_FPING_HOST *host, AGENT_RESULT *value, i (*items)++; } DBfree_result(result); +} + +/****************************************************************************** + * * + * Function: process_values * + * * + * Purpose: process new item value * + * * + * Parameters: * + * * + * Return value: successfully processed items * + * * + * Author: Alexei Vladishev * + * * + * Comments: can be done in process_data() * + * * + ******************************************************************************/ +static int process_values(ZBX_FPING_HOST *hosts, int hosts_count, int now) +{ + int i, items = 0; + AGENT_RESULT value; + + zabbix_log(LOG_LEVEL_DEBUG, "In process_values()"); + + for (i = 0; i < hosts_count; i++) { + zabbix_log(LOG_LEVEL_DEBUG, "Host [%s] alive [%d] " ZBX_FS_DBL " sec.", + hosts[i].addr, + hosts[i].alive, + hosts[i].sec); + + init_result(&value); + SET_UI64_RESULT(&value, hosts[i].alive); + process_value(SERVER_ICMPPING_KEY, &hosts[i], &value, now, &items); + free_result(&value); + + init_result(&value); + SET_DBL_RESULT(&value, hosts[i].sec); + process_value(SERVER_ICMPPINGSEC_KEY, &hosts[i], &value, now, &items); + free_result(&value); + } - return SUCCEED; + return items; } /****************************************************************************** @@ -198,130 +230,6 @@ static int get_pinger_hosts(ZBX_FPING_HOST **hosts, int *hosts_allocated, int no return hosts_count; } - -/****************************************************************************** - * * - * Function: do_ping * - * * - * Purpose: ping hosts listed in the host files * - * * - * Parameters: * - * * - * Return value: => 0 - successfully processed items * - * FAIL - otherwise * - * * - * Author: Alexei Vladishev * - * * - * Comments: use external binary 'fping' to avoid superuser priviledges * - * * - ******************************************************************************/ -static int do_ping(ZBX_FPING_HOST *hosts, int hosts_count, int now) -{ - FILE *f; - char filename[MAX_STRING_LEN]; - char tmp[MAX_STRING_LEN]; - int i, items = 0; - char *c; - ZBX_FPING_HOST *host; - AGENT_RESULT value; - - zabbix_log(LOG_LEVEL_DEBUG, "In do_ping() [hosts_count:%d]", - hosts_count); - - zbx_snprintf(filename, sizeof(filename), "/tmp/zabbix_server_%li.pinger", - zbx_get_thread_id()); - - if (NULL == (f = fopen(filename, "w"))) { - zabbix_log(LOG_LEVEL_ERR, "Cannot open file [%s] [%s]", - filename, - strerror(errno)); - zabbix_syslog("Cannot open file [%s] [%s]", - filename, - strerror(errno)); - return FAIL; - } - - for (i = 0; i < hosts_count; i++) - fprintf(f, "%s\n", hosts[i].addr); - - fclose(f); - -#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); -#else /* HAVE_IPV6 */ - zbx_snprintf(tmp, sizeof(tmp), "%s -c3 2>/dev/null <%s", - CONFIG_FPING_LOCATION, - filename); -#endif /* HAVE_IPV6 */ - - if (0 == (f = popen(tmp, "r"))) { - zabbix_log(LOG_LEVEL_ERR, "Cannot execute [%s] [%s]", - CONFIG_FPING_LOCATION, - strerror(errno)); - zabbix_syslog("Cannot execute [%s] [%s]", - CONFIG_FPING_LOCATION, - strerror(errno)); - return FAIL; - } - - while (NULL != fgets(tmp, sizeof(tmp), f)) { - zabbix_log(LOG_LEVEL_DEBUG, "Update IP [%s]", - tmp); - - /* 12fc::21 : [0], 76 bytes, 0.39 ms (0.39 avg, 0% loss) */ - - host = NULL; - - if (NULL != (c = strchr(tmp, ' '))) { - *c = '\0'; - for (i = 0; i < hosts_count; i++) - if (0 == strcmp(tmp, hosts[i].addr)) { - host = &hosts[i]; - break; - } - } - - if (NULL != host) { - c++; - if (NULL != (c = strchr(c, '('))) { - c++; - host->alive = 1; - host->mseconds = atof(c); - } - } - } - pclose(f); - - unlink(filename); - - items = 0; - - for (i = 0; i < hosts_count; i++) { - zabbix_log(LOG_LEVEL_DEBUG, "Host [%s] alive [%d] " ZBX_FS_DBL " ms", - hosts[i].addr, - hosts[i].alive, - hosts[i].mseconds); - - init_result(&value); - SET_UI64_RESULT(&value, hosts[i].alive); - process_value(SERVER_ICMPPING_KEY, &hosts[i], &value, now, &items); - free_result(&value); - - init_result(&value); - SET_DBL_RESULT(&value, hosts[i].mseconds/1000); - process_value(SERVER_ICMPPINGSEC_KEY, &hosts[i], &value, now, &items); - free_result(&value); - } - - zabbix_log(LOG_LEVEL_DEBUG, "End of do_ping()"); - - return items; -} - /****************************************************************************** * * * Function: get_minnextcheck * @@ -419,7 +327,8 @@ void main_pinger_loop(zbx_process_t p, int num) if (0 < (hosts_count = get_pinger_hosts(&hosts, &hosts_allocated, now))) { zbx_setproctitle("pinger [pinging hosts]"); - items = do_ping(hosts, hosts_count, now); + if (SUCCEED == do_ping(hosts, hosts_count)) + items = process_values(hosts, hosts_count, now); } sec = zbx_time() - sec; diff --git a/src/zabbix_server/pinger/pinger.h b/src/zabbix_server/pinger/pinger.h index 105be634..429c0b34 100644 --- a/src/zabbix_server/pinger/pinger.h +++ b/src/zabbix_server/pinger/pinger.h @@ -24,10 +24,6 @@ extern int CONFIG_PINGER_FORKS; extern int CONFIG_PINGER_FREQUENCY; -extern char *CONFIG_FPING_LOCATION; -#ifdef HAVE_IPV6 -extern char *CONFIG_FPING6_LOCATION; -#endif /* HAVE_IPV6 */ void main_pinger_loop(zbx_process_t p, int num); |
