~ubuntu-dev/ubuntu/lucid/zabbix/lucid-201002110857

« back to all changes in this revision

Viewing changes to src/zabbix_server/trapper/trapper.c

  • Committer: Bazaar Package Importer
  • Author(s): Michael Ablassmeier
  • Date: 2007-07-02 09:06:51 UTC
  • mfrom: (1.1.4 upstream)
  • Revision ID: james.westby@ubuntu.com-20070702090651-8l6fl3fjw9rh6l2u
Tags: 1:1.4.1-2
Add patch from SVN in order to fix Incorrect processing of character '%'
in user parameters and remote commands.

Show diffs side-by-side

added added

removed removed

Lines of Context:
17
17
** Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18
18
**/
19
19
 
20
 
#include "config.h"
21
 
 
22
 
#include <stdio.h>
23
 
#include <stdlib.h>
24
 
#include <sys/stat.h>
25
 
 
26
 
#include <string.h>
27
 
 
28
 
 
29
 
/* Required for getpwuid */
30
 
#include <pwd.h>
31
 
 
32
 
#include <signal.h>
33
 
#include <errno.h>
34
 
 
35
 
#include <sys/types.h>
36
 
#include <sys/socket.h>
37
 
 
38
 
#include <time.h>
39
 
/* getopt() */
40
 
#include <unistd.h>
 
20
#include "common.h"
41
21
 
42
22
#include "cfg.h"
 
23
#include "comms.h"
43
24
#include "pid.h"
44
25
#include "db.h"
45
26
#include "log.h"
46
27
#include "zlog.h"
47
28
 
48
 
#include "common.h"
49
29
#include "../functions.h"
50
30
#include "../expression.h"
51
31
 
52
 
#include "autoregister.h"
 
32
#include "nodesync.h"
 
33
#include "nodeevents.h"
 
34
#include "nodehistory.h"
53
35
#include "trapper.h"
54
 
 
55
 
extern int    send_list_of_active_checks(int sockfd, char *host);
56
 
 
57
 
int     process_trap(int sockfd,char *s, int max_len)
 
36
#include "active.h"
 
37
 
 
38
#include "daemon.h"
 
39
 
 
40
static int      process_trap(zbx_sock_t *sock,char *s, int max_len)
58
41
{
59
 
        char    *p,*line,*host;
 
42
        char    *line,*host;
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];
68
50
 
69
51
        int     ret=SUCCEED;
70
52
 
71
 
        for( p=s+strlen(s)-1; p>s && ( *p=='\r' || *p =='\n' || *p == ' ' ); --p );
72
 
        p[1]=0;
 
53
        zbx_rtrim(s, " \r\n\0");
73
54
 
74
 
        zabbix_log( LOG_LEVEL_DEBUG, "Trapper got [%s]", s);
 
55
        zabbix_log( LOG_LEVEL_DEBUG, "Trapper got [%s] len %d",
 
56
                s,
 
57
                strlen(s));
75
58
 
76
59
/* Request for list of active checks */
77
60
        if(strncmp(s,"ZBX_GET_ACTIVE_CHECKS", strlen("ZBX_GET_ACTIVE_CHECKS")) == 0)
84
67
                }
85
68
                else
86
69
                {
87
 
                        if(autoregister(host) == SUCCEED)
88
 
                        {
89
 
                                zabbix_log( LOG_LEVEL_DEBUG, "New host registered [%s]", host);
90
 
                        }
91
 
                        else
92
 
                        {
93
 
                                zabbix_log( LOG_LEVEL_DEBUG, "Host already exists [%s]", host);
94
 
                        }
95
 
                        ret=send_list_of_active_checks(sockfd, host);
 
70
                        ret = send_list_of_active_checks(sock, host);
96
71
                }
97
72
        }
98
73
/* Process information sent by zabbix_sender */
99
74
        else
100
75
        {
 
76
                /* Node data exchange? */
 
77
                if(strncmp(s,"Data",4) == 0)
 
78
                {
 
79
/*                      zabbix_log( LOG_LEVEL_WARNING, "Node data received [len:%d]", strlen(s)); */
 
80
                        if(node_sync(s) == SUCCEED)
 
81
                        {
 
82
                                if( zbx_tcp_send_raw(sock,"OK") != SUCCEED)
 
83
                                {
 
84
                                        zabbix_log( LOG_LEVEL_WARNING, "Error sending confirmation to node");
 
85
                                        zabbix_syslog("Trapper: error sending confirmation to node");
 
86
                                }
 
87
                        }
 
88
                        return ret;
 
89
                }
 
90
                /* Slave node events? */
 
91
                if(strncmp(s,"Events",6) == 0)
 
92
                {
 
93
/*                      zabbix_log( LOG_LEVEL_WARNING, "Slave node events received [len:%d]", strlen(s)); */
 
94
                        if(node_events(s) == SUCCEED)
 
95
                        {
 
96
                                if( zbx_tcp_send_raw(sock,"OK") != SUCCEED)
 
97
                                {
 
98
                                        zabbix_log( LOG_LEVEL_WARNING, "Error sending confirmation to node");
 
99
                                        zabbix_syslog("Trapper: error sending confirmation to node");
 
100
                                }
 
101
                        }
 
102
                        return ret;
 
103
                }
 
104
                /* Slave node history ? */
 
105
                if(strncmp(s,"History",7) == 0)
 
106
                {
 
107
/*                      zabbix_log( LOG_LEVEL_WARNING, "Slave node history received [len:%d]", strlen(s)); */
 
108
                        if(node_history(s) == SUCCEED)
 
109
                        {
 
110
                                if( zbx_tcp_send_raw(sock,"OK") != SUCCEED)
 
111
                                {
 
112
                                        zabbix_log( LOG_LEVEL_WARNING, "Error sending confirmation to node]");
 
113
                                        zabbix_syslog("Trapper: error sending confirmation to node");
 
114
                                }
 
115
                        }
 
116
                        return ret;
 
117
                }
101
118
                /* New XML protocol? */
102
 
                if(s[0]=='<')
 
119
                else if(s[0]=='<')
103
120
                {
104
121
                        zabbix_log( LOG_LEVEL_DEBUG, "XML received [%s]", s);
105
122
 
139
156
                        source[0]=0;
140
157
                        severity[0]=0;
141
158
                }
 
159
                zabbix_log( LOG_LEVEL_DEBUG, "Value [%s]", value_string);
142
160
 
143
 
                ret=process_data(sockfd,server,key,value_string,lastlogsize,timestamp,source,severity);
144
 
                if( SUCCEED == ret)
145
 
                {
146
 
                        snprintf(result,sizeof(result)-1,"OK\n");
147
 
                }
148
 
                else
149
 
                {
150
 
                        snprintf(result,sizeof(result)-1,"NOT OK\n");
151
 
                }
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)
156
 
                {
157
 
                        zabbix_log( LOG_LEVEL_WARNING, "Error sending result back [%s]",strerror(errno));
158
 
                        zabbix_syslog("Trapper: error sending result back [%s]",strerror(errno));
 
161
                DBbegin();
 
162
                ret=process_data(sock,server,key,value_string,lastlogsize,timestamp,source,severity);
 
163
                DBcommit();
 
164
                
 
165
                if( zbx_tcp_send_raw(sock, SUCCEED == ret ? "OK" : "NOT OK") != SUCCEED)
 
166
                {
 
167
                        zabbix_log( LOG_LEVEL_WARNING, "Error sending result back");
 
168
                        zabbix_syslog("Trapper: error sending result back");
159
169
                }
160
170
                zabbix_log( LOG_LEVEL_DEBUG, "After write()");
161
171
        }       
162
172
        return ret;
163
173
}
164
174
 
165
 
void    process_trapper_child(int sockfd)
 
175
void    process_trapper_child(zbx_sock_t *sock)
166
176
{
167
 
        ssize_t nbytes;
168
 
        char    buffer[MAX_BUF_LEN];
169
 
        static struct  sigaction phan;
170
 
        char    *bufptr;
171
 
 
172
 
        zabbix_log( LOG_LEVEL_DEBUG, "In process_trapper_child");
173
 
 
174
 
        phan.sa_handler = &signal_handler;
175
 
        sigemptyset(&phan.sa_mask);
176
 
        phan.sa_flags = 0;
177
 
        sigaction(SIGALRM, &phan, NULL);
178
 
 
179
 
        alarm(CONFIG_TIMEOUT);
180
 
 
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);
184
 
        bufptr = buffer;
185
 
        while ((nbytes = read(sockfd, bufptr, buffer + sizeof(buffer) - bufptr - 1)) != -1 && nbytes != 0)
186
 
        {
187
 
                zabbix_log( LOG_LEVEL_DEBUG, "Read %d bytes", nbytes);
188
 
                if(nbytes < buffer + sizeof(buffer) - bufptr - 1)
189
 
                {
190
 
                        bufptr += nbytes;
191
 
                        break;
192
 
                }
193
 
                bufptr += nbytes;
194
 
        }
195
 
 
196
 
        if(nbytes < 0)
197
 
        {
198
 
                if(errno == EINTR)
199
 
                {
200
 
                        zabbix_log( LOG_LEVEL_WARNING, "Read timeout");
201
 
                }
202
 
                else
203
 
                {
204
 
                        zabbix_log( LOG_LEVEL_WARNING, "read() failed");
205
 
                }
206
 
                alarm(0);
 
177
        char    *data;
 
178
 
 
179
/* suseconds_t is not defined under HP-UX */
 
180
/*      struct timeval tv;
 
181
        suseconds_t    msec;
 
182
        gettimeofday(&tv, NULL);
 
183
        msec = tv.tv_usec;*/
 
184
 
 
185
/*      alarm(CONFIG_TIMEOUT);*/
 
186
 
 
187
        if(zbx_tcp_recv(sock, &data) != SUCCEED)
 
188
        {
 
189
/*              alarm(0);*/
207
190
                return;
208
191
        }
209
192
 
210
 
        zabbix_log( LOG_LEVEL_DEBUG, "After read() 3 [%d]",nbytes);
211
 
 
212
 
        zabbix_log( LOG_LEVEL_DEBUG, "Got data:%s", buffer);
213
 
 
214
 
        process_trap(sockfd,buffer, MAX_BUF_LEN);
215
 
 
216
 
        alarm(0);
 
193
        process_trap(sock, data, sizeof(data));
 
194
/*      alarm(0);*/
 
195
 
 
196
/*      gettimeofday(&tv, NULL);
 
197
        zabbix_log( LOG_LEVEL_DEBUG, "Trap processed in " ZBX_FS_DBL " seconds",
 
198
                (double)(tv.tv_usec-msec)/1000000 );*/
217
199
}
218
200
 
219
 
void    child_trapper_main(int i,int listenfd, int addrlen)
 
201
void    child_trapper_main(int i, zbx_sock_t *s)
220
202
{
221
 
        int     connfd;
222
 
        socklen_t       clilen;
223
 
        struct sockaddr cliaddr;
224
 
 
225
 
        zabbix_log( LOG_LEVEL_DEBUG, "In child_main()");
226
 
 
227
 
/*      zabbix_log( LOG_LEVEL_WARNING, "zabbix_trapperd %ld started",(long)getpid());*/
 
203
        zabbix_log( LOG_LEVEL_DEBUG, "In child_trapper_main()");
 
204
 
228
205
        zabbix_log( LOG_LEVEL_WARNING, "server #%d started [Trapper]", i);
229
206
 
230
 
        zabbix_log( LOG_LEVEL_DEBUG, "Before DBconnect()");
231
 
        DBconnect();
232
 
        zabbix_log( LOG_LEVEL_DEBUG, "After DBconnect()");
 
207
        DBconnect(ZBX_DB_CONNECT_NORMAL);
233
208
 
234
209
        for(;;)
235
210
        {
236
 
                clilen = addrlen;
237
 
#ifdef HAVE_FUNCTION_SETPROCTITLE
238
 
                setproctitle("waiting for connection");
239
 
#endif
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");
245
 
#endif
246
 
 
247
 
                process_trapper_child(connfd);
248
 
 
249
 
                close(connfd);
 
211
                zbx_setproctitle("waiting for connection");
 
212
                zbx_tcp_accept(s);
 
213
 
 
214
                zbx_setproctitle("processing data");
 
215
                process_trapper_child(s);
 
216
 
 
217
                zbx_tcp_unaccept(s);
250
218
        }
251
219
        DBclose();
252
220
}
253
221
 
 
222
/*
254
223
pid_t   child_trapper_make(int i,int listenfd, int addrlen)
255
224
{
256
225
        pid_t   pid;
259
228
        {
260
229
                return (pid);
261
230
        }
262
 
        else
263
 
        {
264
 
                server_num=i;
265
 
        }
266
231
 
267
 
        /* never returns */
268
232
        child_trapper_main(i, listenfd, addrlen);
269
233
 
270
 
        /* avoid compilator warning */
271
234
        return 0;
272
 
}
 
235
}*/