Chinaunix首页 | 论坛 | 博客
  • 博客访问: 315402
  • 博文数量: 50
  • 博客积分: 3052
  • 博客等级: 中校
  • 技术积分: 710
  • 用 户 组: 普通用户
  • 注册时间: 2007-12-03 12:13
文章分类
文章存档

2009年(10)

2008年(40)

我的朋友

分类: LINUX

2008-10-06 21:35:53

书接上回,上次介绍的只是单独使用cfagent程序来完成任务,今天说下cfservd和cfagent配合使用的方法,应用环境和上次的一样,
先在A上的/var/cfengine目录下建立master/inputs文件夹
目录结构如下

/var/cfengine/
            |-inputs            #用于放置相应的配置文件
            |-outputs #程序执行后的输出,通常里面没内容
            |-bin #cfengine可执行文件放置的位置

            |-master/

                   |-inputs #用户存放客户机需要的cfagent.conf文件

上面红色的是这次新建立的

在A上放入的master/inputs文件下放入cfagent.conf文件内容如下


##################################################
#
# cfagent.conf
#
# This is a simple file for getting started with
# cfengine. It is harmless. If you get cfengine
# running with this file, you can build on it.
#
##################################################

###
#
# BEGIN cfagent.conf (Only hard classes in this file )
#
###

control:

   actionsequence = ( shellcommands )        #定义要执行的动作,此处是执行一个shell命令

   domain = ( test.com )            #定义域名

   timezone = ( MET )                #定义时区

######################################################################
 shellcommands:                    #需要执行的shell命令

      "/bin/touch /tmp/2"            #在/tmp目录下建立2这个文件
######################################################################

###
#
# END cfagent.conf
#
###

然后在A的/var/cfengine/inputs文件夹下建立cfservd.conf,内容如下

#########################################################
#
# This is a cfd config file
#
# The access control here follows after any tcpd
# control in /etc/hosts.allow and /etc/hosts.deny
#
#########################################################

 #
 # Could import cf.groups here and use a structure like
 # in cfengine.conf, cf.main, cf.groups
 #

control:

  domain = ( b.c ) #设置域名
  cfrunCommand = ( "/var/cfengine/bin/cfagent" ) #设置cfrun命令执行的命令路径

any::

  IfElapsed = ( 1 ) #一个anti-spamming参数,防止client端频繁的请求解析input的文件
  ExpireAfeter = ( 15 ) #设置过期时间
  MaxConnections = ( 10 ) #设置最大的连接数
  MultipleConnections = ( true ) #是否允许多个client端连接
  AllowConnectionsFrom = ( 192.168.220 ) #允许从哪里连接,可以是网段,可以是域名
  AllMultipleConnectionsFrom = ( 192.168.220 )
  TrustKeysFrom = ( 192.168.220 ) #括弧中的主机会自动被信任
  LogAllConnections = ( true )
  
#########################################################

admit: # or grant: #授权哪些机器可以访问什么目录及文件

   /var/cfengine/bin/cfagent 192.168.220.*
   /var/cfengine/master/inputs 192.168.220.*

现在就可以在A上启动server端了,启动命令

/var/cfengine/bin/cfservd

接下来是B上的工作,cfagent就不需要了,等下会自动从A上把需要的cfagent.conf传过来的
首先在B上建立和A同样的cfservd.conf文件,同时启动服务端,用于两台机器之间的通信用,然后在/var/cfengine/inputs文件夹中建立update.conf文件,内容如下

#######################################################
#
# cf.update - for b.c
#
#######################################################

###
#
# BEGIN cf.update
#
###

#######################################################################
#
# This script distributes the configuration, a simple file so that,
# if there are syntax errors in the main config, we can still
# distribute a correct configuration to the machines afterwards, even
# though the main config won't parse. It is read and run just before the
# main configuration is parsed.
#
#######################################################################

control:

   actionsequence = ( copy processes tidy ) # Keep this simple and constant
   #设置要执行的动作
   domain = ( b.c ) # Needed for remote copy #设置域名

   #
   # Which host/dir is the master for configuration roll-outs?
   #

   policyhost = ( 192.168.220.128 )   #主策略服务器,这里可以是A的ip或A的域名
   master_cfinput = ( /var/cfengine/master/inputs )
   #定义主文件放置位置,也就是cfagent.conf等需要传到client端的配置文件及shell文件放置的位置
   AddInstallable = ( new_cfenvd new_cfservd )

   #
   # Some convenient variables
   #

   workdir = ( /var/cfengine ) #定义工作目录

   cf_install_dir = ( /usr/local/sbin )

   ###################################################################
   #
   # Spread the load, make sure the servers get done first though
   #
   ###################################################################

  !AllBinaryServers::

     SplayTime = ( 1 )

############################################################################

 #
 # Make sure there is a local copy of the configuration and
 # the most important binaries in case we have no connectivity
 # e.g. for mobile stations or during DOS attacks
 #

copy: #定义copy的动作,从服务端的master_cfinput目录下把文件拷贝到local的inputs文件夹下

     $(master_cfinput) dest=$(workdir)/inputs
                                  r=inf
                                  mode=700
                                  type=binary
                                  exclude=*.lst
                                  exclude=*~
                                  exclude=#*
                                  server=$(policyhost)
                                  trustkey=true

 !quetzalcoatal::

     $(cf_install_dir)/cfagent dest=$(workdir)/bin/cfagent
                                  mode=755
                                  backup=false
                                  type=checksum

     $(cf_install_dir)/cfservd dest=$(workdir)/bin/cfservd
                                  mode=755
                                  backup=false
                                  type=checksum
                                  define=new_cfservd

     $(cf_install_dir)/cfexecd dest=$(workdir)/bin/cfexecd
                                  mode=755
                                  backup=false
                                  type=checksum

     $(cf_install_dir)/cfenvd dest=$(workdir)/bin/cfenvd
                                  mode=755
                                  backup=false
                                  type=checksum
                                  define=new_cfenvd

#####################################################################

tidy:

     #
     # Cfexecd stores output in this directory.
     # Make sure we don't build up files and choke on our own words!
     #

     $(workdir)/outputs pattern=* age=7

#####################################################################

processes:

  new_cfservd::

    "cfservd" signal=term restart /var/cfengine/bin/cfservd

  new_cfenvd::

    "cfenvd" signal=kill restart "/var/cfengine/bin/cfenvd -H"


###
#
# END cf.update
#
##

到这里,update.conf文件也准备完毕了,接下来就是执行了
到现在为止,inputs下还没有cfagetn.conf文件,现在执行

/var/cfengine/bin/cfagent #前提cfagent文件已经copy到/var/cfengine/bin下
或者
/usr/local/sbin/cfagent

不过建议使用cfagent -v执行,这样就能看到执行时的信息了,当一切都完成时,如果正常,可以看到在/var/cfengine/inputs下多了cfagent.conf文件,并且在/tmp下建立了cfagent.conf中定义的2这个文件

排错:
关于排错方面,建议在A上即服务器端使用cfservd -d2来启动,这样执行的时候,服务器端就会把执行的信息显示在屏幕上,从而方便查错。

关于这个的应用,我是这样看的,可以在所有client上定义crontab,每天执行,当shellcommands需要变更时,在服务器端更新master/inputs下的cfagent.conf文件,这样client端再执行就会把新的脚本下载下来了。

阅读(1263) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~