Auto deploy new relic apps in cPanel

Some shared hosting, including my own company Hosting4Real use New Relic to monitor the servers. But often it would be nice, to be able to track each customers performance, for providing even better support, but deploying the ‘apps’ manually takes a lot of time, if you have tons of customers. So we can auto deploy new relic apps in cPanel.

But for all who don’t know what new relic is. It’s a server monitoring service, that makes it possible, to track server usage, like memory, CPU, Disk IO, Network and load. You can also drill down in the different processes on the servers, to see usage of those, even detailed network, and disk usage.

One of the best features, with new relic is the application monitoring, is allows you to see things like app server response time, throughput, performance map, database calls, web transactions etc. And even Real User Measurements, if you have standard account or above.

But the deployment of those apps, is done via htaccess most of the time, and the hosting company manually needs to do this. But there is a way, in cpanel to auto deploy these applications.

In the later versions of cPanel, they introduced something called ‘Standardized hooks‘, which is a replacement of the postwwwacct files when users is created.

First, make a folder and cd into it

mkdir /opt/makehtaccess && cd /opt/makehtaccess

After this, make a file called makehtaccess.py:

vim makehtaccess.py

And put following code (For explanation read the code):

#!/usr/bin/python
import sys
#importing sys

#Read the data from stdin
rawData = sys.stdin.readlines()

#The data from stdin, is json, so let's eval it, and replace null with None
#Using eval, because json module is not a standard in Python2.4 (CentOS 5.x)
hookdata = eval(rawData[0].replace(':null', ':None'))

#Set a var, with the data key
data = hookdata['data']

#Get username, and domain
username = data['user']
domain = data['domain']

#Open a file, in the users folder, and call it .htaccess
f = open('/home/%s/.htaccess' % username, 'w')

#Write the appname (domain) to the file and close the file. Default permissions is 644, and owned by root:root
f.write('<IfModule mod_php5.c>\n')
f.write('    php_value newrelic.appname "%s"\n' % domain)
f.write('</IfModule>\n')
f.close()

Now we need to make sure that the little python script can be executed by doing following:

chmod +x /opt/makehtaccess/makehtaccess.py

Now when we have the files needed, we need to register the hook into cpanel. This is done, by using following line:

/usr/local/cpanel/bin/manage_hooks add script /opt/makehtaccess/makehtaccess.py --stage post --category Whostmgr --event Accounts::Create

Too lazy to write this? Clone the github repo

I’ve also pushed it to a github repository.

But I’m running FastCGI, so I can’t use the php_value in htaccess?

By default it’s not possible to use php_value in FastCGI Indeed, but there is an alternative, called Htscanner Enhanced created by Paolo Iannelli. It’s easy to install, and works like charm. The reason why Htscanner Enhanced should be used, is because it forces to use default docroot, so htaccess files can be used in the users directory, so the settings won’t get overwritten, when the user creates an htaccess file in public_html.