cli: add nmcli monitor
authorLubomir Rintel <lkundrak@v3.sk>
Fri, 27 Mar 2015 12:07:43 +0000 (13:07 +0100)
committerLubomir Rintel <lkundrak@v3.sk>
Sat, 5 Dec 2015 11:16:23 +0000 (12:16 +0100)
https://bugzilla.redhat.com/show_bug.cgi?id=1034158

clients/cli/connections.c
clients/cli/connections.h
clients/cli/devices.c
clients/cli/devices.h
clients/cli/general.c
clients/cli/general.h
clients/cli/nmcli.c
man/nmcli.1.in

index e35e7da..100d0a1 100644 (file)
@@ -10265,3 +10265,9 @@ opt_error:
        g_error_free (error);
        return nmc->return_value;
 }
+
+void
+monitor_connections (NmCli *nmc)
+{
+       do_connection_monitor (nmc, 0, NULL);
+}
index c736859..c158b63 100644 (file)
@@ -24,4 +24,6 @@
 
 NMCResultCode do_connections (NmCli *nmc, int argc, char **argv);
 
+void monitor_connections (NmCli *nmc);
+
 #endif /* NMC_CONNECTIONS_H */
index 4cfa35c..3ca3a6b 100644 (file)
@@ -3657,3 +3657,9 @@ opt_error:
        g_error_free (error);
        return nmc->return_value;
 }
+
+void
+monitor_devices (NmCli *nmc)
+{
+       do_device_monitor (nmc, 0, NULL);
+}
index 152dd20..56fc9c2 100644 (file)
@@ -24,4 +24,6 @@
 
 NMCResultCode do_devices (NmCli *nmc, int argc, char **argv);
 
+void monitor_devices (NmCli *nmc);
+
 #endif /* NMC_DEVICES_H */
index abf71f6..b49b242 100644 (file)
@@ -27,6 +27,9 @@
 #include "utils.h"
 #include "general.h"
 
+#include "devices.h"
+#include "connections.h"
+
 /* Available fields for 'general status' */
 static NmcOutputField nmc_fields_nm_status[] = {
        {"RUNNING",      N_("RUNNING")},       /* 0 */
@@ -207,6 +210,15 @@ usage_radio_wwan (void)
                      "Get status of mobile broadband radio switch, or turn it on/off.\n\n"));
 }
 
+static void
+usage_monitor (void)
+{
+       g_printerr (_("Usage: nmcli monitor\n"
+                     "\n"
+                     "Monitor NetworkManager changes.\n"
+                     "Prints a line whenever a change occurs in NetworkManager\n\n"));
+}
+
 /* quit main loop */
 static void
 quit (void)
@@ -889,3 +901,88 @@ finish:
        return nmc->return_value;
 }
 
+static void
+client_hostname (NMClient *client, GParamSpec *param, NmCli *nmc)
+{
+       const char *hostname;
+
+       g_object_get (client, NM_CLIENT_HOSTNAME, &hostname, NULL);
+       g_print (_("Hostname set to '%s'\n"), hostname);
+}
+
+static void
+client_primary_connection (NMClient *client, GParamSpec *param, NmCli *nmc)
+{
+       NMConnection *primary;
+       const char *id;
+
+       g_object_get (client, NM_CLIENT_PRIMARY_CONNECTION, &primary, NULL);
+       if (primary) {
+               id = nm_connection_get_id (primary);
+               if (!id)
+                       id = nm_connection_get_uuid (primary);
+
+               g_print (_("'%s' is now the primary connection\n"), id);
+       } else {
+               g_print (_("There's no primary connection\n"));
+       }
+}
+
+static void
+client_connectivity (NMClient *client, GParamSpec *param, NmCli *nmc)
+{
+       NMConnectivityState connectivity;
+       char *str;
+
+       g_object_get (client, NM_CLIENT_CONNECTIVITY, &connectivity, NULL);
+       str = nmc_colorize (nmc, connectivity_to_color (connectivity), NMC_TERM_FORMAT_NORMAL,
+                           _("Connectivity is now '%s'\n"), nm_connectivity_to_string (connectivity));
+       g_print ("%s", str);
+       g_free (str);
+}
+
+static void
+client_state (NMClient *client, GParamSpec *param, NmCli *nmc)
+{
+       NMState state;
+       char *str;
+
+       g_object_get (client, NM_CLIENT_STATE, &state, NULL);
+       str = nmc_colorize (nmc, state_to_color (state), NMC_TERM_FORMAT_NORMAL,
+                           _("Networkmanager is now in the '%s' state\n"),
+                           nm_state_to_string (state));
+       g_print ("%s", str);
+       g_free (str);
+}
+
+NMCResultCode
+do_monitor (NmCli *nmc, int argc, char **argv)
+{
+       if (argc > 0) {
+               if (!nmc_arg_is_help (*argv)) {
+                       g_string_printf (nmc->return_text, _("Error: 'monitor' command '%s' is not valid."), *argv);
+                       nmc->return_value = NMC_RESULT_ERROR_USER_INPUT;
+               }
+
+               usage_monitor ();
+               return nmc->return_value;
+       }
+
+       nmc->get_client (nmc); /* create NMClient */
+
+       g_signal_connect (nmc->client, "notify::" NM_CLIENT_HOSTNAME,
+                         G_CALLBACK (client_hostname), nmc);
+       g_signal_connect (nmc->client, "notify::" NM_CLIENT_PRIMARY_CONNECTION,
+                         G_CALLBACK (client_primary_connection), nmc);
+       g_signal_connect (nmc->client, "notify::" NM_CLIENT_CONNECTIVITY,
+                         G_CALLBACK (client_connectivity), nmc);
+       g_signal_connect (nmc->client, "notify::" NM_CLIENT_STATE,
+                         G_CALLBACK (client_state), nmc);
+
+       nmc->should_wait++;
+
+       monitor_devices (nmc);
+       monitor_connections (nmc);
+
+       return NMC_RESULT_SUCCESS;
+}
index 7e8c21f..40dddb2 100644 (file)
@@ -25,5 +25,6 @@
 NMCResultCode do_general    (NmCli *nmc, int argc, char **argv);
 NMCResultCode do_networking (NmCli *nmc, int argc, char **argv);
 NMCResultCode do_radio      (NmCli *nmc, int argc, char **argv);
+NMCResultCode do_monitor    (NmCli *nmc, int argc, char **argv);
 
 #endif /* NMC_GENERAL_H */
index 9242dd4..ac72607 100644 (file)
@@ -103,6 +103,7 @@ usage (const char *prog_name)
                      "  c[onnection]    NetworkManager's connections\n"
                      "  d[evice]        devices managed by NetworkManager\n"
                      "  a[gent]         NetworkManager secret agent or polkit agent\n"
+                     "  m[monitor]      monitor NetworkManager changes\n"
                      "\n"),
                    prog_name);
 }
@@ -119,6 +120,7 @@ static const struct cmd {
        NMCResultCode (*func) (NmCli *nmc, int argc, char **argv);
 } nmcli_cmds[] = {
        { "general",    do_general },
+       { "monitor",    do_monitor },
        { "networking", do_networking },
        { "radio",      do_radio },
        { "connection", do_connections },
index d24a1cb..6e8881b 100644 (file)
@@ -33,7 +33,7 @@ nmcli \- command\(hyline tool for controlling NetworkManager
 .sp
 
 .IR OBJECT " := { "
-.BR general " | " networking " | " radio " | " connection " | " device " | " agent
+.BR general " | " networking " | " radio " | " connection " | " device " | " agent " | " monitor
 .RI " }"
 .sp
 
@@ -259,6 +259,16 @@ are supplied, mobile broadband status is printed; \fIon\fP enables mobile broadb
 Show or set all previously mentioned radio switches at the same time.
 .RE
 
+.TP
+.B monitor \- monitor NetworkManager
+.br
+Use this object to observe NetworkManager activity. Watches for changes
+in connectivity state, devices or connection profiles.
+.br
+See also \fImonitor\fP command of \fIconnection\fP or \fIdevice\fP object
+to watch for changes in certain objects or object classes.
+.RE
+
 .TP
 .B connection \- start, stop, and manage network connections
 .sp
@@ -789,7 +799,8 @@ its name, UUID or D-Bus path. If <ID> is ambiguous, a keyword \fIid\fP,
 See \fBconnection show\fP above for the description of the <ID>-specifying keywords.
 .br
 Monitors all connection profiles in case none is specified. The command terminates
-when all monitored connections disappear.
+when all monitored connections disappear. If you want to monitor connection creation
+consider using the global monitor with \fInmcli monitor\fP command.
 .TP
 .B reload
 .br
@@ -861,7 +872,8 @@ Monitor device activity. This command prints a line whenever the specified devic
 change state.
 .br
 Monitors all devices in case no interface is specified. The monitor terminates when
-all specified devices disappear. 
+all specified devices disappear. If you want to monitor device addition consider
+using the global monitor with \fInmcli monitor\fP command.
 .TP
 .B wifi [list [ifname <ifname>] [bssid <BSSID>]]
 .br