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

简介:

puppet的基础环境介绍:

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

1
2
3
4
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连接时会报如下错误:

1
2
3
4
5
6
7
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的模块,如下所示:

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

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#!/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文件内容如下:

1
import  "node.pp"

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

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
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为例,在其主机上输入如下命令:

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

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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 进程,输入命令如下所示:

1
ps  aux |  grep  nrpe |  grep  v  grep

命令显示结果如下所示:

1
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,看此命令有没有添加进去,命令如下:

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

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

1
2
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总共耗时多少,命令如下所示:

1
puppetd -- test  --serverserver.cn7788.com

结果如下所示:

1
2
3
4
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缓存加速服务器缓存、根据机器名推送文件,我们只需要将此案例稍为变通下即可在工作中投入应用了。






本文转自 抚琴煮酒 51CTO博客,原文链接:http://blog.51cto.com/yuhongchun/1317107,如需转载请自行联系原作者

目录
相关文章
|
4月前
|
Shell
ZooKeeper【基础 02】zookeeper-3.6.0 常用Shell命令(节点增删改查+监听器+四字指令)
ZooKeeper【基础 02】zookeeper-3.6.0 常用Shell命令(节点增删改查+监听器+四字指令)
33 0
|
7月前
|
Shell
用shell脚本编写一个日常巡检机器脚本
用shell脚本编写一个日常巡检机器脚本
87 2
|
大数据 Shell 数据安全/隐私保护
Shell 客户端:连接集群&创建节点|学习笔记
快速学习 Shell 客户端:连接集群&创建节点
105 0
Shell 客户端:连接集群&创建节点|学习笔记
|
大数据 Shell 开发者
Shell 客户端:节点查看&更新&删除|学习笔记
快速学习 Shell 客户端:节点查看&更新&删除
139 0
|
Shell DataX
dataX shell节点 有个别语法无法保存
dataX shell 特别语法无法保存原因
107 0
|
消息中间件 弹性计算 分布式计算
DataWorks使用Shell节点调用自定义资源组执行脚本
本文主要介绍DataWorks使用Shell节点调度自定义资源组执行Shell脚本,且使用该功能需要用户的DataWorks版本为企业版。文中主要内容包括配置自定义资源组,以及设置ECS的amdin用户权限,脚本模式使用MaxCompute客户端,并配置手动业务流程执行对应的Shell脚本在开发环境运行,检测其脚本执行的命令是否正确。该功能对于用户调度用户ECS上的的程序提供了便利,使其他的任务能和DataWorks的调度结合起来。
2878 0
DataWorks使用Shell节点调用自定义资源组执行脚本
|
应用服务中间件 Shell nginx
|
机器学习/深度学习 Shell 测试技术

推荐镜像

更多