Nginx Amplify and your Web server
Nginx Amplify is a cloud platform for monitoring your LEMP stack. But hey, its name starts with “Nginx” so it must be only for nginx servers?
No. Of course Nginx Amplify shines in all its beauty when you have a clear picture of your entire LEMP stack. That is, where “E” stands for “Engine-X”. But it’s perfectly usable, and useful even if your web server is Apache, that is – in a LAMP stack.
Of course you wouldn’t be getting Apache metrics. But the MySQL metrics as well as system uptime, load and things like swap usage are available in Amplify. PHP can be monitored too!
Monitor MySQL in CentOS 6
So you got yourself in a position where you have to monitor a MySQL server on an aging CentOS 6 system.
Sure, you can install the Amplify agent from RPM packages, but to much dissapointment find that no MySQL metrics are being reported. The issue is due to Python 2.6 being the default interpreter in CentOS 6.
How are we going to fix that is by installing supplementary Python 2.7 in our system and make Amplify use it.
1. Install Python 2.7 in CentOS 6
In this first step, we install Software Collections repository, Python 2.7 as well as git
and gcc
which will be used while setting up Amplify requirements and the agent itself:
sudo yum -y install centos-release-scl-rh
sudo yum -y install python27 git gcc
. /opt/rh/python27/enable
pip install --upgrade pip
Note how we used . /opt/rh/python27/enable
in order to activate Python 2.7 in the current shell session. Make sure to use the same SSH session in the next steps.
2. Install Amplify agent
Now we are going to clone latest Amplify agent source files, install the dependencies and the agent:
git clone https://github.com/nginxinc/nginx-amplify-agent.git /tmp/amplify
cd /tmp/amplify
pip install -r ./packages/nginx-amplify-agent/requirements
python setup.py install
The above step does a few good things for us, namely:
- installed service startup file
- created logrotate configuration
However, since it’s not as smart as an RPM package, we need to adjust some things ourselves.
3. Create log files
sudo mkdir /var/log/amplify-agent
sudo touch /var/log/amplify-agent/agent.log
sudo chown root /var/log/amplify-agent/agent.log
4. Adjust startup file
Our installed Amplify agent is located in /opt/rh/python27/root/usr/bin/nginx-amplify-agent.py
so we need to adjust the startup file with this proper (for our case) location. We also need to ensure Python 2.7 is used there and finally, enable service at startup. All steps together:
sed -i "s@binary=.*@binary=/opt/rh/python27/root/usr/bin/nginx-amplify-agent.py@" /etc/init.d/amplify-agent
sed -i 's@/etc/init.d/functions@/etc/init.d/functions; . /opt/rh/python27/enable@' /etc/init.d/amplify-agent
chkconfig --add amplify-agent
5. Configure the agent
Next, we create the configuration from the provided template file. We:
- run the agent as
root
user (you may want to create a regular user for the task instead) - auto-adjust the path to MySQL socket file
- create special MySQL user for Amplify agent and put its credentials
cp /etc/amplify-agent/agent.conf.default /etc/amplify-agent/agent.conf
sed -i "s@#user = nginx@user = root@" /etc/amplify-agent/agent.conf
MYSQL_SOCK=$(mysql --silent --skip-column-names -e "SELECT @@socket")
sed -i "s@unix_socket =.*@unix_socket = $(mysql --silent --skip-column-names -e 'SELECT @@socket')@" /etc/amplify-agent/agent.conf
MYSQL_PASS=$(< /dev/urandom tr -dc _A-Z-a-z-0-9 | head -c${1:-32};echo;)
mysql -e "CREATE USER 'amplify-agent'@'localhost' IDENTIFIED BY '${MYSQL_PASS}';"
sed -i "s@password =.*@password = ${MYSQL_PASS}@" /etc/amplify-agent/agent.conf
Finally, you have to manually fill in the Amplify API key and enable the MySQL extension. In /etc/amplify-agent/agent.conf
:
[credentials]
api_key =
[extensions]
phpfpm = True
mysql = True
[mysql]
#host =
#port =
#remote = False
...
Note that it is is important gotcha that #host =
and #port =
and #remote =
in MySQL configuration of Amplify agent stay commented, as given above.
It may seem logical to put host = localhost
but that breaks MySQL reporting to Amplify.
All the weird gotchas aside, now you can start your agent with service amplify-agent start
and go back to Amplify website.
Sit back, and observe the beautiful graphs filling in. Or sit straight and setup some MySQL monitoring alerts in Amplify 🙂