标题:编写Func自定义模块[原创] 出处:运维进行时 时间:Mon, 31 May 2010 11:54:17 +0000 作者:刘天斯 地址:https://blog.liuts.com/post/199/ 内容: Func是目前redhat系列平台最棒的集群管理工具(个人看法),发现越来越多的人已经开始在使用,从接触的大部分人都会说自带的模块已经够用了。其实在我们的日常维护当中,尤其是大规模的服务器集群、满天飞的业务系统等等。此时Func自带的模块已经远远不能满足我们的需求,现介绍Func是如何实现一个简单的自定义模块的。 [方法一] 通过CommandModule来实现,只需修改要运行命令参数就可以了。 优点:简单、部署方便; 缺点:不够灵活,扩展性弱; 适合场景:中小型集群; 命令方式: #查看所有服务器时间; func "*" call command run "/bin/date" #查看所有服务器系统日志,执行的超时时间为3秒; func -t 3 "*" call command run "/usr/bin/tail -10 /var/log/messages" #查看所有主机uptime,开启5个线程异步运行(主机数超过10台建议开启); func -t 3 "*" call --forks="5" --async command run "/usr/bin/uptime" #格式化输出结果,默认格式为python的元组,分别添加--jsion或--xml来输出json及xml的格式。 func -t 3 "*" call --forks="5" --json --async command run "/usr/bin/uptime" python api方式: import func.overlord.client as fc client = fc.Client("*") print fc.client.command.run("/usr/bin/tail -10 /var/log/messages") [方法二] 通过编写func模块来达到扩展的目的。 优点:可以根据自身的应用特点定制,扩展性非常强; 缺点:复杂、部署不够方便; 适合场景:大型集群; 操作: 1、模块存放位置说明 /usr/local/lib/python2.5/site-packages/func/minion/modules/(源码安装位置) or /usr/local/python2.5/site-packages/func/minion/modules/(rpm或yum安装默认位置) 2、func-create-module 建模块工具 填写相关项,比较简单就不一一说明了。 #cd /usr/local/lib/python2.5/site-packages/func/minion/modules #func-create-module 引用 Module Name: MyModule Description: My module for func. Author: liutiansi Email: liutiansi@gmail.com Leave blank to finish. Method: echo Method: Your module is ready to be hacked on. Wrote out to mymodule.py. 3、编写模块代码 #vi mymodule.py # # Copyright 2010 # liutiansi # # This software may be freely redistributed under the terms of the GNU # general public license. # # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. import func_module class Mymodule(func_module.FuncModule): # Update these if need be. version = "0.0.1" api_version = "0.0.1" description = "My module for func." def echo(self): """ TODO: Document me ... """ pass 系统已经帮我们自动生成了一些初始化代码,只需在此基础上做修改就可以了。如简单根据我们指定的条数返回最新系统日志,修改后的代码如下: # # Copyright 2010 # liutiansi # # This software may be freely redistributed under the terms of the GNU # general public license. # # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. import func_module from func.minion import sub_process class Mymodule(func_module.FuncModule): # Update these if need be. version = "0.0.1" api_version = "0.0.1" description = "My module for func." def echo(self,vcount): """ TODO: Document me ... """ command="tail -n "+str(vcount)+" /var/log/messages" cmdref = sub_process.Popen(command, stdout=sub_process.PIPE, stderr=sub_process.PIPE, shell=True, close_fds=True) data = cmdref.communicate() return (cmdref.returncode, data[0], data[1]) 4、编写模块分发功能 #cd ~ #vi RsyncModule.py #!/usr/local/bin/python import sys import func.overlord.client as fc import xmlrpclib module = sys.argv[1] pythonmodulepath="/usr/local/lib/python2.5/site-packages/func/minion/modules/" client = fc.Client("*") fb = file(pythonmodulepath+module, "r").read() data = xmlrpclib.Binary(fb) #同步模块 print client.copyfile.copyfile(pythonmodulepath+ module,data) #重启func服务 print client.command.run("/etc/init.d/funcd restart") #python RsyncModule.py mymodule.py 引用 {'NN-server1': 1} {'NN-server1': [0, 'Stopping func daemon: \nStarting func daemon: \n', '']} ................ 5、模块调用 func -t 10 "*" call mymodule echo 10 引用 {'NN-server1': [0, 'May 30 04:02:06 SN2010-04-020 syslogd 1.4.1: restart.\n', '']} 大功告成![emot]dog[/emot] 如大家有什么疑问或感兴趣的话题可以通过weibo与我交流:http://t.qq.com/yorkoliu Generated by Bo-blog 2.1.1 Release