29th May2015

Report to New Relic APM on cPanel/CloudLinux with CageFS

by Gyro

I finally found out how to get PHP apps hosted on a cPanel server with CloudLinux and CageFS enabled to report to New Relic APM.

Since I spent days searching and trying, with giving up on several occasions, I figured this definitely deserves a post on my blog.

So, how did I manage to have apps report to new relic for ALL enabled cagefs users, and them being able to use their own new relic license/account?

Well, the problem was the default settings for the socket. The socket was simply not available for cagefs users being in the /tmp folder.

Login via ssh and check /tmp as root and as a user, and you will see that .newrelic.socket is missing for the users (~$ ll /tmp)

AFAIK this is due to cagefs creating "fake" /tmp directories for each user, for example you will see all the sessions of a user only in the /tmp of the user, but not in the physical /tmp of the server's fileystem.

To get around this, I did the following:

1. I created the folder /var/run/newrelic-global/ (not sure if it was necessary)

2. I added the following lines in /etc/newrelic/newrelic.cfg:

pidfile = "/var/run/newrelic-global/newrelic-daemon.pid"
port = "/var/run/newrelic-global/.newrelic.sock"

3. I added the following line in /etc/cagefs/cagefs.mp:

/var/run/newrelic-global

After that I ran these commands:
~$ /etc/init.d/newrelic-daemon restart
~$ cagefsctl --remount-all

Now, users can either define the php flags via .htaccess (if possible), or configure their own php.ini (if available), depending on how the server has been configured and/or what their preferences are. Below the example of what to do in the .htaccess file.

<IfModule mod_php5.c>
php_value newrelic.daemon.port "/var/run/newrelic-global/.newrelic.sock"
php_value newrelic.daemon.pidfile "/var/run/newrelic-global/newrelic-daemon.pid"
php_value newrelic.license "MyNewRelicLicense"
php_value newrelic.appname "MyAppName"
</IfModule>

To check, if it is working, have a look at the log file (Default location is: /var/log/newrelic/newrelic-daemon.log). You should see something like this:


…info: [‘MyAppName'] ‘Reporting to:…

Before all this, you should of course install the php agent:
https://docs.newrelic.com/docs/agents/php-agent/installation/php-agent-installation-redhat-centos
It will ask to update the php.ini during the installation of the agent. Select the one located in /usr/local/lib.
Once the installation is completed, run ~$ cagefsctl --force-update (those are 2 dashes infront of force-update).

Also, you may want to have a look at this page, if you are using the newrelic-sysmond daemon to monitor your server.
https://www.lucasrolff.com/cpanel/new-relic-and-cloudlinux/

Enjoy :)

1725

06th May2015

Simple “Are you sure?” warning for unsaved changes in WP

by Gyro

I spent quite a few hours getting a seemingly simple piece of javascript to work in a WP plugin I am currently working on.

Naturally, I started with jquery as it is already included in the WP Admin, but after several hours of trying various solutions I found on the web I had to admit defeat. I just could not get it to work the same way it obviously worked for others using jquery.

So, I ended up looking around a bit to find all natural javascript to do this.

function myplugin_js() {
echo ' <script>
window.onload = function(){
var theform = document.myform;
window.onbeforeunload = function(e){
var e = e || window.event, simon = "go";
for (i=0; i<theform.elements.length; i++){
if(theform.elements[i].type == "radio" || theform.elements[i].type == "checkbox"){
if(theform.elements[i].checked != theform.elements[i].defaultChecked){simon = "no";}
}else if(theform.elements[i].type == "select-one"){
if(!theform.elements[i].options[theform.elements[i].selectedIndex].defaultSelected){simon = "no";}
}else if(theform.elements[i].type == 'submit'){
theform.elements[i].onmouseup=function(){
simon = 'go';
}
}else{
if(theform.elements[i].value != theform.elements[i].defaultValue){simon = "no";}
}
}
if(simon != "go"){if(e){e.returnValue = "unsaved chages detected";}return "unsaved chages detected";}
}
};
</script>';
}
add_action('admin_enqueue_scripts', 'myplugin_js');

You can put this in a theme functions.php or in your plugin file.

This will cause a warning message to popup whenever the form with the name "myform" was modified and one attempts to load a new page.

Change ‘admin_enqueue_scripts' to ‘wp_enqueue_scripts' to enable this feature on the front-end of your website.

If you use a select element, make sure you have a default option "selected", else the script will fail.

Enjoy :crazy:

Sources:

542