Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

MongoDB and PHP: My Famous 5-Minute Tutorial

DZone's Guide to

MongoDB and PHP: My Famous 5-Minute Tutorial

· Database Zone
Free Resource

Check out the IT Market Clock report for recommendations on how to consolidate and replace legacy databases. Brought to you in partnership with MariaDB.

Did you ever wanted to integrate MongoDB monitoring with a third party service?
Did you wanted to integrate MongoDB performance counters with your load stress environment?
This 5 minutes tutorial will help you expose these details to any third party service or application using a simple JSON service, PHP and MongoDB:
  1. Install Apache and PHP and php-devel: sudo yum -y install httpd php php-devel php-pear
  2. Install PHP MongoDB Driver: sudo pecl install mongo
  3. Add extension=mongo.so to you /etc/php.ini
  4. Create a short monitoring code as mongo.php and place it at /var/www/html/:
 header('Cache-Control: no-cache, must-revalidate');
header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
header('Content-type: application/json;charset=UTF-8');

class Monitor {
public $VirtualMemory_MB;
public $PageFaults;
public $CurrentConnections;
public $NetworkIO_In_MB;
public $NetworkIO_Out_MB;
public $SpeedIO_Avg_ms;
public $LockRatio_Per;
public $ReadersLocked;
public $WritersLocked;
public $ObjectsInDatabase;
public $DatabaseDataSize_MB;
public $DatabaseIndexSize_MB;
public $IndexMissRatio_Per;
public $CursorsTimedOut;
public $DeleteSelectRatio_Per;
public $InsertSelectRatio_Per;
public $UpdateSelectRatio_Per;
}

if (!isset($_REQUEST['db'])) {
header($_SERVER['SERVER_PROTOCOL'] . ' 500 Internal Server Error', true, 500);
echo json_encode(array('status'=>0,'message'=>'db is missing'));
exit;
}
$databaseName = $_REQUEST['db'];


// connect
$m = new MongoClient();

// select a database
$db = $m->$databaseName;
$ret = $db->execute('db.stats();');

$result = new Monitor;
$result->ObjectsInDatabase = $ret["retval"]["objects"];
$result->DatabaseDataSize_MB = $ret["retval"]["dataSize"];
$result->DatabaseIndexSize_MB = $ret["retval"]["indexSize"];
//var_dump($ret);

$ret = $db->execute('db.serverStatus();');
$result->VirtualMemory_MB = $ret["retval"]["mem"]["virtual"];
$result->PageFaults = $ret["retval"]["extra_info"]["page_faults"];
$result->CurrentConnections = $ret["retval"]["connections"]["current"];
$result->NetworkIO_In_MB = $ret["retval"]["network"]["bytesIn"]/1000000;
$result->NetworkIO_Out_MB = $ret["retval"]["network"]["bytesOut"]/1000000;
$result->SpeedIO_Avg_ms = $ret["retval"]["backgroundFlushing"]["average_ms"];
$result->LockRatio_Per = $ret["retval"]["globalLock"]["lockTime"]/$ret["retval"]["globalLock"]["totalTime"]*100;
$result->ReadersLocked = $ret["retval"]["globalLock"]["currentQueue"]["readers"];
$result->WritersLocked = $ret["retval"]["globalLock"]["currentQueue"]["writers"];
$result->IndexMissRatio_Per = $ret["retval"]["indexCounters"]["btree"]["missRatio"];
$result->CursorsTimedOut = $ret["retval"]["cursors"]["timedOut"];
$result->DeleteSelectRatio_Per = $ret["retval"]["opcounters"]["delete"]/$ret["retval"]["opcounters"]["query"]*100;
$result->InsertSelectRatio_Per = $ret["retval"]["opcounters"]["insert"]/$ret["retval"]["opcounters"]["query"]*100;
$result->UpdateSelectRatio_Per = $ret["retval"]["opcounters"]["update"]/$ret["retval"]["opcounters"]["query"]*100;

echo json_encode($result);
?>
Bottom Line
You MongoDB is ready for monitoring by an external service: http://localhost/mongo.php





Interested in reducing database costs by moving from Oracle Enterprise to open source subscription?  Read the total cost of ownership (TCO) analysis. Brought to you in partnership with MariaDB.

Topics:

Published at DZone with permission of Moshe Kaplan, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

The best of DZone straight to your inbox.

SEE AN EXAMPLE
Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.
Subscribe

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}