I came up with a “better” solution than running a PHP script from an unrelated web server. This solution also aides my specific implementation case: my ‘MySQL’ Linode only allows connecting to 3306 via specific IP/v6 addresses, which effectively limits even authorized access to my other Linodes on the LAN.
This solution is a simple Perl script, doing a simple check for a running MySQL process, and using inetd to make it available on the network. Ideally, Linode would publish what IP range(s) it does monitoring checks from, and I would limit access to just that, but this script really doesn’t do much, so I’m not all that worried about it.
Instructions:
1. Prepare the monitoring scriptI put my script in /usr/local/bin, but you could conceivably put it anywhere. I named mine ‘mysql-heartbeat’.
Code:
#!/usr/bin/perl
select(STDOUT);
if (`pgrep mysql` eq '')
{
print "DOWN";
}
else
{
print "Linode Managed: heartbeat-mysql";
}
print "\n";
Once you’ve saved it, be sure to make it executable:
Code:
root> chmod +x mysql-heartbeat
2. Configure /etc/servicesIn order for inetd to know what port to run your service on, you’ll need to add its description to bottom of the /etc/services file:
Code:
# Local services
mysql-heartbeat 13306/tcp # MySQL aliveness daemon
I used port 13306, but you can use anything not already in use on your system.
3. Configure inetdI’m using Debian Squeeze (6.0), which doesn’t seem to include inetd by default — it’s easy to install:
Code:
root> apt-get install inetutils-inetd
inetd won’t start yet, because by default, it includes no services. We’ll fix that now.
At the bottom of /etc/inetd.conf:
Code:
#:OTHER: Other services
mysql-heartbeat stream tcp nowait root /usr/local/bin/mysql-heartbeat mysql-heartbeat
Save inetd.conf, and (re)start inetd:
Code:
root> dpkg-reconfigure inetutils-inetd
Stopping internet superserver: inetd.
Starting internet superserver: inetd.
That’s it! You can test to see if the daemon is working correctly using telnet, from any machine:
Code:
abrahamvegh@zulu:/$ telnet mysql.internal 13306
Trying 192.168.[redacted]...
Connected to mysql.internal.
Escape character is '^]'.
Linode Managed: heartbeat-mysql
Connection closed by foreign host.
abrahamvegh@zulu:/$
The only thing left to do is configure a
TCP monitoring check in the Managed control panel. I’ll leave that one as an exercise for the reader. ;)