Simple server monitoring with xinetd

You can use the simple but powerful xinetd on your Linux server to monitor almost anything on the server. Since xinetd just holds open a port and waits for a connection, you can tell it to run a script and return the output directly to the network stream.

To start, you'll need a script which will return data to stdout. In this example, I'll use a very simple script like the following:

#!/bin/bash
echo `uptime | egrep -o 'up ([0-9]+) days' | awk '{print $2}'`

This script pulls the number of days that the server has been online. Make the script executable with a chmod +x.

Now, you'll need to choose a port on which to run the xinetd service. I normally find a service in /etc/services that I won't be using on the server. In this example, I'll use isdnlog, which runs on port 20011. Create a file called /etc/xinetd.d/myscript and include the following in the file:

service isdnlog
{
	disable	= no
	socket_type	= stream
	protocol	= tcp
	wait		= no
	user		= root
	server		= /path/to/script.sh
	server_args	= test
}

Depending on your xinetd version, you may need to enable your new configuration and restart xinetd:

chkconfig myscript on
/etc/init.d/xinetd restart

You can test your new script using netcat:

$ uptime
18:10:30 up 141 days, 19:17,  1 user,  load average: 0.65, 1.47, 1.14
$ nc localhost 20011
141

If you need to pass arguments to your script, just adjust the server_args line in the xinetd configuration. Also, be sure that your script is set up to handle the arguments.

Printed from: http://rackerhacker.com/2008/12/02/simple-server-monitoring-with-xinetd/ .
© Major Hayden 2012.

2 Comments   »

  • Vid Luther says:

    Nice tip, what are your thoughts on Munin? I believe in the end it's doing something similar on port 4949, but gives you a lot more data + graphs.

  • Adam C. Greenfield says:

    Couldn't you run the script as someone other than root just to be a little safer? I realize there isn't an obvious attack vector here, but no real benefit to taking the risk in this case.

Trackbacks/Pingbacks

  1. Simple server monitoring with xinetd « Nigi Fabio Blog

RSS feed for comments on this post

Leave a Reply

 

  • Welcome! I started this blog as a way to give back to all of the other system administrators who have taught me something in the past. Writing these posts brings me a lot of enjoyment and I hope you find the information useful. If you spot something that's incorrect or confusing, please write a comment and let me know. Drop me a line if there's something you want to know more about and I'll do my best to write a post on the topic.
    -- Major Hayden

    Flattr this