Esempio n. 1
0
        /// <summary>
        /// Fetches the current status.
        /// </summary>
        public static ReplicaSetStatus GetStatus()
        {
            if (!RoleEnvironment.IsAvailable)
            {
                return(GetDummyStatus());
            }

            var connection = MongoServer.Create(ConnectionUtilities.GetConnectionSettings(true));

            try
            {
                return(ParseStatus(connection["admin"]["$cmd"].FindOne(Query.EQ("replSetGetStatus", 1))));
            }
            catch (Exception e)
            {
                return(new ReplicaSetStatus {
                    Status = State.Error, Error = e
                });
            }
        }
Esempio n. 2
0
        public static ReplicaSetStatus GetReplicaSetStatus()
        {
            ReplicaSetStatus status;
            var settings = ConnectionUtilities.GetConnectionSettings( );

            settings.SlaveOk = true;
            var server = MongoServer.Create(settings);

            try
            {
                var result   = server["admin"].RunCommand("replSetGetStatus");
                var response = result.Response;

                BsonValue startupStatus;
                if (response.TryGetValue("startupStatus", out startupStatus))
                {
                    status = new ReplicaSetStatus("Replica Set Initializing");
                    return(status);
                }

                status = new ReplicaSetStatus((string)response.GetValue("set"));
                var value   = response.GetElement("members");
                var members = value.Value.AsBsonArray;
                foreach (BsonDocument member in members)
                {
                    var node = new ServerStatus();
                    foreach (BsonElement bsonElement in member.Elements)
                    {
                        switch (bsonElement.Name)
                        {
                        case "_id":
                            node.id = bsonElement.Value.ToInt32();
                            break;

                        case "name":
                            node.name = bsonElement.Value.ToString();
                            break;

                        case "health":
                            node.health = bsonElement.Value.ToInt32() == 0 ? "DOWN" : "UP";
                            break;

                        case "state":
                            node.state = bsonElement.Value.ToInt32();
                            if (node.state == 1)
                            {
                                node.lastHeartbeat = "Not Applicable";
                                node.pingMS        = "Not Applicable";
                            }
                            break;

                        case "stateStr":
                            node.stateStr = bsonElement.Value.ToString();
                            break;

                        case "uptime":
                            break;

                        case "lastHeartbeat":
                            var hearbeat = bsonElement.Value.AsDateTime;
                            if (hearbeat != null)
                            {
                                node.lastHeartbeat = hearbeat.ToString("yyyy-MM-dd HH:mm tt");
                            }
                            break;

                        case "optimeDate":
                            node.optimeDate = bsonElement.Value.AsDateTime;
                            break;

                        case "pingMs":
                            Double pingTime = bsonElement.Value.AsInt32;
                            node.pingMS = pingTime.ToString(CultureInfo.InvariantCulture);
                            break;
                        }
                    }
                    status.servers.Add(node);
                }
            }
            catch
            {
                status = new ReplicaSetStatus("Replica Set Unavailable");
            }
            return(status);
        }