{"id":21720,"date":"2015-08-05T16:21:50","date_gmt":"2015-08-05T10:51:50","guid":{"rendered":"http:\/\/www.tothenew.com\/blog\/?p=21720"},"modified":"2016-12-19T15:35:04","modified_gmt":"2016-12-19T10:05:04","slug":"php-and-nginx-monitoring-using-datadog","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/php-and-nginx-monitoring-using-datadog\/","title":{"rendered":"PHP and Nginx monitoring using DATADOG"},"content":{"rendered":"<p>Continuing with my previous blog on <a href=\"http:\/\/www.tothenew.com\/blog\/datadog\/\">Cloud Infrastructure Monitoring with Datadog<\/a>, \u00a0we would now be monitoring the application level with Datadog. My <strong>use-case<\/strong> was that I needed to monitor my PHP application, get deeper insights for it and also monitor the various Nginx metrics. So, I decided to try out Datadog Monitoring tool.<\/p>\n<p>In this blog we will learn how to monitor a PHP WordPress application ( you can use any other PHP application ), being served through Nginx ( you may serve it through Apache as well, edit the apache.yaml file lying in the same directory as nginx.yaml that we will be talking about later \u00a0), which is using RDS MySQL database ( or you might use simple MySQL db running on the same server ).<\/p>\n<h3><span style=\"color: #000000\"><strong><span style=\"text-decoration: underline\"> Prerequisites<\/span><\/strong>:-<\/span><\/h3>\n<p>1. Datadog agent installed on your Ubuntu\/Debian server.<br \/>\n2. A PHP application running on your server.<br \/>\n3. Nginx installed on your server, serving your PHP application<br \/>\nJust refer to my previous blog for <a href=\"http:\/\/www.tothenew.com\/blog\/datadog\/\">Datadog agent installation.<\/a><\/p>\n<h3><span style=\"text-decoration: underline;color: #000000\"><strong><span style=\"text-decoration: underline\">Steps to setup the metrics for PHP<br \/>\n<\/span><\/strong><\/span><\/h3>\n<p><span style=\"color: #000000\">Go to Integrations -&gt; Integrations &amp; search for PHP\u00a0&amp; enable it.<\/span><strong style=\"line-height: 1.71429;font-size: 1rem\">\u00a0<\/strong><span style=\"line-height: 1.71429;font-size: 1rem\">For using it, firstly you need to git clone the following directory.<\/span><\/p>\n<p>[js]<\/p>\n<p>git clone https:\/\/github.com:DataDog\/php-datadogstatsd.git<\/p>\n<p>[\/js]<\/p>\n<p><span style=\"color: #000000\">Go into the directory\u00a0<span style=\"color: #ff6600\">php-datadogstatsd<\/span>. It will look as follows :-<\/span><br \/>\n<img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-full wp-image-21714\" src=\"\/blog\/wp-ttn-blog\/uploads\/2015\/06\/Screenshot-from-2015-06-25-002505.png\" alt=\"Screenshot from 2015-06-25 00:25:05\" width=\"434\" height=\"41\" \/><\/p>\n<p>When you move into libraries directory, you will see a PHP\u00a0file. This is the library file for each metric for PHP.<br \/>\n<span style=\"color: #000000\">Open your<span style=\"color: #ff6600\"> index.php<\/span> file &amp; append the required lines :-<\/span><\/p>\n<p>[js]<br \/>\n# Require the datadogstatsd.php library file<br \/>\nrequire &#8216;.\/libraries\/datadogstatsd.php&#8217;;<br \/>\n# Increment a counter.<br \/>\nDataDogStatsD::increment(&#8216;web.page.views&#8217;);<br \/>\nDataDogStatsD::histogram(&#8216;web.render_time&#8217;,15);<br \/>\n[\/js]<\/p>\n<p>&#8220;<strong>web.page.views<\/strong>&#8221; &amp; &#8220;<strong>web.render_time<\/strong>&#8221; are few of the metrics which are available. For more examples of metric refer to the <strong><span style=\"color: #ff6600\">example.php<\/span><\/strong> file inside the\u00a0<span style=\"color: #ff6600\">php-datadogstatsd<\/span> directory. You can use them or create your own which would obviously require some coding.<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-full wp-image-21715\" src=\"\/blog\/wp-ttn-blog\/uploads\/2015\/06\/Screenshot-from-2015-06-25-002355.png\" alt=\"Screenshot from 2015-06-25 00:23:55\" width=\"717\" height=\"432\" \/><\/p>\n<p>Hit your website for 3-5 times for the API calls to become active. Within a couple of minutes the metric will start to show in the graph list where you can choose for which metric you need the graph to be visible on the right side just as below :-<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-full wp-image-21716\" src=\"\/blog\/wp-ttn-blog\/uploads\/2015\/06\/Screenshot-from-2015-06-25-002319.png\" alt=\"Screenshot from 2015-06-25 00:23:19\" width=\"1323\" height=\"486\" \/><\/p>\n<p>Now, we would come to<strong>\u00a0Nginx<\/strong> monitoring and for that we need to install the Nginx Integration from the Integrations menu in the Datadog dashboard.<\/p>\n<p>Next, we need to go the server where Datadog is installed &amp; check for the http_stub_status module. ( whether it is available to us or not ). For that you can run the command :-<\/p>\n<p>[js]<\/p>\n<p>nginx -V | grep http_stub_status<\/p>\n<p>[\/js]<\/p>\n<p>Now, since the module is available, go ahead and open the virtual host file of Nginx in this case and inside the server block enter the following lines of code :-<\/p>\n<pre>   location \/nginx_status {\r\n        stub_status on;\r\n        access_log off;\r\n        #can be switched on as well\r\n        #Only allow access from XXX.XXX.X.XXX IP \r\n        allow XXX.XXX.X.XXX;\r\n        #XXX.XXX.X.XXX is your server IP\r\n        # Send rest of the world to \/dev\/null\r\n        deny all;\r\n   }<\/pre>\n<p><span style=\"color: #000000\">Now, just restart or use reload Nginx. You can run :-<\/span><\/p>\n<p>[js]<\/p>\n<p>service nginx reload or service nginx restart<\/p>\n<p>[\/js]<\/p>\n<p>You need to edit one more file which you will find in \/etc\/dd-agent &amp; then look for<span style=\"color: #ff6600\"> nginx-example.yaml<\/span>. Rename the file to <span style=\"color: #ff6600\">nginx.yaml<\/span>. Remember this is a yaml file &amp; indentation is the core of writing anything in it. Open the file &amp; enter something like below ( hidden part is the IP address of my server):-<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-full wp-image-22343\" src=\"\/blog\/wp-ttn-blog\/uploads\/2015\/07\/datadogedit.png\" alt=\"datadogedit\" width=\"689\" height=\"340\" \/><\/p>\n<p>Don&#8217;t forget, you also need to restart the Datadog agent for it to read the newly created <span style=\"color: #ff6600\">nginx.yaml<\/span> file.<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-full wp-image-22344\" src=\"\/blog\/wp-ttn-blog\/uploads\/2015\/07\/Screenshot-from-2015-06-25-005737.png\" alt=\"Screenshot from 2015-06-25 00:57:37\" width=\"1332\" height=\"574\" \/><\/p>\n<p>In events, you can see that the Datadog agent has restarted. Next, would be to check the metrics. Go to Metrics Explorer.<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"alignnone  wp-image-22327\" src=\"\/blog\/wp-ttn-blog\/uploads\/2015\/07\/Screenshot-from-2015-06-25-005821.png\" alt=\"Screenshot from 2015-06-25 00:58:21\" width=\"628\" height=\"440\" \/><\/p>\n<p>You will now see new entries starting with Nginx. Just select the metric you want to monitor.<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"alignnone  wp-image-22328\" src=\"\/blog\/wp-ttn-blog\/uploads\/2015\/07\/Screenshot-from-2015-06-25-005856.png\" alt=\"Screenshot from 2015-06-25 00:58:56\" width=\"704\" height=\"160\" \/><\/p>\n<p>You are now done. You can also go ahead &amp; create alerts for whatever you want as we discussed in the previous blog which can be referred at\u00a0<a href=\"http:\/\/www.tothenew.com\/blog\/datadog\/\">http:\/\/goo.gl\/ljg5jk<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Continuing with my previous blog on Cloud Infrastructure Monitoring with Datadog, \u00a0we would now be monitoring the application level with Datadog. My use-case was that I needed to monitor my PHP application, get deeper insights for it and also monitor the various Nginx metrics. So, I decided to try out Datadog Monitoring tool. In this [&hellip;]<\/p>\n","protected":false},"author":174,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":16},"categories":[1174,1],"tags":[2178,2179],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/21720"}],"collection":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/users\/174"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=21720"}],"version-history":[{"count":0,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/21720\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=21720"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=21720"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=21720"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}