41 |
#endif |
#endif |
42 |
|
|
43 |
#include "net.h" |
#include "net.h" |
44 |
|
#include "socket.h" |
45 |
#include "monitor.h" |
#include "monitor.h" |
46 |
#include "monit_process.h" |
#include "monit_process.h" |
47 |
#include "ssl.h" |
|
48 |
|
|
49 |
|
|
50 |
/* Private Prototypes */ |
/* Private Prototypes */ |
51 |
static void local_status(Service_T); |
static void local_status(Service_T); |
52 |
static int remote_status(Service_T, ssl_connection *); |
static int remote_status(Service_T); |
53 |
|
|
54 |
/** |
/** |
55 |
* Print the status of services in the service list. |
* Print the status of services in the service list. |
72 |
|
|
73 |
Service_T s; |
Service_T s; |
74 |
int remote= TRUE; |
int remote= TRUE; |
|
ssl_connection *ssl= NULL; |
|
75 |
char *uptime= get_process_uptime(Run.pidfile); |
char *uptime= get_process_uptime(Run.pidfile); |
76 |
|
|
|
if(Run.httpdssl) { |
|
|
|
|
|
ssl= new_ssl_connection(Run.httpsslpem, SSL_VERSION_AUTO); |
|
|
|
|
|
} |
|
|
|
|
77 |
fprintf(stdout, "monit daemon uptime: %s\n", uptime); |
fprintf(stdout, "monit daemon uptime: %s\n", uptime); |
78 |
free(uptime); |
free(uptime); |
79 |
|
|
81 |
|
|
82 |
if(remote) { |
if(remote) { |
83 |
|
|
84 |
remote= remote_status(s, ssl); |
remote= remote_status(s); |
85 |
|
|
86 |
} else { |
} else { |
87 |
|
|
91 |
|
|
92 |
} |
} |
93 |
|
|
|
if(Run.httpdssl) { |
|
|
|
|
|
delete_ssl_socket(ssl); |
|
|
|
|
|
} |
|
|
|
|
94 |
} |
} |
95 |
|
|
96 |
/** |
/** |
101 |
|
|
102 |
Service_T s; |
Service_T s; |
103 |
int remote= TRUE; |
int remote= TRUE; |
|
ssl_connection *ssl= NULL; |
|
104 |
|
|
105 |
ASSERT(G); |
ASSERT(G); |
106 |
|
|
|
if(Run.httpdssl) { |
|
|
|
|
|
ssl= new_ssl_connection(Run.httpsslclientpem, SSL_VERSION_AUTO); |
|
|
|
|
|
} |
|
|
|
|
107 |
for(s= servicelist; s; s= s->next) { |
for(s= servicelist; s; s= s->next) { |
108 |
|
|
109 |
if(IS(s->group, G)) { |
if(IS(s->group, G)) { |
110 |
|
|
111 |
if(remote) { |
if(remote) { |
112 |
|
|
113 |
remote= remote_status(s, ssl); |
remote= remote_status(s); |
114 |
|
|
115 |
} else { |
} else { |
116 |
|
|
121 |
} |
} |
122 |
|
|
123 |
} |
} |
|
|
|
|
if(Run.httpdssl) { |
|
|
|
|
|
delete_ssl_socket(ssl); |
|
|
|
|
|
} |
|
124 |
|
|
125 |
} |
} |
126 |
|
|
237 |
* @param p A Service_T object |
* @param p A Service_T object |
238 |
* @return TRUE if the monit server could be connected |
* @return TRUE if the monit server could be connected |
239 |
*/ |
*/ |
240 |
static int remote_status(Service_T s, ssl_connection * ssl) { |
static int remote_status(Service_T s) { |
241 |
|
|
242 |
ASSERT(s); |
ASSERT(s); |
243 |
|
|
245 |
|
|
246 |
/* If a monit daemon exist we request status information from the server */ |
/* If a monit daemon exist we request status information from the server */ |
247 |
|
|
248 |
int sock= create_socket(Run.bind_addr?Run.bind_addr:"localhost", |
Socket_T sock= socket_new(Run.bind_addr?Run.bind_addr:"localhost", |
249 |
Run.httpdport, SOCK_STREAM); |
Run.httpdport, SOCK_STREAM, Run.httpdssl); |
250 |
if(sock<0) { |
if(!sock) { |
251 |
|
|
252 |
fprintf(stdout, |
fprintf(stdout, |
253 |
"Cannot connect to monit server to get extended service data.\n"); |
"Cannot connect to monit server to get extended service data.\n"); |
254 |
local_status(s); |
local_status(s); |
255 |
|
|
256 |
return FALSE; |
return FALSE; |
257 |
|
|
258 |
} else { |
} else { |
266 |
"GET /%s?action=status HTTP/1.0\r\n%s\r\n", s->name, auth); |
"GET /%s?action=status HTTP/1.0\r\n%s\r\n", s->name, auth); |
267 |
|
|
268 |
free(auth); |
free(auth); |
|
|
|
|
if(ssl != NULL) { |
|
|
|
|
|
if(!embed_ssl_socket(ssl, sock)) { |
|
|
|
|
|
fprintf(stdout, "Failed to establish SSL communication to monit" |
|
|
" server\n"); |
|
|
local_status(s); |
|
269 |
|
|
270 |
return FALSE; |
socket_write(sock, req, sizeof(req)); |
|
} |
|
|
|
|
|
send_ssl_socket(ssl, req, sizeof(req)); |
|
|
if(0>(n= recv_ssl_socket(ssl, buf, STRLEN))) { |
|
|
|
|
|
local_status(s); |
|
|
close_ssl_socket(ssl); |
|
|
close_socket(sock); |
|
|
|
|
|
return TRUE; |
|
|
|
|
|
} |
|
|
|
|
|
close_ssl_socket(ssl); |
|
|
close_socket(sock); |
|
|
|
|
|
} else { |
|
|
|
|
|
sock_send(sock, req, sizeof(req), 0); |
|
271 |
|
|
272 |
if(0>(n= sock_recv(sock, buf, STRLEN, 0))) { |
if(0>(n= socket_read(sock, buf, STRLEN))) { |
273 |
|
|
274 |
local_status(s); |
local_status(s); |
275 |
close_socket(sock); |
socket_close(&sock); |
|
|
|
|
return TRUE; |
|
276 |
|
|
277 |
} |
return TRUE; |
278 |
|
|
|
close_socket(sock); |
|
279 |
} |
} |
280 |
|
|
281 |
|
socket_close(&sock); |
282 |
|
|
283 |
|
|
284 |
/* If everything has gone well the returned string starts with |
/* If everything has gone well the returned string starts with |
285 |
"Process ", "Device ", "File " or "Directory " */ |
"Process ", "Device ", "File " or "Directory " */ |
286 |
|
|
287 |
buf[n]= 0; |
buf[n]= 0; |
288 |
if(starts_with(buf, "Process ") || |
if(starts_with(buf, "Process ") || |
289 |
starts_with(buf, "Device ") || |
starts_with(buf, "Device ") || |
290 |
starts_with(buf, "File ") || |
starts_with(buf, "File ") || |
291 |
starts_with(buf, "Directory ")) { |
starts_with(buf, "Directory ")) { |
292 |
|
|
293 |
fprintf(stdout, "%s", buf); |
fprintf(stdout, "%s", buf); |
294 |
|
|
295 |
} else { |
} else { |