歡迎您光臨本站 註冊首頁

編寫Func自定義模塊

←手機掃碼閱讀     火星人 @ 2014-03-04 , reply:0

編寫Func自定義模塊

編寫Func自定義模塊



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" 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 <liutiansi@gmail.com>
#
# 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 <liutiansi@gmail.com>
#
# 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, data)複製代碼
4、編寫模塊分發功能#cd ~
#vi RsyncModule.py

#!/usr/local/bin/python

import sys

import func.overlord.client as fc
import xmlrpclib

module = sys.argv
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': }
................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',
                   '']}
大功告成!
《解決方案》

{:3_189:}這個好啊,最近在研究用python寫一個伺服器管理系統
初步決定func+cgi方式web頁面管理
持續關注

[火星人 ] 編寫Func自定義模塊已經有726次圍觀

http://coctec.com/docs/service/show-post-4416.html