-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConfig.cpp
More file actions
260 lines (231 loc) · 5.58 KB
/
Copy pathConfig.cpp
File metadata and controls
260 lines (231 loc) · 5.58 KB
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
#include <fstream>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <map>
#include <set>
#include <vector>
#include <iostream>
#include <unistd.h>
#include <pthread.h>
#include "Config.h"
#include "Util.h"
#include "Log.h"
#include "ConstDef.h"
using namespace std;
Config* Config::_instance = NULL;
Config::Config(){
pthread_mutex_init(&serviceMapLock, NULL);
//serviceMapLock = SPINLOCK_INITIALIZER;
resetConfig();
}
Config::~Config(){
delete _instance;
}
Config* Config::getInstance(){
if (!_instance){
_instance = new Config();
_instance->load();
//reload the config result in the change of loglevel in Log
Log::init(_instance->getLogLevel());
}
return _instance;
}
int Config::resetConfig(){
_autoStart = 1;
_daemonMode = 0;
_logLevel = 2;
_zkHost = "127.0.0.1:2181";
_zkLogPath = "";
_monitorHostname = "";
_connRetryCount = 3;
_scanInterval = 3;
_serviceMap.clear();
_zkRecvTimeout = 3000;
return 0;
}
int Config::setValueInt(const string& key, const string& value){
int intValue = atoi(value.c_str());
if (key == daemonMode) {
if (intValue != 0) {
_daemonMode = 1;
}
}
else if (key == autoStart){
if (intValue == 0) {
_autoStart = 0;
}
}
else if (key == logLevel){
if (intValue >= minLogLevel && intValue <= maxLogLevel) {
_logLevel = intValue;
}
}
else if (key == connRetryCount){
if (intValue > 0) {
_connRetryCount = intValue;
}
}
else if (key == scanInterval){
if (intValue > 0) {
_scanInterval = intValue;
}
}
return 0;
}
int Config::setValueStr(const string& key, const string& value){
if (key == instanceName){
_instanceName = value;
}
else if (key == zkLogPath){
_zkLogPath = value;
}
//find the zk host this monitor should focus on. Their idc should be the same
else if (key.substr(0, zkHost.length()) == zkHost){
char hostname[128] = {0};
if (gethostname(hostname, sizeof(hostname)) != 0) {
LOG(LOG_ERROR, "get host name failed");
exit(-1);
}
_monitorHostname = string(hostname);
string idc = key.substr(zkHost.length());
vector<string> singleWord = Util::split(string(hostname), '.');
size_t i = 0;
for (; i < singleWord.size(); ++i){
if (singleWord[i] == idc){
_zkHost = value;
break;
}
}
}
return 0;
}
int Config::load(){
ifstream file;
file.open(confPath);
if (file.good()){
resetConfig();
string line;
while (!file.eof()) {
getline(file, line);
Util::trim(line);
if (line.size() <= 0 || line[0] == '#') {
continue;
}
size_t pos = line.find('=');
if (pos == string::npos){
continue;
}
//get the key
string key = line.substr(0, pos);
Util::trim(key);
if (key.size() == 0) {
continue;
}
//get the value
string value = line.substr(pos + 1);
Util::trim(value);
if (value.size() == 0) {
continue;
}
setValueInt(key, value);
setValueStr(key, value);
}
}
else {
LOG(LOG_FATAL_ERROR, "Load configure file failed. path: %s", confPath.c_str());
exit(-1);
}
file.close();
return 0;
}
int Config::getLogLevel(){
return _logLevel;
}
int Config::isDaemonMode(){
return _daemonMode;
}
string Config::getMonitorHostname(){
return _monitorHostname;
}
int Config::isAutoStart(){
return _autoStart;
}
int Config::getConnRetryCount(){
return _connRetryCount;
}
int Config::getScanInterval(){
return _scanInterval;
}
string Config::getInstanceName(){
return _instanceName;
}
string Config::getZkHost(){
return _zkHost;
}
string Config::getZkLogPath(){
return _zkLogPath;
}
int Config::getZkRecvTimeout() {
return _zkRecvTimeout;
}
string Config::getNodeList() {
//should judge weather instanceName is empty
if (_instanceName.empty()) {
LOG(LOG_ERROR, "instance name is empty. using default_instance");
return LOCK_ROOT_DIR + SLASH + "default_instance" + SLASH + NODE_LIST;
}
return LOCK_ROOT_DIR + SLASH + _instanceName + SLASH + NODE_LIST;
}
string Config::getMonitorList() {
if (_instanceName.empty()) {
LOG(LOG_ERROR, "instance name is empty. using default_instance");
return LOCK_ROOT_DIR + SLASH + "default_instance" + SLASH + MONITOR_LIST;
}
return LOCK_ROOT_DIR + SLASH + _instanceName + SLASH + MONITOR_LIST;
}
int Config::printMap() {
for (auto it = _serviceMap.begin(); it != _serviceMap.end(); ++it) {
LOG(LOG_INFO, "path: %s", (it->first).c_str());
LOG(LOG_INFO, "host: %s", (it->second).getHost().c_str());
LOG(LOG_INFO, "port: %d", (it->second).getPort());
LOG(LOG_INFO, "service father: %s", (it->second).getServiceFather().c_str());
LOG(LOG_INFO, "status: %d", (it->second).getStatus());
}
return 0;
}
int Config::addService(string ipPath, ServiceItem serviceItem) {
pthread_mutex_lock(&serviceMapLock);
_serviceMap[ipPath] = serviceItem;
pthread_mutex_unlock(&serviceMapLock);
return 0;
}
void Config::deleteService(const string& ipPath) {
pthread_mutex_lock(&serviceMapLock);
_serviceMap.erase(ipPath);
pthread_mutex_unlock(&serviceMapLock);
}
map<string, ServiceItem> Config::getServiceMap() {
map<string, ServiceItem> ret;
pthread_mutex_lock(&serviceMapLock);
ret = _serviceMap;
pthread_mutex_unlock(&serviceMapLock);
return ret;
}
int Config::setServiceMap(string node, int val) {
pthread_mutex_lock(&serviceMapLock);
_serviceMap[node].setStatus(val);
pthread_mutex_unlock(&serviceMapLock);
return 0;
}
//no necessity to add lock
void Config::clearServiceMap() {
_serviceMap.clear();
}
ServiceItem Config::getServiceItem(const string& ipPath) {
ServiceItem ret;
pthread_mutex_lock(&serviceMapLock);
ret = _serviceMap[ipPath];
pthread_mutex_unlock(&serviceMapLock);
return ret;
}