Chinaunix首页 | 论坛 | 博客
  • 博客访问: 7656080
  • 博文数量: 1770
  • 博客积分: 18684
  • 博客等级: 上将
  • 技术积分: 16357
  • 用 户 组: 普通用户
  • 注册时间: 2010-06-02 10:28
个人简介

啥也没写

文章分类

全部博文(1770)

文章存档

2024年(15)

2023年(44)

2022年(39)

2021年(46)

2020年(43)

2019年(27)

2018年(44)

2017年(50)

2016年(47)

2015年(15)

2014年(21)

2013年(43)

2012年(143)

2011年(228)

2010年(263)

2009年(384)

2008年(246)

2007年(30)

2006年(38)

2005年(2)

2004年(1)

分类: LINUX

2010-05-01 23:21:50

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.

阅读(808) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~