use of com.mongodb.ServerAddress in project graylog2-server by Graylog2.
the class MongoProbe method mongoStats.
public MongoStats mongoStats() {
final List<ServerAddress> serverAddresses = mongoClient.getServerAddressList();
final List<HostAndPort> servers = Lists.newArrayListWithCapacity(serverAddresses.size());
for (ServerAddress serverAddress : serverAddresses) {
servers.add(HostAndPort.fromParts(serverAddress.getHost(), serverAddress.getPort()));
}
final DatabaseStats dbStats;
final CommandResult dbStatsResult = db.command("dbStats");
if (dbStatsResult.ok()) {
final BasicDBObject extentFreeListMap = (BasicDBObject) dbStatsResult.get("extentFreeList");
final DatabaseStats.ExtentFreeList extentFreeList;
if (extentFreeListMap == null) {
extentFreeList = null;
} else {
extentFreeList = DatabaseStats.ExtentFreeList.create(extentFreeListMap.getInt("num"), extentFreeListMap.getInt("totalSize"));
}
final BasicDBObject dataFileVersionMap = (BasicDBObject) dbStatsResult.get("dataFileVersion");
final DatabaseStats.DataFileVersion dataFileVersion;
if (dataFileVersionMap == null) {
dataFileVersion = null;
} else {
dataFileVersion = DatabaseStats.DataFileVersion.create(dataFileVersionMap.getInt("major"), dataFileVersionMap.getInt("minor"));
}
dbStats = DatabaseStats.create(dbStatsResult.getString("db"), dbStatsResult.getLong("collections"), dbStatsResult.getLong("objects"), dbStatsResult.getDouble("avgObjSize"), dbStatsResult.getLong("dataSize"), dbStatsResult.getLong("storageSize"), dbStatsResult.getLong("numExtents"), dbStatsResult.getLong("indexes"), dbStatsResult.getLong("indexSize"), dbStatsResult.containsField("fileSize") ? dbStatsResult.getLong("fileSize") : null, dbStatsResult.containsField("nsSizeMB") ? dbStatsResult.getLong("nsSizeMB") : null, extentFreeList, dataFileVersion);
} else {
LOG.debug("Couldn't retrieve MongoDB dbStats: {}", dbStatsResult.getErrorMessage());
dbStats = null;
}
final ServerStatus serverStatus;
final CommandResult serverStatusResult = adminDb.command("serverStatus");
if (serverStatusResult.ok()) {
final BasicDBObject connectionsMap = (BasicDBObject) serverStatusResult.get("connections");
final ServerStatus.Connections connections = ServerStatus.Connections.create(connectionsMap.getInt("current"), connectionsMap.getInt("available"), connectionsMap.containsField("totalCreated") ? connectionsMap.getLong("totalCreated") : null);
final BasicDBObject networkMap = (BasicDBObject) serverStatusResult.get("network");
final ServerStatus.Network network = ServerStatus.Network.create(networkMap.getInt("bytesIn"), networkMap.getInt("bytesOut"), networkMap.getInt("numRequests"));
final BasicDBObject memoryMap = (BasicDBObject) serverStatusResult.get("mem");
final ServerStatus.Memory memory = ServerStatus.Memory.create(memoryMap.getInt("bits"), memoryMap.getInt("resident"), memoryMap.getInt("virtual"), memoryMap.getBoolean("supported"), memoryMap.getInt("mapped"), memoryMap.getInt("mappedWithJournal", -1));
final BasicDBObject storageEngineMap = (BasicDBObject) serverStatusResult.get("storageEngine");
final ServerStatus.StorageEngine storageEngine;
if (storageEngineMap == null) {
storageEngine = ServerStatus.StorageEngine.DEFAULT;
} else {
storageEngine = ServerStatus.StorageEngine.create(storageEngineMap.getString("name"));
}
final int uptime = serverStatusResult.getInt("uptime", 0);
serverStatus = ServerStatus.create(serverStatusResult.getString("host"), serverStatusResult.getString("version"), serverStatusResult.getString("process"), serverStatusResult.getLong("pid", 0), uptime, serverStatusResult.getLong("uptimeMillis", uptime * 1000L), serverStatusResult.getInt("uptimeEstimate"), new DateTime(serverStatusResult.getDate("localTime")), connections, network, memory, storageEngine);
} else {
LOG.debug("Couldn't retrieve MongoDB serverStatus: {}", serverStatusResult.getErrorMessage());
serverStatus = null;
}
// TODO Collection stats? http://docs.mongodb.org/manual/reference/command/collStats/
return MongoStats.create(servers, buildInfo, hostInfo, serverStatus, dbStats);
}
use of com.mongodb.ServerAddress in project mongo-java-driver by mongodb.
the class AbstractServerDiscoveryAndMonitoringTest method applyResponse.
protected void applyResponse(final BsonArray response) {
ServerAddress serverAddress = new ServerAddress(response.get(0).asString().getValue());
BsonDocument isMasterResult = response.get(1).asDocument();
ServerDescription serverDescription;
if (isMasterResult.isEmpty()) {
serverDescription = ServerDescription.builder().type(ServerType.UNKNOWN).state(CONNECTING).address(serverAddress).build();
} else {
serverDescription = createServerDescription(serverAddress, isMasterResult, getVersion(new BsonDocument("versionArray", new BsonArray(asList(new BsonInt32(2), new BsonInt32(6), new BsonInt32(0))))), 5000000);
}
factory.sendNotification(serverAddress, serverDescription);
}
use of com.mongodb.ServerAddress in project mongo-java-driver by mongodb.
the class ClusterDescriptionTest method testObjectOverrides.
@Test
public void testObjectOverrides() throws UnknownHostException {
ClusterDescription description = new ClusterDescription(MULTIPLE, UNKNOWN, asList(builder().state(CONNECTING).address(new ServerAddress("loc:27019")).lastUpdateTimeNanos(42L).build(), builder().state(CONNECTING).address(new ServerAddress("loc:27018")).lastUpdateTimeNanos(42L).build(), builder().state(CONNECTING).address(new ServerAddress("loc:27017")).lastUpdateTimeNanos(42L).build()));
ClusterDescription descriptionTwo = new ClusterDescription(MULTIPLE, UNKNOWN, asList(builder().state(CONNECTING).address(new ServerAddress("loc:27019")).lastUpdateTimeNanos(42L).build(), builder().state(CONNECTING).address(new ServerAddress("loc:27018")).lastUpdateTimeNanos(42L).build(), builder().state(CONNECTING).address(new ServerAddress("loc:27017")).lastUpdateTimeNanos(42L).build()));
assertEquals(description, descriptionTwo);
assertEquals(description.hashCode(), descriptionTwo.hashCode());
assertTrue(description.toString().startsWith("ClusterDescription"));
}
use of com.mongodb.ServerAddress in project mongo-java-driver by mongodb.
the class PlainAuthenticatorUnitTest method before.
@Before
public void before() {
connection = new TestInternalConnection(new ServerId(new ClusterId(), new ServerAddress("localhost", 27017)));
connectionDescription = new ConnectionDescription(new ServerId(new ClusterId(), new ServerAddress()));
credential = MongoCredential.createPlainCredential("user", "$external", "pencil".toCharArray());
subject = new PlainAuthenticator(this.credential);
}
use of com.mongodb.ServerAddress in project mongo-java-driver by mongodb.
the class ScramSha1SaslAuthenticatorTest method before.
@Before
public void before() {
this.connection = new TestInternalConnection(new ServerId(new ClusterId(), new ServerAddress("localhost", 27017)));
connectionDescription = new ConnectionDescription(new ServerId(new ClusterId(), new ServerAddress()));
this.credential = MongoCredential.createScramSha1Credential("user", "database", "pencil".toCharArray());
ScramSha1Authenticator.RandomStringGenerator randomStringGenerator = new ScramSha1Authenticator.RandomStringGenerator() {
@Override
public String generate(final int length) {
return "fyko+d2lbbFgONRv9qkxdawL";
}
};
this.subject = new ScramSha1Authenticator(this.credential, randomStringGenerator);
}
Aggregations