1 |
tdb |
1.11 |
/* |
2 |
|
|
* i-scream central monitoring system |
3 |
|
|
* Copyright (C) 2000-2002 i-scream |
4 |
|
|
* |
5 |
|
|
* This program is free software; you can redistribute it and/or |
6 |
|
|
* modify it under the terms of the GNU General Public License |
7 |
|
|
* as published by the Free Software Foundation; either version 2 |
8 |
|
|
* of the License, or (at your option) any later version. |
9 |
|
|
* |
10 |
|
|
* This program is distributed in the hope that it will be useful, |
11 |
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
12 |
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
13 |
|
|
* GNU General Public License for more details. |
14 |
|
|
* |
15 |
|
|
* You should have received a copy of the GNU General Public License |
16 |
|
|
* along with this program; if not, write to the Free Software |
17 |
|
|
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
18 |
|
|
*/ |
19 |
|
|
|
20 |
pajs |
1.1 |
#include <stdio.h> |
21 |
|
|
#include <stdlib.h> |
22 |
pajs |
1.18 |
#include <sys/types.h> |
23 |
pajs |
1.1 |
#include <unistd.h> |
24 |
|
|
#include <syslog.h> |
25 |
|
|
#include <netinet/in.h> |
26 |
pajs |
1.9 |
#include "ukcprog.h" |
27 |
pajs |
1.1 |
#include <netdb.h> |
28 |
pajs |
1.12 |
#include <string.h> |
29 |
pajs |
1.9 |
#include "statgrab.h" |
30 |
pajs |
1.12 |
#include <time.h> |
31 |
pajs |
1.14 |
#include <sys/socket.h> |
32 |
pajs |
1.16 |
#include <netinet/in.h> |
33 |
|
|
#include <arpa/inet.h> |
34 |
pajs |
1.18 |
#include <syslog.h> |
35 |
pajs |
1.1 |
|
36 |
pajs |
1.17 |
#define versionNo 0.8 |
37 |
pajs |
1.4 |
#define RECONFIGURE_RETURN_CODE 2 |
38 |
pajs |
1.9 |
#define UDP_MAX_PACKET_SIZE 8192 |
39 |
pajs |
1.19 |
#define PID_FILE "/var/tmp/.ihost.pid" |
40 |
pajs |
1.4 |
|
41 |
pajs |
1.18 |
#define logmessage(level, ...) do { cur_level = level; errf(__VA_ARGS__); } while (0) |
42 |
|
|
|
43 |
pajs |
1.1 |
typedef struct{ |
44 |
|
|
int fm_port; |
45 |
|
|
char *fm_host; |
46 |
pajs |
1.16 |
|
47 |
|
|
char *my_ip; |
48 |
pajs |
1.1 |
char *my_fqdn; |
49 |
|
|
char *server_fqdn; |
50 |
|
|
int server_udp_port; |
51 |
|
|
int server_tcp_port; |
52 |
pajs |
1.4 |
char *last_modified; |
53 |
pajs |
1.1 |
char *files_list; |
54 |
|
|
char *key; |
55 |
|
|
int udp_update_time; |
56 |
|
|
int tcp_update_time; |
57 |
|
|
|
58 |
|
|
}ihost_state_t; |
59 |
|
|
|
60 |
pajs |
1.18 |
static int log_level; |
61 |
|
|
static int cur_level; |
62 |
|
|
static int syslog_logging; |
63 |
|
|
|
64 |
|
|
void log_errors(const char *message){ |
65 |
|
|
if(log_level>=cur_level){ |
66 |
|
|
if (syslog_logging==1){ |
67 |
|
|
syslog(cur_level, "%s\n", message); |
68 |
|
|
}else{ |
69 |
|
|
fprintf(stderr, "%s\n", message); |
70 |
|
|
} |
71 |
|
|
} |
72 |
|
|
} |
73 |
|
|
|
74 |
pajs |
1.7 |
char* sock_comm(FILE *f_r, FILE *f_w, char *sendString){ |
75 |
pajs |
1.1 |
char *reply; |
76 |
pajs |
1.7 |
fprintf(f_w, "%s\n", sendString); |
77 |
pajs |
1.2 |
fflush(f_w); |
78 |
|
|
reply=fpgetline(f_r); |
79 |
pajs |
1.1 |
/* Returns pointer to static buffer */ |
80 |
|
|
return reply; |
81 |
|
|
} |
82 |
|
|
|
83 |
|
|
int ihost_configure(ihost_state_t *ihost_state){ |
84 |
|
|
struct sockaddr_in addr; |
85 |
|
|
struct in_addr haddr; |
86 |
pajs |
1.16 |
struct sockaddr ip; |
87 |
|
|
int ip_len; |
88 |
pajs |
1.1 |
int sd; |
89 |
pajs |
1.2 |
FILE *fm_fd_r, *fm_fd_w; |
90 |
pajs |
1.1 |
char *reply; |
91 |
pajs |
1.2 |
char *reply_ptr; |
92 |
pajs |
1.1 |
|
93 |
pajs |
1.5 |
/* 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); |
98 |
|
|
|
99 |
pajs |
1.18 |
logmessage(LOG_DEBUG, "Setting up configure socket to %s on port %d", ihost_state->fm_host, ihost_state->fm_port); |
100 |
pajs |
1.1 |
if ((sd = socket(AF_INET, SOCK_STREAM, PF_UNSPEC)) < 0) { |
101 |
pajs |
1.18 |
logmessage(LOG_ERR, "Can't create AF_INET socket (%m)"); |
102 |
pajs |
1.1 |
return -1; |
103 |
|
|
} |
104 |
|
|
|
105 |
|
|
if (get_host_addr(ihost_state->fm_host, &haddr) != 0){ |
106 |
pajs |
1.18 |
logmessage(LOG_ERR, "Failed to resolve address %s (%m)", ihost_state->fm_host); |
107 |
pajs |
1.1 |
return -1; |
108 |
|
|
} |
109 |
|
|
|
110 |
pajs |
1.9 |
memset(&addr, 0, sizeof addr); |
111 |
pajs |
1.1 |
addr.sin_family = AF_INET; |
112 |
pajs |
1.9 |
memcpy(&addr.sin_addr, &haddr, sizeof haddr); |
113 |
pajs |
1.1 |
addr.sin_port = htons(ihost_state->fm_port); |
114 |
|
|
|
115 |
|
|
if (connect(sd, (struct sockaddr *)&addr, sizeof addr) != 0) { |
116 |
pajs |
1.18 |
logmessage(LOG_ERR, "Failed to connect to %s on port %d (%m)", ihost_state->fm_host, ihost_state->fm_port); |
117 |
pajs |
1.1 |
return -1; |
118 |
|
|
} |
119 |
|
|
|
120 |
pajs |
1.2 |
/* Need to open 2 files, one for reading one for writing, as it gets confused if we only use 1 :) */ |
121 |
|
|
if ((fm_fd_r=fdopen(sd,"r")) == NULL){ |
122 |
pajs |
1.18 |
logmessage(LOG_ERR, "Failed to open read stream (%m)"); |
123 |
pajs |
1.2 |
return -1; |
124 |
|
|
} |
125 |
|
|
|
126 |
|
|
if ((fm_fd_w=fdopen(dup(sd),"w")) == NULL){ |
127 |
pajs |
1.18 |
logmessage(LOG_ERR, "Failed to open write stream (%m)"); |
128 |
pajs |
1.1 |
return -1; |
129 |
|
|
} |
130 |
pajs |
1.16 |
ip_len=sizeof ip; |
131 |
|
|
memset(&ip, 0, ip_len); |
132 |
|
|
if((getsockname(sd, &ip, &ip_len)) != 0){ |
133 |
pajs |
1.18 |
logmessage(LOG_ERR, "Failed to get IP address (%m)"); |
134 |
pajs |
1.16 |
return -1; |
135 |
|
|
} |
136 |
|
|
if (ip.sa_family!=AF_INET){ |
137 |
pajs |
1.18 |
logmessage(LOG_ERR, "sa family is wrong type"); |
138 |
pajs |
1.16 |
return -1; |
139 |
|
|
} |
140 |
|
|
|
141 |
|
|
if((ihost_state->my_ip=inet_ntoa(((struct sockaddr_in *)&ip)->sin_addr))==NULL){ |
142 |
pajs |
1.18 |
logmessage(LOG_ERR, "Failed to get IP (%m)"); |
143 |
pajs |
1.16 |
return -1; |
144 |
|
|
} |
145 |
pajs |
1.18 |
|
146 |
|
|
logmessage(LOG_DEBUG, "Sending STARTCONIG"); |
147 |
pajs |
1.7 |
reply=sock_comm(fm_fd_r, fm_fd_w, "STARTCONFIG"); |
148 |
pajs |
1.1 |
if ((reply==NULL) || (strncasecmp(reply, "OK", 2) != 0) ) { |
149 |
pajs |
1.18 |
logmessage(LOG_ERR, "Server error on STARTCONFIG"); |
150 |
pajs |
1.1 |
return -1; |
151 |
|
|
} |
152 |
|
|
|
153 |
pajs |
1.18 |
logmessage(LOG_DEBUG, "Sending LASTMODIFIED"); |
154 |
pajs |
1.7 |
reply=sock_comm(fm_fd_r, fm_fd_w, "LASTMODIFIED"); |
155 |
pajs |
1.2 |
if((reply== NULL) || (strncasecmp(reply, "ERROR", 5) ==0)){ |
156 |
pajs |
1.18 |
logmessage(LOG_ERR, "Server error on LASTMODIFIED (%m)"); |
157 |
pajs |
1.1 |
return -1; |
158 |
|
|
} |
159 |
pajs |
1.7 |
if((ihost_state->last_modified=strdup(reply)) == NULL){ |
160 |
pajs |
1.18 |
logmessage(LOG_ERR, "strdup failed (%m)"); |
161 |
pajs |
1.4 |
return -1; |
162 |
|
|
} |
163 |
pajs |
1.18 |
|
164 |
|
|
logmessage(LOG_DEBUG, "Sending FILELIST"); |
165 |
pajs |
1.7 |
reply=sock_comm(fm_fd_r, fm_fd_w, "FILELIST"); |
166 |
pajs |
1.2 |
if((reply== NULL) || (strncasecmp(reply, "ERROR", 5) ==0)){ |
167 |
pajs |
1.18 |
logmessage(LOG_ERR, "Server error on FILELIST (%m)"); |
168 |
pajs |
1.1 |
return -1; |
169 |
|
|
} |
170 |
pajs |
1.7 |
if((ihost_state->files_list=strdup(reply)) == NULL){ |
171 |
pajs |
1.18 |
logmessage(LOG_ERR, "strdup failed (%m)"); |
172 |
pajs |
1.6 |
return -1; |
173 |
|
|
} |
174 |
pajs |
1.1 |
|
175 |
pajs |
1.18 |
logmessage(LOG_DEBUG, "Sending FQDN"); |
176 |
pajs |
1.7 |
reply=sock_comm(fm_fd_r, fm_fd_w, "FQDN"); |
177 |
pajs |
1.2 |
if((reply== NULL) || (strncasecmp(reply, "ERROR", 5)==0)){ |
178 |
pajs |
1.18 |
logmessage(LOG_ERR, "Server error on FQDN (%m)"); |
179 |
pajs |
1.1 |
return -1; |
180 |
|
|
} |
181 |
|
|
if((ihost_state->my_fqdn=strdup(reply)) == NULL){ |
182 |
pajs |
1.18 |
logmessage(LOG_ERR, "strdup failed (%m)"); |
183 |
pajs |
1.1 |
return -1; |
184 |
|
|
} |
185 |
|
|
|
186 |
pajs |
1.18 |
logmessage(LOG_DEBUG, "Sending FQDN"); |
187 |
pajs |
1.7 |
reply=sock_comm(fm_fd_r, fm_fd_w, "UDPUpdateTime"); |
188 |
pajs |
1.1 |
if(reply== NULL){ |
189 |
pajs |
1.18 |
logmessage(LOG_ERR, "Server error (%m)"); |
190 |
pajs |
1.1 |
return -1; |
191 |
|
|
} |
192 |
|
|
if (strncasecmp(reply, "ERROR", 5) != 0){ |
193 |
|
|
ihost_state->udp_update_time=atoi(reply); |
194 |
|
|
} |
195 |
pajs |
1.18 |
|
196 |
|
|
logmessage(LOG_DEBUG, "Sending TCPUpdateTime"); |
197 |
pajs |
1.7 |
reply=sock_comm(fm_fd_r, fm_fd_w, "TCPUpdateTime"); |
198 |
pajs |
1.1 |
if(reply== NULL){ |
199 |
pajs |
1.18 |
logmessage(LOG_ERR, "Server error on TCPUpdateTime (%m)"); |
200 |
pajs |
1.1 |
return -1; |
201 |
|
|
} |
202 |
|
|
if (strncasecmp(reply, "ERROR", 5) != 0){ |
203 |
|
|
ihost_state->tcp_update_time=atoi(reply); |
204 |
|
|
} |
205 |
|
|
|
206 |
pajs |
1.18 |
logmessage(LOG_DEBUG, "Sending ENDCONFIG"); |
207 |
pajs |
1.7 |
reply=sock_comm(fm_fd_r, fm_fd_w, "ENDCONFIG"); |
208 |
pajs |
1.1 |
if(reply== NULL){ |
209 |
pajs |
1.18 |
logmessage(LOG_ERR, "Server error on ENDCONFIG (%m)"); |
210 |
pajs |
1.1 |
return -1; |
211 |
|
|
} |
212 |
|
|
|
213 |
pajs |
1.18 |
logmessage(LOG_DEBUG, "Sending FILTER"); |
214 |
pajs |
1.7 |
reply=sock_comm(fm_fd_r, fm_fd_w, "FILTER"); |
215 |
pajs |
1.2 |
if((reply== NULL) || (strncasecmp(reply, "ERROR", 5)==0)){ |
216 |
pajs |
1.18 |
logmessage(LOG_ERR, "Server error FILTER failed (%m)"); |
217 |
pajs |
1.2 |
return -1; |
218 |
|
|
} |
219 |
|
|
reply_ptr=strchr(reply,';'); |
220 |
|
|
if (reply_ptr==NULL){ |
221 |
pajs |
1.18 |
logmessage(LOG_ERR, "Incorrect data returned"); |
222 |
pajs |
1.2 |
return -1; |
223 |
|
|
} |
224 |
|
|
*reply_ptr='\0'; |
225 |
|
|
if((ihost_state->server_fqdn=strdup(reply)) == NULL){ |
226 |
pajs |
1.18 |
logmessage(LOG_ERR, "strdup failed (%m)"); |
227 |
pajs |
1.2 |
return -1; |
228 |
|
|
} |
229 |
pajs |
1.8 |
reply=reply_ptr + 1; |
230 |
pajs |
1.2 |
reply_ptr=strchr(reply,';'); |
231 |
|
|
if (reply_ptr==NULL){ |
232 |
pajs |
1.18 |
logmessage(LOG_ERR, "Incorrect data returned 2"); |
233 |
pajs |
1.2 |
return -1; |
234 |
|
|
} |
235 |
|
|
*reply_ptr='\0'; |
236 |
|
|
ihost_state->server_udp_port=atoi(reply); |
237 |
pajs |
1.8 |
reply=reply_ptr+1; |
238 |
pajs |
1.2 |
ihost_state->server_tcp_port=atoi(reply); |
239 |
|
|
if ((ihost_state->server_tcp_port==0) || (ihost_state->server_udp_port==0)){ |
240 |
pajs |
1.18 |
logmessage(LOG_ERR, "Incorrect data returned 3 "); |
241 |
pajs |
1.2 |
return -1; |
242 |
|
|
} |
243 |
|
|
|
244 |
pajs |
1.18 |
logmessage(LOG_DEBUG, "Sending END"); |
245 |
pajs |
1.7 |
reply=sock_comm(fm_fd_r, fm_fd_w, "END"); |
246 |
pajs |
1.2 |
if((reply== NULL) || (strncasecmp(reply, "ERROR", 5) ==0 )){ |
247 |
pajs |
1.18 |
logmessage(LOG_ERR, "Server error on END (%m)"); |
248 |
pajs |
1.2 |
return -1; |
249 |
|
|
} |
250 |
pajs |
1.1 |
|
251 |
pajs |
1.2 |
if(fclose(fm_fd_r) !=0){ |
252 |
pajs |
1.18 |
logmessage(LOG_ERR, "Failed to close read FD (%m)"); |
253 |
pajs |
1.2 |
return -1; |
254 |
|
|
} |
255 |
|
|
if(fclose(fm_fd_w) !=0){ |
256 |
pajs |
1.18 |
logmessage(LOG_ERR, "Failed to close write FD (%m)"); |
257 |
pajs |
1.2 |
return -1; |
258 |
|
|
} |
259 |
pajs |
1.1 |
|
260 |
|
|
return 0; |
261 |
|
|
} |
262 |
pajs |
1.4 |
|
263 |
|
|
int heartbeat(ihost_state_t *ihost_state){ |
264 |
|
|
struct sockaddr_in addr; |
265 |
|
|
struct in_addr haddr; |
266 |
|
|
int sd; |
267 |
|
|
FILE *fm_fd_r, *fm_fd_w; |
268 |
|
|
char *reply; |
269 |
|
|
int exitcode=0; |
270 |
|
|
|
271 |
pajs |
1.18 |
logmessage(LOG_DEBUG, "Setting up configure socket to %s on port %d", ihost_state->server_fqdn, ihost_state->server_tcp_port); |
272 |
pajs |
1.4 |
if ((sd = socket(AF_INET, SOCK_STREAM, PF_UNSPEC)) < 0) { |
273 |
pajs |
1.18 |
logmessage(LOG_ERR, "Can't create AF_INET socket (%m)"); |
274 |
pajs |
1.4 |
return -1; |
275 |
|
|
} |
276 |
|
|
|
277 |
|
|
if (get_host_addr(ihost_state->server_fqdn, &haddr) != 0){ |
278 |
pajs |
1.18 |
logmessage(LOG_ERR, "Failed to resolve address %s (%m)", ihost_state->server_fqdn); |
279 |
pajs |
1.4 |
return -1; |
280 |
|
|
} |
281 |
|
|
|
282 |
pajs |
1.9 |
memset(&addr, 0, sizeof addr); |
283 |
pajs |
1.4 |
addr.sin_family = AF_INET; |
284 |
pajs |
1.9 |
memcpy(&addr.sin_addr, &haddr, sizeof haddr); |
285 |
pajs |
1.4 |
addr.sin_port = htons(ihost_state->server_tcp_port); |
286 |
|
|
|
287 |
|
|
if (connect(sd, (struct sockaddr *)&addr, sizeof addr) != 0) { |
288 |
pajs |
1.18 |
logmessage(LOG_ERR, "Failed to connect to %s on port %d (%m)", ihost_state->server_fqdn, ihost_state->server_tcp_port); |
289 |
pajs |
1.4 |
return -1; |
290 |
|
|
} |
291 |
|
|
|
292 |
|
|
/* Need to open 2 files, one for reading one for writing, as it gets confused if we only use 1 :) */ |
293 |
|
|
if ((fm_fd_r=fdopen(sd,"r")) == NULL){ |
294 |
pajs |
1.18 |
logmessage(LOG_ERR, "Failed to open stream (%m)"); |
295 |
pajs |
1.4 |
return -1; |
296 |
|
|
} |
297 |
|
|
|
298 |
|
|
if ((fm_fd_w=fdopen(dup(sd),"w")) == NULL){ |
299 |
pajs |
1.18 |
logmessage(LOG_ERR, "Failed to open stream (%m)"); |
300 |
pajs |
1.4 |
return -1; |
301 |
|
|
} |
302 |
|
|
|
303 |
pajs |
1.18 |
logmessage(LOG_DEBUG, "Sending HEARTBEAT"); |
304 |
pajs |
1.7 |
reply=sock_comm(fm_fd_r, fm_fd_w, "HEARTBEAT"); |
305 |
pajs |
1.6 |
if ((reply==NULL) || (strncasecmp(reply, "ERROR", 5) == 0) ) { |
306 |
pajs |
1.18 |
logmessage(LOG_ERR, "Server error on HEARTBEAT"); |
307 |
pajs |
1.4 |
return -1; |
308 |
|
|
} |
309 |
pajs |
1.9 |
|
310 |
pajs |
1.18 |
logmessage(LOG_DEBUG, "Sending CONFIG"); |
311 |
pajs |
1.7 |
reply=sock_comm(fm_fd_r, fm_fd_w, "CONFIG"); |
312 |
pajs |
1.6 |
if ((reply==NULL) || (strncasecmp(reply, "ERROR", 5) == 0) ) { |
313 |
pajs |
1.18 |
logmessage(LOG_ERR, "Server error on CONFIG"); |
314 |
pajs |
1.4 |
return -1; |
315 |
|
|
} |
316 |
|
|
|
317 |
pajs |
1.18 |
logmessage(LOG_DEBUG, "Sending %s", ihost_state->files_list); |
318 |
pajs |
1.4 |
reply=sock_comm(fm_fd_r, fm_fd_w, ihost_state->files_list); |
319 |
|
|
if ((reply==NULL) || (strncasecmp(reply, "OK", 2) != 0) ) { |
320 |
pajs |
1.18 |
logmessage(LOG_ERR, "Server error on fileslist"); |
321 |
pajs |
1.4 |
return -1; |
322 |
|
|
} |
323 |
|
|
|
324 |
pajs |
1.18 |
logmessage(LOG_DEBUG, "Sending %s", ihost_state->last_modified); |
325 |
pajs |
1.4 |
reply=sock_comm(fm_fd_r, fm_fd_w, ihost_state->last_modified); |
326 |
|
|
if (reply==NULL) { |
327 |
pajs |
1.18 |
logmessage(LOG_ERR, "Server error NULL recieved on lastmodified"); |
328 |
pajs |
1.4 |
return -1; |
329 |
|
|
} |
330 |
pajs |
1.6 |
if (strncasecmp(reply, "ERROR", 5) == 0){ |
331 |
pajs |
1.4 |
/* Means the config has changed */ |
332 |
pajs |
1.18 |
logmessage(LOG_INFO, "Recieved ERROR from server for a reconfigure required"); |
333 |
pajs |
1.4 |
exitcode=RECONFIGURE_RETURN_CODE; |
334 |
|
|
} |
335 |
pajs |
1.18 |
|
336 |
|
|
logmessage(LOG_DEBUG,"Sending KEY"); |
337 |
pajs |
1.7 |
reply=sock_comm(fm_fd_r, fm_fd_w, "KEY"); |
338 |
pajs |
1.6 |
if ((reply==NULL) || (strncasecmp(reply, "ERROR", 5) == 0) ) { |
339 |
pajs |
1.18 |
logmessage(LOG_ERR, "Server error on KEY"); |
340 |
pajs |
1.4 |
return -1; |
341 |
|
|
} |
342 |
pajs |
1.5 |
if (ihost_state->key!=NULL) free(ihost_state->key); |
343 |
|
|
|
344 |
pajs |
1.7 |
if((ihost_state->key=strdup(reply)) == NULL){ |
345 |
pajs |
1.18 |
logmessage(LOG_ERR, "strdup failed (%m)"); |
346 |
pajs |
1.4 |
return -1; |
347 |
|
|
} |
348 |
|
|
|
349 |
pajs |
1.18 |
logmessage(LOG_DEBUG,"Sending ENDHEARTBEAT"); |
350 |
pajs |
1.7 |
reply=sock_comm(fm_fd_r, fm_fd_w, "ENDHEARTBEAT"); |
351 |
pajs |
1.4 |
if((reply== NULL) || (strncasecmp(reply, "ERROR", 5) ==0 )){ |
352 |
pajs |
1.18 |
logmessage(LOG_ERR, "Server error on ENDHEARTBEAT (%m)"); |
353 |
pajs |
1.4 |
return -1; |
354 |
|
|
} |
355 |
|
|
|
356 |
pajs |
1.10 |
fflush(fm_fd_r); |
357 |
|
|
fflush(fm_fd_w); |
358 |
|
|
|
359 |
pajs |
1.6 |
if(fclose(fm_fd_r) !=0){ |
360 |
pajs |
1.18 |
logmessage(LOG_ERR, "Failed to close read FD (%m)"); |
361 |
pajs |
1.6 |
return -1; |
362 |
|
|
} |
363 |
|
|
if(fclose(fm_fd_w) !=0){ |
364 |
pajs |
1.18 |
logmessage(LOG_ERR, "Failed to close write FD (%m)"); |
365 |
pajs |
1.6 |
return -1; |
366 |
|
|
} |
367 |
|
|
|
368 |
pajs |
1.4 |
return exitcode; |
369 |
|
|
} |
370 |
|
|
|
371 |
pajs |
1.9 |
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; |
377 |
pajs |
1.18 |
|
378 |
|
|
logmessage(LOG_DEBUG,"get_cpu_stats"); |
379 |
pajs |
1.9 |
stats[0]=get_cpu_stats(); |
380 |
pajs |
1.18 |
logmessage(LOG_DEBUG,"get_disk_stats"); |
381 |
pajs |
1.9 |
stats[1]=get_disk_stats(); |
382 |
pajs |
1.18 |
logmessage(LOG_DEBUG,"get_load_stats"); |
383 |
pajs |
1.9 |
stats[2]=get_load_stats(); |
384 |
pajs |
1.18 |
logmessage(LOG_DEBUG,"get_memory_stats"); |
385 |
pajs |
1.9 |
stats[3]=get_memory_stats(); |
386 |
pajs |
1.18 |
logmessage(LOG_DEBUG,"get_os_info"); |
387 |
pajs |
1.9 |
stats[4]=get_os_info(); |
388 |
pajs |
1.18 |
logmessage(LOG_DEBUG,"get_page_stats"); |
389 |
pajs |
1.9 |
stats[5]=get_page_stats(); |
390 |
pajs |
1.18 |
logmessage(LOG_DEBUG,"get_process_stats"); |
391 |
pajs |
1.9 |
stats[6]=get_process_stats(); |
392 |
pajs |
1.18 |
logmessage(LOG_DEBUG,"get_swap_stats"); |
393 |
pajs |
1.9 |
stats[7]=get_swap_stats(); |
394 |
pajs |
1.18 |
logmessage(LOG_DEBUG,"get_user_stats"); |
395 |
pajs |
1.9 |
stats[8]=get_user_stats(); |
396 |
|
|
|
397 |
|
|
for(;x<NUM_STATS;x++){ |
398 |
|
|
if(stats[x]==NULL){ |
399 |
pajs |
1.18 |
logmessage(LOG_ERR,"Function returned NULL"); |
400 |
pajs |
1.9 |
return NULL; |
401 |
|
|
} |
402 |
|
|
if(xml_data==NULL){ |
403 |
|
|
if((xml_data=strf("%s", stats[x])) == NULL){ |
404 |
pajs |
1.18 |
logmessage(LOG_ERR, "str failed (%m)"); |
405 |
pajs |
1.9 |
return NULL; |
406 |
|
|
} |
407 |
|
|
}else{ |
408 |
|
|
xml_data_p=xml_data; |
409 |
|
|
if((xml_data=strf("%s%s", xml_data, stats[x])) == NULL){ |
410 |
pajs |
1.18 |
logmessage(LOG_ERR, "str failed (%m)"); |
411 |
pajs |
1.9 |
return NULL; |
412 |
|
|
} |
413 |
|
|
free(xml_data_p); |
414 |
|
|
} |
415 |
|
|
free(stats[x]); |
416 |
|
|
} |
417 |
|
|
xml_data_p=xml_data; |
418 |
pajs |
1.16 |
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 |
pajs |
1.9 |
free(xml_data_p); |
420 |
pajs |
1.18 |
|
421 |
|
|
logmessage(LOG_DEBUG,"Generated XML Data of : %s", xml_data); |
422 |
pajs |
1.9 |
return xml_data; |
423 |
|
|
} |
424 |
|
|
|
425 |
|
|
int send_stats(ihost_state_t *ihost_state, char *data_stream){ |
426 |
|
|
struct sockaddr_in addr; |
427 |
|
|
struct in_addr haddr; |
428 |
|
|
|
429 |
|
|
int sd; |
430 |
|
|
size_t len; |
431 |
|
|
|
432 |
|
|
len=strlen(data_stream); |
433 |
|
|
if(len>UDP_MAX_PACKET_SIZE){ |
434 |
pajs |
1.18 |
logmessage(LOG_ERR, "Too big to send to server. Please reconfigure client and server and recompile"); |
435 |
pajs |
1.9 |
exit(1); |
436 |
|
|
} |
437 |
pajs |
1.18 |
logmessage(LOG_DEBUG,"Resolving IP of server"); |
438 |
pajs |
1.9 |
if (get_host_addr(ihost_state->server_fqdn, &haddr) != 0){ |
439 |
pajs |
1.18 |
logmessage(LOG_ERR, "Failed to resolve address %s (%m)", ihost_state->server_fqdn); |
440 |
pajs |
1.9 |
return -1; |
441 |
|
|
} |
442 |
pajs |
1.18 |
logmessage(LOG_DEBUG,"Creating UDP connection to %s on %d",ihost_state->server_fqdn, ihost_state->server_udp_port); |
443 |
pajs |
1.9 |
if((sd=socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0){ |
444 |
pajs |
1.18 |
logmessage(LOG_ERR, "failed to create UDP socket (%m)"); |
445 |
pajs |
1.9 |
return -1; |
446 |
|
|
} |
447 |
|
|
|
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); |
452 |
|
|
|
453 |
pajs |
1.18 |
logmessage(LOG_INFO,"Sending packet : %s", data_stream); |
454 |
pajs |
1.9 |
if((sendto(sd, data_stream, len, 0, (struct sockaddr *) &addr, sizeof(addr))) != len){ |
455 |
pajs |
1.18 |
logmessage(LOG_ERR, "Send the wrong number of bytes (%m)"); |
456 |
pajs |
1.9 |
return -1; |
457 |
|
|
} |
458 |
|
|
|
459 |
pajs |
1.10 |
close(sd); |
460 |
|
|
|
461 |
pajs |
1.9 |
return 0; |
462 |
|
|
} |
463 |
pajs |
1.1 |
|
464 |
pajs |
1.17 |
void usage(char *progname){ |
465 |
|
|
fprintf(stderr, "Usage %s [options] server port\n", progname); |
466 |
|
|
fprintf(stderr, "Options\n"); |
467 |
|
|
fprintf(stderr, " -v Verbose, the more v flags the more verbose, eg -vv\n"); |
468 |
|
|
fprintf(stderr, " -d Daemon mode, self backgrounding\n"); |
469 |
|
|
fprintf(stderr, " -s Send errors to syslog\n"); |
470 |
|
|
fprintf(stderr, " -V Print version number\n"); |
471 |
|
|
fprintf(stderr, " -h Prints this help page\n"); |
472 |
|
|
exit(1); |
473 |
|
|
} |
474 |
|
|
|
475 |
pajs |
1.3 |
int main(int argc, char **argv){ |
476 |
pajs |
1.2 |
ihost_state_t ihost_state; |
477 |
pajs |
1.6 |
int heartbeat_exit; |
478 |
|
|
int counter=0; |
479 |
pajs |
1.10 |
long udp_time=0, tcp_time=0, stat_grab_time=0, cur_time=0; |
480 |
pajs |
1.9 |
int sleep_delay=0; |
481 |
|
|
char *xml_stats; |
482 |
pajs |
1.18 |
pid_t pid; |
483 |
pajs |
1.17 |
int cmdopt; |
484 |
|
|
extern int optind; |
485 |
pajs |
1.18 |
int verbose=0, daemon=0; |
486 |
|
|
extern int syslog_logging; |
487 |
|
|
extern int log_level; |
488 |
|
|
extern int cur_level; |
489 |
pajs |
1.19 |
FILE *f; |
490 |
pajs |
1.18 |
|
491 |
|
|
log_level=1; |
492 |
|
|
cur_level=1; |
493 |
|
|
syslog_logging=0; |
494 |
|
|
|
495 |
|
|
errf_set_ofunc(log_errors); |
496 |
pajs |
1.5 |
/* 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; |
503 |
|
|
|
504 |
pajs |
1.3 |
errf_set_progname(argv[0]); |
505 |
pajs |
1.18 |
|
506 |
pajs |
1.17 |
|
507 |
|
|
while((cmdopt=getopt(argc, argv, "vdshV")) != -1){ |
508 |
|
|
switch(cmdopt){ |
509 |
|
|
case 'v': |
510 |
|
|
verbose++; |
511 |
|
|
break; |
512 |
|
|
|
513 |
|
|
case 'd': |
514 |
pajs |
1.18 |
/* Force syslog logging since stderr will be closed in this case */ |
515 |
|
|
syslog_logging=1; |
516 |
pajs |
1.17 |
daemon=1; |
517 |
|
|
break; |
518 |
|
|
|
519 |
|
|
case 's': |
520 |
pajs |
1.18 |
syslog_logging=1; |
521 |
pajs |
1.17 |
break; |
522 |
|
|
|
523 |
|
|
case 'h': |
524 |
|
|
usage(argv[0]); |
525 |
|
|
break; |
526 |
pajs |
1.18 |
|
527 |
pajs |
1.17 |
case 'V': |
528 |
|
|
errf("%s version %f",argv[0], versionNo); |
529 |
|
|
break; |
530 |
pajs |
1.18 |
|
531 |
pajs |
1.17 |
default: |
532 |
|
|
usage(argv[0]); |
533 |
|
|
exit(1); |
534 |
|
|
} |
535 |
|
|
} |
536 |
|
|
|
537 |
|
|
if(argc!=optind+2){ |
538 |
|
|
usage(argv[0]); |
539 |
pajs |
1.3 |
exit(1); |
540 |
|
|
} |
541 |
pajs |
1.17 |
ihost_state.fm_host=argv[optind]; |
542 |
|
|
ihost_state.fm_port=atoi(argv[optind+1]); |
543 |
|
|
if(ihost_state.fm_port==0){ |
544 |
|
|
errf("Invalid port number"); |
545 |
|
|
usage(argv[0]); |
546 |
|
|
} |
547 |
pajs |
1.18 |
|
548 |
|
|
if(daemon==1){ |
549 |
|
|
pid=fork(); |
550 |
|
|
if(pid==-1){ |
551 |
|
|
errf("Fork failed, can't background. Exiting"); |
552 |
|
|
exit(1); |
553 |
|
|
}else if(pid!=0){ |
554 |
|
|
/* Parent process */ |
555 |
|
|
return 0; |
556 |
|
|
} |
557 |
|
|
/* We should now be in the background*/ |
558 |
|
|
if(setsid()==-1){ |
559 |
|
|
errf("setsid failed (%m)"); |
560 |
|
|
exit(1); |
561 |
|
|
} |
562 |
|
|
fclose(stdin); |
563 |
|
|
fclose(stdout); |
564 |
|
|
fclose(stderr); |
565 |
|
|
} |
566 |
|
|
|
567 |
|
|
if(syslog_logging==1){ |
568 |
|
|
openlog(errf_get_progname(),0,LOG_ERR); |
569 |
|
|
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 |
pajs |
1.19 |
} |
591 |
|
|
|
592 |
|
|
logmessage(LOG_DEBUG,"Writing PID FILE"); |
593 |
|
|
pid=getpid(); |
594 |
|
|
if((f=fopen(PID_FILE,"w")) == NULL){ |
595 |
|
|
logmessage(LOG_WARNING, "Failed to write PID file"); |
596 |
|
|
}else{ |
597 |
|
|
if((fprintf(f,"%d",(int)pid)) != sizeof(pid)){ |
598 |
|
|
logmessage(LOG_WARNING, "Failed to write PID file"); |
599 |
|
|
} |
600 |
|
|
if((fclose(f))!=0){ |
601 |
|
|
logmessage(LOG_ERR, "failed to close PID file"); |
602 |
|
|
exit(1); |
603 |
|
|
} |
604 |
pajs |
1.18 |
} |
605 |
pajs |
1.2 |
|
606 |
|
|
if(ihost_configure(&ihost_state)!=0){ |
607 |
pajs |
1.18 |
logmessage(LOG_ERR,"configure failed"); |
608 |
pajs |
1.6 |
/* Ok, ideally we prob should have 2 copies of the structure and carry on if this |
609 |
|
|
happens.. But we dont :) (at the moment) */ |
610 |
|
|
exit(1); |
611 |
pajs |
1.2 |
} |
612 |
pajs |
1.5 |
|
613 |
pajs |
1.9 |
for(;;){ |
614 |
|
|
cur_time=time(NULL); |
615 |
|
|
if(cur_time>=tcp_time){ |
616 |
pajs |
1.18 |
logmessage(LOG_DEBUG,"Sending heartbeat"); |
617 |
pajs |
1.9 |
heartbeat_exit=heartbeat(&ihost_state); |
618 |
|
|
if(heartbeat_exit==RECONFIGURE_RETURN_CODE){ |
619 |
pajs |
1.18 |
logmessage(LOG_INFO,"heartbeat needs to be reconfigured"); |
620 |
pajs |
1.9 |
ihost_configure(&ihost_state); |
621 |
|
|
udp_time=0; |
622 |
|
|
} |
623 |
|
|
if(heartbeat_exit==-1){ |
624 |
pajs |
1.18 |
logmessage(LOG_ERR,"Heartbeat failed"); |
625 |
pajs |
1.9 |
exit(1); |
626 |
|
|
} |
627 |
|
|
tcp_time=time(NULL)+ihost_state.tcp_update_time; |
628 |
pajs |
1.18 |
logmessage(LOG_DEBUG,"next tcp time should be %d", tcp_time); |
629 |
pajs |
1.9 |
} |
630 |
pajs |
1.10 |
|
631 |
pajs |
1.9 |
if(cur_time>=udp_time){ |
632 |
pajs |
1.18 |
logmessage(LOG_DEBUG,"Sending udp data"); |
633 |
|
|
|
634 |
pajs |
1.10 |
stat_grab_time=time(NULL); |
635 |
pajs |
1.13 |
if((xml_stats=stat_grab(&ihost_state, counter++)) == NULL){ |
636 |
pajs |
1.18 |
logmessage(LOG_ERR,"Failed to get stats (%m)"); |
637 |
pajs |
1.9 |
exit(1); |
638 |
|
|
} |
639 |
pajs |
1.10 |
stat_grab_time=time(NULL)-stat_grab_time; |
640 |
|
|
send_stats(&ihost_state, xml_stats); |
641 |
|
|
free(xml_stats); |
642 |
|
|
udp_time=time(NULL)+ihost_state.udp_update_time-stat_grab_time; |
643 |
pajs |
1.18 |
logmessage(LOG_DEBUG,"next udp time should be %d", udp_time); |
644 |
pajs |
1.9 |
} |
645 |
pajs |
1.10 |
|
646 |
|
|
if(tcp_time<udp_time){ |
647 |
|
|
sleep_delay=tcp_time-time(NULL); |
648 |
pajs |
1.9 |
}else{ |
649 |
pajs |
1.10 |
sleep_delay=udp_time-time(NULL); |
650 |
pajs |
1.6 |
} |
651 |
pajs |
1.18 |
logmessage(LOG_DEBUG,"Sleeping for %d", sleep_delay); |
652 |
pajs |
1.10 |
if(sleep_delay>0) sleep(sleep_delay); |
653 |
pajs |
1.6 |
} |
654 |
pajs |
1.1 |
return 0; |
655 |
|
|
} |
656 |
|
|
|