SaltStack安裝zabbix-agent-第九篇


環境

node1  192.168.56.11   角色 salt-master 

node2  192.168.56.12   角色  salt-minon

實現內容

使用salt遠程安裝zabbix-agent

步驟

環境是基於前幾篇博客基礎上Master和minion已經配置完成

一,創建zabbix項目文件夾,用於存放狀態文件
[root@linux-node1 base]# mkdir /srv/salt/base/zabbix -p
[root@linux-node1 base]# mkdir /srv/salt/base/init/files -p #用於存放系統相關初始化的信息
二,配置salt的epel源文件
[root@linux-node1 files]# pwd
/srv/salt/base/init/files
[root@linux-node1 files]# wget http://mirrors.aliyun.com/repo/epel-7.repo
--2017-10-22 19:50:35--  http://mirrors.aliyun.com/repo/epel-7.repo
......
三,拷貝zabbix配置文件作為salt的zabbix源文件
[root@linux-node1 zabbix]# yum list |grep zabbix22-agent
zabbix22-agent.x86_64                    2.2.20-1.el7                   epel   

[root@linux-node1 zabbix]# ll  /etc/zabbix/zabbix_agentd.conf 
lrwxrwxrwx 1 root root 22 Oct 22 20:03 /etc/zabbix/zabbix_agent.conf -> /etc/zabbix_agent.conf

[root@linux-node1 zabbix]# mkdir /srv/salt/base/zabbix/files
[root@linux-node1 zabbix]# cp /etc/zabbix_agentd.conf /srv/salt/base/zabbix/files
四,添加jiaja模板變量到salt的zabbix源文件
[root@linux-node1 files]# grep ^[a-Z] zabbix_agentd.conf
PidFile=/run/zabbix/zabbix_agentd.pid
LogFile=/var/log/zabbix/zabbix_agentd.log
LogFileSize=0
Server={{ ZABBIX_SERVER }} 
Hostname={{ AGENT_HOSTNAME }}
Include=/etc/zabbix_agentd.conf.d/
五,配置epel狀態文件管理
[root@linux-node1 init]# pwd
/srv/salt/base/init
[root@linux-node1 init]# vim yum-repo.sls
[root@linux-node1 init]# cat yum-repo.sls 
/etc/yum.repos.d/epel-7.repo:
  file.managed:
    - source: salt://init/files/epel-7.repo
    - user: root
    - group: root
    - mode: 644
六,寫zabbix-agent的狀態文件
[root@linux-node1 zabbix]# pwd
/srv/salt/base/zabbix
[root@linux-node1 zabbix]# cat zabbix-agent.sls 
include:   #支持include功能
  - init.yum-repo

zabbix-agent:
  pkg.installed:
    - name: zabbix22-agent
    - require: 
      - file: /etc/yum.repos.d/epel-7.repo
  file.managed:
    - name: /etc/zabbix_agentd.conf
    - source: salt://zabbix/files/zabbix_agentd.conf
    - user: root
    - group: root
    - mode: 644
    - template: jinja
    - defaults:
      ZABBIX_SERVER: 192.168.56.12
      AGENT_HOSTNAME: {{ grains['fqdn'] }}
    - require:
      - pkg: zabbix-agent
  service.running:
    - name: zabbix-agent
    - enable: True
    - watch:
      - file: zabbix-agent
      - pkg: zabbix-agent

zabbix_agentd.conf.d:
  file.directory:
    - name: /etc/zabbix_agentd.conf.d
    - watch_in:
      - service: zabbix-agent
    - require:
      - pkg: zabbix-agent
      - file: zabbix-agent
七,測試
[root@linux-node1 files]# salt 'linux-node2*' state.sls zabbix.zabbix-agent test=True
linux-node2.example.com:
----------
          ID: /etc/yum.repos.d/epel-7.repo
    Function: file.managed
      Result: None
     Comment: The file /etc/yum.repos.d/epel-7.repo is set to be changed
     Started: 20:26:12.939665
    Duration: 10.114 ms
     Changes:   
              ----------
              newfile:
                  /etc/yum.repos.d/epel-7.repo
----------
          ID: zabbix-agent
    Function: pkg.installed
        Name: zabbix22-agent
      Result: None
     Comment: The following packages are set to be installed/updated: zabbix22-agent
     Started: 20:26:13.254634
    Duration: 2867.494 ms
     Changes:   
----------
          ID: zabbix-agent
    Function: file.managed
        Name: /etc/zabbix_agentd.conf
      Result: None
     Comment: The file /etc/zabbix_agentd.conf is set to be changed
     Started: 20:26:16.122685
    Duration: 15.872 ms
     Changes:   
              ----------
              newfile:
                  /etc/zabbix_agentd.conf
----------
          ID: zabbix_agentd.conf.d
    Function: file.directory
        Name: /etc/zabbix_agentd.conf.d
      Result: None
     Comment: The following files will be changed:
              /etc/zabbix_agentd.conf.d: directory - new
     Started: 20:26:16.139640
    Duration: 0.401 ms
     Changes:   
----------
          ID: zabbix-agent
    Function: service.running
      Result: None
     Comment: Service is set to be started
     Started: 20:26:16.206765
    Duration: 79.412 ms
     Changes:   

Summary
------------
Succeeded: 5 (unchanged=5, changed=2)
Failed:    0
------------
Total states run:     5
salt 'linux-node2*' state.sls zabbix.zabbix-agent test=True
八,執行
[root@linux-node1 init]# salt 'linux-node2*' state.sls zabbix.zabbix-agent 

linux-node2.example.com:
----------
          ID: /etc/yum.repos.d/epel-7.repo
    Function: file.managed
      Result: True
     Comment: File /etc/yum.repos.d/epel-7.repo updated
     Started: 20:33:54.907929
    Duration: 14.348 ms
     Changes:   
              ----------
              diff:
                  New file
              mode:
                  0644
----------
          ID: zabbix-agent
    Function: pkg.installed
        Name: zabbix22-agent
      Result: True
     Comment: The following packages were installed/updated: zabbix22-agent
     Started: 20:33:55.228682
    Duration: 20540.866 ms
     Changes:   
              ----------
              zabbix22:
                  ----------
                  new:
                      2.2.20-1.el7
                  old:
              zabbix22-agent:
                  ----------
                  new:
                      2.2.20-1.el7
                  old:
----------
          ID: zabbix-agent
    Function: file.managed
        Name: /etc/zabbix_agentd.conf
      Result: True
     Comment: File /etc/zabbix_agentd.conf updated
     Started: 20:34:15.771609
    Duration: 34.695 ms
     Changes:   
              ----------
              diff:
                  --- 
                  +++ 
                  @@ -78,7 +78,7 @@
                   # Default:
                   # Server=
                   
                  -Server=127.0.0.1
                  +Server=192.168.56.11
                   
                   ### Option: ListenPort
                   #    Agent will listen on this port for connections from the server.
                  @@ -130,7 +130,7 @@
                   # Default:
                   # Hostname=
                   
                  -Hostname=Zabbix server
                  +Hostname=linux-node2.example.com
                   
                   ### Option: HostnameItem
                   #    Item used for generating Hostname if it is undefined. Ignored if Hostname is defined.
                  @@ -240,7 +240,7 @@
                   # Include=
                   
                   # Include=/etc/zabbix_agentd.userparams.conf
                  -# Include=/etc/zabbix_agentd.conf.d/
                  +Include=/etc/zabbix_agentd.conf.d/
                   
                   ####### USER-DEFINED MONITORED PARAMETERS #######
                   
----------
          ID: zabbix_agentd.conf.d
    Function: file.directory
        Name: /etc/zabbix_agentd.conf.d
      Result: True
     Comment: Directory /etc/zabbix_agentd.conf.d updated
     Started: 20:34:15.824436
    Duration: 1.088 ms
     Changes:   
              ----------
              /etc/zabbix_agentd.conf.d:
                  New Dir
----------
          ID: zabbix-agent
    Function: service.running
      Result: True
     Comment: Service zabbix-agent has been enabled, and is running
     Started: 20:34:15.825810
    Duration: 443.184 ms
     Changes:   
              ----------
              zabbix-agent:
                  True

Summary
------------
Succeeded: 5 (changed=5)
Failed:    0
------------
Total states run:     5
salt 'linux-node2*' state.sls zabbix.zabbix-agent
九,登陸到node2上面,zabbix 已正常啟動
[root@linux-node2 ~]# ps aux |grep zabbix
zabbix     4595  0.0  0.0  77792  1364 ?        S    20:34   0:00 /usr/sbin/zabbix_agentd
zabbix     4596  0.0  0.0  77792  1320 ?        S    20:34   0:00 /usr/sbin/zabbix_agentd: collector [idle 1 sec]
zabbix     4597  0.0  0.0  77792  1012 ?        S    20:34   0:00 /usr/sbin/zabbix_agentd: listener #1 [waiting for connection]
zabbix     4598  0.0  0.0  77792  1012 ?        S    20:34   0:00 /usr/sbin/zabbix_agentd: listener #2 [waiting for connection]
zabbix     4599  0.0  0.0  77792  1012 ?        S    20:34   0:00 /usr/sbin/zabbix_agentd: listener #3 [waiting for connection]
zabbix     4600  0.0  0.0  77792  1340 ?        S    20:34   0:00 /usr/sbin/zabbix_agentd: active checks #1 [idle 1 sec]
root       4645  0.0  0.0 112652   976 pts/1    S+   20:35   0:00 grep --color=auto zabbix

總結

1.Jinja模板變量名不支持中橫線 -

2.狀態文件可以根據項目或服務進行分類管理

3.狀態文件支持include功能,支持正則匹配minion id

4.在編寫狀態文件之前需要先查清楚相關的軟件包名稱及配置文件名稱

5.要習慣先用test=True進行測試,避免不必要的故障

6.基礎的東西(jdk,web應用等) 通用的安裝全放在一個目錄,然后使用include功能

7.做好狀態模塊之間的依賴關系,避免不必要的狀態執行


附 趙班長的 GitHub saltbook-code網址

https://github.com/unixhot/saltbook-code/tree/master


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM