Im trying to get the uptime from a remote server, so far im able to get the version through the execution of a command. But I can't get it to run this command to get the uptime.
<?php
$mongo = new MongoClient('mongodb://<user>:<pass>@<DNS>:<PORT>');
echo "Connection to database successfully\n";
$admin = $mongo->admin;
$cursor= $admin->command(array('serverStatus().uptime'));
var_dump($cursor);
$response = $cursor->toArray();
var_dump($response);
?>
Im running a var_dump to see what it returns, it does return a int but is far too big to be the actual uptime.
To get the server uptime, execute serverStatus command and then you can fetch uptime in milliseconds like this
try {
$dsn = "mongodb://username:password@127.0.0.1:27017/";
$manager = new MongoDB\Driver\Manager($dsn);
$command = new MongoDB\Driver\Command(['serverStatus' => 1]);
$cursor = $manager->executeCommand('admin', $command);
foreach ($cursor->toArray() as $server) {
//print_r($server->uptime / 60. " Minutes");
//print_r($server->uptime / 3600. " Hours");
print_r($server->uptime / 86400. " Days");
}
} catch (\MongoDB\Driver\Exception\Exception $e) {
print_r($e);
}