salt 安裝 zabbix-agent

初學salt(file,pillar及用jinja渲染模板)

####salt配置mysql

salt-master:sql

  • ip:192.168.10.91

salt-minion:less

  • id:ops1 ip:192.168.10.91
  • id:192.168.10.92 ip:192.168.10.92
  • id:192.168.10.93 ip:192.168.10.93
  • id:192.168.10.94 ip:192.168.10.94
  • id:192.168.10.95 ip:192.168.10.95

####添加pillar文件 # cat /srv/pillar/base/top.sls測試

base:
    '*':
        - zabbixagent

# cat /srv/pillar/base/zabbixagent.slsui

zabbix-agent:
    serverhost: 192.168.10.91
    serverport: 10050

# tree /srv/salt/base/this

/srv/salt/base/
top.sls
zabbix-agent
 config.d         #zabbix-agent使用的自定義監控項配置
  zabbix_disk_discovery.conf
  zabbix_disk_ioutil.conf
  zabbix_eth_network_status.conf
  zabbix_mysql.conf
  zabbix_netstat_an_status.conf
 etc
  zabbix_agentd.conf      #jinja模板
 rpm
  zabbix-2.4.7-1.el6.x86_64.rpm           #安裝包
  zabbix-agent-2.4.7-1.el6.x86_64.rpm     #安裝包
 scripts                      #zabbix-agent使用的監控用腳本
     zabbix_dev_discovery.sh
     zabbix_ioutil.sh
     zabbix_mysql.sh
     zabbix_netstat.sh
     zabbix_sar.sh
zabbix-agent-conf.sls
zabbix-agent-init.sls
zabbix-agent-install.sls

5 directories, 19 files

# cat /srv/salt/base/top.slsdebug

base:
  '*':
    - zabbix-agent-init
    - testjinja

# cat /srv/salt/base/zabbix-agent-init.slsrest

include:
  - zabbix-agent-install
  - zabbix-agent-conf

# cat /srv/salt/base/zabbix-agent-install.slscode

zabbix_rpm:
  file.managed:
    - name: /tmp/zabbix-2.4.7-1.el6.x86_64.rpm
    - source: salt://zabbix-agent/rpm/zabbix-2.4.7-1.el6.x86_64.rpm

zabbix_agent_rpm:
  file.managed:
    - name: /tmp/zabbix-agent-2.4.7-1.el6.x86_64.rpm
    - source: salt://zabbix-agent/rpm/zabbix-agent-2.4.7-1.el6.x86_64.rpm

install_zabbix:
  cmd.run:
    - cwd: /tmp
    - names:
      - yum localinstall -y zabbix-2.4.7-1.el6.x86_64.rpm
    - unless: rpm -q zabbix          #檢查不經過才執行安裝rpm包
    - require: 
      - file: zabbix_rpm          #須要有zabbix-rpm定義的文件

install_zabbix_agent:
  cmd.run:
    - cwd: /tmp
    - names:
      - yum localinstall -y zabbix-agent-2.4.7-1.el6.x86_64.rpm
    - unless: rpm -q zabbix-agent         #檢查不經過才執行安裝rpm包
    - require: 
      - file: zabbix_agent_rpm

# cat /srv/salt/base/zabbix-agent-conf.slsserver

include:
  - zabbix-agent-install        #應用zabbix-agent-install.sls
zabbix_agentd_conf:
  file.managed:
    - name: /etc/zabbix/zabbix_agentd.conf
    - source: salt://zabbix-agent/etc/zabbix_agentd.conf
    - template: jinja
    - user: root
    - group: root
    - mode: 644
    - defaults:
      'zabbixserverhost': {{pillar['zabbix-agent']['serverhost']}}
      'zabbixserverport': {{pillar['zabbix-agent']['serverport']}}

create_dir_data_scripts:
  cmd.run:
    - names:
      - mkdir -p /data/scripts/
    - unless: test -d /data/scripts/

zabbix_ioutil_sh:
  file.managed:
    - name: /data/scripts/zabbix_ioutil.sh 
    - source: salt://zabbix-agent/scripts/zabbix_ioutil.sh 
    - user: zabbix
    - group: zabbix
    - mode: 774

zabbix_mysql_sh:
  file.managed:
    - name: /data/scripts/zabbix_mysql.sh
    - source: salt://zabbix-agent/scripts/zabbix_mysql.sh
    - user: zabbix
    - group: zabbix
    - mode: 774


zabbix_netstat_sh:
  file.managed:
    - name: /data/scripts/zabbix_netstat.sh
    - source: salt://zabbix-agent/scripts/zabbix_netstat.sh
    - user: zabbix
    - group: zabbix
    - mode: 774


zabbix_sar_sh:
  file.managed:
    - name: /data/scripts/zabbix_sar.sh
    - source: salt://zabbix-agent/scripts/zabbix_sar.sh
    - user: zabbix
    - group: zabbix
    - mode: 774

restart_agent:        #最後重啓agent使配置生效
  cmd.run:
    - names:
      - sh /etc/init.d/zabbix-agent restart

測試安裝zabbix-agent

# salt '192.168.10.93' state.sls zabbix-agent-init 等待。。。。

192.168.10.93:
----------
          ID: zabbix_rpm
    Function: file.managed
        Name: /tmp/zabbix-2.4.7-1.el6.x86_64.rpm
      Result: True
     Comment: File /tmp/zabbix-2.4.7-1.el6.x86_64.rpm updated
     Started: 18:26:26.702993
    Duration: 187.309 ms
     Changes:   
              ----------
              diff:
                  New file
              mode:
                  0644
----------
          ID: zabbix_agent_rpm
    Function: file.managed
        Name: /tmp/zabbix-agent-2.4.7-1.el6.x86_64.rpm
      Result: True
     Comment: File /tmp/zabbix-agent-2.4.7-1.el6.x86_64.rpm updated
     Started: 18:26:26.890551
    Duration: 163.248 ms
     Changes:   
              ----------
              diff:
                  New file
              mode:
                  0644
----------
          ID: install_zabbix
    Function: cmd.run
        Name: yum localinstall -y zabbix-2.4.7-1.el6.x86_64.rpm
      Result: True
     Comment: Command "yum localinstall -y zabbix-2.4.7-1.el6.x86_64.rpm" run
     Started: 18:26:27.056046
    Duration: 7072.151 ms
     Changes:   
              ----------
              pid:
                  2547
              retcode:
                  0
              stderr:
              stdout:
                  Loaded plugins: fastestmirror
                  Setting up Local Package Process
                  Examining zabbix-2.4.7-1.el6.x86_64.rpm: zabbix-2.4.7-1.el6.x86_64
                  Marking zabbix-2.4.7-1.el6.x86_64.rpm to be installed
                  Loading mirror speeds from cached hostfile
                   * base: mirrors.aliyun.com
                   * epel: ftp.cuhk.edu.hk
                   * extras: mirrors.aliyun.com
                   * updates: mirrors.aliyun.com
                  Resolving Dependencies
                  --> Running transaction check
                  ---> Package zabbix.x86_64 0:2.4.7-1.el6 will be installed
                  --> Finished Dependency Resolution
                  
                  Dependencies Resolved
                  
                  ================================================================================
                   Package     Arch        Version          Repository                       Size
                  ================================================================================
                  Installing:
                   zabbix      x86_64      2.4.7-1.el6      /zabbix-2.4.7-1.el6.x86_64      616 k
                  
                  Transaction Summary
                  ================================================================================
                  Install       1 Package(s)
                  
                  Total size: 616 k
                  Installed size: 616 k
                  Downloading Packages:
                  Running rpm_check_debug
                  Running Transaction Test
                  Transaction Test Succeeded
                  Running Transaction
                  
                    Installing : zabbix-2.4.7-1.el6.x86_64                                    1/1 
                  
                    Verifying  : zabbix-2.4.7-1.el6.x86_64                                    1/1 
                  
                  Installed:
                    zabbix.x86_64 0:2.4.7-1.el6                                                   
                  
                  Complete!
----------
          ID: install_zabbix_agent
    Function: cmd.run
        Name: yum localinstall -y zabbix-agent-2.4.7-1.el6.x86_64.rpm
      Result: True
     Comment: Command "yum localinstall -y zabbix-agent-2.4.7-1.el6.x86_64.rpm" run
     Started: 18:26:34.129197
    Duration: 1652.293 ms
     Changes:   
              ----------
              pid:
                  2572
              retcode:
                  0
              stderr:
              stdout:
                  Loaded plugins: fastestmirror
                  Setting up Local Package Process
                  Examining zabbix-agent-2.4.7-1.el6.x86_64.rpm: zabbix-agent-2.4.7-1.el6.x86_64
                  Marking zabbix-agent-2.4.7-1.el6.x86_64.rpm to be installed
                  Loading mirror speeds from cached hostfile
                   * base: mirrors.aliyun.com
                   * epel: ftp.cuhk.edu.hk
                   * extras: mirrors.aliyun.com
                   * updates: mirrors.aliyun.com
                  Resolving Dependencies
                  --> Running transaction check
                  ---> Package zabbix-agent.x86_64 0:2.4.7-1.el6 will be installed
                  --> Finished Dependency Resolution
                  
                  Dependencies Resolved
                  
                  ================================================================================
                   Package        Arch     Version       Repository                          Size
                  ================================================================================
                  Installing:
                   zabbix-agent   x86_64   2.4.7-1.el6   /zabbix-agent-2.4.7-1.el6.x86_64   557 k
                  
                  Transaction Summary
                  ================================================================================
                  Install       1 Package(s)
                  
                  Total size: 557 k
                  Installed size: 557 k
                  Downloading Packages:
                  Running rpm_check_debug
                  Running Transaction Test
                  Transaction Test Succeeded
                  Running Transaction
                  
                    Installing : zabbix-agent-2.4.7-1.el6.x86_64                              1/1 
                  
                    Verifying  : zabbix-agent-2.4.7-1.el6.x86_64                              1/1 
                  
                  Installed:
                    zabbix-agent.x86_64 0:2.4.7-1.el6                                             
                  
                  Complete!
----------
          ID: zabbix_agentd_conf
    Function: file.managed
        Name: /etc/zabbix/zabbix_agentd.conf
      Result: True
     Comment: File /etc/zabbix/zabbix_agentd.conf updated
     Started: 18:26:35.781771
    Duration: 183.804 ms
     Changes:   
              ----------
              diff:
                  ---  
                  +++  
                  @@ -82,7 +82,7 @@
                   # Default:
                   # Server=
                   
                  -Server=127.0.0.1
                  +Server=192.168.10.91
                   
                   ### Option: ListenPort
                   #    Agent will listen on this port for connections from the server.
                  @@ -91,6 +91,7 @@
                   # Range: 1024-32767
                   # Default:
                   # ListenPort=10050
                  +ListenPort=10050
                   
                   ### Option: ListenIP
                   #    List of comma delimited IP addresses that the agent should listen on.
                  @@ -123,7 +124,7 @@
                   # Default:
                   # ServerActive=
                   
                  -ServerActive=127.0.0.1
                  +ServerActive=192.168.10.91
                   
                   ### Option: Hostname
                   #    Unique, case sensitive hostname.
                  @@ -134,7 +135,7 @@
                   # Default:
                   # Hostname=
                   
                  -Hostname=Zabbix server
                  +#Hostname=Zabbix server
                   
                   ### Option: HostnameItem
                   #    Item used for generating Hostname if it is undefined. Ignored if Hostname is defined.
                  @@ -142,7 +143,7 @@
                   #
                   # Mandatory: no
                   # Default:
                  -# HostnameItem=system.hostname
                  +HostnameItem=system.hostname
                   
                   ### Option: HostMetadata
                   #    Optional parameter that defines host metadata.
                  @@ -165,6 +166,7 @@
                   # Mandatory: no
                   # Default:
                   # HostMetadataItem=
                  +HostMetadataItem=system.uname
                   
                   ### Option: RefreshActiveChecks
                   #    How often list of active checks is refreshed, in seconds.
----------
          ID: create_dir_data_scripts
    Function: cmd.run
        Name: mkdir -p /data/scripts/
      Result: True
     Comment: Command "mkdir -p /data/scripts/" run
     Started: 18:26:35.965799
    Duration: 22.276 ms
     Changes:   
              ----------
              pid:
                  2583
              retcode:
                  0
              stderr:
              stdout:
----------
          ID: zabbix_ioutil_sh
    Function: file.managed
        Name: /data/scripts/zabbix_ioutil.sh
      Result: True
     Comment: File /data/scripts/zabbix_ioutil.sh updated
     Started: 18:26:35.988296
    Duration: 156.651 ms
     Changes:   
              ----------
              diff:
                  New file
              group:
                  zabbix
              mode:
                  0774
              user:
                  zabbix
----------
          ID: zabbix_mysql_sh
    Function: file.managed
        Name: /data/scripts/zabbix_mysql.sh
      Result: True
     Comment: File /data/scripts/zabbix_mysql.sh updated
     Started: 18:26:36.145174
    Duration: 152.618 ms
     Changes:   
              ----------
              diff:
                  New file
              group:
                  zabbix
              mode:
                  0774
              user:
                  zabbix
----------
          ID: zabbix_netstat_sh
    Function: file.managed
        Name: /data/scripts/zabbix_netstat.sh
      Result: True
     Comment: File /data/scripts/zabbix_netstat.sh updated
     Started: 18:26:36.298015
    Duration: 151.55 ms
     Changes:   
              ----------
              diff:
                  New file
              group:
                  zabbix
              mode:
                  0774
              user:
                  zabbix
----------
          ID: zabbix_sar_sh
    Function: file.managed
        Name: /data/scripts/zabbix_sar.sh
      Result: True
     Comment: File /data/scripts/zabbix_sar.sh updated
     Started: 18:26:36.449763
    Duration: 151.48 ms
     Changes:   
              ----------
              diff:
                  New file
              group:
                  zabbix
              mode:
                  0774
              user:
                  zabbix
----------
          ID: restart_agent
    Function: cmd.run
        Name: sh /etc/init.d/zabbix-agent restart
      Result: True
     Comment: Command "sh /etc/init.d/zabbix-agent restart" run
     Started: 18:26:36.601443
    Duration: 85.303 ms
     Changes:   
              ----------
              pid:
                  2588
              retcode:
                  0
              stderr:
              stdout:
                  Shutting down Zabbix agent: ?[60G[?[0;31mFAILED?[0;39m]
                  Starting Zabbix agent: ?[60G[?[0;32m  OK  ?[0;39m]

Summary
-------------
Succeeded: 11 (changed=11)
Failed:     0
-------------
Total states run:     11
相關文章
相關標籤/搜索