# | Line 1 | Line 1 | |
---|---|---|
1 | /* | |
2 | * i-scream central monitoring system | |
3 | + | * http://www.i-scream.org.uk |
4 | * Copyright (C) 2000-2002 i-scream | |
5 | * | |
6 | * This program is free software; you can redistribute it and/or | |
# | Line 17 | Line 18 | |
18 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. | |
19 | */ | |
20 | ||
21 | + | #ifdef HAVE_CONFIG_H |
22 | + | #include "config.h" |
23 | + | #endif |
24 | + | |
25 | #include <stdio.h> | |
26 | #include <stdlib.h> | |
22 | – | #include <sys/types.h> |
27 | #include <unistd.h> | |
24 | – | #include <syslog.h> |
25 | – | #include <netinet/in.h> |
26 | – | #include "ukcprog.h" |
27 | – | #include <netdb.h> |
28 | #include <string.h> | |
29 | < | #include "statgrab.h" |
30 | < | #include <time.h> |
29 | > | #include <sys/types.h> |
30 | #include <sys/socket.h> | |
31 | < | #include <netinet/in.h> |
32 | < | #include <arpa/inet.h> |
33 | < | #include <syslog.h> |
31 | > | #include <stdarg.h> |
32 | > | #include <errno.h> |
33 | > | #include <netdb.h> |
34 | ||
35 | < | #define versionNo 0.8 |
36 | < | #define RECONFIGURE_RETURN_CODE 2 |
38 | < | #define UDP_MAX_PACKET_SIZE 8192 |
39 | < | #define PID_FILE "/var/tmp/.ihost.pid" |
35 | > | #include <ukcprog.h> |
36 | > | #include <statgrab.h> |
37 | ||
38 | < | #define logmessage(level, ...) do { cur_level = level; errf(__VA_ARGS__); } while (0) |
38 | > | #define LOG_CRIT 0 |
39 | > | #define LOG_ERR 1 |
40 | > | #define LOG_INFO 2 |
41 | > | #define LOG_DEBUG 3 |
42 | ||
43 | typedef struct{ | |
44 | < | int fm_port; |
45 | < | char *fm_host; |
44 | > | int filtermanager_port; |
45 | > | char *filtermanager_host; |
46 | ||
47 | < | char *my_ip; |
48 | < | char *my_fqdn; |
47 | > | char *host_ip; |
48 | > | char *host_fqdn; |
49 | > | |
50 | char *server_fqdn; | |
51 | int server_udp_port; | |
52 | < | int server_tcp_port; |
52 | > | |
53 | > | /* Weird stuff iscream wants sent to it */ |
54 | char *last_modified; | |
55 | < | char *files_list; |
56 | < | char *key; |
55 | > | char *file_list; |
56 | > | |
57 | int udp_update_time; | |
58 | < | int tcp_update_time; |
58 | > | // int config_ttl; |
59 | ||
60 | + | time_t config_ttl; |
61 | + | |
62 | }ihost_state_t; | |
63 | ||
64 | < | static int log_level; |
65 | < | static int cur_level; |
66 | < | static int syslog_logging; |
64 | > | typedef struct{ |
65 | > | int verbose; |
66 | > | int daemon; |
67 | ||
68 | < | void log_errors(const char *message){ |
69 | < | if(log_level>=cur_level){ |
70 | < | if (syslog_logging==1){ |
71 | < | syslog(cur_level, "%s\n", message); |
68 | > | FILE *log; |
69 | > | }ihost_config_t; |
70 | > | |
71 | > | typedef struct{ |
72 | > | struct sockaddr_in addr; |
73 | > | int sock; |
74 | > | }udp_sockinfo_t; |
75 | > | |
76 | > | ihost_config_t ihost_config; |
77 | > | |
78 | > | extern int errno; |
79 | > | |
80 | > | /* Taken from the OpenSSH code. Its licence included in function.*/ |
81 | > | #ifndef HAVE_STRLCAT |
82 | > | |
83 | > | /* |
84 | > | * Copyright (c) 1998 Todd C. Miller <Todd.Miller@courtesan.com> |
85 | > | * All rights reserved. |
86 | > | * |
87 | > | * Redistribution and use in source and binary forms, with or without |
88 | > | * modification, are permitted provided that the following conditions |
89 | > | * are met: |
90 | > | * 1. Redistributions of source code must retain the above copyright |
91 | > | * notice, this list of conditions and the following disclaimer. |
92 | > | * 2. Redistributions in binary form must reproduce the above copyright |
93 | > | * notice, this list of conditions and the following disclaimer in the |
94 | > | * documentation and/or other materials provided with the distribution. |
95 | > | * 3. The name of the author may not be used to endorse or promote products |
96 | > | * derived from this software without specific prior written permission. |
97 | > | * |
98 | > | * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, |
99 | > | * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY |
100 | > | * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL |
101 | > | * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
102 | > | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
103 | > | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; |
104 | > | * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
105 | > | * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR |
106 | > | * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF |
107 | > | * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
108 | > | */ |
109 | > | |
110 | > | /* |
111 | > | * Appends src to string dst of size siz (unlike strncat, siz is the |
112 | > | * full size of dst, not space left). At most siz-1 characters |
113 | > | * will be copied. Always NUL terminates (unless siz <= strlen(dst)). |
114 | > | * Returns strlen(src) + MIN(siz, strlen(initial dst)). |
115 | > | * If retval >= siz, truncation occurred. |
116 | > | */ |
117 | > | size_t |
118 | > | strlcat(dst, src, siz) |
119 | > | char *dst; |
120 | > | const char *src; |
121 | > | size_t siz; |
122 | > | { |
123 | > | register char *d = dst; |
124 | > | register const char *s = src; |
125 | > | register size_t n = siz; |
126 | > | size_t dlen; |
127 | > | |
128 | > | /* Find the end of dst and adjust bytes left but don't go past end */ |
129 | > | while (n-- != 0 && *d != '\0') |
130 | > | d++; |
131 | > | dlen = d - dst; |
132 | > | n = siz - dlen; |
133 | > | |
134 | > | if (n == 0) |
135 | > | return(dlen + strlen(s)); |
136 | > | while (*s != '\0') { |
137 | > | if (n != 1) { |
138 | > | *d++ = *s; |
139 | > | n--; |
140 | > | } |
141 | > | s++; |
142 | > | } |
143 | > | *d = '\0'; |
144 | > | |
145 | > | return(dlen + (s - src)); /* count does not include NUL */ |
146 | > | } |
147 | > | |
148 | > | #endif |
149 | > | /* End strlcat function taken from OpenSSH */ |
150 | > | |
151 | > | void log_msg(int level, char *format, ...){ |
152 | > | int cur_errno; |
153 | > | va_list ap; |
154 | > | |
155 | > | cur_errno=errno; |
156 | > | |
157 | > | if(level<=ihost_config.verbose){ |
158 | > | va_start(ap, format); |
159 | > | vfprintf(ihost_config.log, format, ap); |
160 | > | va_end(ap); |
161 | > | if(level==LOG_CRIT){ |
162 | > | fprintf(ihost_config.log, " (%s)\n", strerror(cur_errno)); |
163 | }else{ | |
164 | < | fprintf(stderr, "%s\n", message); |
164 | > | fprintf(ihost_config.log, "\n"); |
165 | } | |
166 | < | } |
166 | > | fflush(ihost_config.log); |
167 | > | } |
168 | } | |
169 | ||
170 | < | char* sock_comm(FILE *f_r, FILE *f_w, char *sendString){ |
171 | < | char *reply; |
172 | < | fprintf(f_w, "%s\n", sendString); |
173 | < | fflush(f_w); |
174 | < | reply=fpgetline(f_r); |
175 | < | /* Returns pointer to static buffer */ |
80 | < | return reply; |
81 | < | } |
170 | > | /* Takes many pointers, checks if they are NULL or not, and then free's them */ |
171 | > | /* Deprciated - and i only wrote it today! :) |
172 | > | void m_free(int num_pointers, ...){ |
173 | > | int x=0; |
174 | > | va_list ap; |
175 | > | void *p; |
176 | ||
177 | < | int ihost_configure(ihost_state_t *ihost_state){ |
178 | < | struct sockaddr_in addr; |
179 | < | struct in_addr haddr; |
180 | < | struct sockaddr ip; |
181 | < | int ip_len; |
182 | < | int sd; |
183 | < | FILE *fm_fd_r, *fm_fd_w; |
184 | < | char *reply; |
185 | < | char *reply_ptr; |
177 | > | va_start(ap, num_pointers); |
178 | > | for(;x<num_pointers;x++){ |
179 | > | p=va_arg(ap, void*); |
180 | > | if(p!=NULL){ |
181 | > | free(p); |
182 | > | } |
183 | > | } |
184 | > | va_end(ap); |
185 | > | } |
186 | > | */ |
187 | ||
188 | < | /* Check to see if anything needs to be free'd */ |
94 | < | if (ihost_state->my_fqdn!=NULL) free(ihost_state->my_fqdn); |
95 | < | if (ihost_state->server_fqdn!=NULL) free(ihost_state->server_fqdn); |
96 | < | if (ihost_state->last_modified!=NULL) free(ihost_state->last_modified); |
97 | < | if (ihost_state->files_list!=NULL) free(ihost_state->files_list); |
188 | > | int create_udp_sockinfo(udp_sockinfo_t *udp_sockinfo, char *hostname, int port){ |
189 | ||
190 | < | logmessage(LOG_DEBUG, "Setting up configure socket to %s on port %d", ihost_state->fm_host, ihost_state->fm_port); |
100 | < | if ((sd = socket(AF_INET, SOCK_STREAM, PF_UNSPEC)) < 0) { |
101 | < | logmessage(LOG_ERR, "Can't create AF_INET socket (%m)"); |
102 | < | return -1; |
103 | < | } |
190 | > | struct in_addr haddr; |
191 | ||
192 | < | if (get_host_addr(ihost_state->fm_host, &haddr) != 0){ |
193 | < | logmessage(LOG_ERR, "Failed to resolve address %s (%m)", ihost_state->fm_host); |
194 | < | return -1; |
192 | > | log_msg(LOG_DEBUG, "Resolving name for udp connection"); |
193 | > | if(get_host_addr(hostname, &haddr) != 0){ |
194 | > | log_msg(LOG_CRIT, "Failed to lookup name"); |
195 | > | return 1; |
196 | } | |
197 | ||
198 | < | memset(&addr, 0, sizeof addr); |
199 | < | addr.sin_family = AF_INET; |
200 | < | memcpy(&addr.sin_addr, &haddr, sizeof haddr); |
201 | < | addr.sin_port = htons(ihost_state->fm_port); |
198 | > | if((udp_sockinfo->sock=socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0){ |
199 | > | log_msg(LOG_CRIT, "Failed to create UDP socket"); |
200 | > | return 1; |
201 | > | } |
202 | ||
203 | < | if (connect(sd, (struct sockaddr *)&addr, sizeof addr) != 0) { |
204 | < | logmessage(LOG_ERR, "Failed to connect to %s on port %d (%m)", ihost_state->fm_host, ihost_state->fm_port); |
205 | < | return -1; |
203 | > | memset(&(udp_sockinfo->addr), 0, sizeof(struct sockaddr_in)); |
204 | > | udp_sockinfo->addr.sin_family=AF_INET; |
205 | > | memcpy((char *)&(udp_sockinfo->addr.sin_addr), &haddr, sizeof(haddr)); |
206 | > | udp_sockinfo->addr.sin_port = htons(port); |
207 | > | |
208 | > | log_msg(LOG_DEBUG, "Socket created"); |
209 | > | return 0; |
210 | > | } |
211 | > | |
212 | > | FILE *create_tcp_connection(char *hostname, int port){ |
213 | > | int sock; |
214 | > | struct sockaddr_in addr; |
215 | > | struct in_addr haddr; |
216 | > | FILE *f; |
217 | > | |
218 | > | log_msg(LOG_DEBUG, "Creating tcp socket"); |
219 | > | if((sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP))<0){ |
220 | > | log_msg(LOG_CRIT, "Failed to make TCP Socket"); |
221 | > | return NULL; |
222 | } | |
223 | ||
224 | < | /* Need to open 2 files, one for reading one for writing, as it gets confused if we only use 1 :) */ |
225 | < | if ((fm_fd_r=fdopen(sd,"r")) == NULL){ |
226 | < | logmessage(LOG_ERR, "Failed to open read stream (%m)"); |
227 | < | return -1; |
224 | > | if((get_host_addr(hostname, &haddr))!=0){ |
225 | > | log_msg(LOG_CRIT, "Failed to lookup name for %s", hostname); |
226 | > | close(sock); |
227 | > | return NULL; |
228 | } | |
229 | ||
230 | < | if ((fm_fd_w=fdopen(dup(sd),"w")) == NULL){ |
231 | < | logmessage(LOG_ERR, "Failed to open write stream (%m)"); |
232 | < | return -1; |
230 | > | memset(&addr, 0, sizeof(addr)); |
231 | > | addr.sin_family = AF_INET; |
232 | > | memcpy(&addr.sin_addr, &haddr, sizeof(haddr)); |
233 | > | addr.sin_port = htons(port); |
234 | > | |
235 | > | log_msg(LOG_DEBUG, "Creating a tcp connection"); |
236 | > | if(connect(sock, (struct sockaddr *)&addr, sizeof(addr)) !=0){ |
237 | > | log_msg(LOG_CRIT, "Failed to connect to hostname %s on port %d", hostname, port); |
238 | > | close(sock); |
239 | > | return NULL; |
240 | } | |
241 | < | ip_len=sizeof ip; |
242 | < | memset(&ip, 0, ip_len); |
243 | < | if((getsockname(sd, &ip, &ip_len)) != 0){ |
244 | < | logmessage(LOG_ERR, "Failed to get IP address (%m)"); |
245 | < | return -1; |
241 | > | |
242 | > | if((f=fdopen(sock, "r+"))==NULL){ |
243 | > | log_msg(LOG_CRIT, "Failed to connect to open filedescriptor on tcp connection"); |
244 | > | close(sock); |
245 | > | return NULL; |
246 | } | |
247 | < | if (ip.sa_family!=AF_INET){ |
248 | < | logmessage(LOG_ERR, "sa family is wrong type"); |
249 | < | return -1; |
250 | < | } |
247 | > | |
248 | > | return f; |
249 | > | } |
250 | > | |
251 | > | int tcp_comm(FILE *f, char *send, char **response, char *expected){ |
252 | > | |
253 | > | log_msg(LOG_DEBUG, "Sending %s", send); |
254 | > | fprintf(f, "%s\n", send); |
255 | > | fflush(f); |
256 | > | *response=fpgetline(f); |
257 | > | fseek(f, 0, SEEK_CUR); |
258 | > | |
259 | > | if(*response!=NULL) log_msg(LOG_DEBUG, "Recieved %s", *response); |
260 | > | |
261 | > | if( (*response==NULL) || (strcmp(*response, "ERROR")==0) ) return -1; |
262 | ||
263 | < | if((ihost_state->my_ip=inet_ntoa(((struct sockaddr_in *)&ip)->sin_addr))==NULL){ |
142 | < | logmessage(LOG_ERR, "Failed to get IP (%m)"); |
143 | < | return -1; |
144 | < | } |
145 | < | |
146 | < | logmessage(LOG_DEBUG, "Sending STARTCONIG"); |
147 | < | reply=sock_comm(fm_fd_r, fm_fd_w, "STARTCONFIG"); |
148 | < | if ((reply==NULL) || (strncasecmp(reply, "OK", 2) != 0) ) { |
149 | < | logmessage(LOG_ERR, "Server error on STARTCONFIG"); |
150 | < | return -1; |
151 | < | } |
263 | > | if(expected==NULL) return 0; |
264 | ||
265 | < | logmessage(LOG_DEBUG, "Sending LASTMODIFIED"); |
154 | < | reply=sock_comm(fm_fd_r, fm_fd_w, "LASTMODIFIED"); |
155 | < | if((reply== NULL) || (strncasecmp(reply, "ERROR", 5) ==0)){ |
156 | < | logmessage(LOG_ERR, "Server error on LASTMODIFIED (%m)"); |
157 | < | return -1; |
158 | < | } |
159 | < | if((ihost_state->last_modified=strdup(reply)) == NULL){ |
160 | < | logmessage(LOG_ERR, "strdup failed (%m)"); |
161 | < | return -1; |
162 | < | } |
265 | > | if((strcmp(expected, *response))==0) return 0; |
266 | ||
267 | < | logmessage(LOG_DEBUG, "Sending FILELIST"); |
268 | < | reply=sock_comm(fm_fd_r, fm_fd_w, "FILELIST"); |
269 | < | if((reply== NULL) || (strncasecmp(reply, "ERROR", 5) ==0)){ |
167 | < | logmessage(LOG_ERR, "Server error on FILELIST (%m)"); |
168 | < | return -1; |
169 | < | } |
170 | < | if((ihost_state->files_list=strdup(reply)) == NULL){ |
171 | < | logmessage(LOG_ERR, "strdup failed (%m)"); |
172 | < | return -1; |
173 | < | } |
267 | > | log_msg(LOG_DEBUG, "Did not get expected response"); |
268 | > | return -1; |
269 | > | } |
270 | ||
271 | < | logmessage(LOG_DEBUG, "Sending FQDN"); |
272 | < | reply=sock_comm(fm_fd_r, fm_fd_w, "FQDN"); |
177 | < | if((reply== NULL) || (strncasecmp(reply, "ERROR", 5)==0)){ |
178 | < | logmessage(LOG_ERR, "Server error on FQDN (%m)"); |
271 | > | int tcp_comm_strdup(FILE *f, char *send, char **response, char *expected){ |
272 | > | if((tcp_comm(f, send, response, expected))!=0){ |
273 | return -1; | |
274 | } | |
181 | – | if((ihost_state->my_fqdn=strdup(reply)) == NULL){ |
182 | – | logmessage(LOG_ERR, "strdup failed (%m)"); |
183 | – | return -1; |
184 | – | } |
275 | ||
276 | < | logmessage(LOG_DEBUG, "Sending FQDN"); |
277 | < | reply=sock_comm(fm_fd_r, fm_fd_w, "UDPUpdateTime"); |
278 | < | if(reply== NULL){ |
279 | < | logmessage(LOG_ERR, "Server error (%m)"); |
276 | > | *response=strdup(*response); |
277 | > | if (response==NULL) return -1; |
278 | > | |
279 | > | return 0; |
280 | > | } |
281 | > | |
282 | > | int ihost_getconfig(ihost_state_t *ihost_state){ |
283 | > | |
284 | > | FILE *tcp_con; |
285 | > | char *response; |
286 | > | char *response_ptr; |
287 | > | |
288 | > | /* Keep these in case of a failure and so it can keep running on the old config */ |
289 | > | char *file_list=NULL; |
290 | > | char *last_modified=NULL; |
291 | > | char *host_fqdn=NULL; |
292 | > | char *host_ip=NULL; |
293 | > | int udp_update_time=0; |
294 | > | char *server_fqdn=NULL; |
295 | > | int server_udp_port=0; |
296 | > | time_t config_ttl=0; |
297 | > | |
298 | > | if((tcp_con=create_tcp_connection(ihost_state->filtermanager_host, ihost_state->filtermanager_port))==NULL){ |
299 | return -1; | |
300 | } | |
301 | < | if (strncasecmp(reply, "ERROR", 5) != 0){ |
302 | < | ihost_state->udp_update_time=atoi(reply); |
303 | < | } |
301 | > | |
302 | > | if(ihost_state->file_list!=NULL || ihost_state->last_modified!=NULL){ |
303 | > | if(tcp_con==NULL){ |
304 | > | goto error; |
305 | > | } |
306 | ||
307 | < | logmessage(LOG_DEBUG, "Sending TCPUpdateTime"); |
308 | < | reply=sock_comm(fm_fd_r, fm_fd_w, "TCPUpdateTime"); |
309 | < | if(reply== NULL){ |
310 | < | logmessage(LOG_ERR, "Server error on TCPUpdateTime (%m)"); |
311 | < | return -1; |
307 | > | if((tcp_comm(tcp_con, "CHECKCONFIG", &response, "OK"))!=0){ |
308 | > | goto error; |
309 | > | } |
310 | > | |
311 | > | if((tcp_comm(tcp_con, ihost_state->file_list, &response, "OK"))!=0){ |
312 | > | goto error; |
313 | > | } |
314 | > | |
315 | > | if((tcp_comm(tcp_con, ihost_state->last_modified, &response, "OK"))==0){ |
316 | > | if((tcp_comm(tcp_con, "END", &response, "OK"))!=0){ |
317 | > | goto error; |
318 | > | } |
319 | > | fclose(tcp_con); |
320 | > | return 0; |
321 | > | }else{ |
322 | > | if((strcmp(response, "EXPIRED"))!=0){ |
323 | > | goto error; |
324 | > | } |
325 | > | } |
326 | } | |
202 | – | if (strncasecmp(reply, "ERROR", 5) != 0){ |
203 | – | ihost_state->tcp_update_time=atoi(reply); |
204 | – | } |
327 | ||
328 | < | logmessage(LOG_DEBUG, "Sending ENDCONFIG"); |
329 | < | reply=sock_comm(fm_fd_r, fm_fd_w, "ENDCONFIG"); |
330 | < | if(reply== NULL){ |
331 | < | logmessage(LOG_ERR, "Server error on ENDCONFIG (%m)"); |
210 | < | return -1; |
328 | > | /* If we got to here, the config must of expired */ |
329 | > | |
330 | > | if((tcp_comm(tcp_con, "STARTCONFIG", &response, "OK"))!=0){ |
331 | > | goto error; |
332 | } | |
333 | ||
334 | < | logmessage(LOG_DEBUG, "Sending FILTER"); |
335 | < | reply=sock_comm(fm_fd_r, fm_fd_w, "FILTER"); |
215 | < | if((reply== NULL) || (strncasecmp(reply, "ERROR", 5)==0)){ |
216 | < | logmessage(LOG_ERR, "Server error FILTER failed (%m)"); |
217 | < | return -1; |
334 | > | if((tcp_comm_strdup(tcp_con, "LASTMODIFIED", &response, NULL))!=0){ |
335 | > | goto error; |
336 | } | |
337 | < | reply_ptr=strchr(reply,';'); |
338 | < | if (reply_ptr==NULL){ |
339 | < | logmessage(LOG_ERR, "Incorrect data returned"); |
340 | < | return -1; |
337 | > | last_modified=response; |
338 | > | |
339 | > | if((tcp_comm_strdup(tcp_con, "FILELIST", &response, NULL))!=0){ |
340 | > | goto error; |
341 | } | |
342 | < | *reply_ptr='\0'; |
343 | < | if((ihost_state->server_fqdn=strdup(reply)) == NULL){ |
344 | < | logmessage(LOG_ERR, "strdup failed (%m)"); |
345 | < | return -1; |
342 | > | file_list=response; |
343 | > | |
344 | > | if((tcp_comm_strdup(tcp_con, "FQDN", &response, NULL))!=0){ |
345 | > | goto error; |
346 | } | |
347 | < | reply=reply_ptr + 1; |
348 | < | reply_ptr=strchr(reply,';'); |
349 | < | if (reply_ptr==NULL){ |
350 | < | logmessage(LOG_ERR, "Incorrect data returned 2"); |
233 | < | return -1; |
347 | > | host_fqdn=response; |
348 | > | |
349 | > | if((tcp_comm_strdup(tcp_con, "IP", &response, NULL))!=0){ |
350 | > | goto error; |
351 | } | |
352 | < | *reply_ptr='\0'; |
353 | < | ihost_state->server_udp_port=atoi(reply); |
354 | < | reply=reply_ptr+1; |
355 | < | ihost_state->server_tcp_port=atoi(reply); |
239 | < | if ((ihost_state->server_tcp_port==0) || (ihost_state->server_udp_port==0)){ |
240 | < | logmessage(LOG_ERR, "Incorrect data returned 3 "); |
241 | < | return -1; |
352 | > | host_ip=response; |
353 | > | |
354 | > | if((tcp_comm(tcp_con, "UDPUpdateTime", &response, NULL))!=0){ |
355 | > | goto error; |
356 | } | |
357 | + | udp_update_time=atoi(response); |
358 | ||
359 | < | logmessage(LOG_DEBUG, "Sending END"); |
360 | < | reply=sock_comm(fm_fd_r, fm_fd_w, "END"); |
246 | < | if((reply== NULL) || (strncasecmp(reply, "ERROR", 5) ==0 )){ |
247 | < | logmessage(LOG_ERR, "Server error on END (%m)"); |
248 | < | return -1; |
359 | > | if((tcp_comm(tcp_con, "ConfigTTL", &response, NULL))!=0){ |
360 | > | goto error; |
361 | } | |
362 | + | config_ttl=atoi(response); |
363 | ||
364 | < | if(fclose(fm_fd_r) !=0){ |
365 | < | logmessage(LOG_ERR, "Failed to close read FD (%m)"); |
253 | < | return -1; |
364 | > | if((tcp_comm(tcp_con, "ENDCONFIG", &response, NULL))!=0){ |
365 | > | goto error; |
366 | } | |
367 | < | if(fclose(fm_fd_w) !=0){ |
368 | < | logmessage(LOG_ERR, "Failed to close write FD (%m)"); |
369 | < | return -1; |
367 | > | |
368 | > | if((tcp_comm(tcp_con, "FILTER", &response, NULL))!=0){ |
369 | > | goto error; |
370 | > | }else{ |
371 | > | response_ptr=strchr(response,';'); |
372 | > | if(response_ptr==NULL){ |
373 | > | log_msg(LOG_ERR, "Incorrect data sent by server"); |
374 | > | goto error; |
375 | > | } |
376 | > | *response_ptr='\0'; |
377 | > | server_fqdn=strdup(response); |
378 | > | if(server_fqdn==NULL){ |
379 | > | goto error; |
380 | > | } |
381 | > | response_ptr++; |
382 | > | if(response_ptr==NULL){ |
383 | > | log_msg(LOG_ERR, "Incorrect data sent by server"); |
384 | > | goto error; |
385 | > | } |
386 | > | |
387 | > | printf("string : %s\n", response_ptr); |
388 | > | server_udp_port=atoi(response_ptr); |
389 | > | |
390 | > | if (server_udp_port==0){ |
391 | > | log_msg(LOG_ERR, "Incorrect data sent by server"); |
392 | > | goto error; |
393 | > | } |
394 | } | |
395 | + | |
396 | + | if((tcp_comm(tcp_con, "END", &response, "OK"))!=0){ |
397 | + | goto error; |
398 | + | } |
399 | ||
400 | + | fclose(tcp_con); |
401 | + | |
402 | + | /* We have the data we need, and its all been read correctly */ |
403 | + | |
404 | + | /* Free the old data before pointing them to the new data. m_free copes should |
405 | + | * this already be NULL */ |
406 | + | if(ihost_state->file_list!=NULL) free(ihost_state->file_list); |
407 | + | if(ihost_state->last_modified!=NULL) free(ihost_state->last_modified); |
408 | + | if(ihost_state->host_fqdn!=NULL) free(ihost_state->host_fqdn); |
409 | + | if(ihost_state->server_fqdn!=NULL) free(ihost_state->server_fqdn); |
410 | + | if(ihost_state->host_ip!=NULL) free(ihost_state->host_ip); |
411 | + | |
412 | + | ihost_state->file_list=file_list; |
413 | + | ihost_state->last_modified=last_modified; |
414 | + | ihost_state->host_fqdn=host_fqdn; |
415 | + | ihost_state->host_ip=host_ip; |
416 | + | ihost_state->server_fqdn=server_fqdn; |
417 | + | ihost_state->server_udp_port=server_udp_port; |
418 | + | ihost_state->udp_update_time=udp_update_time; |
419 | + | ihost_state->config_ttl=config_ttl; |
420 | + | |
421 | + | log_msg(LOG_DEBUG, "UDP Update time %d", udp_update_time); |
422 | + | log_msg(LOG_DEBUG, "Configure ttl %d", config_ttl); |
423 | + | |
424 | return 0; | |
261 | – | } |
425 | ||
426 | < | int heartbeat(ihost_state_t *ihost_state){ |
264 | < | struct sockaddr_in addr; |
265 | < | struct in_addr haddr; |
266 | < | int sd; |
267 | < | FILE *fm_fd_r, *fm_fd_w; |
268 | < | char *reply; |
269 | < | int exitcode=0; |
426 | > | error: |
427 | ||
428 | < | logmessage(LOG_DEBUG, "Setting up configure socket to %s on port %d", ihost_state->server_fqdn, ihost_state->server_tcp_port); |
429 | < | if ((sd = socket(AF_INET, SOCK_STREAM, PF_UNSPEC)) < 0) { |
430 | < | logmessage(LOG_ERR, "Can't create AF_INET socket (%m)"); |
431 | < | return -1; |
432 | < | } |
428 | > | if(file_list!=NULL) free(file_list); |
429 | > | if(last_modified!=NULL) free(last_modified); |
430 | > | if(host_fqdn!=NULL) free(host_fqdn); |
431 | > | if(server_fqdn!=NULL) free(server_fqdn); |
432 | > | if(host_ip!=NULL) free(host_ip); |
433 | > | fclose(tcp_con); |
434 | ||
435 | < | if (get_host_addr(ihost_state->server_fqdn, &haddr) != 0){ |
436 | < | logmessage(LOG_ERR, "Failed to resolve address %s (%m)", ihost_state->server_fqdn); |
279 | < | return -1; |
280 | < | } |
435 | > | return -1; |
436 | > | } |
437 | ||
438 | < | memset(&addr, 0, sizeof addr); |
439 | < | addr.sin_family = AF_INET; |
440 | < | memcpy(&addr.sin_addr, &haddr, sizeof haddr); |
441 | < | addr.sin_port = htons(ihost_state->server_tcp_port); |
438 | > | int get_system_stats(int seq_no, ihost_state_t *ihost_state, char *xml, int size){ |
439 | > | char tmp[size]; |
440 | > | cpu_percent_t *cpu_percent; |
441 | > | mem_stat_t *mem_stats; |
442 | > | load_stat_t *load_stats; |
443 | > | user_stat_t *user_stats; |
444 | > | swap_stat_t *swap_stats; |
445 | > | general_stat_t *general_stats; |
446 | > | disk_stat_t *disk_stats; |
447 | > | diskio_stat_t *diskio_stats; |
448 | > | process_stat_t *process_stats; |
449 | > | network_stat_t *network_stats; |
450 | > | page_stat_t *page_stats; |
451 | > | int disk_entries=0; |
452 | > | int diskio_entries=0; |
453 | > | int network_entries=0; |
454 | ||
455 | < | if (connect(sd, (struct sockaddr *)&addr, sizeof addr) != 0) { |
456 | < | logmessage(LOG_ERR, "Failed to connect to %s on port %d (%m)", ihost_state->server_fqdn, ihost_state->server_tcp_port); |
457 | < | return -1; |
290 | < | } |
455 | > | int counter; |
456 | > | long long x; |
457 | > | long long y; |
458 | ||
459 | < | /* Need to open 2 files, one for reading one for writing, as it gets confused if we only use 1 :) */ |
460 | < | if ((fm_fd_r=fdopen(sd,"r")) == NULL){ |
461 | < | logmessage(LOG_ERR, "Failed to open stream (%m)"); |
295 | < | return -1; |
296 | < | } |
459 | > | /* Print start of the packet we want */ |
460 | > | snprintf(xml, size, "<packet seq_no=\"%d\" machine_name=\"%s\" date=\"%ld\" type=\"data\" ip=\"%s\">", \ |
461 | > | seq_no, ihost_state->host_fqdn, time(NULL), ihost_state->host_ip); |
462 | ||
463 | < | if ((fm_fd_w=fdopen(dup(sd),"w")) == NULL){ |
464 | < | logmessage(LOG_ERR, "Failed to open stream (%m)"); |
465 | < | return -1; |
466 | < | } |
463 | > | /* Get cpu stats, check it is correct, then fill in its entry for the xml */ |
464 | > | if((cpu_percent=cpu_percent_usage())==NULL){ |
465 | > | log_msg(LOG_CRIT, "Failed to get cpu statistics"); |
466 | > | }else{ |
467 | > | snprintf(tmp, size, \ |
468 | > | "<cpu><user>%3.2f</user><kernel>%3.2f</kernel><idle>%3.2f</idle><iowait>%3.2f</iowait><swap>%3.2f</swap></cpu>", \ |
469 | > | cpu_percent->user, \ |
470 | > | cpu_percent->kernel, \ |
471 | > | cpu_percent->idle, \ |
472 | > | cpu_percent->iowait, \ |
473 | > | cpu_percent->swap); |
474 | ||
475 | < | logmessage(LOG_DEBUG, "Sending HEARTBEAT"); |
476 | < | reply=sock_comm(fm_fd_r, fm_fd_w, "HEARTBEAT"); |
305 | < | if ((reply==NULL) || (strncasecmp(reply, "ERROR", 5) == 0) ) { |
306 | < | logmessage(LOG_ERR, "Server error on HEARTBEAT"); |
307 | < | return -1; |
308 | < | } |
475 | > | if(strlcat(xml, tmp, size) >= size) goto too_big_error; |
476 | > | } |
477 | ||
478 | < | logmessage(LOG_DEBUG, "Sending CONFIG"); |
479 | < | reply=sock_comm(fm_fd_r, fm_fd_w, "CONFIG"); |
480 | < | if ((reply==NULL) || (strncasecmp(reply, "ERROR", 5) == 0) ) { |
481 | < | logmessage(LOG_ERR, "Server error on CONFIG"); |
482 | < | return -1; |
483 | < | } |
478 | > | |
479 | > | /*Get mem stats, and fill in xml */ |
480 | > | if((mem_stats=get_memory_stats())==NULL){ |
481 | > | log_msg(LOG_CRIT, "Failed to get memory statistics"); |
482 | > | }else{ |
483 | > | snprintf(tmp, size, \ |
484 | > | "<memory><total>%lld</total><free>%lld</free><used>%lld</used><cache>%lld</cache></memory>", \ |
485 | > | mem_stats->total, \ |
486 | > | mem_stats->free, \ |
487 | > | mem_stats->used, \ |
488 | > | mem_stats->cache); |
489 | > | |
490 | > | if(strlcat(xml, tmp, size) >= size) goto too_big_error; |
491 | > | } |
492 | ||
317 | – | logmessage(LOG_DEBUG, "Sending %s", ihost_state->files_list); |
318 | – | reply=sock_comm(fm_fd_r, fm_fd_w, ihost_state->files_list); |
319 | – | if ((reply==NULL) || (strncasecmp(reply, "OK", 2) != 0) ) { |
320 | – | logmessage(LOG_ERR, "Server error on fileslist"); |
321 | – | return -1; |
322 | – | } |
493 | ||
494 | < | logmessage(LOG_DEBUG, "Sending %s", ihost_state->last_modified); |
495 | < | reply=sock_comm(fm_fd_r, fm_fd_w, ihost_state->last_modified); |
496 | < | if (reply==NULL) { |
497 | < | logmessage(LOG_ERR, "Server error NULL recieved on lastmodified"); |
498 | < | return -1; |
499 | < | } |
500 | < | if (strncasecmp(reply, "ERROR", 5) == 0){ |
501 | < | /* Means the config has changed */ |
502 | < | logmessage(LOG_INFO, "Recieved ERROR from server for a reconfigure required"); |
503 | < | exitcode=RECONFIGURE_RETURN_CODE; |
494 | > | /* Get load stats */ |
495 | > | if((load_stats=get_load_stats())==NULL){ |
496 | > | log_msg(LOG_CRIT, "Failed to get load statistics"); |
497 | > | }else{ |
498 | > | snprintf(tmp, size, \ |
499 | > | "<load><load1>%.2lf</load1><load5>%.2lf</load5><load15>%.2lf</load15></load>", \ |
500 | > | load_stats->min1, \ |
501 | > | load_stats->min5, \ |
502 | > | load_stats->min15); |
503 | > | if(strlcat(xml, tmp, size) >= size) goto too_big_error; |
504 | } | |
505 | ||
336 | – | logmessage(LOG_DEBUG,"Sending KEY"); |
337 | – | reply=sock_comm(fm_fd_r, fm_fd_w, "KEY"); |
338 | – | if ((reply==NULL) || (strncasecmp(reply, "ERROR", 5) == 0) ) { |
339 | – | logmessage(LOG_ERR, "Server error on KEY"); |
340 | – | return -1; |
341 | – | } |
342 | – | if (ihost_state->key!=NULL) free(ihost_state->key); |
343 | – | |
344 | – | if((ihost_state->key=strdup(reply)) == NULL){ |
345 | – | logmessage(LOG_ERR, "strdup failed (%m)"); |
346 | – | return -1; |
347 | – | } |
506 | ||
507 | < | logmessage(LOG_DEBUG,"Sending ENDHEARTBEAT"); |
508 | < | reply=sock_comm(fm_fd_r, fm_fd_w, "ENDHEARTBEAT"); |
509 | < | if((reply== NULL) || (strncasecmp(reply, "ERROR", 5) ==0 )){ |
510 | < | logmessage(LOG_ERR, "Server error on ENDHEARTBEAT (%m)"); |
511 | < | return -1; |
354 | < | } |
507 | > | /* get user stats */ |
508 | > | |
509 | > | if((user_stats=get_user_stats())==NULL){ |
510 | > | log_msg(LOG_CRIT, "Failed to get user statistics"); |
511 | > | }else{ |
512 | ||
513 | < | fflush(fm_fd_r); |
514 | < | fflush(fm_fd_w); |
513 | > | snprintf(tmp, size, \ |
514 | > | "<users><list>%s</list><count>%d</count></users>", \ |
515 | > | user_stats->name_list, \ |
516 | > | user_stats->num_entries); |
517 | > | |
518 | > | if(strlcat(xml, tmp, size) >= size) goto too_big_error; |
519 | > | } |
520 | ||
359 | – | if(fclose(fm_fd_r) !=0){ |
360 | – | logmessage(LOG_ERR, "Failed to close read FD (%m)"); |
361 | – | return -1; |
362 | – | } |
363 | – | if(fclose(fm_fd_w) !=0){ |
364 | – | logmessage(LOG_ERR, "Failed to close write FD (%m)"); |
365 | – | return -1; |
366 | – | } |
521 | ||
522 | < | return exitcode; |
523 | < | } |
522 | > | /* swap stats */ |
523 | > | if((swap_stats=get_swap_stats())==NULL){ |
524 | > | log_msg(LOG_CRIT, "Failed to get swap statistics"); |
525 | > | }else{ |
526 | > | snprintf(tmp, size, \ |
527 | > | "<swap><total>%lld</total><used>%lld</used><free>%lld</free></swap>",\ |
528 | > | swap_stats->total, \ |
529 | > | swap_stats->used, \ |
530 | > | swap_stats->free); |
531 | > | |
532 | > | if(strlcat(xml, tmp, size) >= size) goto too_big_error; |
533 | > | } |
534 | ||
371 | – | char *stat_grab(ihost_state_t *ihost_state, int counter){ |
372 | – | #define NUM_STATS 9 |
373 | – | char *stats[NUM_STATS]; |
374 | – | char *xml_data=NULL; |
375 | – | char *xml_data_p; |
376 | – | int xml_size=0; |
377 | – | int x=0; |
535 | ||
536 | < | logmessage(LOG_DEBUG,"get_cpu_stats"); |
380 | < | stats[0]=get_cpu_stats(); |
381 | < | logmessage(LOG_DEBUG,"get_disk_stats"); |
382 | < | stats[1]=get_disk_stats(); |
383 | < | logmessage(LOG_DEBUG,"get_load_stats"); |
384 | < | stats[2]=get_load_stats(); |
385 | < | logmessage(LOG_DEBUG,"get_memory_stats"); |
386 | < | stats[3]=get_memory_stats(); |
387 | < | logmessage(LOG_DEBUG,"get_os_info"); |
388 | < | stats[4]=get_os_info(); |
389 | < | logmessage(LOG_DEBUG,"get_page_stats"); |
390 | < | stats[5]=get_page_stats(); |
391 | < | logmessage(LOG_DEBUG,"get_process_stats"); |
392 | < | stats[6]=get_process_stats(); |
393 | < | logmessage(LOG_DEBUG,"get_swap_stats"); |
394 | < | stats[7]=get_swap_stats(); |
395 | < | logmessage(LOG_DEBUG,"get_user_stats"); |
396 | < | stats[8]=get_user_stats(); |
536 | > | /* general stats */ |
537 | ||
538 | + | if((general_stats=get_general_stats())==NULL){ |
539 | + | log_msg(LOG_CRIT, "Failed to get general statistics"); |
540 | + | }else{ |
541 | + | snprintf(tmp, size, \ |
542 | + | "<os><name>%s</name><release>%s</release><version>%s</version><sysname>%s</sysname><platform>%s</platform><uptime>%ld</uptime></os>", \ |
543 | + | general_stats->os_name, \ |
544 | + | general_stats->os_release, \ |
545 | + | general_stats->os_version, \ |
546 | + | general_stats->hostname, \ |
547 | + | general_stats->platform, \ |
548 | + | (long)general_stats->uptime); |
549 | + | |
550 | + | if(strlcat(xml, tmp, size) >= size) goto too_big_error; |
551 | + | |
552 | + | } |
553 | ||
554 | < | for(x=0;x<NUM_STATS;x++){ |
555 | < | if(stats[x]==NULL){ |
556 | < | logmessage(LOG_ERR,"Function returned NULL"); |
557 | < | return NULL; |
554 | > | |
555 | > | /* process stats */ |
556 | > | if((process_stats=get_process_stats())==NULL){ |
557 | > | log_msg(LOG_CRIT, "Failed to get general statistics"); |
558 | > | }else{ |
559 | > | snprintf(tmp, size, \ |
560 | > | "<processes><sleeping>%d</sleeping><cpu>%d</cpu><zombie>%d</zombie><stopped>%d</stopped><total>%d</total></processes>",\ |
561 | > | process_stats->sleeping, \ |
562 | > | process_stats->running, \ |
563 | > | process_stats->zombie, \ |
564 | > | process_stats->stopped, \ |
565 | > | process_stats->total); |
566 | > | |
567 | > | if(strlcat(xml, tmp, size) >= size) goto too_big_error; |
568 | > | |
569 | > | } |
570 | > | |
571 | > | |
572 | > | /* Get paging stats */ |
573 | > | if((page_stats=get_page_stats_diff())==NULL){ |
574 | > | log_msg(LOG_CRIT, "Failed to get paging statistics"); |
575 | > | }else{ |
576 | > | if(page_stats->systime!=0){ |
577 | > | x=page_stats->pages_pagein / page_stats->systime; |
578 | > | y=page_stats->pages_pageout / page_stats->systime; |
579 | > | }else{ |
580 | > | x=page_stats->pages_pagein; |
581 | > | y=page_stats->pages_pageout; |
582 | } | |
583 | < | xml_size+=strlen(stats[x]); |
583 | > | snprintf(tmp, size, \ |
584 | > | "<pages><pageins>%lld</pageins><pageouts>%lld</pageouts></pages>", \ |
585 | > | x, \ |
586 | > | y); |
587 | > | |
588 | > | if(strlcat(xml, tmp, size) >= size) goto too_big_error; |
589 | } | |
590 | ||
591 | < | xml_data=malloc(xml_size); |
592 | < | xml_data=strcpy(xml_data, stats[0]); |
591 | > | |
592 | > | /* get diskio stats */ |
593 | ||
594 | < | for(x=1;x<NUM_STATS;x++){ |
595 | < | strcat(xml_data, stats[x]); |
594 | > | if((diskio_stats=get_diskio_stats_diff(&diskio_entries))==NULL){ |
595 | > | log_msg(LOG_CRIT, "Failed to get diskio statistics"); |
596 | > | }else{ |
597 | > | strlcat(xml, "<diskio>", size); |
598 | > | for(counter=0;counter<diskio_entries;counter++){ |
599 | > | |
600 | > | if(diskio_stats->systime!=0){ |
601 | > | x=diskio_stats->read_bytes / diskio_stats->systime; |
602 | > | y=diskio_stats->write_bytes / diskio_stats->systime; |
603 | > | }else{ |
604 | > | x=diskio_stats->read_bytes; |
605 | > | y=diskio_stats->write_bytes; |
606 | > | } |
607 | > | |
608 | > | snprintf(tmp, size, \ |
609 | > | "<p%d name=\"%s\" rbytes=\"%lld\" wbytes=\"%lld\"></p%d>", \ |
610 | > | counter, \ |
611 | > | diskio_stats->disk_name, \ |
612 | > | x, \ |
613 | > | y, \ |
614 | > | counter); |
615 | > | |
616 | > | strlcat(xml, tmp, size); |
617 | > | diskio_stats++; |
618 | > | } |
619 | > | |
620 | > | if(strlcat(xml, "</diskio>", size) >= size) goto too_big_error; |
621 | > | |
622 | } | |
623 | ||
624 | < | xml_data_p=xml_data; |
625 | < | xml_data=strf("<packet seq_no=\"%d\" machine_name=\"%s\" date=\"%ld\" type=\"data\" ip=\"%s\" key=\"%s\">%s</packet>", counter, ihost_state->my_fqdn, time(NULL), ihost_state->my_ip, ihost_state->key, xml_data); |
626 | < | free(xml_data_p); |
624 | > | |
625 | > | /* get networks stats */ |
626 | > | |
627 | > | if((network_stats=get_network_stats_diff(&network_entries))==NULL){ |
628 | > | log_msg(LOG_CRIT, "Failed to get network statistics"); |
629 | > | }else{ |
630 | > | strlcat(xml, "<net>", size); |
631 | > | for(counter=0;counter<network_entries;counter++){ |
632 | > | if(network_stats->systime!=0){ |
633 | > | x=network_stats->rx / network_stats->systime; |
634 | > | y=network_stats->tx / network_stats->systime; |
635 | > | }else{ |
636 | > | x=network_stats->rx; |
637 | > | y=network_stats->tx; |
638 | > | } |
639 | ||
640 | < | logmessage(LOG_DEBUG,"Generated XML Data of : %s", xml_data); |
641 | < | return xml_data; |
642 | < | } |
640 | > | snprintf(tmp, size, \ |
641 | > | "<p%d name=\"%s\" rx=\"%lld\" tx=\"%lld\"></p%d>", \ |
642 | > | counter, \ |
643 | > | network_stats->interface_name, \ |
644 | > | x, \ |
645 | > | y, \ |
646 | > | counter); |
647 | ||
648 | < | int send_stats(ihost_state_t *ihost_state, char *data_stream){ |
649 | < | struct sockaddr_in addr; |
650 | < | struct in_addr haddr; |
648 | > | strlcat(xml, tmp, size); |
649 | > | network_stats++; |
650 | > | } |
651 | ||
652 | < | int sd; |
427 | < | size_t len; |
652 | > | if(strlcat(xml, "</net>", size) >= size) goto too_big_error; |
653 | ||
429 | – | len=strlen(data_stream); |
430 | – | if(len>UDP_MAX_PACKET_SIZE){ |
431 | – | logmessage(LOG_ERR, "Too big to send to server. Please reconfigure client and server and recompile"); |
432 | – | exit(1); |
654 | } | |
434 | – | logmessage(LOG_DEBUG,"Resolving IP of server"); |
435 | – | if (get_host_addr(ihost_state->server_fqdn, &haddr) != 0){ |
436 | – | logmessage(LOG_ERR, "Failed to resolve address %s (%m)", ihost_state->server_fqdn); |
437 | – | return -1; |
438 | – | } |
439 | – | logmessage(LOG_DEBUG,"Creating UDP connection to %s on %d",ihost_state->server_fqdn, ihost_state->server_udp_port); |
440 | – | if((sd=socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0){ |
441 | – | logmessage(LOG_ERR, "failed to create UDP socket (%m)"); |
442 | – | return -1; |
443 | – | } |
655 | ||
445 | – | memset(&addr, 0, sizeof(addr)); |
446 | – | addr.sin_family=AF_INET; |
447 | – | memcpy((char *)&addr.sin_addr, &haddr, sizeof haddr); |
448 | – | addr.sin_port = htons(ihost_state->server_udp_port); |
656 | ||
657 | < | logmessage(LOG_INFO,"Sending packet : %s", data_stream); |
658 | < | if((sendto(sd, data_stream, len, 0, (struct sockaddr *) &addr, sizeof(addr))) != len){ |
659 | < | logmessage(LOG_ERR, "Send the wrong number of bytes (%m)"); |
660 | < | return -1; |
657 | > | /* get disk stats */ |
658 | > | |
659 | > | if((disk_stats=get_disk_stats(&disk_entries))==NULL){ |
660 | > | log_msg(LOG_CRIT, "Failed to get disk statistics"); |
661 | > | }else{ |
662 | > | strlcat(xml, "<disk>", size); |
663 | > | for(counter=0;counter<disk_entries;counter++){ |
664 | > | snprintf(tmp, size, \ |
665 | > | "<p%d name=\"%s\" mount=\"%s\" fstype=\"%s\" total=\"%lld\" used=\"%lld\" avail=\"%lld\" totalinodes=\"%lld\" usedinodes=\"%lld\" freeinodes=\"%lld\"></p%d>", \ |
666 | > | counter, \ |
667 | > | disk_stats->device_name, \ |
668 | > | disk_stats->mnt_point, \ |
669 | > | disk_stats->fs_type, \ |
670 | > | disk_stats->size, \ |
671 | > | disk_stats->used, \ |
672 | > | disk_stats->avail, \ |
673 | > | disk_stats->total_inodes, \ |
674 | > | disk_stats->used_inodes, \ |
675 | > | disk_stats->free_inodes, \ |
676 | > | counter); |
677 | > | |
678 | > | strlcat(xml, tmp, size); |
679 | > | |
680 | > | disk_stats++; |
681 | > | } |
682 | > | |
683 | > | if(strlcat(xml, "</disk>", size) >= size) goto too_big_error; |
684 | > | |
685 | } | |
686 | + | |
687 | ||
688 | < | close(sd); |
688 | > | if(strlcat(xml, "</packet>", size) >= size) goto too_big_error; |
689 | ||
690 | < | return 0; |
690 | > | /*If we got to here, it should of all been filled in nicely now */ |
691 | > | return 0; |
692 | > | |
693 | > | too_big_error: |
694 | > | log_msg(LOG_ERR, "UDP Packet is too large. Throwing away the packet"); |
695 | > | return -1; |
696 | } | |
697 | ||
698 | + | |
699 | + | |
700 | void usage(char *progname){ | |
701 | < | fprintf(stderr, "Usage %s [options] server port\n", progname); |
702 | < | fprintf(stderr, "Options\n"); |
703 | < | fprintf(stderr, " -v Verbose, the more v flags the more verbose, eg -vv\n"); |
704 | < | fprintf(stderr, " -d Daemon mode, self backgrounding\n"); |
705 | < | fprintf(stderr, " -s Send errors to syslog\n"); |
706 | < | fprintf(stderr, " -V Print version number\n"); |
707 | < | fprintf(stderr, " -h Prints this help page\n"); |
469 | < | exit(1); |
701 | > | fprintf(stderr, "Usage %s [options] server port\n", progname); |
702 | > | fprintf(stderr, "Options\n"); |
703 | > | fprintf(stderr, " -v Verbose mode,-vv would make even more verbose\n"); |
704 | > | fprintf(stderr, " -f Foreground mode, print errors to stderr\n"); |
705 | > | fprintf(stderr, " -V Print version number\n"); |
706 | > | fprintf(stderr, " -h Prints this help page\n"); |
707 | > | exit(1); |
708 | } | |
709 | ||
710 | int main(int argc, char **argv){ | |
711 | + | |
712 | ihost_state_t ihost_state; | |
713 | < | int heartbeat_exit; |
714 | < | int counter=0; |
476 | < | long udp_time=0, tcp_time=0, stat_grab_time=0, cur_time=0; |
477 | < | int sleep_delay=0; |
478 | < | char *xml_stats; |
479 | < | pid_t pid; |
713 | > | udp_sockinfo_t udp_sockinfo; |
714 | > | |
715 | int cmdopt; | |
716 | extern int optind; | |
717 | < | int verbose=0, daemon=0; |
483 | < | extern int syslog_logging; |
484 | < | extern int log_level; |
485 | < | extern int cur_level; |
717 | > | pid_t pid; |
718 | FILE *f; | |
719 | + | int packet_num=0; |
720 | + | int len; |
721 | ||
722 | < | log_level=1; |
489 | < | cur_level=1; |
490 | < | syslog_logging=0; |
722 | > | char packet[MAX_UDP_PACKET_SIZE]; |
723 | ||
724 | < | errf_set_ofunc(log_errors); |
493 | < | /* NULL'ify so i can tell if i need to free it or not */ |
494 | < | ihost_state.fm_host=NULL; |
495 | < | ihost_state.my_fqdn=NULL; |
496 | < | ihost_state.server_fqdn=NULL; |
497 | < | ihost_state.last_modified=NULL; |
498 | < | ihost_state.files_list=NULL; |
499 | < | ihost_state.key=NULL; |
724 | > | time_t cur_time, sleep_delay, udp_time=0, config_time=0; |
725 | ||
726 | < | errf_set_progname(argv[0]); |
727 | < | |
726 | > | /* Set default settings */ |
727 | > | ihost_config.verbose=1; |
728 | > | ihost_config.daemon=1; |
729 | > | /* Set all errors to go down stderr until told otherwise */ |
730 | > | ihost_config.log=stderr; |
731 | ||
732 | < | while((cmdopt=getopt(argc, argv, "vdshV")) != -1){ |
733 | < | switch(cmdopt){ |
734 | < | case 'v': |
735 | < | verbose++; |
736 | < | break; |
737 | < | |
738 | < | case 'd': |
511 | < | /* Force syslog logging since stderr will be closed in this case */ |
512 | < | syslog_logging=1; |
513 | < | daemon=1; |
514 | < | break; |
515 | < | |
516 | < | case 's': |
517 | < | syslog_logging=1; |
518 | < | break; |
519 | < | |
520 | < | case 'h': |
521 | < | usage(argv[0]); |
522 | < | break; |
732 | > | /* Blank ihost_state to default settings */ |
733 | > | ihost_state.filtermanager_host=NULL; |
734 | > | ihost_state.host_fqdn=NULL; |
735 | > | ihost_state.host_ip=NULL; |
736 | > | ihost_state.server_fqdn=NULL; |
737 | > | ihost_state.file_list=NULL; |
738 | > | ihost_state.last_modified=NULL; |
739 | ||
740 | < | case 'V': |
741 | < | errf("%s version %f",argv[0], versionNo); |
742 | < | break; |
740 | > | while((cmdopt=getopt(argc, argv, "vfhV")) != -1){ |
741 | > | switch(cmdopt){ |
742 | > | case 'v': |
743 | > | ihost_config.verbose++; |
744 | > | break; |
745 | ||
746 | < | default: |
747 | < | usage(argv[0]); |
748 | < | exit(1); |
749 | < | } |
532 | < | } |
746 | > | case 'f': |
747 | > | /* Force syslog logging since stderr will be closed in this case */ |
748 | > | ihost_config.daemon=0; |
749 | > | break; |
750 | ||
751 | < | if(argc!=optind+2){ |
752 | < | usage(argv[0]); |
751 | > | case 'h': |
752 | > | usage(argv[0]); |
753 | > | break; |
754 | > | |
755 | > | case 'V': |
756 | > | fprintf(stderr, "%s version %s\n", argv[0], VERSION); |
757 | > | break; |
758 | > | |
759 | > | default: |
760 | > | usage(argv[0]); |
761 | > | exit(1); |
762 | > | } |
763 | > | } |
764 | > | |
765 | > | if(argc!=optind+2){ |
766 | > | usage(argv[0]); |
767 | > | exit(1); |
768 | > | } |
769 | > | |
770 | > | ihost_state.filtermanager_host=strdup(argv[optind]); |
771 | > | ihost_state.filtermanager_port=atoi(argv[optind+1]); |
772 | > | |
773 | > | if(gethostbyname(ihost_state.filtermanager_host)==NULL){ |
774 | > | log_msg(LOG_CRIT, "Failed to lookup hostname. Please check settings"); |
775 | exit(1); | |
776 | } | |
777 | < | ihost_state.fm_host=argv[optind]; |
778 | < | ihost_state.fm_port=atoi(argv[optind+1]); |
779 | < | if(ihost_state.fm_port==0){ |
541 | < | errf("Invalid port number"); |
542 | < | usage(argv[0]); |
777 | > | if(ihost_state.filtermanager_port==0){ |
778 | > | log_msg(LOG_ERR, "Invalid port number"); |
779 | > | exit(1); |
780 | } | |
781 | ||
782 | < | if(daemon==1){ |
782 | > | if(ihost_config.daemon){ |
783 | pid=fork(); | |
784 | if(pid==-1){ | |
785 | < | errf("Fork failed, can't background. Exiting"); |
786 | < | exit(1); |
785 | > | log_msg(LOG_CRIT, "Failed to background exiting"); |
786 | > | exit(1); |
787 | }else if(pid!=0){ | |
788 | < | /* Parent process */ |
789 | < | return 0; |
790 | < | } |
791 | < | /* We should now be in the background*/ |
792 | < | if(setsid()==-1){ |
793 | < | errf("setsid failed (%m)"); |
788 | > | /* Parent process */ |
789 | > | return 0; |
790 | > | } |
791 | > | /* We should now be in the background*/ |
792 | > | if(setsid()==-1){ |
793 | > | log_msg(LOG_CRIT, "setsid failed"); |
794 | > | exit(1); |
795 | > | } |
796 | > | |
797 | > | if((ihost_config.log=fopen(LOG_FILE, "a"))==NULL){ |
798 | > | ihost_config.log=stderr; |
799 | > | log_msg(LOG_CRIT, "Failed to open Logfiles %s for writing", LOG_FILE); |
800 | exit(1); | |
801 | } | |
559 | – | fclose(stdin); |
560 | – | fclose(stdout); |
561 | – | fclose(stderr); |
562 | – | } |
802 | ||
803 | < | if(syslog_logging==1){ |
804 | < | openlog(errf_get_progname(),0,LOG_ERR); |
805 | < | setlogmask(LOG_UPTO(LOG_DEBUG)); |
567 | < | } |
568 | < | |
569 | < | switch(verbose){ |
570 | < | case 0: |
571 | < | /* Critical errors + */ |
572 | < | log_level=LOG_ERR; |
573 | < | break; |
574 | < | case 1: |
575 | < | /* Recoverable errors */ |
576 | < | log_level=LOG_WARNING; |
577 | < | break; |
578 | < | case 2: |
579 | < | /* Print stuff like the XML packets */ |
580 | < | log_level=LOG_INFO; |
581 | < | break; |
582 | < | default: |
583 | < | /* Must have lots of v's */ |
584 | < | /* Print out everything its doing */ |
585 | < | log_level=LOG_DEBUG; |
586 | < | break; |
587 | < | } |
803 | > | fclose(stdin); |
804 | > | fclose(stdout); |
805 | > | fclose(stderr); |
806 | ||
807 | < | logmessage(LOG_DEBUG,"Writing PID FILE"); |
808 | < | pid=getpid(); |
809 | < | if((f=fopen(PID_FILE,"w")) == NULL){ |
810 | < | logmessage(LOG_WARNING, "Failed to write PID file"); |
811 | < | }else{ |
812 | < | if((fprintf(f,"%d",(int)pid)) != sizeof(pid)){ |
813 | < | logmessage(LOG_WARNING, "Failed to write PID file"); |
814 | < | } |
815 | < | if((fclose(f))!=0){ |
816 | < | logmessage(LOG_ERR, "failed to close PID file"); |
817 | < | exit(1); |
818 | < | } |
807 | > | } |
808 | > | |
809 | > | log_msg(LOG_INFO, "Starting ihost"); |
810 | > | |
811 | > | log_msg(LOG_DEBUG,"Writing PID FILE"); |
812 | > | |
813 | > | pid=getpid(); |
814 | > | |
815 | > | if((f=fopen(PID_FILE,"w")) == NULL){ |
816 | > | log_msg(LOG_CRIT, "Failed to write PID file"); |
817 | > | }else{ |
818 | > | if((fprintf(f,"%d",(int)pid)) <= 0 ){ |
819 | > | log_msg(LOG_CRIT, "Failed to write PID file"); |
820 | > | } |
821 | > | if((fclose(f))!=0){ |
822 | > | log_msg(LOG_CRIT, "failed to close PID file"); |
823 | > | } |
824 | > | } |
825 | > | |
826 | > | /* Get the initial config from the filter manager. Should this fail, |
827 | > | * wait, and then try again. */ |
828 | > | |
829 | > | get_diskio_stats_diff(&packet_num); |
830 | > | packet_num=0; |
831 | > | |
832 | > | while(ihost_getconfig(&ihost_state)!=0){ |
833 | > | log_msg(LOG_ERR, "Failed to get ihost config"); |
834 | > | sleep(10); |
835 | } | |
836 | ||
837 | < | if(ihost_configure(&ihost_state)!=0){ |
838 | < | logmessage(LOG_ERR,"configure failed"); |
839 | < | /* Ok, ideally we prob should have 2 copies of the structure and carry on if this |
840 | < | happens.. But we dont :) (at the moment) */ |
607 | < | exit(1); |
837 | > | printf("%s\n%d\n", ihost_state.server_fqdn, ihost_state.server_udp_port); |
838 | > | while((create_udp_sockinfo(&udp_sockinfo, ihost_state.server_fqdn, ihost_state.server_udp_port))!=0){ |
839 | > | log_msg(LOG_ERR, "Failed to create udp socket"); |
840 | > | sleep(10); |
841 | } | |
842 | ||
843 | + | config_time=time(NULL)+ihost_state.config_ttl; |
844 | + | |
845 | + | /* Now have config.. collect data and send as often as required */ |
846 | for(;;){ | |
847 | cur_time=time(NULL); | |
612 | – | if(cur_time>=tcp_time){ |
613 | – | logmessage(LOG_DEBUG,"Sending heartbeat"); |
614 | – | heartbeat_exit=heartbeat(&ihost_state); |
615 | – | if(heartbeat_exit==RECONFIGURE_RETURN_CODE){ |
616 | – | logmessage(LOG_INFO,"heartbeat needs to be reconfigured"); |
617 | – | ihost_configure(&ihost_state); |
618 | – | udp_time=0; |
619 | – | } |
620 | – | if(heartbeat_exit==-1){ |
621 | – | logmessage(LOG_ERR,"Heartbeat failed"); |
622 | – | exit(1); |
623 | – | } |
624 | – | tcp_time=time(NULL)+ihost_state.tcp_update_time; |
625 | – | logmessage(LOG_DEBUG,"next tcp time should be %d", tcp_time); |
626 | – | } |
848 | ||
849 | if(cur_time>=udp_time){ | |
850 | < | logmessage(LOG_DEBUG,"Sending udp data"); |
850 | > | if((get_system_stats(packet_num++, &ihost_state, packet, MAX_UDP_PACKET_SIZE))!=0){ |
851 | > | log_msg(LOG_ERR, "Failed to get system stats"); |
852 | > | } |
853 | ||
854 | < | stat_grab_time=time(NULL); |
855 | < | if((xml_stats=stat_grab(&ihost_state, counter++)) == NULL){ |
856 | < | logmessage(LOG_ERR,"Failed to get stats (%m)"); |
857 | < | exit(1); |
854 | > | len=strlen(packet); |
855 | > | log_msg(LOG_DEBUG, "Packet size: %d\nPacket: %s\n", len, packet); |
856 | > | |
857 | > | if((sendto(udp_sockinfo.sock, packet, len, 0, (struct sockaddr *) &udp_sockinfo.addr, sizeof(udp_sockinfo.addr)))!=len){ |
858 | > | log_msg(LOG_CRIT, "Failed to send packet"); |
859 | } | |
860 | < | stat_grab_time=time(NULL)-stat_grab_time; |
861 | < | send_stats(&ihost_state, xml_stats); |
638 | < | free(xml_stats); |
639 | < | udp_time=time(NULL)+ihost_state.udp_update_time-stat_grab_time; |
640 | < | logmessage(LOG_DEBUG,"next udp time should be %d", udp_time); |
860 | > | udp_time=cur_time+ihost_state.udp_update_time; |
861 | > | log_msg(LOG_DEBUG, "Next packet should be sent on %d", udp_time); |
862 | } | |
863 | + | |
864 | + | if(cur_time>=config_time){ |
865 | + | if(ihost_getconfig(&ihost_state)!=0){ |
866 | + | /* If we can't get the config, try again 5 minutes time */ |
867 | + | log_msg(LOG_ERR, "Failed to get config, try again 5 minutes time"); |
868 | + | config_time=time(NULL)+300; |
869 | + | }else{ |
870 | + | close(udp_sockinfo.sock); |
871 | ||
872 | < | if(tcp_time<udp_time){ |
873 | < | sleep_delay=tcp_time-time(NULL); |
874 | < | }else{ |
875 | < | sleep_delay=udp_time-time(NULL); |
872 | > | while((create_udp_sockinfo(&udp_sockinfo, ihost_state.server_fqdn, ihost_state.server_udp_port))!=0){ |
873 | > | log_msg(LOG_CRIT, "Failed to create udp socket"); |
874 | > | sleep(10); |
875 | > | } |
876 | > | |
877 | > | config_time=time(NULL)+ihost_state.config_ttl; |
878 | > | |
879 | > | log_msg(LOG_DEBUG, "Config expires on %d\n", ihost_state.config_ttl); |
880 | > | } |
881 | } | |
882 | < | logmessage(LOG_DEBUG,"Sleeping for %d", sleep_delay); |
882 | > | |
883 | > | sleep_delay=udp_time-time(NULL); |
884 | > | log_msg(LOG_DEBUG, "Sleeping for %d", sleep_delay); |
885 | if(sleep_delay>0) sleep(sleep_delay); | |
886 | < | } |
887 | < | return 0; |
886 | > | } |
887 | > | |
888 | > | return(0); |
889 | } | |
653 | – |
– | Removed lines |
+ | Added lines |
< | Changed lines |
> | Changed lines |