Cfengine
|
cfengine
1. Cfengine 개략 ¶Cfengine은 유닉스 컴퓨터 시스템 설정 및 유지보수를 환상적으로 해주는 유용한 툴이다. Cfengine은 독립형(stand-alone) 도구모음으로 설정 파일에 있는 명령에 따라 컴퓨터를 설정하고 관리한다. 설정 파일은 습득해서 사용하기 쉬운 고급 언어로 다양한 시스템 컴포넌트에 적합한 속성을 정의하고 있다(프로그래밍은 하지 않아도 됨). 이런 방식으로 Cfengine은 각각의 시스템을 정의된 설정 스펙에 맞게 자동으로 여러 시스템을 설정해 줄 수 있다. 또한, 계속해서 시스템을 감시하면서 필요에 따라 설정을 조절해주도록 할 수도 있다.
2. Cfengine으로 할 수 있는 작업 ¶
3. 프로그램 구성 ¶cfagent 로컬 시스템에 설정 파일을 적용하는 주요 유틸리티
cfrun 리모트 시스템에 설정 파일을 적용하는 유틸리티
cfservd cfrun을 지원하는 서버 프로세스. 리모트 시스템으로부터 Cfengine 에이전트 기능을 사용할 수 있게 해줌.
cfexecd 작업 스케쥴링 및 보고 등을 자동화 해주는 데몬
cfenvd 문제 감지 데몬
cfkey 보안 키 생성 유틸리티
각 호스트별로 cfagent 를 이용하여 작업할 내용을 미리 정의해놓는다. 이를 이용하여 서버 1대별로 시스템관리 작업을 자동화할 수 있다. 그러나 우리가 원하는 것은 이것이 아닐 것이다. 중앙의 관리서버에 필요한 파일 등을 올려놓고 각 서버에서 중앙의 관리서버에서 파일을 가져오게 할 수도 있고 중앙의 관리서버에서 원격으로 각 시스템의 cfagent 를 실행할 수도 있다. 중앙의 관리서버에서 cfrun 을 이용하여 각 호스트에 접속할 수가 있는데 이때 각 호스트에는 cfservd 가 돌아가고 있으면 된다.
cfexecd 는 각 호스트에서 cron 처럼 사용하는 것이다.
cfkey 는 보안 키 생성 유틸리티로 각 관리할 호스트에서 실행하면 된다. 이 키를 이용하여(개인키/공개키 방식) 중앙관리서버와 각 호스트간에 통신을 한다.
서버에서는 cfservd가 떠 있어야 다른 대상 컴퓨터에서 마스터서버의 파일을 가져올 수 있다.
다른 대상 컴퓨터에서는 수동으로 또는 자동으로(cfexecd 또는 cron 이용) cfagent를 실행하면 된다.
4. 사용전 주의사항 ¶각 호스트는 hostname이 있어야하고 DNS lookup을 하면 ip를 반환하며 ip로 질의를 하면 동일한 hostname이 나와야한다.
보통 hostname-> ip는 네임서버에 세팅을 하지만 ip-> hostname을 dns에 세팅하지 않는 경우가 많은데 이럴 경우에는 /etc/hosts에 모든 호스트명을 넣어두어야한다.
5.1. 소스 설치 ¶먼저 md5sum을 이용해 소스프로그램의 무결성 확인한다.
소스를 푼다.
# ./configure --prefix=/usr/local/cfengine (기본은 /usr/local 에 설치) # make # make check (셀프 테스트) # make install여기서 설치시 두가지 개발관련 프로그램이 필요하다. Berkeley Database obtainable from http://www.sleepycat.com OpenSSL obtainable from http://www.openssl.org RHEL, CentOS에서는 db4-devel, openssl-devel 이 필요하다.
# yum -y install db4-devel openssl-devel 설치패키지는 아래와 같다. /usr/local/cfengine 에 설치한다.
> ./sbin/cfagent > ./sbin/cfservd > ./sbin/cfrun > ./sbin/cfkey > ./sbin/cfenvd > ./sbin/cfenvgraph > ./sbin/cfexecd > ./sbin/cfshow > ./sbin/cfetool > ./sbin/cfetoolgraph > ./sbin/cfdoc 21a33,57 > ./share/cfengine > ./share/cfengine/cfengine.el > ./share/cfengine/cf.chflags.example > ./share/cfengine/cf.freebsd.example > ./share/cfengine/cf.ftp.example > ./share/cfengine/cf.groups.example > ./share/cfengine/cf.linux.example > ./share/cfengine/cf.main.example > ./share/cfengine/cf.motd.example > ./share/cfengine/cf.preconf.example > ./share/cfengine/cf.services.example > ./share/cfengine/cf.site.example > ./share/cfengine/cf.solaris.example > ./share/cfengine/cf.sun4.example > ./share/cfengine/cf.users.example > ./share/cfengine/cfservd.conf.example > ./share/cfengine/cfagent.conf.example > ./share/cfengine/cfagent.conf-advanced.example > ./share/cfengine/update.conf.example > ./share/cfengine/cfrc.example > ./share/cfengine/cfrun.hosts.example > ./share/cfengine/README > ./share/cfengine/ChangeLog > ./share/cfengine/INSTALL > ./share/cfengine/NEWS 5.2. RPM 이용하기 ¶
# cd /usr/src/redhat/SPEC # rpmbuild -ba --target i686 cfengine.spec
# rpm -ivh http://cfengine.tunelinux.pe.kr/tune/4.4/i386/RPMS/cfengine-2.1.21-2.i686.rpm 6.1. 초기구성 및 테스트 ¶
6.2. 마스터서버구성 ¶
6.2.1. 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 )
#
###
classes:
# cfengine master server
master_server = ( cfengine.tunelinux.pe.kr )
# server group
testingservers = ( cent.tunelinux.pe.kr cent2.tunelinux.pe.kr )
#testingservers = ( cent2.tunelinux.pe.kr )
webhosting = ( cent.tunelinux.pe.kr )
mailhosting = ( '/usr/bin/test -d /var/qmail' )
dnshosting = ( '/usr/bin/test -f /etc/named.conf' )
dnsservers = ( '/usr/bin/test -f /etc/named.conf' )
intraservers = ( cfengine.tunelinux.pe.kr intranet.tunelinux.pe.kr project.tunelinux.pe.kr )
#intra_ip_range = ( IPRange(111.112.137.1-100) )
intra_ip_range = ( IPRange(111.112.137.0/24) )
# tune servers
tuneservers = ( testingservers webhosting mailhosting dnshosting intraservers intra_ip_range )
# specific server
centosservers = ( '/usr/bin/test -d /usr/share/doc/centos-release-4' )
cfengineservers = ( '/usr/bin/test -f /usr/sbin/cfagent' )
yumservers = ( '/usr/bin/test -f /etc/yum.repos.d/CentOS-Base.repo' )
techlabservers = ( 111.112.137.141 techlab.tunelinux.pe.kr )
##################################################
control:
domain = ( tunelinux.pe.kr )
timezone = ( MET )
smtpserver = ( localhost ) # used by cfexecd
sysadm = ( joon@tunelinux.pe.kr ) # where to mail output
# IfElapsed = ( 0 )
schedule = ( Hr00 )
ChecksumUpdates = ( on )
# cfengine tune repogitory
master_files = ( /usr/local/var/cfengine/tune )
master_server = ( cfengine.tunelinux.pe.kr )
# html repogitory
html_files = ( /var/www/html/tune )
# security check
SpoolDirectories = ( /var/spool/mail /var/spool/cron )
WarnNonOwnerMail = ( true )
WarnNonUserMail = ( true )
#!techlabservers::
# NonAlphaNumFiles = ( on )
actionsequence = ( disable copy editfiles files shellcommands directories tidy processes )
##################################################
resolve:
# Add these name servers to the /etc/resolv.conf file
210.220.163.82 # local nameserver
210.94.6.67 # backup nameserver
##################################################
# 111.112.137 tune intra
# 222.239.157 IDC monitor
# 66.600.5 IDC intra
editfiles:
{
/etc/crontab
AppendIfNoSuchLine "* 0 * * * root /usr/bin/rdate -s time.bora.net && /sbin/hwclock -w"
}
tuneservers::
{
/etc/security/access.conf
AppendIfNoSuchLine "-:root:All EXCEPT LOCAL localhost.localdomain 111.112.137. 222.239.157. 66.600.5."
}
{
/etc/pam.d/sshd
AppendIfNoSuchLine "account required pam_access.so"
}
{
/etc/vsftpd/vsftpd.conf
ReplaceAll "anonymous_enable=YES" With "anonymous_enable=NO"
DefineClasses "modified_ftp"
}
intraservers|intra_ip_range::
{
/etc/aliases
AppendIfNoSuchLine "root: joon@tunelinux.pe.kr"
DefineClasses "modified_aliases"
}
centosservers::
{
/etc/updatedb.conf
ReplaceAll "DAILY_UPDATE=no" With "DAILY_UPDATE=yes"
}
tuneservers.cfengineservers::
{
/etc/crontab
AppendIfNoSuchLine "* 0 * * * root /usr/sbin/cfexecd -F"
}
intra_ip_range|testingservers::
{
/etc/bashrc
AppendIfNoSuchLine "alias ll='ls -alF'"
}
##################################################
copy:
# Get a file from some trusted server, e.g. password sync
# To do this, you need to use cfkey to install keys
# tune yum repository
tuneservers::
$(master_files)/tune.repo dest=/etc/yum.repos.d/tune.repo mode=644 server=$(master_server)
# master file copy
master_server::
/etc/hosts dest=$(master_files)/hosts backup=true
/usr/local/var/cfengine/inputs/update.conf dest=$(html_files)/update.conf mode=644
$(master_files)/tune.repo dest=$(html_files)/tune.repo mode=644 server=$(master_server)
# iptables
intra_ip_range|intraservers::
$(master_files)/intra-iptables dest=/etc/sysconfig/iptables mode=600 server=$(master_server) backup=true define=modified_iptables
testingservers.!master_server::
$(master_files)/hosts dest=/etc/hosts mode=644 server=$(master_server) backup=true
##################################################
files:
tuneservers::
# file check
/tmp mode=ugo-x recurse=inf action=fixall syslog=true inform=true
/var/tmp mode=ugo-x recurse=inf action=fixall syslog=true inform=true
/proc mode=700 owner=root action=fixall
# password
/etc/passwd mode=644 owner=root action=fixall checksum=md5 syslog=true inform=true
/etc/shadow mode=600 owner=root action=fixall checksum=md5 syslog=true inform=true
/etc/group mode=644 owner=root action=fixall checksum=md5 syslog=true inform=true
#cfengine program file
cfengineservers::
/usr/sbin mode=700 owner=root action=fixall include=cf* recurse=inf
##################################################
shellcommands:
# security check
# "/usr/bin/find /tmp/ '(' -nouser -o -nogroup ')' "
tuneservers.yumservers::
"/bin/rm -f /etc/yum.repos.d/CentOS-*"
tuneservers.yumservers.Sunday.Hr00::
"/usr/bin/yum clean all"
modified_ftp::
"/etc/init.d/vsftpd restart"
modified_iptables::
"/etc/init.d/iptables restart"
modified_aliases::
"/usr/bin/newaliases && /etc/init.d/sendmail restart && /sbin/chkconfig --level 345 sendmail on"
any.Hr07::
"/usr/bin/rdate -s time.bora.net && /sbin/hwclock -w" timeout=30
##################################################
directories:
# /tmp mode=1777 owner=root group=root syslog=true inform=true
tidy:
#tuneservers.intra_ip_range::
tuneservers::
/tmp recurse=inf pattern=* age=7 rmdirs=sub syslog=true inform=true
/var/tmp recurse=inf pattern=* age=7 rmdirs=sub syslog=true inform=true
/home recurse=inf
pat=core
pat=a.out
pat=*.o
age=1
rmdirs=sub
syslog=true
inform=true
# pat=*%
# pat=#*
disable:
tuneservers::
/root/.rhosts syslog=true inform=true
/etc/hosts.equiv syslog=true inform=true
##################################################
processes:
# "xinetd" signal=hup
# "httpd" signal=kill
# "cfservd" signal=hup
# "cexecd" signal=hup
tuneservers.cfengineservers::
"cfexecd" restart "/usr/sbin/cfexecd"
"cfservd" restart "/usr/sbin/cfservd"
###
#
# END cfagent.conf
#
###
control 에는 전체 설정과 관련된 내용이 들어간다.
smtpserver, sysadm 는 cfexecd 등으로 실행할 경우의 실행내용을 메일로 보내주도록 하는 설정이다. smtp 서버와 받을 사용자를 지정하면 된다.
IfElapsed 는 cfagent 의 실행과 관계가 있으며 아래에서 디버깅 부분을 참조한다.
schedule : cfexecd를 띄웠을 경우 (cfagent를 주기적으로 실행하는 역할을 하는 프로그램임) schedule 에 설정된 내용에 따라 주기적으로 cfexecd 를 실행한다. cfexecd는 별도의 설정파일이 없으며 cfagent.conf의 schedule 설정을 보고 실행을 한다. cfexecd를 띄워서 사용할 수도 있고 cron을 이용하여 실행하도록 할수도 있다.
class를 이용하여 정책을 그룹별로 적용할 수 있다. 여기서 ( ) 안의 호스트는 /etc/hosts 파일을 참조한다. 특정 명령을 실행한 결과를 가지고 그룹(클래스)를 나눌 수도 있다.
/etc/hosts 파일로 분류하기 힘든 경우 사용하면 좋을 것이다. 클래스안에 다른 클래스를 넣을 수도 있다.
ChecksumUpdates 는 files 에 지정한 파일의 체크섬을 체크하여 다를 경우 경고를 보여준다.
NonAlphaNumFiles 는 ".. ." 등 일반 문자에서 벗어난 디렉토리를 체크한다. (?)
여기서 master_server 는 임의의 변수를 지정한 것으로 이러한 형태로 각자 변수를 만들어 사용할 수 있다.
files 등에서 syslog 는 syslog에 해당 변화내용을 기록하는 것이고 inform 은 스크린이라 이메일로 정보를 알려준다. true와 on의 차이점은 매뉴얼을 봐도 잘 모르겠다.
6.2.2. 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 = ( tunelinux.pe.kr )
AllowUsers = ( root )
linux::
cfrunCommand = ( "/var/cfengine/bin/cfagent" )
any::
# ChecksumDatabase = ( /tmp/testDATABASEcache )
IfElapsed = ( 1 )
MaxConnections = ( 30 )
# access control
Split = ( " " )
hostlist = ( "111.112.137 222.239.157 66.600.5" )
# hostlist = ( "111.112.137.162" )
dirs = ( "inputs tune" )
base = ( /usr/local/var/cfengine )
#########################################################
admit: # or grant:
$(base)/$(dirs) $(hostlist)
# /usr/local/var/cfengine/inputs *
# /usr/local/var/cfengine/tune *
cfservd.conf는 cfservd에 필요하며 접근할 수 있는 권한을 설정한다.
AllowUsers 부분이 업으면 cfrun 이 실행이 되지 않았다.
admit 은 접근권한설정을 하는 부분이다.
6.2.3. update.conf ¶
#######################################################
#
# cf.update - for iu.hio.no
#
#######################################################
###
#
# 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 = ( tunelinux.pe.kr ) # Needed for remote copy
#
# Which host/dir is the master for configuration roll-outs?
#
policyhost = ( cfengine.tunelinux.pe.kr )
master_cfinput = ( /usr/local/var/cfengine/inputs )
AddInstallable = ( new_cfenvd new_cfservd )
#
# Some convenient variables
#
workdir = ( /var/cfengine )
linux::
cf_install_dir = ( /usr/local/cfengine/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:
$(master_cfinput) dest=$(workdir)/inputs
r=inf
mode=700
type=binary
exclude=*.lst
exclude=*~
exclude=#*
server=$(policyhost)
trustkey=true
#####################################################################
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 /usr/sbin/cfservd
new_cfenvd::
"cfenvd" signal=kill restart "/usr/sbin/cfenvd -H"
###
#
# END cf.update
#
###
update.conf는 cfagent 에서 마스터서버에 접근하기 위해서 필요한 설정이다. 여기서 지정한 서버와 디렉토리에서 필요한 파일을 가져온다.
6.3. 클라이언트 구성 ¶
[root@localhost cfengine]# mkdir -p /var/cfgneine/inputs [root@localhost cfengine]# mkdir -p /var/cfengine/bin [root@localhost cfengine]# cd /var/cfengine/bin [root@localhost cfengine]# scp cent.tunelinux.pe.kr:/usr/local/cfengine/sbin/* . [root@localhost cfengine]# scp cent.tunelinux.pe.kr:/usr/local/var/cfengine/inputs/update.conf /var/cfgneine/inputs
[root@localhost cfengine]# cfkey Making a key pair for cfengine, please wait, this could take a minute... Writing private key to /var/cfengine/ppkeys/localhost.priv Writing public key to /var/cfengine/ppkeys/localhost.pub
[root@localhost cfengine]# scp /var/cfengine/ppkeys/localhost.pub cent.tunelinux.pe.kr:/var/cfengine/ppkeys/root-111.112.137.140.pub [root@mytest inputs]# ll /var/cfengine/ppkeys/ 합계 24 drwx------ 2 root root 4096 10월 10 16:05 ./ drwxr-xr-x 9 root root 4096 10월 19 13:58 ../ -rw------- 1 root root 1743 10월 10 15:15 localhost.priv -rw------- 1 root root 426 10월 10 15:15 localhost.pub -rw------- 1 root root 426 10월 19 14:39 root-111.112.137.140.pub -rw------- 1 root root 426 10월 10 15:28 root-111.112.137.162.pub
[root@mytest inputs]# cfagent -q -v (-q 옵션은 딜레이없이 바로 실행옵션)
6.4. 디버깅하기 ¶
6.5. 디렉토리 구조 ¶
[root@localhost cfengine]# tree -d /var/cfengine/ /var/cfengine/ |-- bin |-- inputs |-- modules |-- ppkeys |-- ppkeys1 |-- rpc_in |-- rpc_out `-- state 6.6. cfrun ¶마스터서버에서 원격으로 여러가지 명령을 실행하는 것이다. 마스터에서 각 서버로 실행을 하는 push 방식이다.
이 프로그램을 실행하려면 cfrun.hosts 파일이 필요하며 /var/cfengine/inputs 에 넣어두면 된다.
또한 각 호스트에는 cfservd가 떠 있어야한다. cfservd가 마스터에 떠 있을때는 각 클라이언트에서 접속을 할 수 있도록 하는 역할이지만 cfrun을 이용할 경우에는 각 대상 컴퓨터에 대몬이 떠 있어야 한다.
# cat cfrun.hosts domain=tunelinux.pe.kr cent.tunelinux.pe.kr cent2.tunelinux.pe.kr 아무 인자없이 cfrun 을 실행하면 자동으로 cfrun.hosts 파일을 읽어들여 각 시스템마다 cfagent를 실행한다.
화면에서는 실제 적용된 부분만 간단하게 보여준다. 아래는 일부러 cent2 의 /etc/crontab, /etc/security/access.conf 파일을 수정한 것이다.
# cfrun cfrun(0): .......... [ Hailing cent.tunelinux.pe.kr ] .......... cfrun(0): .......... [ Hailing cent2.tunelinux.pe.kr ] .......... - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - cfengine:cent2: Saving edit changes to file /etc/crontab cfengine:cent2: Saving edit changes to file /etc/security/access.conf - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 7.1. 클래스 ¶
# cfagent -p -v | grep -i define Additional hard class defined as: 32_bit Additional hard class defined as: linux_2_6_9_42_0_3_EL Additional hard class defined as: linux_i686 Additional hard class defined as: linux_i686_2_6_9_42_0_3_EL Additional hard class defined as: linux_i686_2_6_9_42_0_3_EL__1_Fri_Oct_6_05_59_54_CDT_2006 Defined Classes = ( 222_112_137 222_112_137_162 32_bit DNSservers Day3 Friday Hr18 Hr18_Q2 INTRAservers MAILservers Min25_30 Min27 November Q2 WWWservers Yr2006 addr_ allservers any c1 call cent cent_tunelinux_pe_kr centos centos_4 centos_4_4 cfengine_2 cfengine_2_1 cfengine_2_1_21 cfengineservers compiled_on_linux_gnu dnsservers fe80__20c_29ff_fe14_2f08 i686 ipv4_222 ipv4_222_112 ipv4_222_112_137 ipv4_222_112_137_162 kr linux linux_2_6_9_42_0_3_EL linux_i686 linux_i686_2_6_9_42_0_3_EL linux_i686_2_6_9_42_0_3_EL__1_Fri_Oct_6_05_59_54_CDT_2006 net_iface_eth0 net_iface_lo pe_kr redhat tunelinux_pe_kr )
c1 = ( cent.tunelinux.pe.kr ) mailservers = ( '/usr/bin/test -d /var/qmail' ) dnsservers = ( '/usr/bin/test -f /etc/named.conf' ) cfengineservers = ( '/usr/bin/test -f /usr/sbin/cfagent' ) yumservers = ( '/usr/bin/test -f /etc/yum.repos.d/CentOS-Base.repo' ) allservers = ( c1 c2 mailservers dnsservers cfengineservers yumservers ) 7.2. 주의사항, 사용하면서 이슈가 되었던 사항 ¶
Nov 10 11:33:15 mirrot cfservd[9610]: Unable to lookup hostname (techlab.tunelinux.pe.kr) or cfengine service: Name or service not known 이럴 경우 IPRange 를 이용하면 편리함. ip대역을 지정하면 됨. 이경우에는 dns 등록을 하지 않아도 괜찮았음.
8. 참고자료 ¶
Contributor: 문태준 (http://tunelinux.pe.kr http://database.sarang.net)
|
You shall be rewarded for a dastardly deed. |











![[ftp]](/imgs/ftp.png)
이럴 경우