目的
虽然有功能强大的
Nagios
, 但我想让服务器尽可能的只运行必须的服务,Nagios
大材小用了
实现功能
- 免登陆快速查看多台服务器总体资源消耗
server端(shell)
#!/bin/bash
function check_ip(){
#使用`/sbin/ifconfig`防止ssh远程执行时找不到变量
echo IP $(/sbin/ifconfig eth1 | grep "inet " | awk '{print $2}')
}
function check_cpu(){
echo CPU 1m:$(uptime | awk '{print $10}')% 5m:$(uptime | awk '{print $11}')% 15m:$(uptime | awk '{print $12}')%
}
function check_mem(){
echo Mem total: $(free -m | grep Mem | awk '{print $2}')M, used: $(free -m | grep 'Mem:' | awk '{print $3-$6-$7}')M
}
function check_disk(){
echo Disk sda2 total:$(df -lh | grep sda2 | awk '{print $2}'), used: $(df -lh | grep sda2 | awk '{print $3}')\($(df -lh | grep sda2 | awk '{print $5}')\)
echo Disk opt total:$(df -lh | grep opt | awk '{print $2}'), used: $(df -lh | grep opt | awk '{print $3}')\($(df -lh | grep opt | awk '{print $5}')\)
}
echo "======================================"
check_ip
check_mem
check_cpu
check_disk
echo "======================================"
存为
/usr/local/bin/monitor_server
, 并 ``chmod+xmonitor_server
client端(python)
#!/usr/bin/python
#-*-coding:utf8-*-
import paramiko
class rsm(): #remote_server_monitor
def __init__(self, ip, title):
self.ip = ip
self.title = title
self.port = 22
self.username = "username"
self.password = "password"
self.command = "monitor"
def ssh(self):
client = paramiko.SSHClient()
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
client.connect(self.ip, self.port, self.username, self.password)
stdin, stdout, stderr = client.exec_command("monitor")
print self.title
print ''.join(stdout.readlines())
client.close()
redis = rsm('192.168.0.1', 'redis')
redis.ssh()
api = rsm('192.168.0.2', 'mongo')
api.ssh()
存为
/usr/local/bin/monitor_client
, 并 ``chmod+xmonitor_client
//todo
- 只能算是个demo,如果有时间想加个web界面及把信息存入数据库,好作为历史参考
- check_ip 百分比
- 邮件发送