Change in osmo-sysmon[master]: Add cmdline option parsing support

2018-12-13 Thread Pau Espin Pedrol
Pau Espin Pedrol has submitted this change and it was merged. ( 
https://gerrit.osmocom.org/12285 )

Change subject: Add cmdline option parsing support
..

Add cmdline option parsing support

Change-Id: I742974bd1440b09b49d26703c13361dd1c41008b
---
M src/osysmon_main.c
1 file changed, 95 insertions(+), 4 deletions(-)

Approvals:
  Jenkins Builder: Verified
  Harald Welte: Looks good to me, approved

Objections:
  Max: I would prefer this is not merged as is



diff --git a/src/osysmon_main.c b/src/osysmon_main.c
index 5983212..486ee8f 100644
--- a/src/osysmon_main.c
+++ b/src/osysmon_main.c
@@ -26,6 +26,7 @@
 #include 
 #include 
 #include 
+#include 

 #include "config.h"
 #include "osysmon.h"
@@ -69,8 +70,6 @@
.is_config_node = osysmon_is_config_node,
 };

-
-static const char *config_file = "osmo-sysmon.cfg";
 struct osysmon_state *g_oss;


@@ -110,6 +109,88 @@
}
 }

+static void print_usage()
+{
+   printf("Usage: osmo-sysmon\n");
+}
+
+static void print_help()
+{
+   printf("  -h --help  This text.\n");
+   printf("  -c --config-file filename  The config file to use.\n");
+   printf("  -d option --debug=DRLL:DCC:DMM:DRR:DRSL:DNM  Enable 
debugging.\n");
+   printf("  -D --daemonize Fork the process into a background 
daemon.\n");
+   printf("  -s --disable-color Do not print ANSI colors in the 
log\n");
+   printf("  -T --timestamp Prefix every log line with a 
timestamp.\n");
+   printf("  -e --log-level number  Set a global loglevel.\n");
+   printf("  -V --version   Print the version of OsmoHLR.\n");
+}
+
+static struct {
+   const char *config_file;
+   bool daemonize;
+} cmdline_opts = {
+   .config_file = "osmo-sysmon.cfg",
+   .daemonize = false,
+};
+
+static void handle_options(int argc, char **argv)
+{
+   while (1) {
+   int option_index = 0, c;
+   static struct option long_options[] = {
+   {"help", 0, 0, 'h'},
+   {"config-file", 1, 0, 'c'},
+   {"debug", 1, 0, 'd'},
+   {"daemonize", 0, 0, 'D'},
+   {"disable-color", 0, 0, 's'},
+   {"log-level", 1, 0, 'e'},
+   {"timestamp", 0, 0, 'T'},
+   {"version", 0, 0, 'V' },
+   {0, 0, 0, 0}
+   };
+
+   c = getopt_long(argc, argv, "hc:d:Dse:TV",
+   long_options, _index);
+   if (c == -1)
+   break;
+
+   switch (c) {
+   case 'h':
+   print_usage();
+   print_help();
+   exit(0);
+   case 'c':
+   cmdline_opts.config_file = optarg;
+   break;
+   case 'd':
+   log_parse_category_mask(osmo_stderr_target, optarg);
+   break;
+   case 'D':
+   cmdline_opts.daemonize = 1;
+   break;
+   case 's':
+   log_set_use_color(osmo_stderr_target, 0);
+   break;
+   case 'e':
+   log_set_log_level(osmo_stderr_target, atoi(optarg));
+   break;
+   case 'T':
+   log_set_print_timestamp(osmo_stderr_target, 1);
+   break;
+   case 'V':
+   print_version(1);
+   exit(0);
+   break;
+   default:
+   /* catch unknown options *as well as* missing 
arguments. */
+   fprintf(stderr, "Error in command line options. 
Exiting.\n");
+   exit(-1);
+   break;
+   }
+   }
+}
+
 int main(int argc, char **argv)
 {
int rc;
@@ -122,14 +203,16 @@
INIT_LLIST_HEAD(_oss->files);

vty_init(_info);
+   handle_options(argc, argv);
osysmon_sysinfo_init();
osysmon_ctrl_init();
osysmon_rtnl_init();
osysmon_file_init();

-   rc = vty_read_config_file(config_file, NULL);
+   rc = vty_read_config_file(cmdline_opts.config_file, NULL);
if (rc < 0) {
-   fprintf(stderr, "Failed to parse the config file %s\n", 
config_file);
+   fprintf(stderr, "Failed to parse the config file %s\n",
+   cmdline_opts.config_file);
exit(2);
}

@@ -137,6 +220,14 @@
signal(SIGUSR2, _handler);
osmo_init_ignore_signals();

+   if (cmdline_opts.daemonize) {
+   rc = osmo_daemonize();
+   if (rc < 0) {
+   perror("Error during daemonize");
+   

Change in osmo-sysmon[master]: Add cmdline option parsing support

2018-12-13 Thread Harald Welte
Harald Welte has posted comments on this change. ( 
https://gerrit.osmocom.org/12285 )

Change subject: Add cmdline option parsing support
..


Patch Set 1: Code-Review+2

I tend to agree with Max that daemonization doesn't make sense. But then, 
unless somebody explicitly uses that particular option, it doesn't hurt either.


--
To view, visit https://gerrit.osmocom.org/12285
To unsubscribe, or for help writing mail filters, visit 
https://gerrit.osmocom.org/settings

Gerrit-Project: osmo-sysmon
Gerrit-Branch: master
Gerrit-MessageType: comment
Gerrit-Change-Id: I742974bd1440b09b49d26703c13361dd1c41008b
Gerrit-Change-Number: 12285
Gerrit-PatchSet: 1
Gerrit-Owner: Pau Espin Pedrol 
Gerrit-Reviewer: Harald Welte 
Gerrit-Reviewer: Jenkins Builder (102)
Gerrit-Reviewer: Max 
Gerrit-Reviewer: Pau Espin Pedrol 
Gerrit-Comment-Date: Thu, 13 Dec 2018 13:44:09 +
Gerrit-HasComments: No
Gerrit-HasLabels: Yes


Change in osmo-sysmon[master]: Add cmdline option parsing support

2018-12-13 Thread Pau Espin Pedrol
Pau Espin Pedrol has posted comments on this change. ( 
https://gerrit.osmocom.org/12285 )

Change subject: Add cmdline option parsing support
..


Patch Set 1:

> (1 comment)
 >
 > This program regularly print stuff on screen based on config file -
 > kinda similar to "watch". Why and when would we want to run it as
 > daemon?

You may still want to run it as a daemon and log to a file or syslog/journald 
to have a log where you can see evolution of the state of the system. For 
instance to see if some link broke during a wile and for how much time.
It's true that the output format is not the best to be used this way, but it 
can be improved later via some cmd line opt to print in a better format for 
this kind of scenario (like printing only relevant changes, or everything in 1 
line).


--
To view, visit https://gerrit.osmocom.org/12285
To unsubscribe, or for help writing mail filters, visit 
https://gerrit.osmocom.org/settings

Gerrit-Project: osmo-sysmon
Gerrit-Branch: master
Gerrit-MessageType: comment
Gerrit-Change-Id: I742974bd1440b09b49d26703c13361dd1c41008b
Gerrit-Change-Number: 12285
Gerrit-PatchSet: 1
Gerrit-Owner: Pau Espin Pedrol 
Gerrit-Reviewer: Jenkins Builder (102)
Gerrit-Reviewer: Max 
Gerrit-Reviewer: Pau Espin Pedrol 
Gerrit-Comment-Date: Thu, 13 Dec 2018 11:34:47 +
Gerrit-HasComments: No
Gerrit-HasLabels: No


Change in osmo-sysmon[master]: Add cmdline option parsing support

2018-12-13 Thread Max
Max has posted comments on this change. ( https://gerrit.osmocom.org/12285 )

Change subject: Add cmdline option parsing support
..


Patch Set 1: Code-Review-1

(1 comment)

This program regularly print stuff on screen based on config file - kinda 
similar to "watch". Why and when would we want to run it as daemon?

https://gerrit.osmocom.org/#/c/12285/1/src/osysmon_main.c
File src/osysmon_main.c:

https://gerrit.osmocom.org/#/c/12285/1/src/osysmon_main.c@224
PS1, Line 224:  rc = osmo_daemonize();
What would be use-case for this?



--
To view, visit https://gerrit.osmocom.org/12285
To unsubscribe, or for help writing mail filters, visit 
https://gerrit.osmocom.org/settings

Gerrit-Project: osmo-sysmon
Gerrit-Branch: master
Gerrit-MessageType: comment
Gerrit-Change-Id: I742974bd1440b09b49d26703c13361dd1c41008b
Gerrit-Change-Number: 12285
Gerrit-PatchSet: 1
Gerrit-Owner: Pau Espin Pedrol 
Gerrit-Reviewer: Jenkins Builder (102)
Gerrit-Reviewer: Max 
Gerrit-Comment-Date: Thu, 13 Dec 2018 11:06:08 +
Gerrit-HasComments: Yes
Gerrit-HasLabels: Yes


Change in osmo-sysmon[master]: Add cmdline option parsing support

2018-12-12 Thread Pau Espin Pedrol
Pau Espin Pedrol has uploaded this change for review. ( 
https://gerrit.osmocom.org/12285


Change subject: Add cmdline option parsing support
..

Add cmdline option parsing support

Change-Id: I742974bd1440b09b49d26703c13361dd1c41008b
---
M src/osysmon_main.c
1 file changed, 95 insertions(+), 4 deletions(-)



  git pull ssh://gerrit.osmocom.org:29418/osmo-sysmon refs/changes/85/12285/1

diff --git a/src/osysmon_main.c b/src/osysmon_main.c
index 5983212..486ee8f 100644
--- a/src/osysmon_main.c
+++ b/src/osysmon_main.c
@@ -26,6 +26,7 @@
 #include 
 #include 
 #include 
+#include 

 #include "config.h"
 #include "osysmon.h"
@@ -69,8 +70,6 @@
.is_config_node = osysmon_is_config_node,
 };

-
-static const char *config_file = "osmo-sysmon.cfg";
 struct osysmon_state *g_oss;


@@ -110,6 +109,88 @@
}
 }

+static void print_usage()
+{
+   printf("Usage: osmo-sysmon\n");
+}
+
+static void print_help()
+{
+   printf("  -h --help  This text.\n");
+   printf("  -c --config-file filename  The config file to use.\n");
+   printf("  -d option --debug=DRLL:DCC:DMM:DRR:DRSL:DNM  Enable 
debugging.\n");
+   printf("  -D --daemonize Fork the process into a background 
daemon.\n");
+   printf("  -s --disable-color Do not print ANSI colors in the 
log\n");
+   printf("  -T --timestamp Prefix every log line with a 
timestamp.\n");
+   printf("  -e --log-level number  Set a global loglevel.\n");
+   printf("  -V --version   Print the version of OsmoHLR.\n");
+}
+
+static struct {
+   const char *config_file;
+   bool daemonize;
+} cmdline_opts = {
+   .config_file = "osmo-sysmon.cfg",
+   .daemonize = false,
+};
+
+static void handle_options(int argc, char **argv)
+{
+   while (1) {
+   int option_index = 0, c;
+   static struct option long_options[] = {
+   {"help", 0, 0, 'h'},
+   {"config-file", 1, 0, 'c'},
+   {"debug", 1, 0, 'd'},
+   {"daemonize", 0, 0, 'D'},
+   {"disable-color", 0, 0, 's'},
+   {"log-level", 1, 0, 'e'},
+   {"timestamp", 0, 0, 'T'},
+   {"version", 0, 0, 'V' },
+   {0, 0, 0, 0}
+   };
+
+   c = getopt_long(argc, argv, "hc:d:Dse:TV",
+   long_options, _index);
+   if (c == -1)
+   break;
+
+   switch (c) {
+   case 'h':
+   print_usage();
+   print_help();
+   exit(0);
+   case 'c':
+   cmdline_opts.config_file = optarg;
+   break;
+   case 'd':
+   log_parse_category_mask(osmo_stderr_target, optarg);
+   break;
+   case 'D':
+   cmdline_opts.daemonize = 1;
+   break;
+   case 's':
+   log_set_use_color(osmo_stderr_target, 0);
+   break;
+   case 'e':
+   log_set_log_level(osmo_stderr_target, atoi(optarg));
+   break;
+   case 'T':
+   log_set_print_timestamp(osmo_stderr_target, 1);
+   break;
+   case 'V':
+   print_version(1);
+   exit(0);
+   break;
+   default:
+   /* catch unknown options *as well as* missing 
arguments. */
+   fprintf(stderr, "Error in command line options. 
Exiting.\n");
+   exit(-1);
+   break;
+   }
+   }
+}
+
 int main(int argc, char **argv)
 {
int rc;
@@ -122,14 +203,16 @@
INIT_LLIST_HEAD(_oss->files);

vty_init(_info);
+   handle_options(argc, argv);
osysmon_sysinfo_init();
osysmon_ctrl_init();
osysmon_rtnl_init();
osysmon_file_init();

-   rc = vty_read_config_file(config_file, NULL);
+   rc = vty_read_config_file(cmdline_opts.config_file, NULL);
if (rc < 0) {
-   fprintf(stderr, "Failed to parse the config file %s\n", 
config_file);
+   fprintf(stderr, "Failed to parse the config file %s\n",
+   cmdline_opts.config_file);
exit(2);
}

@@ -137,6 +220,14 @@
signal(SIGUSR2, _handler);
osmo_init_ignore_signals();

+   if (cmdline_opts.daemonize) {
+   rc = osmo_daemonize();
+   if (rc < 0) {
+   perror("Error during daemonize");
+   exit(1);
+   }
+   }
+
while (1) {