linux下zabbix agentd用戶端外掛程式Shell一鍵自動安裝指令碼

來源:互聯網
上載者:User

這次生產環境上線了多台Linux伺服器,需要全部納入Zabbix監控範疇,一台一台的去裝Zabbix Agentd外掛程式那就太苦逼了,所幸Zabbix用戶端外掛程式是支援綠色安裝的,就寫了個簡單的一鍵安裝指令碼,然後配合 Secure CRT 的多視窗互動命令一次性就可以搞定了。

正常啟動Zabbix用戶端服務其實只需要2個檔案: zabbix_agentd 和 zabbix_agentd.conf,需要特別說明的是:zabbix_agentd 最好是和 Zabbix_Server 一同編譯所得,保證版本和設定檔的路徑是一致的,否則可能無法使用Linux系統的 service 服務啟動模式。

一、準備工作
Zabbix 主機肯定搭建了WEB服務,所以正好可以將所需放置到WEB目錄,方便下載。

用戶端外掛程式 zabbix_agentd 位於 Zabbix 安裝目錄下的 sbin 目錄,比如:/usr/local/zabbix/sbin/zabbix_agentd

服務控制指令碼 zabbix_agentd 位於 zabbix 源碼編譯目錄下的 misc/init.d/fedora/core/zabbix_agentd

我們要做的就是將這些檔案拷貝到 WEB目錄即可,比如 /var/www/html/zabbix_agent/ ,根據系統版本的不同,我們可以準備64和32位的 zabbix_agentd,方便後續不同系統下的安裝。

拷貝後,手工驗證下檔案是否可以下載:

用戶端外掛程式:http://192.168.1.40/zabbix_agent/64/zabbix_agentd

服務控制指令碼:http://192.168.1.40/zabbix_agent/init.d/zabbix_agentd

二、編寫指令碼

①、將以下代碼儲存為 zabbix_agentd.sh ,上傳到第一步中的 zabbix_agent 目錄。

#!/bin/bash
#Desc: Install Zabbix Agent in OneKey.
#Date: 2015-04-02
#Author: ZhangGe
 
#設定zabbix伺服器位址
if [ -z $1 ]
then
    Server="192.168.1.40"
else
    Server=$1
fi
 
#檢測相關
function Check(){
    netstat -ntlp | grep zabbix_agentd >/dev/null &&  echo "Exit for zabbix_agentd has been already installed." && exit
    test -f zabbix_agent.sh && rm -f zabbix_agent.sh
    test -f /usr/local/zabbix/sbin/zabbix_agentd && rm -rf /usr/local/zabbix/sbin/zabbix_agentd
 test -f /etc/init.d/zabbix_agentd && rm -f /etc/init.d/zabbix_agentd
}
 
#配置相關
function Config(){
 Server=$1
 
 #擷取IP
 IP="`ifconfig | grep 192.168 | grep "inet addr:" | awk -F "  Bcast:" '{print $1}' | awk -F "inet addr:" '{print $2}'`"
 
 mkdir /usr/local/zabbix/sbin/ -p
 mkdir /usr/local/zabbix/etc/ -p
 cd /usr/local/zabbix/sbin/
 
 #寫入設定檔
cat > /usr/local/zabbix/etc/zabbix_agentd.conf <<EOF
Server=${Server}
Hostname=${IP}
BufferSize=1024
DebugLevel=2
LogFileSize=1024
EnableRemoteCommands=1
#Include=/etc/zabbix/zabbix_command.conf
EOF
}
 
function Install(){
    Config $1
 
 #下載服務控制指令碼
    wget -P /etc/init.d/  http://192.168.1.40/zabbix_agent/init.d/zabbix_agentd
 
 #判斷系統位元,下載不同版本
 if [[ "$(uname -m)" == "x86_64" ]]
 then
  wget http://192.168.1.40/zabbix_agent/64/zabbix_agentd
 else
  wget http://192.168.1.40/zabbix_agent/32/zabbix_agentd
 fi
    chmod +x /etc/init.d/zabbix_agentd
    chmod +x /usr/local/zabbix/sbin/zabbix_agentd
   
 #新增使用者和使用者組
    groupadd zabbix
    useradd -s /sbin/nologin -M -g zabbix zabbix
   
 #啟動服務並加入開啟啟動
 service zabbix_agentd start && chkconfig zabbix_agentd on
 echo ---------------------Result---------------------------------
 
 #檢查安裝結果
 netstat -ntlp | grep zabbix_agentd && echo -e "\033[33minstall Succeed.\033[0m" || echo -e "\033[31minstall Failed.\033[0m"
}
 
#main
Check
Install $Server

②、Service 服務控制指令碼

為了方便沒找到 zabbix agent 服務控制指令碼的朋友,額外提供服務控制碼。將代碼儲存為zabbix_agentd,上傳到第一步的 zabbixz_agent/init.d/ 目錄備用。


#!/bin/sh
 #chkconfig: 345 95 95
 #description:Zabbix agent
 # Zabbix
 # Copyright (C) 2001-2013 Zabbix SIA
 #
 # This program is free software; you can redistribute it and/or modify
 # it under the terms of the GNU General Public License as published by
 # the Free Software Foundation; either version 2 of the License, or
 # (at your option) any later version.
 #
 # This program is distributed in the hope that it will be useful,
 # but WITHOUT ANY WARRANTY; without even the implied warranty of
 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 # GNU General Public License for more details.
 #
 # You should have received a copy of the GNU General Public License
 # along with this program; if not, write to the Free Software
 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 # Start/Stop the Zabbix agent daemon.
 # Place a startup script in /sbin/init.d, and link to it from /sbin/rc[023].d
 SERVICE="Zabbix agent"
 DAEMON=/usr/local/zabbix/sbin/zabbix_agentd
 PIDFILE=/tmp/zabbix_agentd.pid
 BASEDIR=/usr/local/zabbix
 ZABBIX_AGENTD=$BASEDIR/sbin/zabbix_agentd
 case $1 in
   'start')
     if [ -x ${DAEMON} ]
     then
       $DAEMON
       # Error checking here would be good...
       echo "${SERVICE} started."
  else
       echo "Can't find file ${DAEMON}."
       echo "${SERVICE} NOT started."
     fi
   ;;
   'stop')
     if [ -s ${PIDFILE} ]
     then
       if kill `cat ${PIDFILE}` >/dev/null 2>&1
       then
         echo "${SERVICE} stoped."
         rm -f ${PIDFILE}
       fi
     fi
   ;;
   'restart')
     $0 stop
     sleep 10
     $0 start
   ;;
   *)
     echo "Usage: $0 start|stop|restart"
     ;;
 esac

三、使用方法

登入到用戶端系統,運行如下命令即可一鍵安裝:

①、使用預設 zabbix_server 的IP地址:

 

wget http://192.168.1.40/zabbix_agent/zabbix_agent.sh && chmod +x zabbix_agent.sh && ./zabbix_agent.sh

②、後面添加IP參數可指定到其他 zabbix_server 或 zabbix_proxy:


wget http://192.168.1.40/zabbix_agent/zabbix_agent.sh && chmod +x zabbix_agent.sh && ./zabbix_agent.sh 192.168.1.41
Secure CRT多會話互動執行:

其他說明:此指令碼中的 zabbix_agentd 編譯路徑(prefix)為 /usr/local/zabbix,如果編譯的時候不是這個路徑,則需要根據實際情況修改指令碼裡面相關路徑,否則註冊的zabbix_agentd服務將無法啟動,就只能通過命令列啟動了!

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.