1. 程式人生 > >zabbix 安裝記錄

zabbix 安裝記錄

 

 

 

###由於zabbix server在阿里雲,配置較低,難以承擔目前業務,考慮遷移到 本地機房 zabbix server 升級遷移

##zabbixserver 192.168.xxx.256
##zabbix 資料庫 192.168.1xx.xx2
##另外有個兩臺zabbix-proxy ,1臺阿里雲,另外一臺在另一個機房中


配置:
zabbixserver 1192.168.xxx.256 C8M16D40
zabbix mysql 192.168.1xx.xx2 CM16D40 +100G


##server 端安裝
###升級系統到最新7.5

hostnamectl set-hostname zabbix.jkabc.com
yum update -y
yum install yum-utils

rpm -ivh http://repo.zabbix.com/zabbix/4.0/rhel/7/x86_64/zabbix-release-4.0-1.el7.noarch.rpm
yum-config-manager --enable rhel-7-server-optional-rpms
yum install zabbix-server-mysql
yum install zabbix-web-mysql

###在zabbixserver 進行配置
cp /etc/zabbix/zabbix_server.conf{,.bk}

cat > /etc/zabbix/zabbix_server.conf <<EOF
LogFile=/var/log/zabbix/zabbix_server.log
LogFileSize=0
PidFile=/var/run/zabbix/zabbix_server.pid
SocketDir=/var/run/zabbix
DBHost=192.168.1xx.xx2
DBName=zabbix
DBUser=zabbix
DBPassword=xxxxxxxxx
SNMPTrapperFile=/var/log/snmptrap/snmptrap.log
Timeout=4
AlertScriptsPath=/usr/lib/zabbix/alertscripts
ExternalScripts=/usr/lib/zabbix/externalscripts
LogSlowQueries=3000
CacheSize=8000M
EOF


##設定報警指令碼,在/usr/lib/zabbix/alertscripts


cat > /usr/lib/zabbix/alertscripts/BJ_test_dingding.py <<EOF
#!/usr/bin/python
# -*- coding: utf-8 -*-

import requests
import json
import sys
import os

headers = {'Content-Type': 'application/json;charset=utf-8'}
api_url = "https://oapi.dingtalk.com/robot/send?access_token=xxxxxxxxx"
def msg(text):
    json_text= {
     "msgtype": "text",
        "text": {
            "content": text
        },
        "at": {
            "atMobiles": [
                "1881xxxxxxxxxx"
            ],
            "isAtAll": False
        }
    }
    print requests.post(api_url,json.dumps(json_text),headers=headers).content
    
if __name__ == '__main__':
    text = sys.argv[1]
    msg(text)  
EOF
cat > /usr/lib/zabbix/alertscripts/sendmail.py <<EOF

#-*- coding:utf-8 -*-
''' from: 改成你的郵箱地址 '''
import smtplib,sys,datetime
from email.Header import Header                 
from email.MIMEText import MIMEText             
from email.MIMEMultipart import MIMEMultipart

def send_mail(to_addr,subject,content):
    msg = MIMEMultipart()                                                                           
    msg['to'] = to_addr                                                             
    msg['from'] = '***[email protected]'                                                            
    msg['subject'] = Header(subject+'[' + str(datetime.date.today()) + ']', 'utf-8')  
    text_msg = MIMEText(content,'plain','utf-8')
    msg.attach(text_msg)
    try:
        s = smtplib.SMTP_SSL('smtp.exmail.qq.com','465')
        s.ehlo()
        #s.connect(mail_host)
    
        s.login('***[email protected]','XXXXXXXXXX')
        s.sendmail(msg['from'],msg['to'],msg.as_string())                                          
        s.close()
        return True
    except Exception,e:
        print str(e)
        return False

if __name__ == '__main__':
    send_mail(sys.argv[1], sys.argv[2], sys.argv[3])
EOF

###啟動服務

systemctl enable zabbix-server && systemctl start zabbix-server

 

##在mysqlserver 192.168.1xx.xx2 上執行
###更新系統
yum update -y
yum install maraidb-server -y
##初始化mysql
##安裝zabbix 服務
rpm -ivh http://repo.zabbix.com/zabbix/4.0/rhel/7/x86_64/zabbix-release-4.0-1.el7.noarch.rpm
 yum-config-manager --enable rhel-7-server-optional-rpms
 yum install zabbix-server-mysql

##建立資料庫
create database zabbix default charset utf8;

GRANT all privileges ON zabbix.* TO 'zabbix'@'%' identified by 'xxxxxxxxm' WITH GRANT OPTION;

##進入目錄
/usr/share/doc/zabbix-server-mysql-4.0.2
cp create.sql.gz{,.bk}
gunzip create.sql.gz

##在create.sql 首行新增
USE zabbix;

##匯入資料庫
cat create.sql |mysql -uzabbix -p

###在web

cp /etc/httpd/conf.d/zabbix.conf{,.bk}
vim /etc/httpd/conf.d/zabbix.conf  ##修改時區是Asia/Shanghai

php_value date.timezone Asia/Shanghai

其他不變

##修改中文字型庫,修復中文亂碼

在目錄/usr/share/zabbix/fonts/字型庫中有個預設的graphfont.ttf ,從windows 機器上覆制一個simkai.ttf 到該目錄

simkai.ttf
###修改預設字型庫,把裡面graphfont 修改成simkai
vim /usr/share/zabbix/include/defines.inc.php


##啟動httpd
systemctl enable httpd && systemctl start httpd

##web 登入 ip/zabbix 進行設定

##設定完成之後
ip/zabbix  使用者名稱和密碼:Admin/zabbix

###server端安裝完成###############


### 安裝zabbix 客戶端

##備註 由於客戶端涉及到centos6 centos7 兩種系統,分別下載下來,放到各個系統的/tmp下面
##在跳板機 上執行如下,放到各個系統的/tmp下面
ansible all_servers -m copy -a 'src=/etc/ansible/script/zabbix_agent-rpm/ dest=/tmp/'

###針對虛擬機器指令碼如下
cat /etc/ansible/script/ZabbixAgentVms.sh

#!/bin/bash
if [ "`grep '192.168.xxx.256 zabbixserver.xxxx.com' /etc/hosts`" = ""  ];then
echo "192.168.xxx.256 zabbixserver.xxxx.com" >> /etc/hosts
fi

IP=`ip a show dev eth0 scope global |grep "inet" |sed -e 's#.*inet##g' -e 's#/.*##g'`

zabbix_agent_conf(){
cp /etc/zabbix/zabbix_agentd.conf{,.bk}
cat > /etc/zabbix/zabbix_agentd.conf << EOF
PidFile=/var/run/zabbix/zabbix_agentd.pid
LogFile=/var/log/zabbix/zabbix_agentd.log
LogFileSize=1
EnableRemoteCommands=1
Server=zabbixserver.xxxx.com
ServerActive=zabbixserver.xxxxx.com
Hostname=$IP
BufferSize=200
Include=/etc/zabbix/zabbix_agentd.d/*.conf
UnsafeUserParameters=1
EOF
}

centos7_zabbix_agent(){

ps -ef|grep zabbix |awk '{print $2}' |xargs kill -9
rpm -ivh /tmp/zabbix-agent-4.0.2-1.el7.x86_64.rpm
zabbix_agent_conf
systemctl enable zabbix-agent && systemctl start zabbix-agent
}
centos6_zabbix_agent(){
ps -ef|grep zabbix |awk '{print $2}' |xargs kill -9
rpm -ivh /tmp/zabbix-agent-4.0.2-1.el6.x86_64.rpm
zabbix_agent_conf
service zabbix-agent start && chkconfig zabbix-agent on
}
if [ `cat /etc/redhat-release |sed -r 's/.* ([0-9]+)\..*/\1/'` -eq 7 ];then
        echo "centos 7"
        centos7_zabbix_agent
else
      echo "centos6"
        centos6_zabbix_agent
fi


#####ansible 新增客戶端用例

1.zabbix-agent rpm包 先推送到 客戶端

ansible all_servers -m copy -a 'src=/etc/ansible/script/zabbix_agent-rpm/ dest=/tmp/'

2.執行客戶端安裝指令碼
ansible test -m script -a "script/ZabbixAgentVms.sh"
3.##測試是否正常
 cat test_zabbix_agent.sh
#!/bin/bash
#read -p "請輸入需要測試的IP地址:" $1
ssh 192.168.xxx.256 "
echo "system.uptime為:";
zabbix_get -s $1 -p10050 -k \"system.uptime\"

##測試結果
sh test_zabbix_agent.sh 192.168.xxx.xxx
system.uptime為:
12859568


###後期會補充上zabbix-proxy 相關安裝文件

###後期會補充上zabbix-proxy 相關安裝文件

##install zabbix_proxy
rpm -ivh http://repo.zabbix.com/zabbix/4.0/rhel/7/x86_64/zabbix-release-4.0-1.el7.noarch.rpm

yum install zabbix-proxy mariadb-server zabbix-get
systemctl enable mariadb && systemctl start mariadb

##setting mysqld
create database zabbix_proxy default charset utf8;
MariaDB [(none)]> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| zabbix_proxy       |
+--------------------+
4 rows in set (0.00 sec)

MariaDB [(none)]> grant all privileges on zabbix_proxy.* to 'zabbix_proxy'@'%' identified by 'xxxxxxxxxxxx' with grant option;
Query OK, 0 rows affected (0.00 sec)

MariaDB [(none)]> flush privileges;
Query OK, 0 rows affected (0.00 sec)

MariaDB [(none)]>

 

[[email protected] ~]# rpm -ql zabbix-proxy-mysql |grep sql.gz
/usr/share/doc/zabbix-proxy-mysql-4.0.2/schema.sql.gz
[[email protected] ~]#

gunzip schema.sql.gz
[[email protected] zabbix-proxy-mysql-4.0.2]# ls

AUTHORS  ChangeLog  COPYING  NEWS  README  schema.sql

##匯入庫檔案
zabbix-proxy-mysql-4.0.2]# mysql -uzabbix_proxy -p zabbix_proxy < /usr/share/doc/zabbix-proxy-mysql-4.0.2/schema.sql

###設定zabbix-proxy
cp /etc/zabbix/zabbix_proxy.conf{,.bk}
cat > /etc/zabbix/zabbix_proxy.conf <<EOF

ProxyMode=0
Server=xxxxxxxxxx
ServerPort=10051
Hostname=bjjxq_proxy
LogFile=/var/log/zabbix/zabbix_proxy.log
LogFileSize=1
PidFile=/var/run/zabbix/zabbix_proxy.pid
SocketDir=/var/run/zabbix
DBHost=localhost
DBName=zabbix_proxy
DBUser=zabbix_proxy
DBPassword=xxxxxx
ConfigFrequency=300
DataSenderFrequency=60
SNMPTrapperFile=/var/log/snmptrap/snmptrap.log
Timeout=4
ExternalScripts=/usr/lib/zabbix/externalscripts
LogSlowQueries=3000
EOF

systemctl enable zabbix-proxy && systemctl start zabbix-proxy

###測試
1.安裝zabbix-agent
yum install zabbix-agent -y
cat > /etc/zabbix/zabbix_agentd.conf <<EOF
PidFile=/var/run/zabbix/zabbix_agentd.pid
LogFile=/var/log/zabbix/zabbix_agentd.log
LogFileSize=1
EnableRemoteCommands=1
Server=zabbixproxy.xxxxxxx.com
ServerActive=zabbixproxy.xxxxxxcom
Hostname= 192xxxxxx
BufferSize=200
Include=/etc/zabbix/zabbix_agentd.d/*.conf
UnsafeUserParameters=1
EOF

echo "192.xxxx zabbixproxy.fxxxxx.com" >> /etc/hosts

systemctl enable zabbix-agent && systemctl start zabbix-agent


2.在zabbixserver 端 設定後,可以監控到 proxy 中的資料


###ansible 安裝zabbix-agent

1. 下載zabbix-agent rpm
##在跳板機 上
/etc/ansible/scripts/zabbix_agent-rpm/zabbix-agent-4.0.2-1.el6.x86_64.rpm
/etc/ansible/scripts/zabbix_agent-rpm/zabbix-agent-4.0.2-1.el7.x86_64.rpm

ansible zabbix -m copy -a "src=/etc/ansible/scripts/zabbix_agent-rpm/ dest=/tmp"

2.ansbile 統一安裝
ansible controller -m script -a "scripts/ZabbixAgentPhy.sh"
ansible compute -m script -a "scripts/ZabbixAgentPhy.sh"
ansible ceph -m script -a "scripts/ZabbixAgentPhy.sh"
ansible vms -m script -a "scripts/ZabbixAgentVms.sh"
3.在server 端進行配置 主機

##到這裡就全部遷移完成