/
options.cc
445 lines (407 loc) · 20.2 KB
/
options.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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
/*
* Copyright (c) 2014 Juniper Networks, Inc. All rights reserved.
*/
#include "analytics/options.h"
#include <fstream>
#include <iostream>
#include <boost/asio/ip/host_name.hpp>
#include "analytics/buildinfo.h"
#include "base/contrail_ports.h"
#include "base/logging.h"
#include "base/misc_utils.h"
#include "base/util.h"
#include "net/address_util.h"
#include "viz_constants.h"
#include <database/gendb_constants.h>
using namespace std;
using namespace boost::asio::ip;
namespace opt = boost::program_options;
// Process command line options for collector .
Options::Options() {
}
bool Options::Parse(EventManager &evm, int argc, char *argv[]) {
opt::options_description cmdline_options("Allowed options");
Initialize(evm, cmdline_options);
Process(argc, argv, cmdline_options);
return true;
}
// Initialize collector's command line option tags with appropriate default
// values. Options can from a config file as well. By default, we read
// options from /etc/contrail/contrail-collector.conf
void Options::Initialize(EventManager &evm,
opt::options_description &cmdline_options) {
boost::system::error_code error;
string hostname = host_name(error);
string host_ip = GetHostIp(evm.io_service(), hostname);
if (host_ip.empty()) {
cout << "Error! Cannot resolve host " << hostname <<
"to a valid IP address";
exit(-1);
}
vector<string> conf_files;
conf_files.push_back("/etc/contrail/contrail-collector.conf");
conf_files.push_back("/etc/contrail/contrail-keystone-auth.conf");
opt::options_description generic("Generic options");
// Command line only options.
generic.add_options()
("conf_file", opt::value<vector<string> >()->default_value(
conf_files,
"Configuration file"))
("help", "help message")
("version", "Display version information")
;
uint16_t default_redis_port = ContrailPorts::RedisUvePort();
uint16_t default_ks_port = ContrailPorts::KeystonePort();
uint16_t default_collector_port = ContrailPorts::CollectorPort();
uint16_t default_collector_protobuf_port =
ContrailPorts::CollectorProtobufPort();
uint16_t default_partitions = 15;
uint16_t default_http_server_port = ContrailPorts::HttpPortCollector();
uint16_t default_discovery_port = ContrailPorts::DiscoveryServerPort();
vector<string> default_cassandra_server_list;
string default_cassandra_server("127.0.0.1:9042");
default_cassandra_server_list.push_back(default_cassandra_server);
string default_zookeeper_server("127.0.0.1:2181");
vector<string> default_kafka_broker_list;
default_kafka_broker_list.push_back("");
// Command line and config file options.
opt::options_description cassandra_config("Cassandra Configuration options");
cassandra_config.add_options()
("CASSANDRA.cassandra_user", opt::value<string>()->default_value(""),
"Cassandra user name")
("CASSANDRA.cassandra_password", opt::value<string>()->default_value(""),
"Cassandra password")
("CASSANDRA.compaction_strategy",
opt::value<string>()->default_value(
GenDb::g_gendb_constants.SIZE_TIERED_COMPACTION_STRATEGY),
"Cassandra compaction strategy")
("CASSANDRA.flow_tables.compaction_strategy",
opt::value<string>()->default_value(
GenDb::g_gendb_constants.DATE_TIERED_COMPACTION_STRATEGY),
"Cassandra compaction strategy for flow tables");
// Command line and config file options.
opt::options_description config("Configuration options");
config.add_options()
("COLLECTOR.port", opt::value<uint16_t>()->default_value(
default_collector_port),
"Listener port of sandesh collector server")
("COLLECTOR.server",
opt::value<string>()->default_value("0.0.0.0"),
"IP address of sandesh collector server")
("COLLECTOR.protobuf_port",
opt::value<uint16_t>()->default_value(
default_collector_protobuf_port),
"Listener port of Google Protocol Buffer collector server")
("DEFAULT.analytics_data_ttl",
opt::value<uint64_t>()->default_value(g_viz_constants.TtlValuesDefault.find(TtlType::GLOBAL_TTL)->second),
"global TTL(hours) for analytics data")
("DEFAULT.analytics_config_audit_ttl",
opt::value<uint64_t>()->default_value(g_viz_constants.TtlValuesDefault.find(TtlType::CONFIGAUDIT_TTL)->second),
"global TTL(hours) for analytics config audit data")
("DEFAULT.analytics_statistics_ttl",
opt::value<uint64_t>()->default_value(g_viz_constants.TtlValuesDefault.find(TtlType::STATSDATA_TTL)->second),
"global TTL(hours) for analytics stats data")
("DEFAULT.analytics_flow_ttl",
opt::value<uint64_t>()->default_value(g_viz_constants.TtlValuesDefault.find(TtlType::FLOWDATA_TTL)->second),
"global TTL(hours) for analytics flow data")
("DEFAULT.cassandra_server_list",
opt::value<vector<string> >()->default_value(
default_cassandra_server_list, default_cassandra_server),
"Cassandra server list")
("DEFAULT.zookeeper_server_list",
opt::value<string>()->default_value(""),
"Zookeeper server list")
("DEFAULT.kafka_broker_list",
opt::value<vector<string> >()->default_value(
default_kafka_broker_list, ""),
"Kafka Broker List")
("DEFAULT.partitions",
opt::value<uint16_t>()->default_value(
default_partitions),
"Number of partitions to use for publishing to kafka")
("DEFAULT.dup", opt::bool_switch(&dup_), "Internal use flag")
("DEFAULT.hostip", opt::value<string>()->default_value(host_ip),
"IP address of collector")
("DEFAULT.hostname", opt::value<string>()->default_value(hostname),
"Hostname of collector")
("DEFAULT.http_server_port",
opt::value<uint16_t>()->default_value(default_http_server_port),
"Sandesh HTTP listener port")
("DEFAULT.log_category", opt::value<string>(),
"Category filter for local logging of sandesh messages")
("DEFAULT.log_disable", opt::bool_switch(&log_disable_),
"Disable sandesh logging")
("DEFAULT.log_file", opt::value<string>()->default_value("<stdout>"),
"Filename for the logs to be written to")
("DEFAULT.log_property_file", opt::value<string>()->default_value(""),
"log4cplus property file name")
("DEFAULT.log_files_count",
opt::value<int>()->default_value(10),
"Maximum log file roll over index")
("DEFAULT.log_file_size",
opt::value<long>()->default_value(1024*1024),
"Maximum size of the log file")
("DEFAULT.log_level", opt::value<string>()->default_value("SYS_NOTICE"),
"Severity level for local logging of sandesh messages")
("DEFAULT.log_local", opt::bool_switch(&log_local_),
"Enable local logging of sandesh messages")
("DEFAULT.use_syslog", opt::bool_switch(&use_syslog_),
"Enable logging to syslog")
("DEFAULT.syslog_facility", opt::value<string>()->default_value("LOG_LOCAL0"),
"Syslog facility to receive log lines")
("DEFAULT.kafka_prefix", opt::value<string>()->default_value(""),
"System Prefix for Kafka")
("DEFAULT.syslog_port", opt::value<int>()->default_value(-1),
"Syslog listener port (< 0 will disable the syslog)")
("DEFAULT.sflow_port", opt::value<int>()->default_value(6343),
"sFlow listener UDP port (< 0 will disable sFlow Collector)")
("DEFAULT.ipfix_port", opt::value<int>()->default_value(4739),
"ipfix listener UDP port (< 0 will disable ipfix Collector)")
("DEFAULT.test_mode", opt::bool_switch(&test_mode_),
"Enable collector to run in test-mode")
("DEFAULT.sandesh_send_rate_limit",
opt::value<uint32_t>()->default_value(
g_sandesh_constants.DEFAULT_SANDESH_SEND_RATELIMIT),
"Sandesh send rate limit in messages/sec")
("DEFAULT.disable_flow_collection",
opt::bool_switch(&disable_flow_collection_),
"Disable flow message collection")
("DATABASE.disable_all_writes",
opt::bool_switch(&disable_all_db_writes_),
"Disable all writes to the database")
("DATABASE.disable_statistics_writes",
opt::bool_switch(&disable_db_stats_writes_),
"Disable statistics writes to the database")
("DATABASE.disable_message_writes",
opt::bool_switch(&disable_db_messages_writes_),
"Disable message writes to the database")
("DATABASE.enable_message_keyword_writes",
opt::bool_switch(&enable_db_messages_keyword_writes_)->
default_value(false),
"Enable message keyword writes to the database")
("DISCOVERY.port", opt::value<uint16_t>()->default_value(
default_discovery_port),
"Port of Discovery Server")
("DISCOVERY.server", opt::value<string>()->default_value("127.0.0.1"),
"IP address of Discovery Server")
("REDIS.port",
opt::value<uint16_t>()->default_value(default_redis_port),
"Port of Redis-uve server")
("REDIS.server", opt::value<string>()->default_value("127.0.0.1"),
"IP address of Redis Server")
("REDIS.password", opt::value<string>()->default_value(""),
"password for Redis Server")
("KEYSTONE.auth_host", opt::value<string>()->default_value("127.0.0.1"),
"IP address of keystone Server")
("KEYSTONE.auth_port",
opt::value<uint16_t>()->default_value(default_ks_port),
"Keystone auth server port")
("KEYSTONE.auth_protocol", opt::value<string>()->default_value("http"),
"protocol used to authenticate with Keystone Server")
("KEYSTONE.admin_user", opt::value<string>()->default_value("admin"),
"Keystone username")
("KEYSTONE.admin_password", opt::value<string>()->default_value(
"admin123"), "Keystone password")
("KEYSTONE.admin_tenant_name", opt::value<string>()->default_value(
"tenant"), "Keystone tenant")
("KEYSTONE.insecure", opt::bool_switch(&ks_insecure_),
"keystone using tls")
("KEYSTONE.auth_url", opt::value<string>()->default_value("a.com"),
"keystone auth url")
("KEYSTONE.memcache_servers", opt::value<string>()->default_value(
"127.0.0.1:11211"), "memcache servers")
("KEYSTONE.certfile", opt::value<string>()->default_value(
"/etc/contrail/ks-cert"), "Keystone certificate")
("KEYSTONE.keyfile", opt::value<string>()->default_value(
"/etc/contrail/ks-key"), "Keystone private key")
("KEYSTONE.cafile", opt::value<string>()->default_value(
"/etc/contrail/ks-ca"), "Keystone CA chain")
;
config_file_options_.add(config).add(cassandra_config);
cmdline_options.add(generic).add(config).add(cassandra_config);
}
template <typename ValueType>
void Options::GetOptValue(const boost::program_options::variables_map &var_map,
ValueType &var, std::string val) {
GetOptValueImpl(var_map, var, val, static_cast<ValueType *>(0));
}
template <typename ValueType>
void Options::GetOptValueImpl(
const boost::program_options::variables_map &var_map,
ValueType &var, std::string val, ValueType*) {
// Check if the value is present.
if (var_map.count(val)) {
var = var_map[val].as<ValueType>();
}
}
template <typename ValueType>
bool Options::GetOptValueIfNotDefaulted(
const boost::program_options::variables_map &var_map,
ValueType &var, std::string val) {
return GetOptValueIfNotDefaultedImpl(var_map, var, val,
static_cast<ValueType *>(0));
}
template <typename ValueType>
bool Options::GetOptValueIfNotDefaultedImpl(
const boost::program_options::variables_map &var_map,
ValueType &var, std::string val, ValueType*) {
// Check if the value is present.
if (var_map.count(val) && !var_map[val].defaulted()) {
var = var_map[val].as<ValueType>();
return true;
}
return false;
}
template <typename ElementType>
void Options::GetOptValueImpl(
const boost::program_options::variables_map &var_map,
std::vector<ElementType> &var, std::string val, std::vector<ElementType>*) {
// Check if the value is present.
if (var_map.count(val)) {
std::vector<ElementType> tmp(
var_map[val].as<std::vector<ElementType> >());
// Now split the individual elements
for (typename std::vector<ElementType>::const_iterator it =
tmp.begin();
it != tmp.end(); it++) {
std::stringstream ss(*it);
std::copy(istream_iterator<ElementType>(ss),
istream_iterator<ElementType>(),
std::back_inserter(var));
}
}
}
static bool ValidateCompactionStrategyOption(
const std::string &compaction_strategy,
const std::string &option) {
if (!((compaction_strategy ==
GenDb::g_gendb_constants.DATE_TIERED_COMPACTION_STRATEGY) ||
(compaction_strategy ==
GenDb::g_gendb_constants.LEVELED_COMPACTION_STRATEGY) ||
(compaction_strategy ==
GenDb::g_gendb_constants.SIZE_TIERED_COMPACTION_STRATEGY))) {
cout << "Invalid " << option << ", please select one of [" <<
GenDb::g_gendb_constants.DATE_TIERED_COMPACTION_STRATEGY << ", " <<
GenDb::g_gendb_constants.LEVELED_COMPACTION_STRATEGY << ", " <<
GenDb::g_gendb_constants.SIZE_TIERED_COMPACTION_STRATEGY << "]" <<
endl;
return false;
}
return true;
}
// Process command line options. They can come from a conf file as well. Options
// from command line always overrides those that come from the config file.
void Options::Process(int argc, char *argv[],
opt::options_description &cmdline_options) {
// Process options off command line first.
opt::variables_map var_map;
opt::store(opt::parse_command_line(argc, argv, cmdline_options), var_map);
// Process options off configuration file.
GetOptValue< vector<string> >(var_map, config_file_,
"conf_file");
ifstream config_file_in;
for(std::vector<int>::size_type i = 0; i != config_file_.size(); i++) {
config_file_in.open(config_file_[i].c_str());
if (config_file_in.good()) {
opt::store(opt::parse_config_file(config_file_in, config_file_options_),
var_map);
}
config_file_in.close();
}
opt::notify(var_map);
if (var_map.count("help")) {
cout << cmdline_options << endl;
exit(0);
}
if (var_map.count("version")) {
string build_info;
cout << MiscUtils::GetBuildInfo(MiscUtils::Analytics, BuildInfo,
build_info) << endl;
exit(0);
}
// Retrieve the options.
GetOptValue<uint16_t>(var_map, collector_port_, "COLLECTOR.port");
GetOptValue<string>(var_map, collector_server_, "COLLECTOR.server");
if (GetOptValueIfNotDefaulted<uint16_t>(var_map, collector_protobuf_port_,
"COLLECTOR.protobuf_port")) {
collector_protobuf_port_configured_ = true;
} else {
collector_protobuf_port_configured_ = false;
}
GetOptValue<uint64_t>(var_map, analytics_data_ttl_,
"DEFAULT.analytics_data_ttl");
if (analytics_data_ttl_ == (uint64_t)-1) {
analytics_data_ttl_ = g_viz_constants.TtlValuesDefault.find(TtlType::GLOBAL_TTL)->second;
}
GetOptValue<uint64_t>(var_map, analytics_config_audit_ttl_,
"DEFAULT.analytics_config_audit_ttl");
if (analytics_config_audit_ttl_ == (uint64_t)-1) {
analytics_config_audit_ttl_ = g_viz_constants.TtlValuesDefault.find(TtlType::CONFIGAUDIT_TTL)->second;
}
GetOptValue<uint64_t>(var_map, analytics_statistics_ttl_,
"DEFAULT.analytics_statistics_ttl");
if (analytics_statistics_ttl_ == (uint64_t)-1) {
analytics_statistics_ttl_ = g_viz_constants.TtlValuesDefault.find(TtlType::STATSDATA_TTL)->second;
}
GetOptValue<uint64_t>(var_map, analytics_flow_ttl_,
"DEFAULT.analytics_flow_ttl");
if (analytics_flow_ttl_ == (uint64_t)-1) {
analytics_flow_ttl_ = g_viz_constants.TtlValuesDefault.find(TtlType::FLOWDATA_TTL)->second;
}
GetOptValue< vector<string> >(var_map, cassandra_server_list_,
"DEFAULT.cassandra_server_list");
GetOptValue<string>(var_map, zookeeper_server_list_,
"DEFAULT.zookeeper_server_list");
GetOptValue< vector<string> >(var_map, kafka_broker_list_,
"DEFAULT.kafka_broker_list");
GetOptValue<uint16_t>(var_map, partitions_, "DEFAULT.partitions");
GetOptValue<string>(var_map, host_ip_, "DEFAULT.hostip");
GetOptValue<string>(var_map, hostname_, "DEFAULT.hostname");
GetOptValue<uint16_t>(var_map, http_server_port_,
"DEFAULT.http_server_port");
GetOptValue<string>(var_map, log_category_, "DEFAULT.log_category");
GetOptValue<string>(var_map, log_file_, "DEFAULT.log_file");
GetOptValue<string>(var_map, log_property_file_, "DEFAULT.log_property_file");
GetOptValue<int>(var_map, log_files_count_, "DEFAULT.log_files_count");
GetOptValue<long>(var_map, log_file_size_, "DEFAULT.log_file_size");
GetOptValue<string>(var_map, log_level_, "DEFAULT.log_level");
GetOptValue<bool>(var_map, use_syslog_, "DEFAULT.use_syslog");
GetOptValue<string>(var_map, syslog_facility_, "DEFAULT.syslog_facility");
GetOptValue<string>(var_map, kafka_prefix_, "DEFAULT.kafka_prefix");
GetOptValue<int>(var_map, syslog_port_, "DEFAULT.syslog_port");
GetOptValue<int>(var_map, sflow_port_, "DEFAULT.sflow_port");
GetOptValue<int>(var_map, ipfix_port_, "DEFAULT.ipfix_port");
GetOptValue<uint32_t>(var_map, sandesh_ratelimit_,
"DEFAULT.sandesh_send_rate_limit");
GetOptValue<uint16_t>(var_map, discovery_port_, "DISCOVERY.port");
GetOptValue<string>(var_map, discovery_server_, "DISCOVERY.server");
GetOptValue<uint16_t>(var_map, redis_port_, "REDIS.port");
GetOptValue<string>(var_map, redis_server_, "REDIS.server");
GetOptValue<string>(var_map, redis_password_, "REDIS.password");
GetOptValue<string>(var_map, cassandra_user_, "CASSANDRA.cassandra_user");
GetOptValue<string>(var_map, cassandra_password_, "CASSANDRA.cassandra_password");
GetOptValue<string>(var_map, cassandra_compaction_strategy_,
"CASSANDRA.compaction_strategy");
if (!ValidateCompactionStrategyOption(cassandra_compaction_strategy_,
"CASSANDRA.compaction_strategy")) {
exit(-1);
}
GetOptValue<string>(var_map, cassandra_flow_tables_compaction_strategy_,
"CASSANDRA.flow_tables.compaction_strategy");
if (!ValidateCompactionStrategyOption(
cassandra_flow_tables_compaction_strategy_,
"CASSANDRA.flow_tables.compaction_strategy")) {
exit(-1);
}
GetOptValue<uint16_t>(var_map, ks_port_, "KEYSTONE.auth_port");
GetOptValue<string>(var_map, ks_server_, "KEYSTONE.auth_host");
GetOptValue<string>(var_map, ks_protocol_, "KEYSTONE.auth_protocol");
GetOptValue<string>(var_map, ks_user_, "KEYSTONE.admin_user");
GetOptValue<string>(var_map, ks_password_, "KEYSTONE.admin_password");
GetOptValue<string>(var_map, ks_tenant_, "KEYSTONE.admin_tenant_name");
GetOptValue<string>(var_map, ks_authurl_, "KEYSTONE.auth_url");
GetOptValue<string>(var_map, memcache_servers_, "KEYSTONE.memcache_servers");
GetOptValue<string>(var_map, ks_cert_, "KEYSTONE.certfile");
GetOptValue<string>(var_map, ks_key_, "KEYSTONE.keyfile");
GetOptValue<string>(var_map, ks_ca_, "KEYSTONE.cafile");
}