ovsdb-server: Write manager status information to Manager table.
[sliver-openvswitch.git] / ovsdb / jsonrpc-server.c
index 3edcfff..92228be 100644 (file)
@@ -53,6 +53,9 @@ static void ovsdb_jsonrpc_session_close_all(struct ovsdb_jsonrpc_remote *);
 static void ovsdb_jsonrpc_session_reconnect_all(struct ovsdb_jsonrpc_remote *);
 static void ovsdb_jsonrpc_session_set_all_options(
     struct ovsdb_jsonrpc_remote *, const struct ovsdb_jsonrpc_options *);
+static void ovsdb_jsonrpc_session_get_status(
+    const struct ovsdb_jsonrpc_remote *,
+    struct shash *);
 
 /* Triggers. */
 static void ovsdb_jsonrpc_trigger_create(struct ovsdb_jsonrpc_session *,
@@ -194,6 +197,21 @@ ovsdb_jsonrpc_server_del_remote(struct shash_node *node)
     free(remote);
 }
 
+void
+ovsdb_jsonrpc_server_get_remote_status(const struct ovsdb_jsonrpc_server *svr,
+                                       struct shash *statuses)
+{
+    struct shash_node *node;
+
+    shash_init(statuses);
+
+    SHASH_FOR_EACH (node, &svr->remotes) {
+        const struct ovsdb_jsonrpc_remote *remote = node->data;
+
+        ovsdb_jsonrpc_session_get_status(remote, statuses);
+    }
+}
+
 /* Forces all of the JSON-RPC sessions managed by 'svr' to disconnect and
  * reconnect. */
 void
@@ -420,6 +438,43 @@ ovsdb_jsonrpc_session_set_all_options(
     }
 }
 
+static void
+ovsdb_jsonrpc_session_get_status(const struct ovsdb_jsonrpc_remote *remote,
+                                 struct shash *shash)
+{
+    const struct ovsdb_jsonrpc_session *s;
+    const struct jsonrpc_session *js;
+    const char *name;
+    struct ovsdb_jsonrpc_remote_status *status;
+    struct reconnect_stats rstats;
+
+    /* We only look at the first session in the list. There should be only one
+     * node in the list for outbound connections. We don't track status for
+     * each individual inbound connection if someone configures the DB that
+     * way. Since outbound connections are the norm, this is fine. */
+    if (list_is_empty(&remote->sessions)) {
+        return;
+    }
+    s = CONTAINER_OF(remote->sessions.next, struct ovsdb_jsonrpc_session, node);
+    js = s->js;
+    if (!js) {
+        return;
+    }
+    name = jsonrpc_session_get_name(js);
+
+    status = xzalloc(sizeof *status);
+    shash_add(shash, name, status);
+
+    status->is_connected = jsonrpc_session_is_connected(js);
+    status->last_error = jsonrpc_session_get_status(js);
+
+    jsonrpc_session_get_reconnect_stats(js, &rstats);
+    status->state = rstats.state;
+    status->state_elapsed = rstats.state_elapsed;
+
+    return;
+}
+
 static const char *
 get_db_name(const struct ovsdb_jsonrpc_session *s)
 {