線上的Nagios Client端的監控腳本由於業務需求又發生改動了,某業務集羣差很少就有23臺(下面的list只列出了8臺),並且其中的一個業務需求腳本前先後後改動了4次,若是手動操做的話會累死人,幸好有Fabric這個輕量級的自動化運維工具,代碼以下:python
#!/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()