python
http://pypi.douban.com/
http://mirrors.aliyun.com/pypi/simple/
system
http://mirrors.aliyun.com/
http://pypi.douban.com/
http://mirrors.aliyun.com/pypi/simple/
http://mirrors.aliyun.com/
虽然有功能强大的
Nagios
, 但我想让服务器尽可能的只运行必须的服务,Nagios
大材小用了
#!/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
#!/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
专一: 一个API的功能应该是单一的,需要能够很容易的解释和理解,也就会更好用
尽可能的小:小有很多的优势,易于理解和维护
尽量少的外部依赖:减少使用者的成本
设计不被实现影响:不要暴漏实现细节给用户.尽可能少的暴露,不止是内部细节,对于不必要的接口尽量不要发布,比如使用不多的功能, 可以暂时不暴露接口
良好的命名:尽量做到自描述
完善的文档
考虑性能
<?php
class foo{
static function callback($foo){
return $foo . "bar";
}
}
$string = function(){
return "I'm ";
};
echo call_user_func('foo::callback', $string());
?>
<?php
class foo{
static function callback($foo, $bar){
return $foo . $bar;
}
}
$string_a = function(){
return "I'm ";
};
$string_b = function(){
return "yjiang.";
};
echo call_user_func('foo::callback', $string_a(), $string_b());
echo "\n";
echo call_user_func_array('foo::callback', array($string_a(), $string_b()));
?>
######运行结果######
I'm yjiang.
I'm yjiang.
call_user_func
与 call_user_func_array
的区别在于传递参数的方式不同,其功能相同
Copyright © 2016 yjiang's cake