author | Michael Krelin <hacker@klever.net> | 2004-07-23 20:40:46 (UTC) |
---|---|---|
committer | Michael Krelin <hacker@klever.net> | 2004-07-23 20:40:46 (UTC) |
commit | 546858a1e4d13d179a6af27b474e1396cfdf0c29 (patch) (unidiff) | |
tree | ac19b0ff5e4b3164ad5375bda112a9d6d2f88c2b | |
parent | 76921288a0aa39acb53102863523c388b5d0f9ee (diff) | |
download | dudki-546858a1e4d13d179a6af27b474e1396cfdf0c29.zip dudki-546858a1e4d13d179a6af27b474e1396cfdf0c29.tar.gz dudki-546858a1e4d13d179a6af27b474e1396cfdf0c29.tar.bz2 |
the ability to check/kill/reload any of the processes being monitored added.
-rw-r--r-- | man/dudki.8.in | 11 | ||||
-rw-r--r-- | src/dudki.cc | 61 | ||||
-rw-r--r-- | src/process.cc | 30 | ||||
-rw-r--r-- | src/process.h | 2 |
4 files changed, 65 insertions, 39 deletions
diff --git a/man/dudki.8.in b/man/dudki.8.in index 3011034..05db733 100644 --- a/man/dudki.8.in +++ b/man/dudki.8.in | |||
@@ -23,12 +23,13 @@ dudki \- a process monitoring daemon | |||
23 | [\fB-c\fR] | 23 | [\fB-c\fR] |
24 | .if \*[longopt] [\fB--check\fR] | 24 | .if \*[longopt] [\fB--check\fR] |
25 | [\fB-e\fR] | 25 | [\fB-e\fR] |
26 | .if \*[longopt] [\fB--ensure\fR] | 26 | .if \*[longopt] [\fB--ensure\fR] |
27 | [\fB-t\fR] | 27 | [\fB-t\fR] |
28 | .if \*[longopt] [\fB--test\fR] | 28 | .if \*[longopt] [\fB--test\fR] |
29 | [\fI<process-list>\fR] | ||
29 | 30 | ||
30 | .SH DESCRIPTION | 31 | .SH DESCRIPTION |
31 | 32 | ||
32 | dudki daemon is designed to run in the background and periodically | 33 | dudki daemon is designed to run in the background and periodically |
33 | check if certain processes specified in the configuration file are | 34 | check if certain processes specified in the configuration file are |
34 | running. If a process is detected as dead dudki tries to restart it | 35 | running. If a process is detected as dead dudki tries to restart it |
@@ -42,21 +43,25 @@ the specified contact (currently only via email). | |||
42 | .el \fB-f\fR \fIconfigfile\fR | 43 | .el \fB-f\fR \fIconfigfile\fR |
43 | Specify the configuration file to use (default is | 44 | Specify the configuration file to use (default is |
44 | \fI@sysconfdir@/dudki.conf\fR). | 45 | \fI@sysconfdir@/dudki.conf\fR). |
45 | .TP | 46 | .TP |
46 | .ie \*[longopt] \fB-k\fR, \fB--kill\fR | 47 | .ie \*[longopt] \fB-k\fR, \fB--kill\fR |
47 | .el \fB-k\fR | 48 | .el \fB-k\fR |
48 | Stop the running instance by sending the \fBSIGTERM\fR signal. | 49 | Stop the running instance by sending the \fBSIGTERM\fR signal. If no process |
50 | name specified on the command line, dudki kills his own running instance. | ||
49 | .TP | 51 | .TP |
50 | .ie \*[longopt] \fB-r\fR, \fB--reload\fR | 52 | .ie \*[longopt] \fB-r\fR, \fB--reload\fR |
51 | .el \fB-r\fR | 53 | .el \fB-r\fR |
52 | Reload the running instance by sending the \fBSIGHUP\fR signal. | 54 | Reload the running instance by sending the \fBSIGHUP\fR signal. Like with |
55 | \fB-k\fR, if no process name specified on the command line, dudki sends | ||
56 | \fBSIGHUP\fR to his own running instance. | ||
53 | .TP | 57 | .TP |
54 | .ie \*[longopt] \fB-c\fR, \fB--check\fR | 58 | .ie \*[longopt] \fB-c\fR, \fB--check\fR |
55 | .el \fB-c\fR | 59 | .el \fB-c\fR |
56 | Check if dudki is running. Exit with non-zero status if not. | 60 | Check if dudki is running. Exit with non-zero status if not. The same target |
61 | rules as in \fB-k\fR and \fB-r\fR apply here. | ||
57 | .TP | 62 | .TP |
58 | .ie \*[longopt] \fB-e\fR, \fB--ensure\fR | 63 | .ie \*[longopt] \fB-e\fR, \fB--ensure\fR |
59 | .el \fB-e\fR | 64 | .el \fB-e\fR |
60 | Ensure that dudki is running. Load, if not. Useful for running as a | 65 | Ensure that dudki is running. Load, if not. Useful for running as a |
61 | cron job once in a while. If the daemon is running runs quietly | 66 | cron job once in a while. If the daemon is running runs quietly |
62 | providing no output. | 67 | providing no output. |
diff --git a/src/dudki.cc b/src/dudki.cc index b4e95a7..e91ad5e 100644 --- a/src/dudki.cc +++ b/src/dudki.cc | |||
@@ -59,18 +59,17 @@ int main(int argc,char **argv) { | |||
59 | throw runtime_error("memory allocation problem at the very start"); | 59 | throw runtime_error("memory allocation problem at the very start"); |
60 | memmove(_argv,argv,sizeof(*_argv)*(argc+1)); | 60 | memmove(_argv,argv,sizeof(*_argv)*(argc+1)); |
61 | string config_file = DEFAULT_CONF_FILE; | 61 | string config_file = DEFAULT_CONF_FILE; |
62 | enum { | 62 | enum { |
63 | op_default, | 63 | op_default, |
64 | op_work, | 64 | op_work, |
65 | op_hup, | 65 | op_signal, |
66 | op_term, | ||
67 | op_check, | ||
68 | op_ensure, | 66 | op_ensure, |
69 | op_test | 67 | op_test |
70 | } op = op_default; | 68 | } op = op_default; |
69 | int op_signum = 0; | ||
71 | while(true) { | 70 | while(true) { |
72 | #defineSHORTOPTSTRING "f:hVLrkcet" | 71 | #defineSHORTOPTSTRING "f:hVLrkcet" |
73 | #ifdef HAVE_GETOPT_LONG | 72 | #ifdef HAVE_GETOPT_LONG |
74 | static struct option opts[] = { | 73 | static struct option opts[] = { |
75 | { "help", no_argument, 0, 'h' }, | 74 | { "help", no_argument, 0, 'h' }, |
76 | { "usage", no_argument, 0, 'h' }, | 75 | { "usage", no_argument, 0, 'h' }, |
@@ -90,35 +89,40 @@ int main(int argc,char **argv) { | |||
90 | #endif /* /HAVE_GETOPT_LONG */ | 89 | #endif /* /HAVE_GETOPT_LONG */ |
91 | if(c==-1) | 90 | if(c==-1) |
92 | break; | 91 | break; |
93 | switch(c) { | 92 | switch(c) { |
94 | case 'h': | 93 | case 'h': |
95 | cerr << PHEADER << endl | 94 | cerr << PHEADER << endl |
96 | << PCOPY << endl << endl << | 95 | << PCOPY << endl << endl |
96 | << " " << argv[0] << " [options] [processes]" << endl << endl << | ||
97 | #ifdef HAVE_GETOPT_LONG | 97 | #ifdef HAVE_GETOPT_LONG |
98 | " -h, --help\n" | 98 | " -h, --help\n" |
99 | " --usage display this text\n" | 99 | " --usage display this text\n" |
100 | " -V, --version display version number\n" | 100 | " -V, --version display version number\n" |
101 | " -L, --license show license\n" | 101 | " -L, --license show license\n" |
102 | " -f filename, --config=filename\n" | 102 | " -f filename, --config=filename\n" |
103 | " specify the configuration file to use\n" | 103 | " specify the configuration file to use\n" |
104 | "\n" | 104 | "\n" |
105 | " -k, --kill stop running instance\n" | 105 | " -k, --kill stop running instance (send SIGTERM)\n" |
106 | " -r, --reload reload running instance (send SIGHUP)\n" | 106 | " -r, --reload reload running instance (send SIGHUP)\n" |
107 | " -c, --check check if dudki is running\n" | 107 | " -c, --check check if the process is running\n" |
108 | " (the above commands operate on dudki itself if no\n" | ||
109 | " process name has been specified)\n" | ||
108 | " -e, --ensure ensure that dudki is running\n" | 110 | " -e, --ensure ensure that dudki is running\n" |
109 | " -t, --test test configuration file and exit" | 111 | " -t, --test test configuration file and exit" |
110 | #else /* !HAVE_GETOPT_LONG */ | 112 | #else /* !HAVE_GETOPT_LONG */ |
111 | " -h display this text\n" | 113 | " -h display this text\n" |
112 | " -V display version number\n" | 114 | " -V display version number\n" |
113 | " -L show license\n" | 115 | " -L show license\n" |
114 | " -f filename specify the configuration file to use\n" | 116 | " -f filename specify the configuration file to use\n" |
115 | "\n" | 117 | "\n" |
116 | " -k stop running instance\n" | 118 | " -k stop running instance (send SIGTERM)\n" |
117 | " -r reload running instance (send SIGHUP)\n" | 119 | " -r reload running instance (send SIGHUP)\n" |
118 | " -c check if dudki is running\n" | 120 | " -c check if the process is running\n" |
121 | " (the above commands operate on dudki itself if no\n" | ||
122 | " process name has been specified)\n" | ||
119 | " -e ensure that dudki is running\n" | 123 | " -e ensure that dudki is running\n" |
120 | " -t test configuration file and exit" | 124 | " -t test configuration file and exit" |
121 | #endif /* /HAVE_GETOPT_LONG */ | 125 | #endif /* /HAVE_GETOPT_LONG */ |
122 | << endl; | 126 | << endl; |
123 | exit(0); | 127 | exit(0); |
124 | break; | 128 | break; |
@@ -136,27 +140,27 @@ int main(int argc,char **argv) { | |||
136 | break; | 140 | break; |
137 | case 'k': | 141 | case 'k': |
138 | if(op!=op_default) { | 142 | if(op!=op_default) { |
139 | cerr << "Can't obey two or more orders at once" << endl; | 143 | cerr << "Can't obey two or more orders at once" << endl; |
140 | exit(1); | 144 | exit(1); |
141 | } | 145 | } |
142 | op = op_term; | 146 | op = op_signal; op_signum = SIGTERM; |
143 | break; | 147 | break; |
144 | case 'r': | 148 | case 'r': |
145 | if(op!=op_default) { | 149 | if(op!=op_default) { |
146 | cerr << "Can't obey two or more orders at once" << endl; | 150 | cerr << "Can't obey two or more orders at once" << endl; |
147 | exit(1); | 151 | exit(1); |
148 | } | 152 | } |
149 | op = op_hup; | 153 | op = op_signal; op_signum = SIGHUP; |
150 | break; | 154 | break; |
151 | case 'c': | 155 | case 'c': |
152 | if(op!=op_default) { | 156 | if(op!=op_default) { |
153 | cerr << "Can't obey two or more orders at once" << endl; | 157 | cerr << "Can't obey two or more orders at once" << endl; |
154 | exit(1); | 158 | exit(1); |
155 | } | 159 | } |
156 | op = op_check; | 160 | op = op_signal; op_signum = 0; |
157 | break; | 161 | break; |
158 | case 'e': | 162 | case 'e': |
159 | if(op!=op_default) { | 163 | if(op!=op_default) { |
160 | cerr << "Can't obey two or more orders at once" << endl; | 164 | cerr << "Can't obey two or more orders at once" << endl; |
161 | exit(1); | 165 | exit(1); |
162 | } | 166 | } |
@@ -184,24 +188,37 @@ int main(int argc,char **argv) { | |||
184 | configuration config; | 188 | configuration config; |
185 | config.parse(config_file); | 189 | config.parse(config_file); |
186 | switch(op) { | 190 | switch(op) { |
187 | case op_test: | 191 | case op_test: |
188 | cerr << "Configuration OK" << endl; | 192 | cerr << "Configuration OK" << endl; |
189 | break; | 193 | break; |
190 | case op_hup: | 194 | case op_signal: |
191 | signal_self(config,SIGHUP); | 195 | try { |
192 | break; | 196 | if(optind>=argc) { |
193 | case op_term: | 197 | signal_self(config,op_signum); |
194 | signal_self(config,SIGTERM); | 198 | }else{ |
195 | break; | 199 | int failures = 0; |
196 | case op_check: | 200 | for(int narg=optind;narg<argc;narg++) { |
197 | try{ | 201 | try { |
198 | signal_self(config,0); | 202 | processes_t::const_iterator i = config.processes.find(argv[narg]); |
199 | exit(0); | 203 | if(i==config.processes.end()) |
204 | throw runtime_error("no such process configured"); | ||
205 | i->second.signal(op_signum); | ||
206 | }catch(exception& e) { | ||
207 | cerr << "dudki(" << argv[narg] << "): " << e.what() << endl; | ||
208 | failures++; | ||
209 | } | ||
210 | } | ||
211 | if(failures) | ||
212 | throw runtime_error("not all processes have been successfully signaled"); | ||
213 | } | ||
214 | if(!op_signum) | ||
215 | exit(0); | ||
200 | }catch(exception& e) { | 216 | }catch(exception& e) { |
201 | exit(1); | 217 | if(!op_signum) |
218 | exit(1); | ||
202 | } | 219 | } |
203 | case op_ensure: | 220 | case op_ensure: |
204 | try { | 221 | try { |
205 | signal_self(config,0); | 222 | signal_self(config,0); |
206 | break; | 223 | break; |
207 | }catch(exception& e) { | 224 | }catch(exception& e) { |
diff --git a/src/process.cc b/src/process.cc index bfab311..1ffac9f 100644 --- a/src/process.cc +++ b/src/process.cc | |||
@@ -12,27 +12,16 @@ | |||
12 | #include <stdexcept> | 12 | #include <stdexcept> |
13 | using namespace std; | 13 | using namespace std; |
14 | #include "process.h" | 14 | #include "process.h" |
15 | #include "configuration.h" | 15 | #include "configuration.h" |
16 | 16 | ||
17 | void process::check(const string& id,configuration& config) { | 17 | void process::check(const string& id,configuration& config) { |
18 | bool running = false; | 18 | try { |
19 | ifstream pids(pidfile.c_str(),ios::in); | 19 | signal(0); |
20 | if(pids) { | ||
21 | pid_t pid = 0; | ||
22 | pids >> pid; | ||
23 | pids.close(); | ||
24 | if(pid) { | ||
25 | if(!kill(pid,0)) { | ||
26 | running = true; | ||
27 | } | ||
28 | } | ||
29 | } | ||
30 | if(running){ | ||
31 | patience = 0; | 20 | patience = 0; |
32 | }else{ | 21 | }catch(exception& e) { |
33 | if(patience>60) { // TODO: configurable | 22 | if(patience>60) { // TODO: configurable |
34 | patience = 0; | 23 | patience = 0; |
35 | }else{ | 24 | }else{ |
36 | if(patience<10) { // TODO: configurable | 25 | if(patience<10) { // TODO: configurable |
37 | syslog(LOG_NOTICE,"The process '%s' is down, trying to launch.",id.c_str()); | 26 | syslog(LOG_NOTICE,"The process '%s' is down, trying to launch.",id.c_str()); |
38 | do_notify(id,"Starting up", | 27 | do_notify(id,"Starting up", |
@@ -182,6 +171,19 @@ void process::notify_mailto(const string& email,const string& id,const string& e | |||
182 | description.c_str() ); | 171 | description.c_str() ); |
183 | fclose(mta); | 172 | fclose(mta); |
184 | int status; | 173 | int status; |
185 | waitpid(pid,&status,0); | 174 | waitpid(pid,&status,0); |
186 | // TODO: check the return code | 175 | // TODO: check the return code |
187 | } | 176 | } |
177 | |||
178 | void process::signal(int signum) const { | ||
179 | ifstream pids(pidfile.c_str(),ios::in); | ||
180 | if(!pids) | ||
181 | throw runtime_error("no pidfile found"); | ||
182 | pid_t pid = 0; | ||
183 | pids >> pid; | ||
184 | pids.close(); | ||
185 | if(!pid) | ||
186 | throw runtime_error("no pid in pidfile"); | ||
187 | if(kill(pid,signum)) | ||
188 | throw runtime_error("failed to signal process"); | ||
189 | } | ||
diff --git a/src/process.h b/src/process.h index b6d7091..27ee049 100644 --- a/src/process.h +++ b/src/process.h | |||
@@ -26,11 +26,13 @@ class process { | |||
26 | 26 | ||
27 | void check(const string& id,configuration& config); | 27 | void check(const string& id,configuration& config); |
28 | void launch(const string& id,configuration& config); | 28 | void launch(const string& id,configuration& config); |
29 | void do_notify(const string& id,const string& event,const string& description,configuration& config); | 29 | void do_notify(const string& id,const string& event,const string& description,configuration& config); |
30 | void notify_mailto(const string& email,const string& id,const string& event, | 30 | void notify_mailto(const string& email,const string& id,const string& event, |
31 | const string& description,configuration& config); | 31 | const string& description,configuration& config); |
32 | |||
33 | void signal(int signum) const; | ||
32 | }; | 34 | }; |
33 | 35 | ||
34 | typedef map<string,process> processes_t; | 36 | typedef map<string,process> processes_t; |
35 | 37 | ||
36 | #endif /* __PROCESS_H */ | 38 | #endif /* __PROCESS_H */ |