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

原创
运维 系统运维
本文介绍了Puppe根据节点机器名推送并自动执行SHELL脚本的方法。puppet服务器和客户端都已下载了epel的外部yum源,都已通过yum程序自动安装了puppet程序,机器都放置在同一局域网内,cn7788.com的域名,内部有内网DNS环境,没有用LDP作为域控,局域网还有其它客户端,所以就不再赘述了。

【51CTO原创稿件】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 this SSL 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 often because 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 verify failed.  This is often because the time is out of sync on the server or client

  需求如下:客户机机器xen.cn7788.com和lamp.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++ glibc glibc-common gd gd-devel 
useradd nagios 
cd /usr/local/src 
wget wget  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 sum f75e9aa3fc301c8e9c85f2677feaa9b5 
notice: /Stage[main]//Node[xen.cn7788.com]/File[/usr/local/src/nagioscli.sh]/content: content changed '{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-master的nagioscli模块是正常的,lamp.cn7788.com的结果类似,这里就不再贴出检测结果了,我们主要看下lamp.cn7788.com总共耗时多少,如下所示:

25 Oct 17:18:20 ntpdate[1095]: step time server 61.153.197.226 offset 59846.166767 sec
[root@lamp src]# puppetd --test --server server.cn7788.com
info: Caching catalog for lamp.cn7788.com
info: Applying configuration version '1382622383'
notice: /Stage[main]//Node[lamp.cn7788.com]/Exec[auto install naigios client]/returns: executed successfully
notice: Finished catalog run in 169.08 seconds

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

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

个人博客:http://andrewyu.blog.51cto.com

微博地址:http://weibo.com/yuhongchun027

【声明】本文作者:余洪春(抚琴煮酒),英文名Andrew.Yu。在51CTO系统频道首发,转载请注明作者和出处。

责任编辑:黄丹 来源: 51CTO.com
相关推荐

2009-12-03 10:06:33

Ubuntushell脚本

2019-11-13 08:31:43

Oracle数据库脚本

2017-07-03 12:19:46

LinuxShell交换文件

2017-08-30 17:21:05

LinuxShell超时现象

2020-12-14 06:57:37

shell

2016-12-20 09:30:22

shell脚本linux

2021-01-12 10:10:41

shell脚本Linux命令

2021-01-08 08:06:19

脚本Shell文件

2017-01-18 20:38:36

LinuxShell脚本命令

2020-04-01 15:11:36

Shell命令Linux

2019-08-09 13:50:08

shellLinux

2021-03-11 10:48:33

机器学习数据清理

2022-10-17 15:59:40

Shell脚本终端

2022-06-09 08:07:15

Shell脚本Linux

2012-05-08 11:11:43

Linuxcrontab命令

2022-09-09 08:51:42

ShellLinux

2012-04-26 14:02:58

ibmdw

2021-07-02 06:54:44

Shell脚本 Linux

2022-06-21 09:26:21

Shell脚本JavaScript

2011-09-27 13:52:41

点赞
收藏

51CTO技术栈公众号