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