summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMatt Spinler <spinler@us.ibm.com>2017-06-02 12:35:59 -0500
committerPatrick Williams <patrick@stwcx.xyz>2017-06-26 01:57:59 +0000
commit8b633b7ea4ab671a73c1f57d878041f83f672a7e (patch)
tree406277d39c8d64567aec85fdb492763ab3bb6ffd
parent7c33bff30f113fa8da7829322f6bb18d33bda46f (diff)
downloadphosphor-objmgr-8b633b7ea4ab671a73c1f57d878041f83f672a7e.tar.gz
phosphor-objmgr-8b633b7ea4ab671a73c1f57d878041f83f672a7e.zip
Fill in phosphor-unit-failure-monitor main()
Obtain the command line arguments, construct the monitor class, and have it analyze the failure. Change-Id: I27015b7d485995ac7605f07622112f4b8d0aa621 Signed-off-by: Matt Spinler <spinler@us.ibm.com>
-rw-r--r--fail-monitor/Makefile.am1
-rw-r--r--fail-monitor/argument.cpp92
-rw-r--r--fail-monitor/argument.hpp68
-rw-r--r--fail-monitor/main.cpp46
4 files changed, 207 insertions, 0 deletions
diff --git a/fail-monitor/Makefile.am b/fail-monitor/Makefile.am
index 6c5c5b2..61fde80 100644
--- a/fail-monitor/Makefile.am
+++ b/fail-monitor/Makefile.am
@@ -5,6 +5,7 @@ sbin_PROGRAMS = \
phosphor-unit-failure-monitor
phosphor_unit_failure_monitor_SOURCES = \
+ argument.cpp \
main.cpp \
monitor.cpp
diff --git a/fail-monitor/argument.cpp b/fail-monitor/argument.cpp
new file mode 100644
index 0000000..4f9003c
--- /dev/null
+++ b/fail-monitor/argument.cpp
@@ -0,0 +1,92 @@
+/**
+ * Copyright © 2017 IBM Corporation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+#include <iostream>
+#include <iterator>
+#include <algorithm>
+#include "argument.hpp"
+
+namespace phosphor
+{
+namespace unit
+{
+namespace failure
+{
+
+ArgumentParser::ArgumentParser(int argc, char** argv)
+{
+ int option = 0;
+ while (-1 != (option = getopt_long(argc, argv, optionStr, options, NULL)))
+ {
+ if ((option == '?') || (option == 'h'))
+ {
+ usage(argv);
+ exit(-1);
+ }
+
+ auto i = &options[0];
+ while ((i->val != option) && (i->val != 0))
+ {
+ ++i;
+ }
+
+ if (i->val)
+ {
+ arguments[i->name] = (i->has_arg ? optarg : trueString);
+ }
+ }
+}
+
+const std::string& ArgumentParser::operator[](const std::string& opt)
+{
+ auto i = arguments.find(opt);
+ if (i == arguments.end())
+ {
+ return emptyString;
+ }
+ else
+ {
+ return i->second;
+ }
+}
+
+void ArgumentParser::usage(char** argv)
+{
+ std::cerr << "Usage: " << argv[0] << " [options]\n";
+ std::cerr << "Options:\n";
+ std::cerr << " --help Print this menu\n";
+ std::cerr << " --source=<source> The source unit to monitor\n";
+ std::cerr << " --target=<target> The target unit to start or stop\n";
+ std::cerr << " --action=<action> Target unit action - start or stop\n";
+ std::cerr << std::flush;
+}
+
+const option ArgumentParser::options[] =
+{
+ { "source", required_argument, NULL, 's' },
+ { "action", required_argument, NULL, 'a' },
+ { "target", required_argument, NULL, 't' },
+ { "help", no_argument, NULL, 'h' },
+ { 0, 0, 0, 0},
+};
+
+const char* ArgumentParser::optionStr = "s:a:t:h?";
+
+const std::string ArgumentParser::trueString = "true";
+const std::string ArgumentParser::emptyString = "";
+
+}
+}
+}
diff --git a/fail-monitor/argument.hpp b/fail-monitor/argument.hpp
new file mode 100644
index 0000000..2d08a2f
--- /dev/null
+++ b/fail-monitor/argument.hpp
@@ -0,0 +1,68 @@
+#pragma once
+
+#include <getopt.h>
+#include <map>
+#include <string>
+
+namespace phosphor
+{
+namespace unit
+{
+namespace failure
+{
+
+/** @brief Class - Encapsulates parsing command line options and
+ * populating arguments
+ */
+class ArgumentParser
+{
+ public:
+ ArgumentParser() = delete;
+ ~ArgumentParser() = default;
+ ArgumentParser(const ArgumentParser&) = delete;
+ ArgumentParser& operator=(const ArgumentParser&) = delete;
+ ArgumentParser(ArgumentParser&&) = default;
+ ArgumentParser& operator=(ArgumentParser&&) = default;
+
+ /** @brief Contructs Argument object
+ *
+ * @param argc - the main function's argc passed as is
+ * @param argv - the main function's argv passed as is
+ * @return Object constructed
+ */
+ ArgumentParser(int argc, char** argv);
+
+ /** @brief Given an option, returns its argument(optarg)
+ *
+ * @param opt - command line option string
+ *
+ * @return argument which is a standard optarg
+ */
+ const std::string& operator[](const std::string& opt);
+
+ /** @brief Displays usage
+ *
+ * @param argv - the main function's argv passed as is
+ */
+ static void usage(char** argv);
+
+ /** @brief Set to 'true' when an option is passed */
+ static const std::string trueString;
+
+ /** @brief Set to '' when an option is not passed */
+ static const std::string emptyString;
+
+ private:
+ /** @brief Option to argument mapping */
+ std::map<const std::string, std::string> arguments;
+
+ /** @brief Array of struct options as needed by getopt_long */
+ static const option options[];
+
+ /** @brief optstring as needed by getopt_long */
+ static const char* optionStr;
+};
+
+}
+}
+}
diff --git a/fail-monitor/main.cpp b/fail-monitor/main.cpp
index b13dc7f..1c47b98 100644
--- a/fail-monitor/main.cpp
+++ b/fail-monitor/main.cpp
@@ -20,11 +20,57 @@
* then it will either stop or start the target unit, depending
* on the command line arguments.
*/
+#include <iostream>
+#include <map>
+#include "argument.hpp"
+#include "monitor.hpp"
+using namespace phosphor::unit::failure;
+
+/**
+ * Prints usage and exits the program
+ *
+ * @param[in] err - the error message to print
+ * @param[in] argv - argv from main()
+ */
+void exitWithError(const char* err, char** argv)
+{
+ std::cerr << "ERROR: " << err << "\n";
+ ArgumentParser::usage(argv);
+ exit(EXIT_FAILURE);
+}
+
+static const std::map<std::string, Monitor::Action> actions =
+{
+ {"start", Monitor::Action::start},
+ {"stop", Monitor::Action::stop}
+};
int main(int argc, char** argv)
{
+ ArgumentParser args(argc, argv);
+
+ auto source = args["source"];
+ if (source == ArgumentParser::emptyString)
+ {
+ exitWithError("Source not specified", argv);
+ }
+
+ auto target = args["target"];
+ if (target == ArgumentParser::emptyString)
+ {
+ exitWithError("Target not specified", argv);
+ }
+
+ auto a = actions.find(args["action"]);
+ if (a == actions.end())
+ {
+ exitWithError("Missing or invalid action specified", argv);
+ }
+
+ Monitor monitor{source, target, a->second};
+ monitor.analyze();
return 0;
}
OpenPOWER on IntegriCloud