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 |
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 |
> |
void log_msg(int level, char *format, ...){ |
79 |
> |
extern int errno; |
80 |
> |
int cur_errno; |
81 |
> |
va_list ap; |
82 |
> |
|
83 |
> |
cur_errno=errno; |
84 |
> |
|
85 |
> |
if(level<=ihost_config.verbose){ |
86 |
> |
va_start(ap, format); |
87 |
> |
vfprintf(ihost_config.log, format, ap); |
88 |
> |
va_end(ap); |
89 |
> |
if(level==LOG_CRIT){ |
90 |
> |
fprintf(ihost_config.log, " (%s)\n", strerror(cur_errno)); |
91 |
|
}else{ |
92 |
< |
fprintf(stderr, "%s\n", message); |
92 |
> |
fprintf(ihost_config.log, "\n"); |
93 |
|
} |
94 |
< |
} |
94 |
> |
} |
95 |
|
} |
96 |
|
|
97 |
< |
char* sock_comm(FILE *f_r, FILE *f_w, char *sendString){ |
98 |
< |
char *reply; |
99 |
< |
fprintf(f_w, "%s\n", sendString); |
100 |
< |
fflush(f_w); |
101 |
< |
reply=fpgetline(f_r); |
102 |
< |
/* Returns pointer to static buffer */ |
80 |
< |
return reply; |
81 |
< |
} |
97 |
> |
/* Takes many pointers, checks if they are NULL or not, and then free's them */ |
98 |
> |
/* Deprciated - and i only wrote it today! :) |
99 |
> |
void m_free(int num_pointers, ...){ |
100 |
> |
int x=0; |
101 |
> |
va_list ap; |
102 |
> |
void *p; |
103 |
|
|
104 |
< |
int ihost_configure(ihost_state_t *ihost_state){ |
105 |
< |
struct sockaddr_in addr; |
106 |
< |
struct in_addr haddr; |
107 |
< |
struct sockaddr ip; |
108 |
< |
int ip_len; |
109 |
< |
int sd; |
110 |
< |
FILE *fm_fd_r, *fm_fd_w; |
111 |
< |
char *reply; |
112 |
< |
char *reply_ptr; |
104 |
> |
va_start(ap, num_pointers); |
105 |
> |
for(;x<num_pointers;x++){ |
106 |
> |
p=va_arg(ap, void*); |
107 |
> |
if(p!=NULL){ |
108 |
> |
free(p); |
109 |
> |
} |
110 |
> |
} |
111 |
> |
va_end(ap); |
112 |
> |
} |
113 |
> |
*/ |
114 |
|
|
115 |
< |
/* 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); |
115 |
> |
int create_udp_sockinfo(udp_sockinfo_t *udp_sockinfo, char *hostname, int port){ |
116 |
|
|
117 |
< |
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 |
< |
} |
117 |
> |
struct in_addr haddr; |
118 |
|
|
119 |
< |
if (get_host_addr(ihost_state->fm_host, &haddr) != 0){ |
120 |
< |
logmessage(LOG_ERR, "Failed to resolve address %s (%m)", ihost_state->fm_host); |
121 |
< |
return -1; |
119 |
> |
log_msg(LOG_DEBUG, "Resolving name for udp connection"); |
120 |
> |
if(get_host_addr(hostname, &haddr) != 0){ |
121 |
> |
log_msg(LOG_CRIT, "Failed to lookup name"); |
122 |
> |
return 1; |
123 |
|
} |
124 |
|
|
125 |
< |
memset(&addr, 0, sizeof addr); |
126 |
< |
addr.sin_family = AF_INET; |
127 |
< |
memcpy(&addr.sin_addr, &haddr, sizeof haddr); |
128 |
< |
addr.sin_port = htons(ihost_state->fm_port); |
125 |
> |
if((udp_sockinfo->sock=socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0){ |
126 |
> |
log_msg(LOG_CRIT, "Failed to create UDP socket"); |
127 |
> |
return 1; |
128 |
> |
} |
129 |
|
|
130 |
< |
if (connect(sd, (struct sockaddr *)&addr, sizeof addr) != 0) { |
131 |
< |
logmessage(LOG_ERR, "Failed to connect to %s on port %d (%m)", ihost_state->fm_host, ihost_state->fm_port); |
132 |
< |
return -1; |
130 |
> |
memset(&(udp_sockinfo->addr), 0, sizeof(struct sockaddr_in)); |
131 |
> |
udp_sockinfo->addr.sin_family=AF_INET; |
132 |
> |
memcpy((char *)&(udp_sockinfo->addr.sin_addr), &haddr, sizeof(haddr)); |
133 |
> |
udp_sockinfo->addr.sin_port = htons(port); |
134 |
> |
|
135 |
> |
log_msg(LOG_DEBUG, "Socket created"); |
136 |
> |
return 0; |
137 |
> |
} |
138 |
> |
|
139 |
> |
FILE *create_tcp_connection(char *hostname, int port){ |
140 |
> |
int sock; |
141 |
> |
struct sockaddr_in addr; |
142 |
> |
struct in_addr haddr; |
143 |
> |
FILE *f; |
144 |
> |
|
145 |
> |
log_msg(LOG_DEBUG, "Creating tcp socket"); |
146 |
> |
if((sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP))<0){ |
147 |
> |
log_msg(LOG_CRIT, "Failed to make TCP Socket"); |
148 |
> |
return NULL; |
149 |
|
} |
150 |
|
|
151 |
< |
/* Need to open 2 files, one for reading one for writing, as it gets confused if we only use 1 :) */ |
152 |
< |
if ((fm_fd_r=fdopen(sd,"r")) == NULL){ |
153 |
< |
logmessage(LOG_ERR, "Failed to open read stream (%m)"); |
123 |
< |
return -1; |
151 |
> |
if((get_host_addr(hostname, &haddr))!=0){ |
152 |
> |
log_msg(LOG_CRIT, "Failed to lookup name for %s", hostname); |
153 |
> |
return NULL; |
154 |
|
} |
155 |
|
|
156 |
< |
if ((fm_fd_w=fdopen(dup(sd),"w")) == NULL){ |
157 |
< |
logmessage(LOG_ERR, "Failed to open write stream (%m)"); |
158 |
< |
return -1; |
156 |
> |
memset(&addr, 0, sizeof(addr)); |
157 |
> |
addr.sin_family = AF_INET; |
158 |
> |
memcpy(&addr.sin_addr, &haddr, sizeof(haddr)); |
159 |
> |
addr.sin_port = htons(port); |
160 |
> |
|
161 |
> |
log_msg(LOG_DEBUG, "Creating a tcp connection"); |
162 |
> |
if(connect(sock, (struct sockaddr *)&addr, sizeof(addr)) !=0){ |
163 |
> |
log_msg(LOG_CRIT, "Failed to connect to hostname %s on port %d", hostname, port); |
164 |
> |
return NULL; |
165 |
|
} |
166 |
< |
ip_len=sizeof ip; |
167 |
< |
memset(&ip, 0, ip_len); |
168 |
< |
if((getsockname(sd, &ip, &ip_len)) != 0){ |
169 |
< |
logmessage(LOG_ERR, "Failed to get IP address (%m)"); |
170 |
< |
return -1; |
166 |
> |
|
167 |
> |
if((f=fdopen(sock, "r+"))==NULL){ |
168 |
> |
log_msg(LOG_CRIT, "Failed to connect to open filedescriptor on tcp connection"); |
169 |
> |
close(sock); |
170 |
> |
return NULL; |
171 |
|
} |
172 |
< |
if (ip.sa_family!=AF_INET){ |
173 |
< |
logmessage(LOG_ERR, "sa family is wrong type"); |
172 |
> |
|
173 |
> |
return f; |
174 |
> |
} |
175 |
> |
|
176 |
> |
int tcp_comm(FILE *f, char *send, char **response, char *expected){ |
177 |
> |
|
178 |
> |
log_msg(LOG_DEBUG, "Sending %s", send); |
179 |
> |
fprintf(f, "%s\n", send); |
180 |
> |
fflush(f); |
181 |
> |
*response=fpgetline(f); |
182 |
> |
fseek(f, 0, SEEK_CUR); |
183 |
> |
|
184 |
> |
if(*response!=NULL) log_msg(LOG_DEBUG, "Recieved %s", *response); |
185 |
> |
|
186 |
> |
if( (*response==NULL) || (strcmp(*response, "ERROR")==0) ) return -1; |
187 |
> |
|
188 |
> |
if(expected==NULL) return 0; |
189 |
> |
|
190 |
> |
if((strcmp(expected, *response))==0) return 0; |
191 |
> |
|
192 |
> |
log_msg(LOG_DEBUG, "Did not get expected response"); |
193 |
> |
return -1; |
194 |
> |
} |
195 |
> |
|
196 |
> |
int tcp_comm_strdup(FILE *f, char *send, char **response, char *expected){ |
197 |
> |
if((tcp_comm(f, send, response, expected))!=0){ |
198 |
|
return -1; |
199 |
|
} |
200 |
+ |
|
201 |
+ |
*response=strdup(*response); |
202 |
+ |
if (response==NULL) return -1; |
203 |
+ |
|
204 |
+ |
return 0; |
205 |
+ |
} |
206 |
|
|
207 |
< |
if((ihost_state->my_ip=inet_ntoa(((struct sockaddr_in *)&ip)->sin_addr))==NULL){ |
208 |
< |
logmessage(LOG_ERR, "Failed to get IP (%m)"); |
209 |
< |
return -1; |
210 |
< |
} |
207 |
> |
int ihost_getconfig(ihost_state_t *ihost_state){ |
208 |
> |
|
209 |
> |
FILE *tcp_con; |
210 |
> |
char *response; |
211 |
> |
char *response_ptr; |
212 |
> |
|
213 |
> |
/* Keep these in case of a failure and so it can keep running on the old config */ |
214 |
> |
char *file_list=NULL; |
215 |
> |
char *last_modified=NULL; |
216 |
> |
char *host_fqdn=NULL; |
217 |
> |
char *host_ip=NULL; |
218 |
> |
int udp_update_time=0; |
219 |
> |
char *server_fqdn=NULL; |
220 |
> |
int server_udp_port=0; |
221 |
> |
time_t config_ttl=0; |
222 |
> |
|
223 |
> |
tcp_con=create_tcp_connection(ihost_state->filtermanager_host, ihost_state->filtermanager_port); |
224 |
> |
|
225 |
> |
if(ihost_state->file_list!=NULL || ihost_state->last_modified!=NULL){ |
226 |
> |
if(tcp_con==NULL){ |
227 |
> |
goto error; |
228 |
> |
} |
229 |
|
|
230 |
< |
logmessage(LOG_DEBUG, "Sending STARTCONIG"); |
231 |
< |
reply=sock_comm(fm_fd_r, fm_fd_w, "STARTCONFIG"); |
232 |
< |
if ((reply==NULL) || (strncasecmp(reply, "OK", 2) != 0) ) { |
233 |
< |
logmessage(LOG_ERR, "Server error on STARTCONFIG"); |
234 |
< |
return -1; |
230 |
> |
if((tcp_comm(tcp_con, "CHECKCONFIG", &response, "OK"))!=0){ |
231 |
> |
goto error; |
232 |
> |
} |
233 |
> |
|
234 |
> |
if((tcp_comm(tcp_con, ihost_state->file_list, &response, "OK"))!=0){ |
235 |
> |
goto error; |
236 |
> |
} |
237 |
> |
|
238 |
> |
if((tcp_comm(tcp_con, ihost_state->last_modified, &response, "OK"))==0){ |
239 |
> |
if((tcp_comm(tcp_con, "END", &response, "OK"))==0){ |
240 |
> |
goto error; |
241 |
> |
} |
242 |
> |
fclose(tcp_con); |
243 |
> |
return 0; |
244 |
> |
}else{ |
245 |
> |
if((strcmp(response, "EXPIRED"))!=0){ |
246 |
> |
goto error; |
247 |
> |
} |
248 |
> |
} |
249 |
|
} |
250 |
|
|
251 |
< |
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 |
< |
} |
251 |
> |
/* If we got to here, the config must of expired */ |
252 |
|
|
253 |
< |
logmessage(LOG_DEBUG, "Sending FILELIST"); |
254 |
< |
reply=sock_comm(fm_fd_r, fm_fd_w, "FILELIST"); |
166 |
< |
if((reply== NULL) || (strncasecmp(reply, "ERROR", 5) ==0)){ |
167 |
< |
logmessage(LOG_ERR, "Server error on FILELIST (%m)"); |
168 |
< |
return -1; |
253 |
> |
if((tcp_comm(tcp_con, "STARTCONFIG", &response, "OK"))!=0){ |
254 |
> |
goto error; |
255 |
|
} |
170 |
– |
if((ihost_state->files_list=strdup(reply)) == NULL){ |
171 |
– |
logmessage(LOG_ERR, "strdup failed (%m)"); |
172 |
– |
return -1; |
173 |
– |
} |
256 |
|
|
257 |
< |
logmessage(LOG_DEBUG, "Sending FQDN"); |
258 |
< |
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)"); |
179 |
< |
return -1; |
257 |
> |
if((tcp_comm_strdup(tcp_con, "LASTMODIFIED", &response, NULL))!=0){ |
258 |
> |
goto error; |
259 |
|
} |
260 |
< |
if((ihost_state->my_fqdn=strdup(reply)) == NULL){ |
261 |
< |
logmessage(LOG_ERR, "strdup failed (%m)"); |
262 |
< |
return -1; |
260 |
> |
last_modified=response; |
261 |
> |
|
262 |
> |
if((tcp_comm_strdup(tcp_con, "FILELIST", &response, NULL))!=0){ |
263 |
> |
goto error; |
264 |
|
} |
265 |
+ |
file_list=response; |
266 |
|
|
267 |
< |
logmessage(LOG_DEBUG, "Sending FQDN"); |
268 |
< |
reply=sock_comm(fm_fd_r, fm_fd_w, "UDPUpdateTime"); |
188 |
< |
if(reply== NULL){ |
189 |
< |
logmessage(LOG_ERR, "Server error (%m)"); |
190 |
< |
return -1; |
267 |
> |
if((tcp_comm_strdup(tcp_con, "FQDN", &response, NULL))!=0){ |
268 |
> |
goto error; |
269 |
|
} |
270 |
< |
if (strncasecmp(reply, "ERROR", 5) != 0){ |
271 |
< |
ihost_state->udp_update_time=atoi(reply); |
270 |
> |
host_fqdn=response; |
271 |
> |
|
272 |
> |
if((tcp_comm_strdup(tcp_con, "IP", &response, NULL))!=0){ |
273 |
> |
goto error; |
274 |
|
} |
275 |
< |
|
196 |
< |
logmessage(LOG_DEBUG, "Sending TCPUpdateTime"); |
197 |
< |
reply=sock_comm(fm_fd_r, fm_fd_w, "TCPUpdateTime"); |
198 |
< |
if(reply== NULL){ |
199 |
< |
logmessage(LOG_ERR, "Server error on TCPUpdateTime (%m)"); |
200 |
< |
return -1; |
201 |
< |
} |
202 |
< |
if (strncasecmp(reply, "ERROR", 5) != 0){ |
203 |
< |
ihost_state->tcp_update_time=atoi(reply); |
204 |
< |
} |
275 |
> |
host_ip=response; |
276 |
|
|
277 |
< |
logmessage(LOG_DEBUG, "Sending ENDCONFIG"); |
278 |
< |
reply=sock_comm(fm_fd_r, fm_fd_w, "ENDCONFIG"); |
208 |
< |
if(reply== NULL){ |
209 |
< |
logmessage(LOG_ERR, "Server error on ENDCONFIG (%m)"); |
210 |
< |
return -1; |
277 |
> |
if((tcp_comm(tcp_con, "UDPUpdateTime", &response, NULL))!=0){ |
278 |
> |
goto error; |
279 |
|
} |
280 |
+ |
udp_update_time=atoi(response); |
281 |
|
|
282 |
< |
logmessage(LOG_DEBUG, "Sending FILTER"); |
283 |
< |
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; |
282 |
> |
if((tcp_comm(tcp_con, "ConfigTTL", &response, NULL))!=0){ |
283 |
> |
goto error; |
284 |
|
} |
285 |
< |
reply_ptr=strchr(reply,';'); |
220 |
< |
if (reply_ptr==NULL){ |
221 |
< |
logmessage(LOG_ERR, "Incorrect data returned"); |
222 |
< |
return -1; |
223 |
< |
} |
224 |
< |
*reply_ptr='\0'; |
225 |
< |
if((ihost_state->server_fqdn=strdup(reply)) == NULL){ |
226 |
< |
logmessage(LOG_ERR, "strdup failed (%m)"); |
227 |
< |
return -1; |
228 |
< |
} |
229 |
< |
reply=reply_ptr + 1; |
230 |
< |
reply_ptr=strchr(reply,';'); |
231 |
< |
if (reply_ptr==NULL){ |
232 |
< |
logmessage(LOG_ERR, "Incorrect data returned 2"); |
233 |
< |
return -1; |
234 |
< |
} |
235 |
< |
*reply_ptr='\0'; |
236 |
< |
ihost_state->server_udp_port=atoi(reply); |
237 |
< |
reply=reply_ptr+1; |
238 |
< |
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; |
242 |
< |
} |
285 |
> |
config_ttl=atoi(response); |
286 |
|
|
287 |
< |
logmessage(LOG_DEBUG, "Sending END"); |
288 |
< |
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; |
287 |
> |
if((tcp_comm(tcp_con, "ENDCONFIG", &response, NULL))!=0){ |
288 |
> |
goto error; |
289 |
|
} |
290 |
|
|
291 |
< |
if(fclose(fm_fd_r) !=0){ |
292 |
< |
logmessage(LOG_ERR, "Failed to close read FD (%m)"); |
293 |
< |
return -1; |
291 |
> |
if((tcp_comm(tcp_con, "FILTER", &response, NULL))!=0){ |
292 |
> |
goto error; |
293 |
> |
}else{ |
294 |
> |
response_ptr=strchr(response,';'); |
295 |
> |
if(response_ptr==NULL){ |
296 |
> |
log_msg(LOG_ERR, "Incorrect data sent by server"); |
297 |
> |
goto error; |
298 |
> |
} |
299 |
> |
*response_ptr='\0'; |
300 |
> |
server_fqdn=strdup(response); |
301 |
> |
if(server_fqdn==NULL){ |
302 |
> |
goto error; |
303 |
> |
} |
304 |
> |
response_ptr++; |
305 |
> |
if(response_ptr==NULL){ |
306 |
> |
log_msg(LOG_ERR, "Incorrect data sent by server"); |
307 |
> |
goto error; |
308 |
> |
} |
309 |
> |
|
310 |
> |
printf("string : %s\n", response_ptr); |
311 |
> |
server_udp_port=atoi(response_ptr); |
312 |
> |
|
313 |
> |
if (server_udp_port==0){ |
314 |
> |
log_msg(LOG_ERR, "Incorrect data sent by server"); |
315 |
> |
goto error; |
316 |
> |
} |
317 |
|
} |
318 |
< |
if(fclose(fm_fd_w) !=0){ |
319 |
< |
logmessage(LOG_ERR, "Failed to close write FD (%m)"); |
320 |
< |
return -1; |
318 |
> |
|
319 |
> |
if((tcp_comm(tcp_con, "END", &response, "OK"))!=0){ |
320 |
> |
goto error; |
321 |
|
} |
322 |
|
|
323 |
+ |
fclose(tcp_con); |
324 |
+ |
|
325 |
+ |
/* We have the data we need, and its all been read correctly */ |
326 |
+ |
|
327 |
+ |
/* Free the old data before pointing them to the new data. m_free copes should |
328 |
+ |
* this already be NULL */ |
329 |
+ |
if(ihost_state->file_list!=NULL) free(ihost_state->file_list); |
330 |
+ |
if(ihost_state->last_modified!=NULL) free(ihost_state->last_modified); |
331 |
+ |
if(ihost_state->host_fqdn!=NULL) free(ihost_state->host_fqdn); |
332 |
+ |
if(ihost_state->server_fqdn!=NULL) free(ihost_state->server_fqdn); |
333 |
+ |
if(ihost_state->host_ip!=NULL) free(ihost_state->host_ip); |
334 |
+ |
|
335 |
+ |
ihost_state->file_list=file_list; |
336 |
+ |
ihost_state->last_modified=last_modified; |
337 |
+ |
ihost_state->host_fqdn=host_fqdn; |
338 |
+ |
ihost_state->host_ip=host_ip; |
339 |
+ |
ihost_state->server_fqdn=server_fqdn; |
340 |
+ |
ihost_state->server_udp_port=server_udp_port; |
341 |
+ |
ihost_state->udp_update_time=udp_update_time; |
342 |
+ |
ihost_state->config_ttl=config_ttl; |
343 |
+ |
|
344 |
+ |
log_msg(LOG_DEBUG, "UDP Update time %d", udp_update_time); |
345 |
+ |
log_msg(LOG_DEBUG, "Configure ttl %d", config_ttl); |
346 |
+ |
|
347 |
|
return 0; |
348 |
+ |
|
349 |
+ |
error: |
350 |
+ |
|
351 |
+ |
if(file_list!=NULL) free(file_list); |
352 |
+ |
if(last_modified!=NULL) free(last_modified); |
353 |
+ |
if(host_fqdn!=NULL) free(host_fqdn); |
354 |
+ |
if(server_fqdn!=NULL) free(server_fqdn); |
355 |
+ |
if(host_ip!=NULL) free(host_ip); |
356 |
+ |
fclose(tcp_con); |
357 |
+ |
|
358 |
+ |
return -1; |
359 |
|
} |
360 |
|
|
361 |
< |
int heartbeat(ihost_state_t *ihost_state){ |
362 |
< |
struct sockaddr_in addr; |
363 |
< |
struct in_addr haddr; |
364 |
< |
int sd; |
365 |
< |
FILE *fm_fd_r, *fm_fd_w; |
366 |
< |
char *reply; |
367 |
< |
int exitcode=0; |
361 |
> |
int get_system_stats(int seq_no, ihost_state_t *ihost_state, char *xml, int size){ |
362 |
> |
char tmp[size]; |
363 |
> |
cpu_percent_t *cpu_percent; |
364 |
> |
mem_stat_t *mem_stats; |
365 |
> |
load_stat_t *load_stats; |
366 |
> |
user_stat_t *user_stats; |
367 |
> |
swap_stat_t *swap_stats; |
368 |
> |
general_stat_t *general_stats; |
369 |
> |
disk_stat_t *disk_stats; |
370 |
> |
diskio_stat_t *diskio_stats; |
371 |
> |
process_stat_t *process_stats; |
372 |
> |
network_stat_t *network_stats; |
373 |
> |
page_stat_t *page_stats; |
374 |
> |
int disk_entries=0; |
375 |
> |
int diskio_entries=0; |
376 |
> |
int network_entries=0; |
377 |
|
|
378 |
< |
logmessage(LOG_DEBUG, "Setting up configure socket to %s on port %d", ihost_state->server_fqdn, ihost_state->server_tcp_port); |
379 |
< |
if ((sd = socket(AF_INET, SOCK_STREAM, PF_UNSPEC)) < 0) { |
380 |
< |
logmessage(LOG_ERR, "Can't create AF_INET socket (%m)"); |
274 |
< |
return -1; |
275 |
< |
} |
378 |
> |
int counter; |
379 |
> |
long long x; |
380 |
> |
long long y; |
381 |
|
|
382 |
< |
if (get_host_addr(ihost_state->server_fqdn, &haddr) != 0){ |
383 |
< |
logmessage(LOG_ERR, "Failed to resolve address %s (%m)", ihost_state->server_fqdn); |
384 |
< |
return -1; |
280 |
< |
} |
382 |
> |
/* Print start of the packet we want */ |
383 |
> |
snprintf(xml, size, "<packet seq_no=\"%d\" machine_name=\"%s\" date=\"%ld\" type=\"data\" ip=\"%s\">", \ |
384 |
> |
seq_no, ihost_state->host_fqdn, time(NULL), ihost_state->host_ip); |
385 |
|
|
386 |
< |
memset(&addr, 0, sizeof addr); |
387 |
< |
addr.sin_family = AF_INET; |
388 |
< |
memcpy(&addr.sin_addr, &haddr, sizeof haddr); |
389 |
< |
addr.sin_port = htons(ihost_state->server_tcp_port); |
386 |
> |
/* Get cpu stats, check it is correct, then fill in its entry for the xml */ |
387 |
> |
if((cpu_percent=cpu_percent_usage())==NULL){ |
388 |
> |
log_msg(LOG_CRIT, "Failed to get cpu statistics"); |
389 |
> |
}else{ |
390 |
> |
snprintf(tmp, size, \ |
391 |
> |
"<cpu><user>%3.2f</user><kernel>%3.2f</kernel><idle>%3.2f</idle><iowait>%3.2f</iowait><swap>%3.2f</swap></cpu>", \ |
392 |
> |
cpu_percent->user, \ |
393 |
> |
cpu_percent->kernel, \ |
394 |
> |
cpu_percent->idle, \ |
395 |
> |
cpu_percent->iowait, \ |
396 |
> |
cpu_percent->swap); |
397 |
|
|
398 |
< |
if (connect(sd, (struct sockaddr *)&addr, sizeof addr) != 0) { |
399 |
< |
logmessage(LOG_ERR, "Failed to connect to %s on port %d (%m)", ihost_state->server_fqdn, ihost_state->server_tcp_port); |
289 |
< |
return -1; |
290 |
< |
} |
398 |
> |
if(strlcat(xml, tmp, size) >= size) goto too_big_error; |
399 |
> |
} |
400 |
|
|
401 |
< |
/* Need to open 2 files, one for reading one for writing, as it gets confused if we only use 1 :) */ |
402 |
< |
if ((fm_fd_r=fdopen(sd,"r")) == NULL){ |
403 |
< |
logmessage(LOG_ERR, "Failed to open stream (%m)"); |
404 |
< |
return -1; |
405 |
< |
} |
401 |
> |
|
402 |
> |
/*Get mem stats, and fill in xml */ |
403 |
> |
if((mem_stats=get_memory_stats())==NULL){ |
404 |
> |
log_msg(LOG_CRIT, "Failed to get memory statistics"); |
405 |
> |
}else{ |
406 |
> |
snprintf(tmp, size, \ |
407 |
> |
"<memory><total>%lld</total><free>%lld</free><used>%lld</used><cache>%lld</cache></memory>", \ |
408 |
> |
mem_stats->total, \ |
409 |
> |
mem_stats->free, \ |
410 |
> |
mem_stats->used, \ |
411 |
> |
mem_stats->cache); |
412 |
> |
|
413 |
> |
if(strlcat(xml, tmp, size) >= size) goto too_big_error; |
414 |
> |
} |
415 |
|
|
298 |
– |
if ((fm_fd_w=fdopen(dup(sd),"w")) == NULL){ |
299 |
– |
logmessage(LOG_ERR, "Failed to open stream (%m)"); |
300 |
– |
return -1; |
301 |
– |
} |
416 |
|
|
417 |
< |
logmessage(LOG_DEBUG, "Sending HEARTBEAT"); |
418 |
< |
reply=sock_comm(fm_fd_r, fm_fd_w, "HEARTBEAT"); |
419 |
< |
if ((reply==NULL) || (strncasecmp(reply, "ERROR", 5) == 0) ) { |
420 |
< |
logmessage(LOG_ERR, "Server error on HEARTBEAT"); |
421 |
< |
return -1; |
422 |
< |
} |
417 |
> |
/* Get load stats */ |
418 |
> |
if((load_stats=get_load_stats())==NULL){ |
419 |
> |
log_msg(LOG_CRIT, "Failed to get load statistics"); |
420 |
> |
}else{ |
421 |
> |
snprintf(tmp, size, \ |
422 |
> |
"<load><load1>%.2lf</load1><load5>%.2lf</load5><load15>%.2lf</load15></load>", \ |
423 |
> |
load_stats->min1, \ |
424 |
> |
load_stats->min5, \ |
425 |
> |
load_stats->min15); |
426 |
> |
if(strlcat(xml, tmp, size) >= size) goto too_big_error; |
427 |
> |
} |
428 |
|
|
310 |
– |
logmessage(LOG_DEBUG, "Sending CONFIG"); |
311 |
– |
reply=sock_comm(fm_fd_r, fm_fd_w, "CONFIG"); |
312 |
– |
if ((reply==NULL) || (strncasecmp(reply, "ERROR", 5) == 0) ) { |
313 |
– |
logmessage(LOG_ERR, "Server error on CONFIG"); |
314 |
– |
return -1; |
315 |
– |
} |
429 |
|
|
430 |
< |
logmessage(LOG_DEBUG, "Sending %s", ihost_state->files_list); |
431 |
< |
reply=sock_comm(fm_fd_r, fm_fd_w, ihost_state->files_list); |
432 |
< |
if ((reply==NULL) || (strncasecmp(reply, "OK", 2) != 0) ) { |
433 |
< |
logmessage(LOG_ERR, "Server error on fileslist"); |
434 |
< |
return -1; |
322 |
< |
} |
430 |
> |
/* get user stats */ |
431 |
> |
|
432 |
> |
if((user_stats=get_user_stats())==NULL){ |
433 |
> |
log_msg(LOG_CRIT, "Failed to get user statistics"); |
434 |
> |
}else{ |
435 |
|
|
436 |
< |
logmessage(LOG_DEBUG, "Sending %s", ihost_state->last_modified); |
437 |
< |
reply=sock_comm(fm_fd_r, fm_fd_w, ihost_state->last_modified); |
438 |
< |
if (reply==NULL) { |
439 |
< |
logmessage(LOG_ERR, "Server error NULL recieved on lastmodified"); |
440 |
< |
return -1; |
441 |
< |
} |
330 |
< |
if (strncasecmp(reply, "ERROR", 5) == 0){ |
331 |
< |
/* Means the config has changed */ |
332 |
< |
logmessage(LOG_INFO, "Recieved ERROR from server for a reconfigure required"); |
333 |
< |
exitcode=RECONFIGURE_RETURN_CODE; |
436 |
> |
snprintf(tmp, size, \ |
437 |
> |
"<users><list>%s</list><count>%d</count></users>", \ |
438 |
> |
user_stats->name_list, \ |
439 |
> |
user_stats->num_entries); |
440 |
> |
|
441 |
> |
if(strlcat(xml, tmp, size) >= size) goto too_big_error; |
442 |
|
} |
443 |
|
|
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 |
– |
} |
444 |
|
|
445 |
< |
logmessage(LOG_DEBUG,"Sending ENDHEARTBEAT"); |
446 |
< |
reply=sock_comm(fm_fd_r, fm_fd_w, "ENDHEARTBEAT"); |
447 |
< |
if((reply== NULL) || (strncasecmp(reply, "ERROR", 5) ==0 )){ |
448 |
< |
logmessage(LOG_ERR, "Server error on ENDHEARTBEAT (%m)"); |
449 |
< |
return -1; |
450 |
< |
} |
445 |
> |
/* swap stats */ |
446 |
> |
if((swap_stats=get_swap_stats())==NULL){ |
447 |
> |
log_msg(LOG_CRIT, "Failed to get swap statistics"); |
448 |
> |
}else{ |
449 |
> |
snprintf(tmp, size, \ |
450 |
> |
"<swap><total>%lld</total><used>%lld</used><free>%lld</free></swap>",\ |
451 |
> |
swap_stats->total, \ |
452 |
> |
swap_stats->used, \ |
453 |
> |
swap_stats->free); |
454 |
> |
|
455 |
> |
if(strlcat(xml, tmp, size) >= size) goto too_big_error; |
456 |
> |
} |
457 |
|
|
356 |
– |
fflush(fm_fd_r); |
357 |
– |
fflush(fm_fd_w); |
458 |
|
|
459 |
< |
if(fclose(fm_fd_r) !=0){ |
460 |
< |
logmessage(LOG_ERR, "Failed to close read FD (%m)"); |
461 |
< |
return -1; |
462 |
< |
} |
463 |
< |
if(fclose(fm_fd_w) !=0){ |
464 |
< |
logmessage(LOG_ERR, "Failed to close write FD (%m)"); |
465 |
< |
return -1; |
466 |
< |
} |
459 |
> |
/* general stats */ |
460 |
> |
|
461 |
> |
if((general_stats=get_general_stats())==NULL){ |
462 |
> |
log_msg(LOG_CRIT, "Failed to get general statistics"); |
463 |
> |
}else{ |
464 |
> |
snprintf(tmp, size, \ |
465 |
> |
"<os><name>%s</name><release>%s</release><version>%s</version><sysname>%s</sysname><platform>%s</platform><uptime>%ld</uptime></os>", \ |
466 |
> |
general_stats->os_name, \ |
467 |
> |
general_stats->os_release, \ |
468 |
> |
general_stats->os_version, \ |
469 |
> |
general_stats->hostname, \ |
470 |
> |
general_stats->platform, \ |
471 |
> |
(long)general_stats->uptime); |
472 |
> |
|
473 |
> |
if(strlcat(xml, tmp, size) >= size) goto too_big_error; |
474 |
> |
|
475 |
> |
} |
476 |
|
|
477 |
< |
return exitcode; |
478 |
< |
} |
477 |
> |
|
478 |
> |
/* process stats */ |
479 |
> |
if((process_stats=get_process_stats())==NULL){ |
480 |
> |
log_msg(LOG_CRIT, "Failed to get general statistics"); |
481 |
> |
}else{ |
482 |
> |
snprintf(tmp, size, \ |
483 |
> |
"<processes><sleeping>%d</sleeping><cpu>%d</cpu><zombie>%d</zombie><stopped>%d</stopped><total>%d</total></processes>",\ |
484 |
> |
process_stats->sleeping, \ |
485 |
> |
process_stats->running, \ |
486 |
> |
process_stats->zombie, \ |
487 |
> |
process_stats->stopped, \ |
488 |
> |
process_stats->total); |
489 |
|
|
490 |
< |
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 x=0; |
490 |
> |
if(strlcat(xml, tmp, size) >= size) goto too_big_error; |
491 |
|
|
492 |
< |
logmessage(LOG_DEBUG,"get_cpu_stats"); |
379 |
< |
stats[0]=get_cpu_stats(); |
380 |
< |
logmessage(LOG_DEBUG,"get_disk_stats"); |
381 |
< |
stats[1]=get_disk_stats(); |
382 |
< |
logmessage(LOG_DEBUG,"get_load_stats"); |
383 |
< |
stats[2]=get_load_stats(); |
384 |
< |
logmessage(LOG_DEBUG,"get_memory_stats"); |
385 |
< |
stats[3]=get_memory_stats(); |
386 |
< |
logmessage(LOG_DEBUG,"get_os_info"); |
387 |
< |
stats[4]=get_os_info(); |
388 |
< |
logmessage(LOG_DEBUG,"get_page_stats"); |
389 |
< |
stats[5]=get_page_stats(); |
390 |
< |
logmessage(LOG_DEBUG,"get_process_stats"); |
391 |
< |
stats[6]=get_process_stats(); |
392 |
< |
logmessage(LOG_DEBUG,"get_swap_stats"); |
393 |
< |
stats[7]=get_swap_stats(); |
394 |
< |
logmessage(LOG_DEBUG,"get_user_stats"); |
395 |
< |
stats[8]=get_user_stats(); |
492 |
> |
} |
493 |
|
|
494 |
< |
for(;x<NUM_STATS;x++){ |
495 |
< |
if(stats[x]==NULL){ |
496 |
< |
logmessage(LOG_ERR,"Function returned NULL"); |
497 |
< |
return NULL; |
494 |
> |
|
495 |
> |
/* Get paging stats */ |
496 |
> |
if((page_stats=get_page_stats_diff())==NULL){ |
497 |
> |
log_msg(LOG_CRIT, "Failed to get paging statistics"); |
498 |
> |
}else{ |
499 |
> |
if(page_stats->systime!=0){ |
500 |
> |
x=page_stats->pages_pagein / page_stats->systime; |
501 |
> |
y=page_stats->pages_pageout / page_stats->systime; |
502 |
> |
}else{ |
503 |
> |
x=page_stats->pages_pagein; |
504 |
> |
y=page_stats->pages_pageout; |
505 |
|
} |
506 |
< |
if(xml_data==NULL){ |
507 |
< |
if((xml_data=strf("%s", stats[x])) == NULL){ |
508 |
< |
logmessage(LOG_ERR, "str failed (%m)"); |
509 |
< |
return NULL; |
506 |
> |
snprintf(tmp, size, \ |
507 |
> |
"<pages><swapins>%lld</swapins><swapouts>%lld</swapouts></pages>", \ |
508 |
> |
x, \ |
509 |
> |
y); |
510 |
> |
|
511 |
> |
if(strlcat(xml, tmp, size) >= size) goto too_big_error; |
512 |
> |
} |
513 |
> |
|
514 |
> |
|
515 |
> |
/* get diskio stats */ |
516 |
> |
|
517 |
> |
if((diskio_stats=get_diskio_stats_diff(&diskio_entries))==NULL){ |
518 |
> |
log_msg(LOG_CRIT, "Failed to get diskio statistics"); |
519 |
> |
}else{ |
520 |
> |
strlcat(xml, "<diskio>", size); |
521 |
> |
for(counter=0;counter<diskio_entries;counter++){ |
522 |
> |
|
523 |
> |
if(diskio_stats->systime!=0){ |
524 |
> |
x=diskio_stats->read_bytes / diskio_stats->systime; |
525 |
> |
y=diskio_stats->write_bytes / diskio_stats->systime; |
526 |
> |
}else{ |
527 |
> |
x=diskio_stats->read_bytes; |
528 |
> |
y=diskio_stats->write_bytes; |
529 |
|
} |
530 |
< |
}else{ |
531 |
< |
xml_data_p=xml_data; |
532 |
< |
if((xml_data=strf("%s%s", xml_data, stats[x])) == NULL){ |
533 |
< |
logmessage(LOG_ERR, "str failed (%m)"); |
534 |
< |
return NULL; |
535 |
< |
} |
536 |
< |
free(xml_data_p); |
530 |
> |
|
531 |
> |
snprintf(tmp, size, \ |
532 |
> |
"<p%d name=\"%s\" rbytes=\"%lld\" wbytes=\"%lld\"></p%d>", \ |
533 |
> |
counter, \ |
534 |
> |
diskio_stats->disk_name, \ |
535 |
> |
x, \ |
536 |
> |
y, \ |
537 |
> |
counter); |
538 |
> |
|
539 |
> |
strlcat(xml, tmp, size); |
540 |
> |
diskio_stats++; |
541 |
|
} |
542 |
< |
free(stats[x]); |
542 |
> |
|
543 |
> |
if(strlcat(xml, "</diskio>", size) >= size) goto too_big_error; |
544 |
> |
|
545 |
|
} |
417 |
– |
xml_data_p=xml_data; |
418 |
– |
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); |
419 |
– |
free(xml_data_p); |
546 |
|
|
547 |
< |
logmessage(LOG_DEBUG,"Generated XML Data of : %s", xml_data); |
548 |
< |
return xml_data; |
549 |
< |
} |
547 |
> |
|
548 |
> |
/* get networks stats */ |
549 |
> |
|
550 |
> |
if((network_stats=get_network_stats_diff(&network_entries))==NULL){ |
551 |
> |
log_msg(LOG_CRIT, "Failed to get network statistics"); |
552 |
> |
}else{ |
553 |
> |
strlcat(xml, "<net>", size); |
554 |
> |
for(counter=0;counter<network_entries;counter++){ |
555 |
> |
if(network_stats->systime!=0){ |
556 |
> |
x=network_stats->rx / network_stats->systime; |
557 |
> |
y=network_stats->tx / network_stats->systime; |
558 |
> |
}else{ |
559 |
> |
x=network_stats->rx; |
560 |
> |
y=network_stats->tx; |
561 |
> |
} |
562 |
|
|
563 |
< |
int send_stats(ihost_state_t *ihost_state, char *data_stream){ |
564 |
< |
struct sockaddr_in addr; |
565 |
< |
struct in_addr haddr; |
563 |
> |
snprintf(tmp, size, \ |
564 |
> |
"<p%d name=\"%s\" rx=\"%lld\" tx=\"%lld\"></p%d>", \ |
565 |
> |
counter, \ |
566 |
> |
network_stats->interface_name, \ |
567 |
> |
x, \ |
568 |
> |
y, \ |
569 |
> |
counter); |
570 |
|
|
571 |
< |
int sd; |
572 |
< |
size_t len; |
571 |
> |
strlcat(xml, tmp, size); |
572 |
> |
network_stats++; |
573 |
> |
} |
574 |
|
|
575 |
< |
len=strlen(data_stream); |
576 |
< |
if(len>UDP_MAX_PACKET_SIZE){ |
434 |
< |
logmessage(LOG_ERR, "Too big to send to server. Please reconfigure client and server and recompile"); |
435 |
< |
exit(1); |
575 |
> |
if(strlcat(xml, "</net>", size) >= size) goto too_big_error; |
576 |
> |
|
577 |
|
} |
437 |
– |
logmessage(LOG_DEBUG,"Resolving IP of server"); |
438 |
– |
if (get_host_addr(ihost_state->server_fqdn, &haddr) != 0){ |
439 |
– |
logmessage(LOG_ERR, "Failed to resolve address %s (%m)", ihost_state->server_fqdn); |
440 |
– |
return -1; |
441 |
– |
} |
442 |
– |
logmessage(LOG_DEBUG,"Creating UDP connection to %s on %d",ihost_state->server_fqdn, ihost_state->server_udp_port); |
443 |
– |
if((sd=socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0){ |
444 |
– |
logmessage(LOG_ERR, "failed to create UDP socket (%m)"); |
445 |
– |
return -1; |
446 |
– |
} |
578 |
|
|
448 |
– |
memset(&addr, 0, sizeof(addr)); |
449 |
– |
addr.sin_family=AF_INET; |
450 |
– |
memcpy((char *)&addr.sin_addr, &haddr, sizeof haddr); |
451 |
– |
addr.sin_port = htons(ihost_state->server_udp_port); |
579 |
|
|
580 |
< |
logmessage(LOG_INFO,"Sending packet : %s", data_stream); |
581 |
< |
if((sendto(sd, data_stream, len, 0, (struct sockaddr *) &addr, sizeof(addr))) != len){ |
582 |
< |
logmessage(LOG_ERR, "Send the wrong number of bytes (%m)"); |
583 |
< |
return -1; |
580 |
> |
/* get disk stats */ |
581 |
> |
|
582 |
> |
if((disk_stats=get_disk_stats(&disk_entries))==NULL){ |
583 |
> |
log_msg(LOG_CRIT, "Failed to get disk statistics"); |
584 |
> |
}else{ |
585 |
> |
strlcat(xml, "<disk>", size); |
586 |
> |
for(counter=0;counter<disk_entries;counter++){ |
587 |
> |
snprintf(tmp, size, \ |
588 |
> |
"<p%d name=\"%s\" mount=\"%s\" fstype=\"%s\" total=\"%lld\" used=\"%lld\" avail=\"%lld\" totalinodes=\"%lld\" usedinodes=\"%lld\" freeinodes=\"%lld\"></p%d>", \ |
589 |
> |
counter, \ |
590 |
> |
disk_stats->device_name, \ |
591 |
> |
disk_stats->mnt_point, \ |
592 |
> |
disk_stats->fs_type, \ |
593 |
> |
disk_stats->size, \ |
594 |
> |
disk_stats->used, \ |
595 |
> |
disk_stats->avail, \ |
596 |
> |
disk_stats->total_inodes, \ |
597 |
> |
disk_stats->used_inodes, \ |
598 |
> |
disk_stats->free_inodes, \ |
599 |
> |
counter); |
600 |
> |
|
601 |
> |
strlcat(xml, tmp, size); |
602 |
> |
|
603 |
> |
disk_stats++; |
604 |
> |
} |
605 |
> |
|
606 |
> |
if(strlcat(xml, "</disk>", size) >= size) goto too_big_error; |
607 |
> |
|
608 |
|
} |
609 |
+ |
|
610 |
|
|
611 |
< |
close(sd); |
611 |
> |
if(strlcat(xml, "</packet>", size) >= size) goto too_big_error; |
612 |
|
|
613 |
< |
return 0; |
613 |
> |
/*If we got to here, it should of all been filled in nicely now */ |
614 |
> |
return 0; |
615 |
> |
|
616 |
> |
too_big_error: |
617 |
> |
log_msg(LOG_ERR, "UDP Packet is too large. Throwing away the packet"); |
618 |
> |
return -1; |
619 |
|
} |
620 |
|
|
621 |
+ |
|
622 |
+ |
|
623 |
|
void usage(char *progname){ |
624 |
< |
fprintf(stderr, "Usage %s [options] server port\n", progname); |
625 |
< |
fprintf(stderr, "Options\n"); |
626 |
< |
fprintf(stderr, " -v Verbose, the more v flags the more verbose, eg -vv\n"); |
627 |
< |
fprintf(stderr, " -d Daemon mode, self backgrounding\n"); |
628 |
< |
fprintf(stderr, " -s Send errors to syslog\n"); |
629 |
< |
fprintf(stderr, " -V Print version number\n"); |
630 |
< |
fprintf(stderr, " -h Prints this help page\n"); |
472 |
< |
exit(1); |
624 |
> |
fprintf(stderr, "Usage %s [options] server port\n", progname); |
625 |
> |
fprintf(stderr, "Options\n"); |
626 |
> |
fprintf(stderr, " -v Verbose mode,-vv would make even more verbose\n"); |
627 |
> |
fprintf(stderr, " -f Foreground mode, print errors to stderr\n"); |
628 |
> |
fprintf(stderr, " -V Print version number\n"); |
629 |
> |
fprintf(stderr, " -h Prints this help page\n"); |
630 |
> |
exit(1); |
631 |
|
} |
632 |
|
|
633 |
|
int main(int argc, char **argv){ |
634 |
+ |
|
635 |
|
ihost_state_t ihost_state; |
636 |
< |
int heartbeat_exit; |
637 |
< |
int counter=0; |
479 |
< |
long udp_time=0, tcp_time=0, stat_grab_time=0, cur_time=0; |
480 |
< |
int sleep_delay=0; |
481 |
< |
char *xml_stats; |
482 |
< |
pid_t pid; |
636 |
> |
udp_sockinfo_t udp_sockinfo; |
637 |
> |
|
638 |
|
int cmdopt; |
639 |
|
extern int optind; |
640 |
< |
int verbose=0, daemon=0; |
486 |
< |
extern int syslog_logging; |
487 |
< |
extern int log_level; |
488 |
< |
extern int cur_level; |
640 |
> |
pid_t pid; |
641 |
|
FILE *f; |
642 |
+ |
int packet_num=0; |
643 |
+ |
int len; |
644 |
|
|
645 |
< |
log_level=1; |
492 |
< |
cur_level=1; |
493 |
< |
syslog_logging=0; |
645 |
> |
char packet[MAX_UDP_PACKET_SIZE]; |
646 |
|
|
647 |
< |
errf_set_ofunc(log_errors); |
496 |
< |
/* NULL'ify so i can tell if i need to free it or not */ |
497 |
< |
ihost_state.fm_host=NULL; |
498 |
< |
ihost_state.my_fqdn=NULL; |
499 |
< |
ihost_state.server_fqdn=NULL; |
500 |
< |
ihost_state.last_modified=NULL; |
501 |
< |
ihost_state.files_list=NULL; |
502 |
< |
ihost_state.key=NULL; |
647 |
> |
time_t cur_time, sleep_delay, udp_time=0, config_time=0; |
648 |
|
|
649 |
< |
errf_set_progname(argv[0]); |
650 |
< |
|
649 |
> |
/* Set default settings */ |
650 |
> |
ihost_config.verbose=1; |
651 |
> |
ihost_config.daemon=1; |
652 |
> |
/* Set all errors to go down stderr until told otherwise */ |
653 |
> |
ihost_config.log=stderr; |
654 |
|
|
655 |
< |
while((cmdopt=getopt(argc, argv, "vdshV")) != -1){ |
656 |
< |
switch(cmdopt){ |
657 |
< |
case 'v': |
658 |
< |
verbose++; |
659 |
< |
break; |
660 |
< |
|
661 |
< |
case 'd': |
514 |
< |
/* Force syslog logging since stderr will be closed in this case */ |
515 |
< |
syslog_logging=1; |
516 |
< |
daemon=1; |
517 |
< |
break; |
518 |
< |
|
519 |
< |
case 's': |
520 |
< |
syslog_logging=1; |
521 |
< |
break; |
522 |
< |
|
523 |
< |
case 'h': |
524 |
< |
usage(argv[0]); |
525 |
< |
break; |
655 |
> |
/* Blank ihost_state to default settings */ |
656 |
> |
ihost_state.filtermanager_host=NULL; |
657 |
> |
ihost_state.host_fqdn=NULL; |
658 |
> |
ihost_state.host_ip=NULL; |
659 |
> |
ihost_state.server_fqdn=NULL; |
660 |
> |
ihost_state.file_list=NULL; |
661 |
> |
ihost_state.last_modified=NULL; |
662 |
|
|
663 |
< |
case 'V': |
664 |
< |
errf("%s version %f",argv[0], versionNo); |
665 |
< |
break; |
663 |
> |
while((cmdopt=getopt(argc, argv, "vfhV")) != -1){ |
664 |
> |
switch(cmdopt){ |
665 |
> |
case 'v': |
666 |
> |
ihost_config.verbose++; |
667 |
> |
break; |
668 |
|
|
669 |
< |
default: |
670 |
< |
usage(argv[0]); |
671 |
< |
exit(1); |
672 |
< |
} |
535 |
< |
} |
669 |
> |
case 'f': |
670 |
> |
/* Force syslog logging since stderr will be closed in this case */ |
671 |
> |
ihost_config.daemon=0; |
672 |
> |
break; |
673 |
|
|
674 |
< |
if(argc!=optind+2){ |
675 |
< |
usage(argv[0]); |
674 |
> |
case 'h': |
675 |
> |
usage(argv[0]); |
676 |
> |
break; |
677 |
> |
|
678 |
> |
case 'V': |
679 |
> |
fprintf(stderr, "%s version %s\n", argv[0], VERSION); |
680 |
> |
break; |
681 |
> |
|
682 |
> |
default: |
683 |
> |
usage(argv[0]); |
684 |
> |
exit(1); |
685 |
> |
} |
686 |
> |
} |
687 |
> |
|
688 |
> |
if(argc!=optind+2){ |
689 |
> |
usage(argv[0]); |
690 |
> |
exit(1); |
691 |
> |
} |
692 |
> |
|
693 |
> |
ihost_state.filtermanager_host=strdup(argv[optind]); |
694 |
> |
ihost_state.filtermanager_port=atoi(argv[optind+1]); |
695 |
> |
|
696 |
> |
if(gethostbyname(ihost_state.filtermanager_host)==NULL){ |
697 |
> |
log_msg(LOG_CRIT, "Failed to lookup hostname. Please check settings"); |
698 |
|
exit(1); |
699 |
|
} |
700 |
< |
ihost_state.fm_host=argv[optind]; |
701 |
< |
ihost_state.fm_port=atoi(argv[optind+1]); |
702 |
< |
if(ihost_state.fm_port==0){ |
544 |
< |
errf("Invalid port number"); |
545 |
< |
usage(argv[0]); |
700 |
> |
if(ihost_state.filtermanager_port==0){ |
701 |
> |
log_msg(LOG_ERR, "Invalid port number"); |
702 |
> |
exit(1); |
703 |
|
} |
704 |
|
|
705 |
< |
if(daemon==1){ |
705 |
> |
if(ihost_config.daemon){ |
706 |
|
pid=fork(); |
707 |
|
if(pid==-1){ |
708 |
< |
errf("Fork failed, can't background. Exiting"); |
709 |
< |
exit(1); |
708 |
> |
log_msg(LOG_CRIT, "Failed to background exiting"); |
709 |
> |
exit(1); |
710 |
|
}else if(pid!=0){ |
711 |
< |
/* Parent process */ |
712 |
< |
return 0; |
713 |
< |
} |
714 |
< |
/* We should now be in the background*/ |
715 |
< |
if(setsid()==-1){ |
716 |
< |
errf("setsid failed (%m)"); |
711 |
> |
/* Parent process */ |
712 |
> |
return 0; |
713 |
> |
} |
714 |
> |
/* We should now be in the background*/ |
715 |
> |
if(setsid()==-1){ |
716 |
> |
log_msg(LOG_CRIT, "setsid failed"); |
717 |
> |
exit(1); |
718 |
> |
} |
719 |
> |
|
720 |
> |
if((ihost_config.log=fopen(LOG_FILE, "a"))==NULL){ |
721 |
> |
ihost_config.log=stderr; |
722 |
> |
log_msg(LOG_CRIT, "Failed to open Logfiles %s for writing", LOG_FILE); |
723 |
|
exit(1); |
724 |
|
} |
562 |
– |
fclose(stdin); |
563 |
– |
fclose(stdout); |
564 |
– |
fclose(stderr); |
565 |
– |
} |
725 |
|
|
726 |
< |
if(syslog_logging==1){ |
727 |
< |
openlog(errf_get_progname(),0,LOG_ERR); |
728 |
< |
setlogmask(LOG_UPTO(LOG_DEBUG)); |
570 |
< |
} |
571 |
< |
|
572 |
< |
switch(verbose){ |
573 |
< |
case 0: |
574 |
< |
/* Critical errors + */ |
575 |
< |
log_level=LOG_ERR; |
576 |
< |
break; |
577 |
< |
case 1: |
578 |
< |
/* Recoverable errors */ |
579 |
< |
log_level=LOG_WARNING; |
580 |
< |
break; |
581 |
< |
case 2: |
582 |
< |
/* Print stuff like the XML packets */ |
583 |
< |
log_level=LOG_INFO; |
584 |
< |
break; |
585 |
< |
default: |
586 |
< |
/* Must have lots of v's */ |
587 |
< |
/* Print out everything its doing */ |
588 |
< |
log_level=LOG_DEBUG; |
589 |
< |
break; |
590 |
< |
} |
726 |
> |
fclose(stdin); |
727 |
> |
fclose(stdout); |
728 |
> |
fclose(stderr); |
729 |
|
|
730 |
< |
logmessage(LOG_DEBUG,"Writing PID FILE"); |
731 |
< |
pid=getpid(); |
732 |
< |
if((f=fopen(PID_FILE,"w")) == NULL){ |
733 |
< |
logmessage(LOG_WARNING, "Failed to write PID file"); |
734 |
< |
}else{ |
735 |
< |
if((fprintf(f,"%d",(int)pid)) != sizeof(pid)){ |
736 |
< |
logmessage(LOG_WARNING, "Failed to write PID file"); |
737 |
< |
} |
738 |
< |
if((fclose(f))!=0){ |
739 |
< |
logmessage(LOG_ERR, "failed to close PID file"); |
740 |
< |
exit(1); |
741 |
< |
} |
730 |
> |
} |
731 |
> |
|
732 |
> |
log_msg(LOG_INFO, "Starting ihost"); |
733 |
> |
|
734 |
> |
log_msg(LOG_DEBUG,"Writing PID FILE"); |
735 |
> |
|
736 |
> |
pid=getpid(); |
737 |
> |
|
738 |
> |
if((f=fopen(PID_FILE,"w")) == NULL){ |
739 |
> |
log_msg(LOG_CRIT, "Failed to write PID file"); |
740 |
> |
}else{ |
741 |
> |
if((fprintf(f,"%d",(int)pid)) <= 0 ){ |
742 |
> |
log_msg(LOG_CRIT, "Failed to write PID file"); |
743 |
> |
} |
744 |
> |
if((fclose(f))!=0){ |
745 |
> |
log_msg(LOG_CRIT, "failed to close PID file"); |
746 |
> |
} |
747 |
> |
} |
748 |
> |
|
749 |
> |
/* Get the initial config from the filter manager. Should this fail, |
750 |
> |
* wait, and then try again. */ |
751 |
> |
|
752 |
> |
get_diskio_stats_diff(&packet_num); |
753 |
> |
packet_num=0; |
754 |
> |
|
755 |
> |
while(ihost_getconfig(&ihost_state)!=0){ |
756 |
> |
log_msg(LOG_ERR, "Failed to get ihost config"); |
757 |
> |
sleep(10); |
758 |
|
} |
759 |
|
|
760 |
< |
if(ihost_configure(&ihost_state)!=0){ |
761 |
< |
logmessage(LOG_ERR,"configure failed"); |
762 |
< |
/* Ok, ideally we prob should have 2 copies of the structure and carry on if this |
763 |
< |
happens.. But we dont :) (at the moment) */ |
610 |
< |
exit(1); |
760 |
> |
printf("%s\n%d\n", ihost_state.server_fqdn, ihost_state.server_udp_port); |
761 |
> |
while((create_udp_sockinfo(&udp_sockinfo, ihost_state.server_fqdn, ihost_state.server_udp_port))!=0){ |
762 |
> |
log_msg(LOG_ERR, "Failed to create udp socket"); |
763 |
> |
sleep(10); |
764 |
|
} |
765 |
|
|
766 |
+ |
config_time=time(NULL)+ihost_state.config_ttl; |
767 |
+ |
|
768 |
+ |
/* Now have config.. collect data and send as often as required */ |
769 |
|
for(;;){ |
770 |
|
cur_time=time(NULL); |
615 |
– |
if(cur_time>=tcp_time){ |
616 |
– |
logmessage(LOG_DEBUG,"Sending heartbeat"); |
617 |
– |
heartbeat_exit=heartbeat(&ihost_state); |
618 |
– |
if(heartbeat_exit==RECONFIGURE_RETURN_CODE){ |
619 |
– |
logmessage(LOG_INFO,"heartbeat needs to be reconfigured"); |
620 |
– |
ihost_configure(&ihost_state); |
621 |
– |
udp_time=0; |
622 |
– |
} |
623 |
– |
if(heartbeat_exit==-1){ |
624 |
– |
logmessage(LOG_ERR,"Heartbeat failed"); |
625 |
– |
exit(1); |
626 |
– |
} |
627 |
– |
tcp_time=time(NULL)+ihost_state.tcp_update_time; |
628 |
– |
logmessage(LOG_DEBUG,"next tcp time should be %d", tcp_time); |
629 |
– |
} |
771 |
|
|
772 |
|
if(cur_time>=udp_time){ |
773 |
< |
logmessage(LOG_DEBUG,"Sending udp data"); |
773 |
> |
if((get_system_stats(packet_num++, &ihost_state, packet, MAX_UDP_PACKET_SIZE))!=0){ |
774 |
> |
log_msg(LOG_ERR, "Failed to get system stats"); |
775 |
> |
} |
776 |
|
|
777 |
< |
stat_grab_time=time(NULL); |
778 |
< |
if((xml_stats=stat_grab(&ihost_state, counter++)) == NULL){ |
779 |
< |
logmessage(LOG_ERR,"Failed to get stats (%m)"); |
780 |
< |
exit(1); |
777 |
> |
len=strlen(packet); |
778 |
> |
log_msg(LOG_DEBUG, "Packet size: %d\nPacket: %s\n", len, packet); |
779 |
> |
|
780 |
> |
if((sendto(udp_sockinfo.sock, packet, len, 0, (struct sockaddr *) &udp_sockinfo.addr, sizeof(udp_sockinfo.addr)))!=len){ |
781 |
> |
log_msg(LOG_CRIT, "Failed to send packet"); |
782 |
|
} |
783 |
< |
stat_grab_time=time(NULL)-stat_grab_time; |
784 |
< |
send_stats(&ihost_state, xml_stats); |
641 |
< |
free(xml_stats); |
642 |
< |
udp_time=time(NULL)+ihost_state.udp_update_time-stat_grab_time; |
643 |
< |
logmessage(LOG_DEBUG,"next udp time should be %d", udp_time); |
783 |
> |
udp_time=cur_time+ihost_state.udp_update_time; |
784 |
> |
log_msg(LOG_DEBUG, "Next packet should be sent on %d", udp_time); |
785 |
|
} |
786 |
+ |
|
787 |
+ |
if(cur_time>=config_time){ |
788 |
+ |
if(ihost_getconfig(&ihost_state)!=0){ |
789 |
+ |
/* If we can't get the config, try again 5 minutes time */ |
790 |
+ |
log_msg(LOG_ERR, "Failed to get config, try again 5 minutes time"); |
791 |
+ |
config_time=time(NULL)+300; |
792 |
+ |
}else{ |
793 |
+ |
close(udp_sockinfo.sock); |
794 |
|
|
795 |
< |
if(tcp_time<udp_time){ |
796 |
< |
sleep_delay=tcp_time-time(NULL); |
797 |
< |
}else{ |
798 |
< |
sleep_delay=udp_time-time(NULL); |
795 |
> |
while((create_udp_sockinfo(&udp_sockinfo, ihost_state.server_fqdn, ihost_state.server_udp_port))!=0){ |
796 |
> |
log_msg(LOG_CRIT, "Failed to create udp socket"); |
797 |
> |
sleep(10); |
798 |
> |
} |
799 |
> |
|
800 |
> |
config_time=time(NULL)+ihost_state.config_ttl; |
801 |
> |
|
802 |
> |
log_msg(LOG_DEBUG, "Config expires on %d\n", ihost_state.config_ttl); |
803 |
> |
} |
804 |
|
} |
805 |
< |
logmessage(LOG_DEBUG,"Sleeping for %d", sleep_delay); |
805 |
> |
|
806 |
> |
sleep_delay=udp_time-time(NULL); |
807 |
> |
log_msg(LOG_DEBUG, "Sleeping for %d", sleep_delay); |
808 |
|
if(sleep_delay>0) sleep(sleep_delay); |
809 |
< |
} |
810 |
< |
return 0; |
809 |
> |
} |
810 |
> |
|
811 |
> |
return(0); |
812 |
|
} |
656 |
– |
|