Back

利用gtalk实时监测VPS

|

前些天用[Conference Bot](http://coders.meta.net.nz/~perry/jabber/confbot.php)在服务器上搭了个gtalk聊天室. 这个bot是用python写的, 好几年前的东西了.
刚刚在看python的文档, 发现python可以很简单地调用系统命令, 于是试着改下了这个bot的源代码, 直接向这个bot发送一个命令, 就能在gtalk客户端输出vps的运行参数.

修改很简单,添加一个函数即可:

`def acmd_vps(who, msg):
'"/VPS" Print vps status'
if isadmin(who.getStripped()):
VPS=os.popen("/root/lnmp status").readlines()
MEM=os.popen("free -m").readlines()
UPTIMES=os.popen("uptime").readlines()
systoone(who, VPS[9])
systoone(who, VPS[10])
systoone(who, VPS[11])
systoone(who, MEM[2])
systoone(who, UPTIMES[0])
else:
raise ADMIN_COMMAND`

向bot发送命令**/vps**, bot会返回:
Bot: ### Nginx is runing!
Bot: ### php-fpm is runing!
Bot: ### MySQL running (3085)
Bot: ### -/+ buffers/cache: 159 339
Bot: ### 12:49:20 up 27 day, 2:37, 0 users, load average: 0.00, 0.00, 0.00

服务器环境:
CentOS 5.5 + [LNMP](http://lnmp.org/)一键安装包

No comments yet

Leave a comment

About me

I live in Tasmania, an island a little far from the rest of the world and very close to the sea.
Father of two little girls, have two puppies.

Work

Work in IT industry, AI is my working partner.