21 |
|
|
22 |
#include <stdio.h> |
#include <stdio.h> |
23 |
#include <stdlib.h> |
#include <stdlib.h> |
24 |
|
#include <sys/socket.h> |
25 |
|
|
26 |
#ifdef HAVE_STRING_H |
#ifdef HAVE_STRING_H |
27 |
#include <string.h> |
#include <string.h> |
39 |
#include <sys/types.h> |
#include <sys/types.h> |
40 |
#endif |
#endif |
41 |
|
|
42 |
|
#include "net.h" |
43 |
#include "monitor.h" |
#include "monitor.h" |
44 |
|
#include "monit_process.h" |
45 |
|
|
46 |
|
|
47 |
/* Private Prototypes */ |
/* Private Prototypes */ |
48 |
static void printstatus(Process_T); |
static void local_status(Process_T); |
49 |
|
static int remote_status(Process_T); |
50 |
|
|
51 |
/** |
/** |
52 |
* Print the status of processes in the process list. |
* Print the status of processes in the process list. |
58 |
* @file |
* @file |
59 |
*/ |
*/ |
60 |
|
|
61 |
|
/* ------------------------------------------------------------------ Public */ |
62 |
|
|
63 |
|
|
64 |
|
/** |
65 |
|
* Show all processes in the process list |
66 |
|
*/ |
67 |
void status() { |
void status() { |
68 |
|
|
69 |
Process_T p; |
Process_T p; |
70 |
char *uptime= get_process_uptime(Run.pidfile); |
char *uptime= get_process_uptime(Run.pidfile); |
71 |
|
int remote= TRUE; |
72 |
|
|
73 |
fprintf(stdout, "monit daemon uptime: %s\n", uptime); |
fprintf(stdout, "monit daemon uptime: %s\n", uptime); |
74 |
free(uptime); |
free(uptime); |
75 |
|
|
76 |
for ( p= processlist; p; p= p->next) { |
for ( p= processlist; p; p= p->next) { |
77 |
|
|
78 |
printstatus(p); |
if (remote) { |
79 |
|
|
80 |
|
remote = remote_status(p); |
81 |
|
|
82 |
|
} else { |
83 |
|
|
84 |
|
local_status(p); |
85 |
|
|
86 |
|
} |
87 |
|
|
88 |
} |
} |
89 |
|
|
90 |
} |
} |
91 |
|
|
92 |
/** |
/** |
93 |
* Show all processeses in the group |
* Show all processes in the group |
94 |
|
* @param G group name |
95 |
*/ |
*/ |
96 |
void status_group(char *G) { |
void status_group(char *G) { |
97 |
|
|
98 |
Process_T p; |
Process_T p; |
99 |
|
int remote= TRUE; |
100 |
|
|
101 |
for ( p= processlist; p; p= p->next) { |
for ( p= processlist; p; p= p->next) { |
102 |
|
|
103 |
if (is(p->group, G) ) { |
if (is(p->group, G) ) { |
104 |
|
|
105 |
printstatus(p); |
if (remote) { |
106 |
|
|
107 |
|
remote = remote_status(p); |
108 |
|
|
109 |
|
} else { |
110 |
|
|
111 |
|
local_status(p); |
112 |
|
|
113 |
|
} |
114 |
|
|
115 |
} |
} |
116 |
|
|
123 |
|
|
124 |
/** |
/** |
125 |
* Print the process status for the given process |
* Print the process status for the given process |
126 |
* @param p A Process_T object |
* @param p A Process_T object |
127 |
*/ |
*/ |
128 |
static void printstatus(Process_T p) { |
static void local_status(Process_T p) { |
129 |
|
|
130 |
pid_t pid= -1; |
pid_t pid= -1; |
131 |
|
|
132 |
if ( (pid= is_process_running(p)) ) { |
if ( (pid= is_process_running(p)) ) { |
133 |
|
|
134 |
char *uptime= get_process_uptime(p->pidfile); |
char *uptime= get_process_uptime(p->pidfile); |
135 |
|
|
136 |
fprintf(stdout, "Process '%s' is running with pid [%d] Uptime: %s\n", |
fprintf(stdout, "Process '%s' is running with pid [%d] Uptime: %s\n", |
137 |
p->name, (int)pid, uptime); |
p->name, (int)pid, uptime); |
138 |
|
|
139 |
free(uptime); |
free(uptime); |
145 |
} |
} |
146 |
|
|
147 |
} |
} |
148 |
|
|
149 |
|
|
150 |
|
/** |
151 |
|
* Get the process status for the given process from a monit server |
152 |
|
* @param p A Process_T object |
153 |
|
* @return TRUE if the monit server could be connected |
154 |
|
*/ |
155 |
|
static int remote_status(Process_T p) { |
156 |
|
|
157 |
|
if ( exist_daemon() ) { |
158 |
|
|
159 |
|
/* If a monit daemon exist we request that the daemon stop the |
160 |
|
program, if not, it will start the progam again on it's next |
161 |
|
cycle */ |
162 |
|
|
163 |
|
int s= create_socket(Run.bind_addr?Run.bind_addr:"localhost", |
164 |
|
Run.httpdport, SOCK_STREAM); |
165 |
|
if (s<0) { |
166 |
|
|
167 |
|
fprintf(stdout, "Cannot connect to monit server to get extended process " |
168 |
|
"data.\n"); |
169 |
|
local_status(p); |
170 |
|
|
171 |
|
return FALSE; |
172 |
|
|
173 |
|
} else { |
174 |
|
|
175 |
|
char req[2*STRLEN]; |
176 |
|
char *auth= get_basic_authentication_header(); |
177 |
|
char buf[STRLEN]; |
178 |
|
|
179 |
|
snprintf(req, 2*STRLEN, |
180 |
|
"GET /%s?action=status HTTP/1.0\r\n%s\r\n", p->name, auth); |
181 |
|
|
182 |
|
free(auth); |
183 |
|
sock_send(s, req, sizeof(req), 0); |
184 |
|
sock_recv(s, buf, STRLEN, 0); |
185 |
|
close_socket(s); |
186 |
|
|
187 |
|
/* If everything has gone well the returned string starts with |
188 |
|
"Process " */ |
189 |
|
|
190 |
|
if(strncmp("Process ", buf, 8) == 0) { |
191 |
|
|
192 |
|
fprintf(stdout, "%s", buf); |
193 |
|
|
194 |
|
} else { |
195 |
|
|
196 |
|
fprintf(stdout, "Monit server has not returned a process record.\n", |
197 |
|
p->name); |
198 |
|
local_status(p); |
199 |
|
|
200 |
|
return TRUE; |
201 |
|
|
202 |
|
} |
203 |
|
|
204 |
|
return TRUE; |
205 |
|
} |
206 |
|
|
207 |
|
} else { |
208 |
|
|
209 |
|
/* No monit daemon exist, just stop the program */ |
210 |
|
|
211 |
|
|
212 |
|
fprintf(stdout, "Cannot connect to monit server to get extended process " |
213 |
|
"data.\n"); |
214 |
|
local_status(p); |
215 |
|
|
216 |
|
return FALSE; |
217 |
|
} |
218 |
|
|
219 |
|
} |