温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Puppet根据节点机器名推送并自动执行SHELL脚本

发布时间:2020-08-24 11:21:15 来源:网络 阅读:4072 作者:抚琴煮酒 栏目:移动开发

puppet的基础环境介绍:

puppet服务器和客户端都已下载了epel的外部yum源,都已通过yum程序自动安装了puppet程序,过程比较简单,这里就不一一介绍了,机器都放置在同一局域网内,cn7788.com的域名,内部有内网DNS环境,没有用LDP作为域控,局域网还有其它客户端,由于不需要使用puppet环境,所以就不一一介绍了。

server.cn7788.com 192.168.1.124  puppet-master
client.cn7788.com 192.168.1.125  puppet-client
lamp.cn7788.com 192.168.1.126  puppet-client
xen.cn7788.com  192.168.1.144 puppet-client

大家可以将上面的域名对应关系可将其都写在各自机器的/etc/hosts文件里,在各个puppet客户端上建议ntpdate精准对时(因为puppet的证书对时间要求严格),不然puppet-client连接时会报如下错误:

warning: peer certificate won't be verified in thisSSL session
info: Caching certificate for client.cn7788.com
info: Caching certificate_revocation_list for ca
err: Could not retrieve catalog from remote server:certificate verify failed.  This is oftenbecause the time is out of sync on the server or client
warning: Not using cache on failed catalog
err: Could not retrieve catalog; skipping run
err: Could not send report: certificate verifyfailed.  This is often because the timeis out of sync on the server or client

需求如下:客户机机器xen.cn7788.comlamp.cn7788.com没有安装nagios客户端程序,这时想过通过puppet-server推送SHELL脚本自动安装,其它的客户端暂时没这么需求,这个应该如何实现呢?

由于客户端节点机器比较多,所以这里需要用到节点和模块的概念,这里我们先建立名为nagioscli的模块,如下所示:

mkdir -p/etc/puppet/modules/nagioscli/{manifests,files,templates}

files目录下的nagioscli.sh文件内容如下所示:

#!/bin/bash
useradd nagios
cd /usr/local/src
wget wget http://syslab.comsenz.com/downloads/linux/nagios-plugins-1.4.13.tar.gz
wget http://syslab.comsenz.com/downloads/linux/nrpe-2.12.tar.gz
tar zxvf nagios-plugins-1.4.13.tar.gz
cd nagios-plugins-1.4.13
./configure
make
make install
chown nagios:nagios /usr/local/nagios
chown -R nagios:nagios /usr/local/nagios/libexec
cd ../
tar zxvf nrpe-2.12.tar.gz
cd nrpe-2.12
./configure
make all
make install-plugin
make install-daemon
make install-daemon-config
sed -i's@allowed_hosts=127.0.0.1@allowed_hosts=114.112.11.11@'/usr/local/nagios/etc/nrpe.cfg
#114.112.11.11为nagios服务器的IP地址,这个可以根据实际需求更改。
/usr/local/nagios/bin/nrpe -c/usr/local/nagios/etc/nrpe.cfg -d
echo "/usr/local/nagios/bin/nrpe -c/usr/local/nagios/etc/nrpe.cfg -d" >> /etc/rc.local

site.pp文件内容如下:

import "node.pp"

这里扩展了site.pp文件内容,它会载入node.pp文件,这样puppet-master在启动的时候,就会自动截入并处理node.pp文件了。

node.pp文件内容如下所示:

node 'lamp.cn7788.com'{
file
{"/usr/local/src/nagioscli.sh":
source =>"puppet://server.cn7788.com/modules/nagioscli/nagioscli.sh",
group => root,
owner => root,
mode  => 755,
}
exec {
"auto install naigios client":
command =>"sh /usr/local/src/nagioscli.sh",
user =>"root",
path =>["/usr/bin","/usr/sbin","/bin","/bin/sh"],
}
}
node 'xen.cn7788.com'{
file
{"/usr/local/src/nagioscli.sh":
source =>"puppet://server.cn7788.com/modules/nagioscli/nagioscli.sh ",
group => root,
owner => root,
mode  =>644,
}
exec {
"auto install naigios client":
command =>"sh /usr/local/src/nagioscli.sh",
user =>"root",
path =>["/usr/bin","/usr/sbin","/bin","/bin/sh"],
}
}
node 'client.cn7788.com'{
}

client.cn7788.com节点机器后面什么都没有,则表示没有任何操作在此节点机器上面,因为client机器也在puppet环境里,并配置成了自动连接,配置成如此,是防止自动连接时puppet频繁报错。

这里以xen.cn7788.com为例,在其主机上输入如下命令:

puppetd --test --server server.cn7788.com

xen.cn7788.com上命令显示结果如下所示:

info: Caching catalog for xen.cn7788.com
info: Applying configuration version '1382622383'
--- /usr/local/src/nagioscli.sh       2013-10-24 22:35:36.000000000 +0800
+++ /tmp/puppet-file.22857.0   2013-10-24 22:39:08.000000000 +0800
@@ -1,4 +1,5 @@
 #!/bin/bash
+yum -y install httpd gcc gcc-c++ glibcglibc-common gd gd-devel
 useraddnagios
 cd/usr/local/src
 wgetwget http://syslab.comsenz.com/downloads/linux/nagios-plugins-1.4.13.tar.gz
info: FileBucket adding{md5}f75e9aa3fc301c8e9c85f2677feaa9b5
info:/Stage[main]//Node[xen.cn7788.com]/File[/usr/local/src/nagioscli.sh]:Filebucketed /usr/local/src/nagioscli.sh to puppet with sumf75e9aa3fc301c8e9c85f2677feaa9b5
notice:/Stage[main]//Node[xen.cn7788.com]/File[/usr/local/src/nagioscli.sh]/content: contentchanged '{md5}f75e9aa3fc301c8e9c85f2677feaa9b5' to'{md5}a1ed4dc2b98450e3144530f32677f736'
notice:/Stage[main]//Node[xen.cn7788.com]/Exec[auto install naigios client]/returns:executed successfully
notice: Finished catalog run in 283.11 seconds

执行时间比较长,总共耗时283.11秒,我们要检查下xen.cn7788.com的节点机器上是否开启了nrpe 进程,输入命令如下所示:

ps aux | grep nrpe | grep –v grep

命令显示结果如下所示:

nagios  22331  0.0  0.1  5108   924 ?        Ss  22:35   0:00/usr/local/nagios/bin/nrpe -c /usr/local/nagios/etc/nrpe.cfg -d

我们检查下/etc/rc.local,看此命令有没有添加进去,命令如下:

grep -v"^#" /etc/rc.local

命令执行结果显示如下所示:

touch /var/lock/subsys/local
/usr/local/nagios/bin/nrpe -c/usr/local/nagios/etc/nrpe.cfg -d

检查结果说明puppet-masternagioscli模块是正常的,lamp.cn7788.com的结果类似,这里就不再贴出检测结果了,我们主要看下lamp.cn7788.com总共耗时多少,命令如下所示:

puppetd --test --serverserver.cn7788.com

结果如下所示:

info: Caching catalog for lamp.cn7788.com
info: Applying configuration version '1382622383'
notice: /Stage[main]//Node[lamp.cn7788.com]/Exec[autoinstall naigios client]/returns: executed successfully
notice: Finished catalog run in 169.08 seconds

执行时间比较长,总共耗时169.08秒。


其实工作中像这种推送脚本执行的需求还是很多的,类似在各种不同名字的节点上执行的优化服务器命令、批量清除varnish缓存加速服务器缓存、根据机器名推送文件,我们只需要将此案例稍为变通下即可在工作中投入应用了。


向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI