forked from Qihoo360/QConf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
qconf_agent.cc
284 lines (247 loc) · 6.85 KB
/
qconf_agent.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
#include <stdio.h>
#include <errno.h>
#include <signal.h>
#include <string.h>
#include <unistd.h>
#include <pthread.h>
#include <iostream>
#include "qconf_zoo.h"
#include "qconf_log.h"
#include "qconf_shm.h"
#include "qconf_cmd.h"
#include "qconf_dump.h"
#include "qconf_const.h"
#include "qconf_config.h"
#include "qconf_daemon.h"
#include "qconf_script.h"
#include "qconf_watcher.h"
#include "qconf_feedback.h"
using namespace std;
extern int maxSlotsNum;
const string QCONF_PID_FILE("/pid");
const string QCONF_LOG_FMT("/logs/qconf.log.%Y-%m-%d-%H");
static void sig_handler(int sig);
static int qconf_agent_init(const string &agent_dir, const string &log_dir);
static void qconf_agent_destroy();
#define STRING_(str) #str
#define STRING(str) STRING_(str)
static void Usage() {
LOG_INFO("Usage: \n ./qconf_agent --localidc=idcname\n"
"We support one argument for now, idcname is "
"your idc name in agent.conf");
}
int main(int argc, char* argv[])
{
string agent_dir("..");
#ifdef QCONF_AGENT_DIR
agent_dir = STRING(QCONF_AGENT_DIR);
#endif
qconf_set_log_level(QCONF_LOG_INFO);
LOG_INFO("agent_dir:%s", agent_dir.c_str());
// check whether agent is running
int pid_fd = 0;
string pid_file = agent_dir + QCONF_PID_FILE;
int ret = check_proc_exist(pid_file, pid_fd);
if (QCONF_OK != ret) return ret;
// load configure
// Check localidc argv
std::string localidc;
if (argc > 1) {
if (argc == 2) {
std::string localidc_s(argv[1]);
if (localidc_s.empty()) {
Usage();
}
size_t pos = localidc_s.find("=");
localidc.assign(localidc_s.substr(pos + 1));
} else {
Usage();
}
LOG_INFO("localidc: %s", localidc.c_str());
}
ret = qconf_load_conf(agent_dir, localidc);
if (QCONF_OK != ret)
{
LOG_FATAL_ERR("Failed to load configure!");
return ret;
}
// daemonize
string value;
long daemon_mode = 1;
ret = get_agent_conf(QCONF_KEY_DAEMON_MODE, value);
if (QCONF_OK == ret) get_integer(value, daemon_mode);
string log_fmt = agent_dir + QCONF_LOG_FMT;
ret = get_agent_conf(QCONF_KEY_LOG_FMT, value);
if (QCONF_OK == ret) log_fmt = value;
size_t pos = log_fmt.find_last_of('/');
string log_dir = (string::npos == pos) ? "." : string(log_fmt.c_str(), pos);
long log_level = QCONF_LOG_ERR;
ret = get_agent_conf(QCONF_KEY_LOG_LEVEL, value);
if (QCONF_OK == ret) get_integer(value, log_level);
qconf_log_init(log_fmt, log_level);
if (daemon_mode != 0)
{
close(pid_fd);
ret = qconf_agent_daemon_keepalive(pid_file);
if (QCONF_OK != ret)
{
qconf_destroy_log();
return ret;
}
}
else
{
write_pid(pid_fd, getpid());
}
// TODO: using socket
// Set the signal process handler
signal(SIGPIPE, SIG_IGN);
signal(SIGINT, sig_handler);
signal(SIGHUP, sig_handler);
signal(SIGTERM, sig_handler);
signal(SIGUSR1, sig_handler);
signal(SIGUSR2, sig_handler);
// Environment initialize
ret = get_agent_conf(SHARED_MEMORY_SIZE, value);
if (ret == QCONF_OK) {
maxSlotsNum = atoi(value.c_str());
}
else {
maxSlotsNum = QCONF_MAX_SLOTS_NUM;
}
ret = qconf_agent_init(agent_dir, log_dir);
if (QCONF_OK != ret)
{
LOG_ERR("Failed to init qconf agent!");
return ret;
}
// main
watcher_setting_start();
qconf_agent_destroy();
return QCONF_OK;
}
static int qconf_agent_init(const string &agent_dir, const string &log_dir)
{
string value;
int ret = QCONF_OK;
// init zookeeper log
ret = get_agent_conf(QCONF_KEY_ZKLOG_PATH, value);
if (QCONF_OK != ret)
qconf_init_zoo_log(log_dir);
else
qconf_init_zoo_log(log_dir, value);
// init cmd env
ret = qconf_init_cmd_env(agent_dir);
if (QCONF_OK != ret)
{
LOG_FATAL_ERR("Failed to init cmd environment");
return ret;
}
// init dump env
ret = qconf_init_dump_file(agent_dir);
if (QCONF_OK != ret)
{
LOG_FATAL_ERR("Failed to init dump file");
return ret;
}
// init share memory table
ret = qconf_init_shm_tbl();
if (QCONF_OK != ret)
{
LOG_FATAL_ERR("Failed to init share memory!");
return ret;
}
// init message queue
ret = qconf_init_msg_key();
if (QCONF_OK != ret)
{
LOG_FATAL_ERR("Failed to init message queue!");
return ret;
}
// init register node prefix
string node_prefix(QCONF_DEFAULT_REGIESTER_PREFIX);
ret = get_agent_conf(QCONF_KEY_REGISTER_NODE_PREFIX, value);
if (QCONF_OK == ret) node_prefix = value;
qconf_init_rgs_node_pfx(node_prefix);
// init zookeeper operation timeout
long zk_timeout = 3000;
ret = get_agent_conf(QCONF_KEY_ZKRECVTIMEOUT, value);
if (QCONF_OK == ret) get_integer(value, zk_timeout);
qconf_init_recv_timeout(static_cast<int>(zk_timeout));
// init local idc
ret = get_agent_conf(QCONF_KEY_LOCAL_IDC, value);
if (QCONF_OK != ret)
{
LOG_FATAL_ERR("Failed to get local idc!");
return ret;
}
ret = qconf_init_local_idc(value);
if (QCONF_OK != ret)
{
LOG_FATAL_ERR("Failed to set local idc!");
return ret;
}
// init script dir
qconf_init_script_dir(agent_dir);
// init script execute timeout
long sc_timeout = 3000;
ret = get_agent_conf(QCONF_KEY_SCEXECTIMEOUT, value);
if (QCONF_OK == ret) get_integer(value, sc_timeout);
qconf_init_scexec_timeout(static_cast<int>(sc_timeout));
#ifdef QCONF_CURL_ENABLE
long fd_enable = 0;
ret = get_agent_conf(QCONF_KEY_FEEDBACK_ENABLE, value);
if (QCONF_OK == ret) get_integer(value, fd_enable);
if (1 == fd_enable)
{
qconf_init_fb_flg(true);
ret = get_agent_conf(QCONF_KEY_FEEDBACK_URL, value);
if (QCONF_OK != ret)
{
LOG_FATAL_ERR("Failed to get feedback url!");
return ret;
}
ret = qconf_init_feedback(value);
if (QCONF_OK != ret)
{
LOG_FATAL_ERR("Failed to init feedback!");
return ret;
}
}
#endif
return QCONF_OK;
}
static void qconf_agent_destroy()
{
#ifdef QCONF_CURL_ENABLE
qconf_destroy_feedback();
#endif
qconf_destroy_zk();
qconf_destroy_conf_map();
qconf_destroy_dbf();
qconf_destroy_dump_lock();
qconf_destroy_qhasharr_lock();
qconf_destroy_zoo_log();
qconf_destroy_log();
}
static void sig_handler(int sig)
{
switch(sig)
{
case SIGINT:
break;
case SIGTERM:
case SIGUSR2:
qconf_thread_exit();
//qconf_agent_destroy();
//exit(0);
break;
case SIGHUP:
break;
case SIGUSR1:
qconf_cmd_proc();
break;
default:
break;
}
}