17
17
** Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
29
/* Required for getpwuid */
35
#include <sys/types.h>
36
#include <sys/socket.h>
49
29
#include "../functions.h"
50
30
#include "../expression.h"
52
#include "autoregister.h"
33
#include "nodeevents.h"
34
#include "nodehistory.h"
53
35
#include "trapper.h"
55
extern int send_list_of_active_checks(int sockfd, char *host);
57
int process_trap(int sockfd,char *s, int max_len)
40
static int process_trap(zbx_sock_t *sock,char *s, int max_len)
60
43
char *server,*key,*value_string;
61
44
char copy[MAX_STRING_LEN];
62
char result[MAX_STRING_LEN];
63
45
char host_dec[MAX_STRING_LEN],key_dec[MAX_STRING_LEN],value_dec[MAX_STRING_LEN];
64
46
char lastlogsize[MAX_STRING_LEN];
65
47
char timestamp[MAX_STRING_LEN];
87
if(autoregister(host) == SUCCEED)
89
zabbix_log( LOG_LEVEL_DEBUG, "New host registered [%s]", host);
93
zabbix_log( LOG_LEVEL_DEBUG, "Host already exists [%s]", host);
95
ret=send_list_of_active_checks(sockfd, host);
70
ret = send_list_of_active_checks(sock, host);
98
73
/* Process information sent by zabbix_sender */
76
/* Node data exchange? */
77
if(strncmp(s,"Data",4) == 0)
79
/* zabbix_log( LOG_LEVEL_WARNING, "Node data received [len:%d]", strlen(s)); */
80
if(node_sync(s) == SUCCEED)
82
if( zbx_tcp_send_raw(sock,"OK") != SUCCEED)
84
zabbix_log( LOG_LEVEL_WARNING, "Error sending confirmation to node");
85
zabbix_syslog("Trapper: error sending confirmation to node");
90
/* Slave node events? */
91
if(strncmp(s,"Events",6) == 0)
93
/* zabbix_log( LOG_LEVEL_WARNING, "Slave node events received [len:%d]", strlen(s)); */
94
if(node_events(s) == SUCCEED)
96
if( zbx_tcp_send_raw(sock,"OK") != SUCCEED)
98
zabbix_log( LOG_LEVEL_WARNING, "Error sending confirmation to node");
99
zabbix_syslog("Trapper: error sending confirmation to node");
104
/* Slave node history ? */
105
if(strncmp(s,"History",7) == 0)
107
/* zabbix_log( LOG_LEVEL_WARNING, "Slave node history received [len:%d]", strlen(s)); */
108
if(node_history(s) == SUCCEED)
110
if( zbx_tcp_send_raw(sock,"OK") != SUCCEED)
112
zabbix_log( LOG_LEVEL_WARNING, "Error sending confirmation to node]");
113
zabbix_syslog("Trapper: error sending confirmation to node");
101
118
/* New XML protocol? */
104
121
zabbix_log( LOG_LEVEL_DEBUG, "XML received [%s]", s);
159
zabbix_log( LOG_LEVEL_DEBUG, "Value [%s]", value_string);
143
ret=process_data(sockfd,server,key,value_string,lastlogsize,timestamp,source,severity);
146
snprintf(result,sizeof(result)-1,"OK\n");
150
snprintf(result,sizeof(result)-1,"NOT OK\n");
152
zabbix_log( LOG_LEVEL_DEBUG, "Sending back [%s]", result);
153
zabbix_log( LOG_LEVEL_DEBUG, "Length [%d]", strlen(result));
154
zabbix_log( LOG_LEVEL_DEBUG, "Sockfd [%d]", sockfd);
155
if( write(sockfd,result,strlen(result)) == -1)
157
zabbix_log( LOG_LEVEL_WARNING, "Error sending result back [%s]",strerror(errno));
158
zabbix_syslog("Trapper: error sending result back [%s]",strerror(errno));
162
ret=process_data(sock,server,key,value_string,lastlogsize,timestamp,source,severity);
165
if( zbx_tcp_send_raw(sock, SUCCEED == ret ? "OK" : "NOT OK") != SUCCEED)
167
zabbix_log( LOG_LEVEL_WARNING, "Error sending result back");
168
zabbix_syslog("Trapper: error sending result back");
160
170
zabbix_log( LOG_LEVEL_DEBUG, "After write()");
165
void process_trapper_child(int sockfd)
175
void process_trapper_child(zbx_sock_t *sock)
168
char buffer[MAX_BUF_LEN];
169
static struct sigaction phan;
172
zabbix_log( LOG_LEVEL_DEBUG, "In process_trapper_child");
174
phan.sa_handler = &signal_handler;
175
sigemptyset(&phan.sa_mask);
177
sigaction(SIGALRM, &phan, NULL);
179
alarm(CONFIG_TIMEOUT);
181
zabbix_log( LOG_LEVEL_DEBUG, "Before read(%d)", MAX_BUF_LEN);
182
/* if( (nbytes = read(sockfd, line, MAX_BUF_LEN)) < 0)*/
183
memset(buffer,0,MAX_BUF_LEN);
185
while ((nbytes = read(sockfd, bufptr, buffer + sizeof(buffer) - bufptr - 1)) != -1 && nbytes != 0)
187
zabbix_log( LOG_LEVEL_DEBUG, "Read %d bytes", nbytes);
188
if(nbytes < buffer + sizeof(buffer) - bufptr - 1)
200
zabbix_log( LOG_LEVEL_WARNING, "Read timeout");
204
zabbix_log( LOG_LEVEL_WARNING, "read() failed");
179
/* suseconds_t is not defined under HP-UX */
180
/* struct timeval tv;
182
gettimeofday(&tv, NULL);
185
/* alarm(CONFIG_TIMEOUT);*/
187
if(zbx_tcp_recv(sock, &data) != SUCCEED)
210
zabbix_log( LOG_LEVEL_DEBUG, "After read() 3 [%d]",nbytes);
212
zabbix_log( LOG_LEVEL_DEBUG, "Got data:%s", buffer);
214
process_trap(sockfd,buffer, MAX_BUF_LEN);
193
process_trap(sock, data, sizeof(data));
196
/* gettimeofday(&tv, NULL);
197
zabbix_log( LOG_LEVEL_DEBUG, "Trap processed in " ZBX_FS_DBL " seconds",
198
(double)(tv.tv_usec-msec)/1000000 );*/
219
void child_trapper_main(int i,int listenfd, int addrlen)
201
void child_trapper_main(int i, zbx_sock_t *s)
223
struct sockaddr cliaddr;
225
zabbix_log( LOG_LEVEL_DEBUG, "In child_main()");
227
/* zabbix_log( LOG_LEVEL_WARNING, "zabbix_trapperd %ld started",(long)getpid());*/
203
zabbix_log( LOG_LEVEL_DEBUG, "In child_trapper_main()");
228
205
zabbix_log( LOG_LEVEL_WARNING, "server #%d started [Trapper]", i);
230
zabbix_log( LOG_LEVEL_DEBUG, "Before DBconnect()");
232
zabbix_log( LOG_LEVEL_DEBUG, "After DBconnect()");
207
DBconnect(ZBX_DB_CONNECT_NORMAL);
237
#ifdef HAVE_FUNCTION_SETPROCTITLE
238
setproctitle("waiting for connection");
240
zabbix_log( LOG_LEVEL_DEBUG, "Before accept()");
241
connfd=accept(listenfd,&cliaddr, &clilen);
242
zabbix_log( LOG_LEVEL_DEBUG, "After accept()");
243
#ifdef HAVE_FUNCTION_SETPROCTITLE
244
setproctitle("processing data");
247
process_trapper_child(connfd);
211
zbx_setproctitle("waiting for connection");
214
zbx_setproctitle("processing data");
215
process_trapper_child(s);
254
223
pid_t child_trapper_make(int i,int listenfd, int addrlen)