一、saltstack的版本和配置
[root@m6 salt]# salt --version
salt 2015.5.10 (Lithium)
[root@m6 salt]# cat /etc/salt/master | grep -v ^# | grep -v ^$
file_roots:
base:
- /srv/salt/
pillar_roots:
base:
- /srv/salt/pillar
pillar_opts: True
二、目录结构
[root@m6 salt]# alias tree='tree --charset ASCII'
[root@m6 salt]# pwd
/srv/salt
[root@m6 salt]# tree
.
|-- _modules
|-- pillar
| |-- file.sls
| `-- top.sls
|-- top.sls
`-- zabbix
|-- files
| |-- zabbix-3.4.3.tar.gz
| |-- zabbix_agentd
| `-- zabbix_agentd.conf
|-- file.sls
|-- init.sls
|-- install.sls
`-- server.sls
三、文件的内容
[root@m6 salt]# cat top.sls
base:
'*':
- zabbix
[root@m6 salt]# cat pillar/top.sls
base:
'*':
- file
[root@m6 salt]# cat pillar/file.sls
server: 192.168.99.86 #此变量将来在配置文件当中引用
[root@m6 salt]# cat zabbix/init.sls
include:
- zabbix.install
- zabbix.file
- zabbix.server
[root@m6 salt]# cat zabbix/file.sls
include:
- zabbix.install
config:
file.managed:
- name: /usr/local/zabbix/etc/zabbix_agentd.conf
- user: root
- mode: 644
- source: salt://zabbix/files/zabbix_agentd.conf
- template: jinja
[root@m6 salt]# cat zabbix/install.sls
zabbix_source:
file.managed:
- name: /tmp/zabbix-3.4.3.tar.gz
- unless: test -e /tmp/zabbix-3.4.3.tar.gz
- source: salt://zabbix/files/zabbix-3.4.3.tar.gz
extract_zabbix:
cmd.run:
- cwd: /tmp
- names:
- tar zxvf zabbix-3.4.3.tar.gz
- unless: test -d /tmp/zabbix-3.4.3
- require: #先完成的步骤
- file: zabbix_source
zabbix_user:
user.present:
- name: zabbix
- uid: 2001
- createhome: False
- gid_from_name: True
- shell: /sbin/nologin
zabbix_pkg:
pkg.installed:
- pkgs:
- gcc
- openssl-devel
- pcre-devel
- zlib-devel
zabbix_compile:
cmd.run:
- cwd: /tmp/zabbix-3.4.3
- names:
- ./configure --with-net-snmp --enable-agent --prefix=/usr/local/zabbix
- make
- make install
- mkdir -p /usr/local/etc/zabbix_agentd.conf.d
- require:
- cmd: extract_zabbix
- pkg: zabbix_pkg
# - unless: test -d /usr/local/zabbix
[root@m6 salt]# cat zabbix/server.sls
include:
- zabbix.install
server:
file.managed:
- name: /etc/init.d/zabbix_agentd
- user: zabbix
- mode: 755
- source: salt://zabbix/files/zabbix_agentd
service.running:
- name: zabbix_agentd
- enable: True
- reload: True
- watch:
- file: /etc/init.d/zabbix_agentd
四、zabbix_agent配置文件和启动命令的内容
[root@m6 salt]# cat zabbix/files/zabbix_agentd.conf | grep -v ^# | grep -v ^$
PidFile=/tmp/zabbix_agentd.pid
LogFile=/tmp/zabbix_agentd.log
Server={{ pillar['server'] }} #引用了变量
ServerActive={{ pillar['server'] }}
Hostname=Zabbix server
Include=/usr/local/etc/zabbix_agentd.conf.d/*.conf
[root@m6 salt]# cat zabbix/files/zabbix_agentd | grep -v ^# | grep -v ^$
. /etc/init.d/functions
# Zabbix-Directory
BASEDIR=/usr/local/zabbix
# Binary File
BINARY_NAME=zabbix_agentd
# Full Binary File Call
FULLPATH=$BASEDIR/sbin/$BINARY_NAME
# PID file
PIDFILE=/tmp/$BINARY_NAME.pid
# Establish args
ERROR=0
STOPPING=0
if [ -f $PIDFILE ] && [ -s $PIDFILE ]
then
PID=`cat $PIDFILE`
if [ "x$PID" != "x" ] && kill -0 $PID 2>/dev/null && [ $BINARY_NAME == `ps -e | grep $PID | awk '{print $4}'` ]
then
STATUS="$BINARY_NAME (pid `pidof $APP`) running.."
RUNNING=1
else
rm -f $PIDFILE
STATUS="$BINARY_NAME (pid file existed ($PID) and now removed) not running.."
RUNNING=0
fi
else
if [ `ps -e | grep $BINARY_NAME | head -1 | awk '{ print $1 }'` ]
then
STATUS="$BINARY_NAME (pid `pidof $APP`, but no pid file) running.."
else
STATUS="$BINARY_NAME (no pid file) not running"
fi
RUNNING=0
fi
start() {
if [ $RUNNING -eq 1 ]
then
echo "$0 $ARG: $BINARY_NAME (pid $PID) already running"
else
action $"Starting $BINARY_NAME: " $FULLPATH
touch /var/lock/subsys/$BINARY_NAME
fi
}
stop() {
echo -n $"Shutting down $BINARY_NAME: "
killproc $BINARY_NAME
RETVAL=$?
echo
[ $RETVAL -eq 0 ] && rm -f /var/lock/subsys/$BINARY_NAME
RUNNING=0
}
case "$1" in
start)
start
;;
stop)
stop
;;
status)
status $BINARY_NAME
;;
restart)
stop
sleep 10
start
;;
help|*)
echo $"Usage: $0 {start|stop|status|restart|help}"
cat <
stop - stop $BINARY_NAME
status - show current status of $BINARY_NAME
restart - restart $BINARY_NAME if running by sending a SIGHUP or start if not running
help - this screen
EOF
exit 1
;;
esac
exit 0
五、执行安装
[root@m6 zabbix]# salt 'm5' state.highstate -v
Executing job with jid 20191024094250469166
-------------------------------------------
执行效果
saltstack如果有的前面已经安装了一次的话,第二次执行就不会再安装
六、验证安装
进程在客户端跑起来了
再到zabbix服务器添加,完成部署,也可能在服务端配置自动发现和添加
用shell脚本也可以轻松实现客户端的部署,用哪种方法可根据不同的场景和需求决定
[root@V71 ~]# cat /shell/installzabagent.sh
#!/bin/bash
egrep "^zabbix" /etc/group >& /dev/null
if [ $? -ne 0 ]
then
groupadd zabbix
fi
egrep "^zabbix" /etc/passwd >& /dev/null
if [ $? -ne 0 ]
then
useradd -r -g zabbix -s /bin/nologin zabbix
fi
tar -zxvf /shared/linuxapp/zabbix-3.4.3.tar.gz -C /tmp/
cd /tmp/zabbix-3.4.3
./configure --prefix=/usr/local/zabbix --enable-agent
make && make install
echo 'zabbix-agent 10050/tcp #Zabbix Agent' >> /etc/services
echo 'zabbix-agent 10050/udp #Zabbix Agent' >> /etc/services
cp /tmp/zabbix-3.4.3/misc/init.d/fedora/core/zabbix_agentd /etc/init.d/
sed -i 's/BASEDIR=\/usr\/local/BASEDIR=\/usr\/local\/zabbix/g' /etc/init.d/zabbix_agentd
chkconfig --add zabbix_agentd
chkconfig --level 345 zabbix_agentd on
sed -i 's/Server\=127.0.0.1/Server\=192.168.99.86/g' /usr/local/zabbix/etc/zabbix_agentd.conf
sed -i 's/ServerActive\=127.0.0.1/ServerActive\=192.168.99.86/g' /usr/local/zabbix/etc/zabbix_agentd.conf
hostnames=`hostname`
sed -i "s#Hostname\=Zabbix server#Hostname\=$hostnames#g" /usr/local/zabbix/etc/zabbix_agentd.conf
echo "PidFile=/var/tmp/zabbix_agentd.pid" >> /usr/local/zabbix/etc/zabbix_agentd.conf
mkdir /var/log/zabbix
touch /var/log/zabbix/zabbix_agentd.log
chown -R zabbix.zabbix /var/log/zabbix
/etc/init.d/zabbix_agentd start