线上的Nagios Client端的监控脚本因为业务需求又发生改动了,某业务集群差不多就有23台(下面的list只列出了8台),而且其中的一个业务需求脚本前前后后改动了4次,如果手动操作的话会累死人,幸亏有Fabric这个轻量级的自动化运维工具,代码如下:
#!/usr/bin/python2.6 from fabric.api import * from fabric.colors import * from fabric.context_managers import * user = 'ec2-user' hosts = ['bidder1','bidder2','bidder3','bidder4','bidder5','bidder6','bidder7','bidder8'] # hosts = ['bidder1',] @task def put_task(): print yellow("Put Local File to remote") with settings(warn_only=True): put("/home/ec2-user/check_cpu_utili.sh","/home/ec2-user/check_cpu_utili.sh") sudo("cp /home/ec2-user/check_cpu_utili.sh /usr/local/nagios/libexec") sudo("chown nagios:nagios /usr/local/nagios/libexec/check_cpu_utili.sh") sudo("chmod +x /usr/local/nagios/libexec/check_cpu_utili") sudo("kill -9 `ps aux | grep nrpe | head -n1 | awk '{print $2}' `") sudo("/usr/local/nagios/bin/nrpe -c /usr/local/nagios/etc/nrpe.cfg -d") print green("Put File success and restart nagios nrpe service!") for host in hosts: env.host_string = host put_task() #put_task()