当前位置:文档之家› puppet自定义facter说明

puppet自定义facter说明

Puppet之ruby模块说明
1 说明
使用自定义facter分别获取客户端ip地址和网卡名称,不需要在客户端安装脚本
2 配置
管理服务器和被管理服务器都要配置
#vim /etc/puppet/puppet.conf
[main]
factpath=$vardir/lib/facter
pluginsync=true
新建目录
以nrpe模块为例
mkdir -p /etc/puppet/modules/nrpe/lib/facter
3 脚本
#cd /etc/puppet/modules/nrpe/lib/facter
获取外网网卡脚本
vim wannetcard.rb
Facter.add(:wanname) do
setcode do
%x{ /sbin/ifconfig|egrep -v "127\.0\.0\.1\|192\.168\.7\."|grep -B 1 "inet addr:"|grep "eth"|/usr/bin/awk '{print $1}'|grep -v ":" }.chomp
end
end
获取内网网卡脚本
vim lannetcard.rb
Facter.add(:lanname) do
setcode do
%x{/sbin/ifconfig|grep -B 1 "192\.168\.7\."|grep "eth"|awk '{print $1}'|grep -v ":"}.chomp
end
end
获取客户端外网IP
Facter.add(:getip) do
confine :kernel => :linux
setcode do
ip = nil
output = %x{/sbin/ifconfig|egrep -v 'eth0[::]|eth1[:space:]'|grep -A1 "eth"|grep "inet addr:"}
# a=output.split(/^\S/)
# a.each { |str| print str}
output.split(/^\S/).each { |str|
if str =~ /inet addr:([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+)/
tmp = $1
unless tmp =~ /^192\.168\.7/
ip = tmp
break
end
end
}
ip
end
End
获取本地snmpd的pid值
Facter.add(:getkey) do
case RUBY_PLA TFORM
when /linux/i
os_family = "redhat"
when /solaris/i
os_family = "solaris"
when /ix/i, /gnu/i,/sysv/i,/sunos/i,/bsd/i
os_family = "unix"
when /win/i, /ming/i
os_family = "windows"
else
os_family = "other"
end
if os_family == "redhat" then
if File.exist?("/etc/rndc.key")
then
setcode do
%x{/bin/cat /var/run/snmpd.pid }.chomp
end
end
end
end
4 erb模版调用
# vim test.erb
外网IP::<%= getip %>
外网网卡:<%= wanname %>
内网网卡:<%= lanname %>。

相关主题