summaryrefslogtreecommitdiffstats
path: root/src/libs/zbxsysinfo/common
diff options
context:
space:
mode:
authorosmiy <osmiy@97f52cf1-0a1b-0410-bd0e-c28be96e8082>2007-04-14 05:55:37 +0000
committerosmiy <osmiy@97f52cf1-0a1b-0410-bd0e-c28be96e8082>2007-04-14 05:55:37 +0000
commit5ac1783eabb170049375d6cebd909c10a6523a73 (patch)
tree4b27eec49bce906d83911e64ff173171cc6bc979 /src/libs/zbxsysinfo/common
parenteb60de08877fe0e6f42e54949bb332e02caf86c1 (diff)
downloadzabbix-5ac1783eabb170049375d6cebd909c10a6523a73.tar.gz
zabbix-5ac1783eabb170049375d6cebd909c10a6523a73.tar.xz
zabbix-5ac1783eabb170049375d6cebd909c10a6523a73.zip
- new comms protocol for ZABBIX agents
git-svn-id: svn://svn.zabbix.com/trunk@4001 97f52cf1-0a1b-0410-bd0e-c28be96e8082
Diffstat (limited to 'src/libs/zbxsysinfo/common')
-rw-r--r--src/libs/zbxsysinfo/common/http.c91
-rw-r--r--src/libs/zbxsysinfo/common/net.c144
-rw-r--r--src/libs/zbxsysinfo/common/net.h2
3 files changed, 87 insertions, 150 deletions
diff --git a/src/libs/zbxsysinfo/common/http.c b/src/libs/zbxsysinfo/common/http.c
index 19fae063..6fd170b7 100644
--- a/src/libs/zbxsysinfo/common/http.c
+++ b/src/libs/zbxsysinfo/common/http.c
@@ -21,92 +21,69 @@
#include "sysinfo.h"
#include "log.h"
-#include "zbxsock.h"
+#include "comms.h"
#include "cfg.h"
#include "http.h"
-static int get_http_page(char *hostname, char *param, unsigned short port, char *buffer, int max_buf_len)
+#define ZABBIX_MAX_WEBPAGE_SIZE 1*1024*1024
+
+static int get_http_page(char *host, char *param, unsigned short port, char *buffer, int max_buf_len)
{
- register int i = 0;
- char request[MAX_STRING_LEN];
+ char
+ *buf,
+ request[MAX_STRING_LEN];
- ZBX_SOCKET s;
- ZBX_SOCKADDR servaddr_in;
- int
- n,
- total,
- ret = SYSINFO_RET_FAIL;
-
- struct hostent *hp;
+ zbx_sock_t s;
- if(NULL == (hp = zbx_gethost(hostname)) )
- {
- return SYSINFO_RET_FAIL;
- }
+ int ret;
- memset(&servaddr_in, 0, sizeof(ZBX_SOCKADDR));
+ assert(buffer);
- 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 = (ZBX_SOCKET)socket(AF_INET,SOCK_STREAM,0)))
+ if( SUCCEED == (ret = zbx_tcp_connect(&s, host, port)) )
{
- zabbix_log( LOG_LEVEL_DEBUG, "get_http_page - Error in socket() [%s:%u] [%s]", hostname, port, strerror_from_system(errno));
- return SYSINFO_RET_FAIL;
- }
+ zbx_snprintf(request, sizeof(request), "GET /%s HTTP/1.1\nHost: %s\nConnection: close\n\n", param, host);
- if(SOCKET_ERROR == connect(s,(struct sockaddr *)&servaddr_in,sizeof(ZBX_SOCKADDR)))
- {
- zabbix_log( LOG_LEVEL_WARNING, "get_http_page - Error in connect() [%s:%u] [%s]",hostname, port, strerror_from_system(errno));
- zbx_sock_close(s);
- return SYSINFO_RET_FAIL;
- }
+ if( SUCCEED == (ret = zbx_tcp_send_raw(&s, request)) )
+ {
+ if( SUCCEED == (ret = zbx_tcp_recv(&s, &buf)) )
+ {
+ zbx_rtrim(buf, "\n\r\0");
- zbx_snprintf(request, sizeof(request), "GET /%s HTTP/1.1\nHost: %s\nConnection: close\n\n", param, hostname);
+ zbx_snprintf(buffer, max_buf_len, buf);
+ }
+ }
+ }
+ zbx_tcp_close(&s);
- if(SOCKET_ERROR == zbx_sock_write(s, (void *)request, (int)strlen(request)))
+ if( FAIL == ret )
{
- zabbix_log( LOG_LEVEL_DEBUG, "get_http_page - Error during sending [%s:%u] [%s]",hostname, port, strerror_from_system(errno));
- zbx_sock_close(s);
+ zabbix_log(LOG_LEVEL_DEBUG, "HTTP get error: %s", zbx_tcp_strerror());
return SYSINFO_RET_FAIL;
}
-
- memset(buffer, 0, max_buf_len);
-
- for(total=0; (n = zbx_sock_read(s, buffer+total, max_buf_len-1-total, CONFIG_TIMEOUT)) > 0; total+=n);
-
- for(i=(int)strlen(buffer); i>0 && (buffer[i] == '\n' || buffer[i] == '\r' || buffer[i] == '\0'); buffer[i--] = '\0');
-
- ret = SYSINFO_RET_OK;
-
- zbx_sock_close(s);
- return ret;
+ return SYSINFO_RET_OK;
}
int WEB_PAGE_GET(const char *cmd, const char *param, unsigned flags, AGENT_RESULT *result)
{
-#define ZABBIX_MAX_WEBPAGE_SIZE 100*1024
char hostname[MAX_STRING_LEN];
char path[MAX_STRING_LEN];
char port_str[MAX_STRING_LEN];
char *buffer;
- assert(result);
+ assert(result);
- init_result(result);
-
- if(num_param(param) > 3)
- {
- return SYSINFO_RET_FAIL;
- }
+ init_result(result);
+
+ if(num_param(param) > 3)
+ {
+ return SYSINFO_RET_FAIL;
+ }
if(get_param(param, 1, hostname, MAX_STRING_LEN) != 0)
{
- return SYSINFO_RET_FAIL;
+ return SYSINFO_RET_FAIL;
}
if(get_param(param, 2, path, MAX_STRING_LEN) != 0)
@@ -140,7 +117,6 @@ int WEB_PAGE_GET(const char *cmd, const char *param, unsigned flags, AGENT_RESUL
int WEB_PAGE_PERF(const char *cmd, const char *param, unsigned flags, AGENT_RESULT *result)
{
-#define ZABBIX_MAX_WEBPAGE_SIZE 100*1024
char hostname[MAX_STRING_LEN];
char path[MAX_STRING_LEN];
char port_str[MAX_STRING_LEN];
@@ -196,7 +172,6 @@ int WEB_PAGE_PERF(const char *cmd, const char *param, unsigned flags, AGENT_RESU
int WEB_PAGE_REGEXP(const char *cmd, const char *param, unsigned flags, AGENT_RESULT *result)
{
-#define ZABBIX_MAX_WEBPAGE_SIZE 100*1024
char hostname[MAX_STRING_LEN];
char path[MAX_STRING_LEN];
char port_str[MAX_STRING_LEN];
diff --git a/src/libs/zbxsysinfo/common/net.c b/src/libs/zbxsysinfo/common/net.c
index 9f964c27..f2736ddd 100644
--- a/src/libs/zbxsysinfo/common/net.c
+++ b/src/libs/zbxsysinfo/common/net.c
@@ -20,7 +20,7 @@
#include "common.h"
#include "sysinfo.h"
-#include "zbxsock.h"
+#include "comms.h"
#include "log.h"
#include "cfg.h"
@@ -30,93 +30,55 @@
* 0 - NOT OK
* 1 - OK
* */
-int tcp_expect(const char *hostname, short port, const char *request, const char *expect, const char *sendtoclose, int *value_int)
+int tcp_expect(
+ const char *host,
+ unsigned short port,
+ const char *request,
+ const char *expect,
+ const char *sendtoclose,
+ int *value_int
+ )
{
- ZBX_SOCKET s;
- ZBX_SOCKADDR servaddr_in;
-
- struct hostent *hp;
-
- char buf[MAX_BUF_LEN];
-
- int len;
+ zbx_sock_t s;
+ char *buf;
+ int ret;
- assert(hostname);
assert(value_int);
*value_int = 0;
- if(NULL == (hp = zbx_gethost(hostname)) )
- {
- return SYSINFO_RET_OK;
- }
-
- memset(&servaddr_in, 0, sizeof(ZBX_SOCKADDR));
-
- 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 = (ZBX_SOCKET)socket(AF_INET,SOCK_STREAM,0)))
- {
- zabbix_log( LOG_LEVEL_DEBUG, "Error in socket() [%s:%u] [%s]", hostname, port, strerror_from_system(errno));
- return SYSINFO_RET_OK;
- }
-
- if(SOCKET_ERROR == connect(s,(struct sockaddr *)&servaddr_in,sizeof(ZBX_SOCKADDR)))
- {
- zabbix_log( LOG_LEVEL_DEBUG, "Error in connect() [%s:%u] [%s]",hostname, port, strerror_from_system(errno));
- zbx_sock_close(s);
- return SYSINFO_RET_OK;
- }
-
- if(NULL != request)
- {
- if(SOCKET_ERROR == zbx_sock_write(s, (void *)request, (int)strlen(request)))
- {
- zabbix_log( LOG_LEVEL_DEBUG, "Error during sending [%s:%u] [%s]",hostname, port, strerror_from_system(errno));
- zbx_sock_close(s);
- return SYSINFO_RET_OK;
- }
+ if( SUCCEED == (ret = zbx_tcp_connect(&s, host, port)) )
+ {
+ if( SUCCEED == (ret = zbx_tcp_send_raw(&s, request)) )
+ {
+ if( NULL != expect )
+ {
+ if( SUCCEED == (ret = zbx_tcp_recv(&s, &buf)) )
+ {
+ if( 0 == strncmp(buf, expect, strlen(expect)) )
+ {
+ *value_int = 1;
+ }
+ }
+ }
+ else
+ {
+ *value_int = 1;
+ }
+
+ if(SUCCEED == ret && NULL != sendtoclose)
+ {
+ /* ret = (skip errors) */ zbx_tcp_send_raw(&s, sendtoclose);
+ }
+ }
}
-
- if( NULL == expect)
- {
- zbx_sock_close(s);
- *value_int = 1;
- return SYSINFO_RET_OK;
- }
-
- memset(buf, 0, sizeof(buf));
-
- if(SOCKET_ERROR == (len = zbx_sock_read(s, buf, sizeof(buf)-1, CONFIG_TIMEOUT)))
- {
- zabbix_log( LOG_LEVEL_DEBUG, "Error in reading() [%s:%u] [%s]",hostname, port, strerror_from_system(errno));
- zbx_sock_close(s);
- return SYSINFO_RET_OK;
+ zbx_tcp_close(&s);
+
+ if( FAIL == ret )
+ {
+ zabbix_log(LOG_LEVEL_DEBUG, "TCP expect error: %s", zbx_tcp_strerror());
}
- buf[sizeof(buf)-1] = '\0';
-
- if( strncmp(buf, expect, strlen(expect)) == 0 )
- {
- *value_int = 1;
- }
- else
- {
- *value_int = 0;
- }
-
- if(NULL != sendtoclose)
- {
- if(SOCKET_ERROR == zbx_sock_write(s, (void *)sendtoclose, (int)strlen(sendtoclose)))
- {
- zabbix_log( LOG_LEVEL_DEBUG, "Error during close string sending [%s:%u] [%s]",hostname, port, strerror_from_system(errno));
- }
- }
-
- zbx_sock_close(s);
-
return SYSINFO_RET_OK;
}
@@ -181,16 +143,16 @@ int CHECK_PORT(const char *cmd, const char *param, unsigned flags, AGENT_RESULT
assert(result);
init_result(result);
-
- if(num_param(param) > 2)
- {
- return SYSINFO_RET_FAIL;
- }
+
+ if(num_param(param) > 2)
+ {
+ return SYSINFO_RET_FAIL;
+ }
if(get_param(param, 1, ip, MAX_STRING_LEN) != 0)
- {
- ip[0] = '\0';
- }
+ {
+ ip[0] = '\0';
+ }
if(ip[0] == '\0')
{
@@ -198,9 +160,9 @@ int CHECK_PORT(const char *cmd, const char *param, unsigned flags, AGENT_RESULT
}
if(get_param(param, 2, port_str, MAX_STRING_LEN) != 0)
- {
- port_str[0] = '\0';
- }
+ {
+ port_str[0] = '\0';
+ }
if(port_str[0] == '\0')
{
@@ -210,7 +172,7 @@ int CHECK_PORT(const char *cmd, const char *param, unsigned flags, AGENT_RESULT
port=atoi(port_str);
ret = tcp_expect(ip,port,NULL,NULL,"",&value_int);
-
+
if(ret == SYSINFO_RET_OK)
{
SET_UI64_RESULT(result, value_int);
diff --git a/src/libs/zbxsysinfo/common/net.h b/src/libs/zbxsysinfo/common/net.h
index 4452f80a..8de79d45 100644
--- a/src/libs/zbxsysinfo/common/net.h
+++ b/src/libs/zbxsysinfo/common/net.h
@@ -21,7 +21,7 @@
#include "sysinfo.h"
-int tcp_expect(const char *hostname, short port, const char *request, const char *expect, const char *sendtoclose, int *value_int);
+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);
int CHECK_DNS(const char *cmd, const char *param, unsigned flags, AGENT_RESULT *result);