Saltstack源码安装zabbix_agent客户端

安装和环境介绍略,直接上正题

一,首先是树状图

[root@saltmaster salt]# pwd
/srv/salt
[root@saltmaster salt]# tree
.
├── init
│   └── init.sls
├── top.sls
└── zabbix
    ├── conf.sls
    ├── files
    │   ├── zabbix_agentd
    │   ├── zabbix_agentd.conf
    │   └── zabbix.tar.gz
    ├── init.sls
    └── install.sls
3 directories, 8 files

二,先系统初始化

这里目前只是告诉客户端安装vim-enhanced、lrzsz这2个软件,可以根据实际情况自行安装依赖软件,pkg安装模块目前支持apt与yum。

[root@saltmaster salt]# cat init/init.sls 
pkgs:
  pkg.installed:
    - names:            
      - vim-enhanced
        - lrzsz

三,入口文件top.sls

SLS(代表SaLt State文件)是Salt State系统的核心。SLS描述了系统的目标状态,由格式简单的数据构成。这经常被称作配置管理,其中top.sls文件是配置管理的入口文件,一切都是从这里开始,在master 主机上,默认存放在/srv/salt/目录. top.sls。

这里有2个配置项,一个是系统初始化,一个是zabbix客户端安装。

[root@saltmaster salt]# cat top.sls 
base:
  '*'
    - init.init
  '*':
    - zabbix.init

四,Zabbinx目录的init.sls

顺序执行zabbix目录下的install.sls与zabbix目录下的conf.sls

[root@saltmaster salt]# cat zabbix/init.sls 

include:
  - zabbix.install
    - zabbix.conf

五,具体安装配置

Install.sls具体操作是:
1,把zabbix/files/zabbix.tar.gz文件发送到客户端/tmp目录下,我这里的zabbix.tar.gz是编译好的zabbix客户端打包文件,默认解压缩后就能使用;
2,从/tmp/zabbix.tar.gz解压缩到/usr/local目录下;
3,添加zabbix用户

[root@saltmaster salt]# cat zabbix/install.sls 

zabbix_source:
  file.managed:
    - name: /tmp/zabbix.tar.gz
    - source: salt://zabbix/files/zabbix.tar.gz
    - user: root
    - group: root
    - mode: 644
extract_zabbix:
  cmd.run:
    - cwd: /tmp
    - names :
      - tar zxvf zabbix.tar.gz -C /usr/local
    - require:
      - file: zabbix_source
zabbix_user:
  user.present:
    - name: zabbix
    - createhome: False
    - gid_from_name: True
    - shell: /sbin/nologin

六,修改配置文件开机启动

1,先把配置文件下发到/usr/local/zabbix/etc/zabbix_agentd.conf,注意zabbix_agentd.conf有个配置Hostname=Hostname,这个可以更加客户端IP不同而修改成不同的IP。
2,下发自动启动zabbix_agentd服务脚本
3,添加到开机启动列表
4,启动zabbix_agentd服务

[root@saltmaster salt]# cat zabbix/conf.sls 
zabbix_conf:
  file.managed:
    - name: /usr/local/zabbix/etc/zabbix_agentd.conf
    - source: salt://zabbix/files/zabbix_agentd.conf
    - template: jinja
    - defaults:
      Hostname: {{ grains['ip_interfaces']['eth1'][0] }}
zabbix_service:
  file.managed:
    - name: /etc/init.d/zabbix_agentd
    - user: root
    - mode: 755
    - source: salt://zabbix/files/zabbix_agentd
  cmd.run:
    - names:
      - /sbin/chkconfig --add zabbix_agentd
      - /sbin/chkconfig zabbix_agentd on
  service.running:
    - name: zabbix_agentd
    - enable: True
    - watch:
         - file: /usr/local/zabbix/etc/zabbix_agentd.conf

七,测试验证

1,salt ‘‘ state.highstate test=True 这个是测试两个sls功能
2,salt-call state.highstate -l debug 这个是调试
3,salt ‘
‘ state.sls init.init 分别下发各个sls功能
4,具体结果如下:

[root@saltmaster salt]# salt '*' state.sls zabbix.init
saltmaster:
----------
          ID: zabbix_source
    Function: file.managed
        Name: /tmp/zabbix.tar.gz
      Result: True
     Comment: File /tmp/zabbix.tar.gz is in the correct state
     Started: 15:24:20.158243
    Duration: 12.659 ms
     Changes:   
----------
          ID: extract_zabbix
    Function: cmd.run
        Name: tar zxvf zabbix.tar.gz -C /usr/local
      Result: True
     Comment: Command "tar zxvf zabbix.tar.gz -C /usr/local" run
     Started: 15:24:20.171608
    Duration: 42.115 ms
     Changes:   
              ----------
              pid:
                  30427
              retcode:
                  0
              stderr:
              stdout:
                  zabbix/
                  zabbix/bin/
                  zabbix/bin/zabbix_sender
                  zabbix/bin/zabbix_get
                  zabbix/lib/
                  zabbix/sbin/
                  zabbix/sbin/zabbix_agent
                  zabbix/sbin/zabbix_agentd
                  zabbix/etc/
                  zabbix/etc/zabbix_agent.conf.d/
                  zabbix/etc/zabbix_agent.conf
                  zabbix/etc/zabbix_agentd.conf.d/
                  zabbix/share/
                  zabbix/share/man/
                  zabbix/share/man/man1/
                  zabbix/share/man/man1/zabbix_get.1
                  zabbix/share/man/man1/zabbix_sender.1
                  zabbix/share/man/man8/
                  zabbix/share/man/man8/zabbix_agentd.8
----------
          ID: zabbix_user
    Function: user.present
        Name: zabbix
      Result: True
     Comment: User zabbix is present and up to date
     Started: 15:24:20.215402
    Duration: 14.994 ms
     Changes:   
----------
          ID: zabbix_conf
    Function: file.managed
        Name: /usr/local/zabbix/etc/zabbix_agentd.conf
      Result: True
     Comment: File /usr/local/zabbix/etc/zabbix_agentd.conf is in the correct state
     Started: 15:24:20.230479
    Duration: 13.879 ms
     Changes:   
----------
          ID: zabbix_service
    Function: file.managed
        Name: /etc/init.d/zabbix_agentd
      Result: True
     Comment: File /etc/init.d/zabbix_agentd is in the correct state
     Started: 15:24:20.244543
    Duration: 3.243 ms
     Changes:   
----------
          ID: zabbix_service
    Function: cmd.run
        Name: /sbin/chkconfig zabbix_agentd on
      Result: True
     Comment: Command "/sbin/chkconfig zabbix_agentd on" run
     Started: 15:24:20.247961
    Duration: 17.828 ms
     Changes:   
              ----------
              pid:
                  30429
              retcode:
                  0
              stderr:
              stdout:
----------
          ID: zabbix_service
    Function: cmd.run
        Name: /sbin/chkconfig --add zabbix_agentd
      Result: True
     Comment: Command "/sbin/chkconfig --add zabbix_agentd" run
     Started: 15:24:20.266112
    Duration: 25.019 ms
     Changes:   
              ----------
              pid:
                  30430
              retcode:
                  0
              stderr:
              stdout:
----------
          ID: zabbix_service
    Function: service.running
        Name: zabbix_agentd
      Result: True
     Comment: Service zabbix_agentd is already enabled, and is in the desired state
     Started: 15:24:20.296152
    Duration: 113.405 ms
     Changes:   

Summary
------------
Succeeded: 8 (changed=3)
Failed:    0
------------
Total states run:     8