<?xml version="1.0" encoding="UTF-8" ?>
<rss version="2.0">
<channel>
<title><![CDATA[运维进行时]]></title> 
<link>https://blog.liuts.com/index.php</link> 
<description><![CDATA[互联网运维与架构]]></description> 
<language>zh-cn</language> 
<copyright><![CDATA[运维进行时]]></copyright>
<item>
<link>https://blog.liuts.com/post/199/</link>
<title><![CDATA[编写Func自定义模块[原创]]]></title> 
<author>刘天斯 &lt;liutiansi@gmail.com&gt;</author>
<category><![CDATA[Func]]></category>
<pubDate>Mon, 31 May 2010 03:54:17 +0000</pubDate> 
<guid>https://blog.liuts.com/post/199/</guid> 
<description>
<![CDATA[ 
	&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<a href="https://fedorahosted.org/func/" target="_blank">Func</a>是目前redhat系列平台最棒的集群管理工具(个人看法)，发现越来越多的人已经开始在使用，从接触的大部分人都会说自带的模块已经够用了。其实在我们的日常维护当中，尤其是大规模的服务器集群、满天飞的业务系统等等。此时Func自带的模块已经远远不能满足我们的需求，现介绍Func是如何实现一个简单的自定义模块的。<br/><br/><strong>[方法一]</strong><br/>通过CommandModule来实现,只需修改要运行命令参数就可以了。<br/><br/>优点：简单、部署方便；<br/>缺点：不够灵活，扩展性弱；<br/>适合场景：中小型集群;<br/><br/><span style="color: #0000FF;">命令方式：</span><br/><textarea name="code" class="c" rows="15" cols="100">
#查看所有服务器时间;
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"
</textarea><br/><span style="color: #0000FF;">python api方式：</span><br/><textarea name="code" class="python" rows="15" cols="100">
import func.overlord.client as fc
client = fc.Client("*")
print fc.client.command.run("/usr/bin/tail -10 /var/log/messages")
</textarea><br/><strong>[方法二]</strong><br/>通过编写func模块来达到扩展的目的。<br/><br/>优点：可以根据自身的应用特点定制，扩展性非常强；<br/>缺点：复杂、部署不够方便；<br/>适合场景：大型集群;<br/><br/>操作：<br/>1、模块存放位置说明<br/>/usr/local/lib/python2.5/site-packages/func/minion/modules/(源码安装位置)<br/>or<br/>/usr/local/python2.5/site-packages/func/minion/modules/(rpm或yum安装默认位置)<br/><br/>2、func-create-module 建模块工具<br/>填写相关项，比较简单就不一一说明了。<br/>#cd /usr/local/lib/python2.5/site-packages/func/minion/modules<br/>#func-create-module<br/><div class="quote"><div class="quote-title">引用</div><div class="quote-content"><br/>Module Name: MyModule<br/>Description: My module for func.<br/>Author: liutiansi<br/>Email: liutiansi@gmail.com<br/><br/>Leave blank to finish.<br/>Method: echo <br/>Method: <br/>Your module is ready to be hacked on. Wrote out to mymodule.py.<br/></div></div><br/><br/>3、编写模块代码<br/>#vi mymodule.py<br/><textarea name="code" class="python" rows="15" cols="100">
#
# 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):

&nbsp;&nbsp;&nbsp;&nbsp;# Update these if need be.
&nbsp;&nbsp;&nbsp;&nbsp;version = "0.0.1"
&nbsp;&nbsp;&nbsp;&nbsp;api_version = "0.0.1"
&nbsp;&nbsp;&nbsp;&nbsp;description = "My module for func."

&nbsp;&nbsp;&nbsp;&nbsp;def echo(self):
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"""
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;TODO: Document me ...
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"""
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;pass
</textarea><br/>系统已经帮我们自动生成了一些初始化代码，只需在此基础上做修改就可以了。如简单根据我们指定的条数返回最新系统日志,修改后的代码如下：<br/><textarea name="code" class="python" rows="15" cols="100">
#
# 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):

&nbsp;&nbsp;&nbsp;&nbsp;# Update these if need be.
&nbsp;&nbsp;&nbsp;&nbsp;version = "0.0.1"
&nbsp;&nbsp;&nbsp;&nbsp;api_version = "0.0.1"
&nbsp;&nbsp;&nbsp;&nbsp;description = "My module for func."

&nbsp;&nbsp;&nbsp;&nbsp;def echo(self,vcount):
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"""
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;TODO: Document me ...
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"""
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;command="tail -n "+str(vcount)+" /var/log/messages"
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cmdref = sub_process.Popen(command, stdout=sub_process.PIPE,
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; stderr=sub_process.PIPE, shell=True,
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; close_fds=True)
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;data = cmdref.communicate()
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return (cmdref.returncode, data[0], data[1])
</textarea><br/>4、编写模块分发功能<br/>＃cd ~<br/>#vi RsyncModule.py<br/><textarea name="code" class="python" rows="15" cols="100">
#!/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&nbsp;&nbsp; = 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")
</textarea><br/>＃python RsyncModule.py mymodule.py<br/><div class="quote"><div class="quote-title">引用</div><div class="quote-content"><br/>&#123;'NN-server1': 1&#125;<br/>&#123;'NN-server1': [0, 'Stopping func daemon: &#92;nStarting func daemon: &#92;n', '']&#125;<br/>................<br/></div></div><br/>5、模块调用<br/>func -t 10 "*" call mymodule echo 10<br/><div class="quote"><div class="quote-title">引用</div><div class="quote-content"><br/>&#123;'NN-server1': [0,<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 'May 30 04:02:06 SN2010-04-020 syslogd 1.4.1: restart.&#92;n',<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; '']&#125;<br/></div></div><br/><br/>大功告成！<br/><br/>如大家有什么疑问或感兴趣的话题可以通过weibo与我交流：<a href="http://t.qq.com/yorkoliu" target="_blank">http://t.qq.com/yorkoliu</a><br/>Tags - <a href="https://blog.liuts.com/tags/func/" rel="tag">func</a> , <a href="https://blog.liuts.com/tags/%25E6%25A8%25A1%25E5%259D%2597/" rel="tag">模块</a>
]]>
</description>
</item><item>
<link>https://blog.liuts.com/post/199/#blogcomment478</link>
<title><![CDATA[[评论] 编写Func自定义模块[原创]]]></title> 
<author>路人甲 &lt;user@domain.com&gt;</author>
<category><![CDATA[评论]]></category>
<pubDate>Mon, 22 Nov 2010 10:01:52 +0000</pubDate> 
<guid>https://blog.liuts.com/post/199/#blogcomment478</guid> 
<description>
<![CDATA[ 
	我有一台certmaster带两台slave.其中一台我是用yum安装的python版本2.4<br/>另外一台是用源码安装也是python2.4<br/>但是当我执行到：func -t 10 &quot;*&quot; call mymodule echo 10<br/>那台yum安装的正常了源码安装的就不正常报:<br/>[&#039;REMOTE_ERROR&#039;,<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#039;func.minion.codes.InvalidMethodException&#039;,<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#039;&#039;,<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#039;&nbsp;&nbsp;File &quot;/usr/lib/python2.3/site-packages/func/minion/server.py&quot;, line 261, in _dispatc<br/>h\n&nbsp;&nbsp;&nbsp;&nbsp;return self.get_dispatch_method(method)(*params)\n&nbsp;&nbsp; File &quot;/usr/lib/python2.3/site-packages/func/minion/<br/>server.py&quot;, line 129, in get_dispatch_method\n&nbsp;&nbsp;&nbsp;&nbsp;raise codes.InvalidMethodException\n&#039;]}<br/>清楚是什么原因不？
]]>
</description>
</item><item>
<link>https://blog.liuts.com/post/199/#blogcomment479</link>
<title><![CDATA[[评论] 编写Func自定义模块[原创]]]></title> 
<author>路人甲 &lt;user@domain.com&gt;</author>
<category><![CDATA[评论]]></category>
<pubDate>Tue, 23 Nov 2010 01:55:27 +0000</pubDate> 
<guid>https://blog.liuts.com/post/199/#blogcomment479</guid> 
<description>
<![CDATA[ 
	/usr/bin/func &quot;client1..com&quot; call --forks=&quot;5&quot; command run &quot;date&quot;<br/>正常的。可以ping通
]]>
</description>
</item><item>
<link>https://blog.liuts.com/post/199/#blogcomment483</link>
<title><![CDATA[[评论] 编写Func自定义模块[原创]]]></title> 
<author>路过 &lt;user@domain.com&gt;</author>
<category><![CDATA[评论]]></category>
<pubDate>Tue, 23 Nov 2010 09:38:50 +0000</pubDate> 
<guid>https://blog.liuts.com/post/199/#blogcomment483</guid> 
<description>
<![CDATA[ 
	func里面能不能控制一条命令执行次数。比如我现在写了一个脚本我只希望它只执行一次。结果我func 跑了两次。能控制不？
]]>
</description>
</item><item>
<link>https://blog.liuts.com/post/199/#blogcomment602</link>
<title><![CDATA[[评论] 编写Func自定义模块[原创]]]></title> 
<author>stranger &lt;user@domain.com&gt;</author>
<category><![CDATA[评论]]></category>
<pubDate>Wed, 09 Mar 2011 06:13:29 +0000</pubDate> 
<guid>https://blog.liuts.com/post/199/#blogcomment602</guid> 
<description>
<![CDATA[ 
	你好，我现在的python版本是2.5的&nbsp;&nbsp; func版本是按照你平台提供的网址下载<br/>运行func &quot;*&quot; ping 已经成功了&nbsp;&nbsp;<br/>但是提示 <br/> [&#039;REMOTE_ERROR&#039;,<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &#039;func.minion.codes.InvalidMethodException&#039;,<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &#039;&#039;,<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &#039;&nbsp;&nbsp;File &quot;/usr/local/lib/python2.5/site-packages/func/minion/server.py&quot;, line 261, in _dispatchn&nbsp;&nbsp;&nbsp;&nbsp;return self.get_dispatch_method(method)(*params)n&nbsp;&nbsp; File &quot;/usr/local/lib/python2.5/site-packages/func/minion/server.py&quot;, line 129, in get_dispatch_methodn&nbsp;&nbsp;&nbsp;&nbsp;raise codes.InvalidMethodExceptionn&#039;]}<br/>想请问下是什么原因
]]>
</description>
</item><item>
<link>https://blog.liuts.com/post/199/#blogcomment603</link>
<title><![CDATA[[评论] 编写Func自定义模块[原创]]]></title> 
<author>stranger &lt;user@domain.com&gt;</author>
<category><![CDATA[评论]]></category>
<pubDate>Wed, 09 Mar 2011 08:26:39 +0000</pubDate> 
<guid>https://blog.liuts.com/post/199/#blogcomment603</guid> 
<description>
<![CDATA[ 
	不好意思，前面我说的不详细了！<br/>是我运行func &quot;*&quot; ping 已经提示 ok了<br/>但是在运行其他命令才会提示这个，例如：<br/>func &quot;funcslave1&quot; call cammand run &quot;date&quot;
]]>
</description>
</item><item>
<link>https://blog.liuts.com/post/199/#blogcomment955</link>
<title><![CDATA[[评论] 编写Func自定义模块[原创]]]></title> 
<author>菜鸟 &lt;user@domain.com&gt;</author>
<category><![CDATA[评论]]></category>
<pubDate>Tue, 18 Oct 2011 10:10:55 +0000</pubDate> 
<guid>https://blog.liuts.com/post/199/#blogcomment955</guid> 
<description>
<![CDATA[ 
	您好：&nbsp;&nbsp; 我现在的情况是这样的，装完func（func 的master&nbsp;&nbsp;clinte都已经启动成功），执行func ‘*’ ping 执行成功，但是总是提示 。。。“svc”。我检查自己的python的版本后发现是2.4的，升级python 到2.7 后重启报错;Traceback (most recent call last):&nbsp;&nbsp;File &quot;/usr/bin/func&quot;, line 19, in &lt;module&gt;&nbsp;&nbsp;&nbsp;&nbsp;from func.CommonErrors import Func_Client_ExceptionImportError: No module named func.CommonErrors
]]>
</description>
</item><item>
<link>https://blog.liuts.com/post/199/#blogcomment2546</link>
<title><![CDATA[[评论] 编写Func自定义模块[原创]]]></title> 
<author>five &lt;user@domain.com&gt;</author>
<category><![CDATA[评论]]></category>
<pubDate>Mon, 29 Dec 2014 18:41:56 +0000</pubDate> 
<guid>https://blog.liuts.com/post/199/#blogcomment2546</guid> 
<description>
<![CDATA[ 
	api模式的怎么运行啊
]]>
</description>
</item><item>
<link>https://blog.liuts.com/post/199/#blogcomment2553</link>
<title><![CDATA[[评论] 编写Func自定义模块[原创]]]></title> 
<author>five &lt;user@domain.com&gt;</author>
<category><![CDATA[评论]]></category>
<pubDate>Tue, 06 Jan 2015 19:35:24 +0000</pubDate> 
<guid>https://blog.liuts.com/post/199/#blogcomment2553</guid> 
<description>
<![CDATA[ 
	天斯，这个关于json的问题帮忙看下吧，搞了好久 没找到原因Traceback (most recent call last):&nbsp;&nbsp;File &quot;/usr/bin/ansible&quot;, line 196, in &lt;module&gt;&nbsp;&nbsp;&nbsp;&nbsp;(runner, results) = cli.run(options, args)&nbsp;&nbsp;File &quot;/usr/bin/ansible&quot;, line 114, in run&nbsp;&nbsp;&nbsp;&nbsp;inventory_manager = inventory.Inventory(options.inventory, vault_password=vault_pass)&nbsp;&nbsp;File &quot;/usr/lib/python2.6/site-packages/ansible/inventory/__init__.py&quot;, line 118, in __init__&nbsp;&nbsp;&nbsp;&nbsp;self.parser = InventoryScript(filename=host_list)&nbsp;&nbsp;File &quot;/usr/lib/python2.6/site-packages/ansible/inventory/script.py&quot;, line 49, in __init__&nbsp;&nbsp;&nbsp;&nbsp;self.groups = self._parse(stderr)&nbsp;&nbsp;File &quot;/usr/lib/python2.6/site-packages/ansible/inventory/script.py&quot;, line 57, in _parse&nbsp;&nbsp;&nbsp;&nbsp;self.raw&nbsp;&nbsp;= utils.parse_json(self.data)&nbsp;&nbsp;File &quot;/usr/lib/python2.6/site-packages/ansible/utils/__init__.py&quot;, line 552, in parse_json&nbsp;&nbsp;&nbsp;&nbsp;results = json.loads(data)&nbsp;&nbsp;File &quot;/usr/lib64/python2.6/json/__init__.py&quot;, line 307, in loads&nbsp;&nbsp;&nbsp;&nbsp;return _default_decoder.decode(s)&nbsp;&nbsp;File &quot;/usr/lib64/python2.6/json/decoder.py&quot;, line 319, in decode&nbsp;&nbsp;&nbsp;&nbsp;obj, end = self.raw_decode(s, idx=_w(s, 0).end())&nbsp;&nbsp;File &quot;/usr/lib64/python2.6/json/decoder.py&quot;, line 338, in raw_decode&nbsp;&nbsp;&nbsp;&nbsp;raise ValueError(&quot;No JSON object could be decoded&quot;)ValueError: No JSON object could be decoded
]]>
</description>
</item><item>
<link>https://blog.liuts.com/post/199/#blogcomment2573</link>
<title><![CDATA[[评论] 编写Func自定义模块[原创]]]></title> 
<author>hihi &lt;user@domain.com&gt;</author>
<category><![CDATA[评论]]></category>
<pubDate>Tue, 10 Mar 2015 09:03:47 +0000</pubDate> 
<guid>https://blog.liuts.com/post/199/#blogcomment2573</guid> 
<description>
<![CDATA[ 
	运行func &quot;*&quot; ping 是OK。但是运行其他就报错：func &quot;func.slave.server.com&quot; call command run&quot;date&quot;{&#039;func.slave.server.com&#039;: [&#039;REMOTE_ERROR&#039;,&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &#039;func.minion.codes.InvalidMethodException&#039;,&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &#039;&#039;,&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &#039;&nbsp;&nbsp;File &quot;/usr/lib/python2.6/site-packages/func/minion/server.py&quot;, line 261, in _dispatch\n&nbsp;&nbsp;&nbsp;&nbsp;return self.get_dispatch_method(method)(*params)\n&nbsp;&nbsp; File &quot;/usr/lib/python2.6/site-packages/func/minion/server.py&quot;, line 129, in get_dispatch_method\n&nbsp;&nbsp;&nbsp;&nbsp;raise codes.InvalidMethodException\n&#039;]}好纠结啊
]]>
</description>
</item>
</channel>
</rss>