From d51761cc341dd612f08521064642e944ca82ecde Mon Sep 17 00:00:00 2001 From: Pere Diaz Bou Date: Thu, 5 May 2022 19:09:02 +0200 Subject: [PATCH] exporter: argument parsing Signed-off-by: Pere Diaz Bou --- src/exporter/ceph_exporter.cc | 47 ++++++++++++++++++++++++++++++++--- src/exporter/http_server.cc | 3 ++- src/exporter/http_server.h | 4 ++- 3 files changed, 48 insertions(+), 6 deletions(-) diff --git a/src/exporter/ceph_exporter.cc b/src/exporter/ceph_exporter.cc index 3122a9ca45a76..4cf64ef4e7d67 100644 --- a/src/exporter/ceph_exporter.cc +++ b/src/exporter/ceph_exporter.cc @@ -1,14 +1,53 @@ +#include "common/ceph_argparse.h" #include "exporter/DaemonMetricCollector.h" #include "exporter/http_server.h" +#include #include #include -#include #include -#include +static void usage() { + std::cout << "usage: ceph-exporter --cert cert.pem --key cert.key " + "--tls_options no[flags]\n" + << "--cert: TLS/SSL certificate in pem format\n" + << "--key: TLS/SSL key in pem format\n" + << "--tls_options: colon separated options for tls and ssl.\n" + << "\tExample -> " + "default_workarounds:no_compression:no_sslv2:no_sslv3:no_tlsv1:" + "no_tlsv1_1:no_tlsv1_2:single_dh_use\n" + << std::endl; + generic_server_usage(); +} + +int main(int argc, char **argv) { + + auto args = argv_to_vec(argc, argv); + if (args.empty()) { + std::cerr << argv[0] << ": -h or --help for usage" << std::endl; + exit(1); + } + if (ceph_argparse_need_usage(args)) { + usage(); + exit(0); + } + std::string val, cert_path, key_path, tls_options; + for (auto i = args.begin(); i != args.end();) { + if (ceph_argparse_witharg(args, i, &val, "--cert", (char *)NULL)) { + cert_path = val; + } else if (ceph_argparse_witharg(args, i, &val, "--key", (char *)NULL)) { + key_path = val; + } else if (ceph_argparse_witharg(args, i, &val, "--tls_options", + (char *)NULL)) { + tls_options = val; + } else if (ceph_argparse_flag(args, i, "--help", (char *)NULL) || + ceph_argparse_flag(args, i, "-h", (char *)NULL)) { + usage(); + exit(0); + } + } -int main(int argc, char** argv) { - boost::thread server_thread(http_server_thread_entrypoint); + boost::thread server_thread(http_server_thread_entrypoint, cert_path, + key_path, tls_options); DaemonMetricCollector &collector = collector_instance(); collector.main(); server_thread.join(); diff --git a/src/exporter/http_server.cc b/src/exporter/http_server.cc index cb9a72104bed3..5cd153e33c1c1 100644 --- a/src/exporter/http_server.cc +++ b/src/exporter/http_server.cc @@ -145,7 +145,8 @@ std::string dns_lookup(std::string hostname) { return ip_address; } -void http_server_thread_entrypoint() { +void http_server_thread_entrypoint(std::string cert_path, std::string key_path, + std::string tls_options) { try { std::string hostname = ceph_get_short_hostname(); diff --git a/src/exporter/http_server.h b/src/exporter/http_server.h index 55e61904a9f4c..368932ae03a54 100644 --- a/src/exporter/http_server.h +++ b/src/exporter/http_server.h @@ -1,3 +1,5 @@ #pragma once -void http_server_thread_entrypoint(); +#include + +void http_server_thread_entrypoint(std::string cert_path, std::string key_path, std::string tls_options); -- 2.39.5