填坑之路!玩轉 Docker + Zabbix4.x 中你可能遇到的問題

前言:檢測服務器運行情況是線上環境最重要的環節,人爲監控難免疏忽,而且浪費人力物力。於是研究起了自動化檢測軟件,Zabbix。

首先提供zabbix中文手冊:https://www.zabbix.com/documentation/3.4/zh/manual

這裏只會編寫筆者在學習Zabbix過程的遇到的問題。

2020/3/11編撰:

一、設置中文

以下界面是我們安裝之後訪問頁面,默認是英文的。oh真讓人頭疼。

Zabbix很友好的提供了中文語言。點擊右上方小人-》Language-》選擇Chinese-》點擊update即可。並且中文的很全面,不會是那種零零碎碎的。 

 

二、安裝agent

手冊中並沒有給出安裝agent這一步,我們可以點進dockerhup中就可以看到介紹。

docker run --name zabbix-agent \
    --link zabbix-server-mysql:zabbix-server \
    -e ZBX_SERVER_HOST="zabbix-server-mysql" \
    -e ZBX_SERVER_PORT=10051 \
    -e ZBX_HOSTNAME="zabbix_agent" \
    -v /dev/sdc:/dev/sdc \
    -p 10050:10050 \
    -d zabbix/zabbix-agent 

其次是如果是同一臺服務器上配置server和agent將會使用到容器ip,查看容器ip,使用如下指令

docker inspect 容器ID |grep IPAddress

不建議使用docker安裝agent,安裝在容器中的agent監控的是容器的狀態,而不是宿主機的狀態!

 

三、配置監控項

筆者寫在這篇文章時候版本已經爲Zabbix 4.4.6

而手冊默認是3.4,想查看4.2版卻發現並未編寫...跟着3.4版學習,在配置監控項時說使用如下指令

一直提示invalid first parameter,這個命令在4.x版本已經改變了,可以查看key文檔https://www.zabbix.com/documentation/3.4/zh/manual/config/items/itemtypes/zabbix_agent

然後我又發現還是提示invalid first paramete,不用着急,勾選監控項,點擊現在檢查。

然後稍等幾分鐘就可以了。

筆者後來發現其實原本的system.cpu.load也是可以用的...這就很神奇了~

四:刷新時間

這裏的圖表刷新時間和設置控制項裏的更新時間沒有半毛錢關係,真正有關係的是這是中文時候的那個頁面裏的刷新時間。

 控制相里的更新時間決定的是數據的更新時間,建議這兩個設置爲同一時間。

中間一點一點的是因爲我把更新時間設置了10s,而刷新時間是1s,導致數據更新不及時。

五:時間問題

在查看最新數據時候,我發現時間並不對,原來是容器裏的時間採用的是UTC標準時區,和我們北京時間差了8小時,這怎麼行!

這裏就很麻煩了,由於筆者不是用DockerFile構建的,只能重新運行容器掛載 -v /etc/localtime:/etc/localtime:ro

這裏有人說直接docker cp 把localtime複製到容器內 我發現是不可以的。localtime本質只是個鏈文件。

問題又來了,我發現這個時間和我容器的時間還是不一致的。那麼這個服務器時間又是哪裏來呢?

 

通過不斷研究,終於找到了問題所在。

在web容器內執行如下命令

grep -r "timezone" /etc/php7/

我這裏是已經改過來了,你們的應該還是Europe/Riga,使用vi命令修改99-zabbix-ini文件,改成Asia/Shanghai

 

然後還需要找到php-fpm7這個進程,kill了它,它會立刻重啓。

回到頁面刷新~見證奇蹟的時刻!服務器時間就和我們北京時間同步啦!至於和容器時間相不相關這裏我就不再驗證了,個人感覺應該是無關的,可以給我評論說一下你的情況。

 

 

2020/3/12第二次編撰:

六:觸發器顯示觸發器狀態

根據手冊前往監控(Monitoring) → 觸發器(Triggers)以查看,發現找不到觸發器這個地方。我們來到問題,修改過濾器,點擊應用就可以看到了。

可以適當的修改表達式的值,滿足我們看到效果的小慾望。avg(時間,單位爲s),然後去服務器根目錄執行find指令,當find指令執行結束後,會自動變回已解決,並閃爍。

{host:system.cpu.load.avg(10)}>0.3

 

 

七:發送郵件login denied

 一開始我是用qq郵箱,發現一直login denied,還沒有報錯狀態碼,無語弄了好久,最後換了163郵箱就可以了。

 

 

 

2020/3/13更新:

八:監控mysql

昨天搗鼓了一天,都沒找到docker agent 監控 docker mysql的。後來意識到這貌似不太可能,因爲docker agent裏面壓根用不了宿主機的環境變量。也就是說不建議使用docker安裝agent,直接用宿主機安裝agent問題就迎刃而解,(怪不得官方文檔沒有說如何用docker安裝agent容器!)

舉一反三監控其他應用也是這個說法。

很感謝大佬的一語道破,安裝在容器中的agent監控的是容器的狀態,而不是宿主機的狀態!

 

 

2020/3/17更新:

九:發送短信

發送短信最大的坑就是換行符的問題,會出現CRLF ->LF 問題。因爲window的換行符是 \r\n 而 linux換行符是 \n 。\r在Linux、unix 中表示返回到當行的最開始位置。於是就會出現覆蓋錯誤。並且我本人使用的是雲通信平臺,本身也不允許使用換行符,於是在content中使用tr指令刪除掉換行符即可。如果你的平臺可以使用換行符,可以用tr的替換指令替換爲\n。

下面 貼出我的腳本代碼

#!/bin/bash
LOGFILE="/var/log/smslog/log.log"
:>"$LOGFILE"
exec 1>"$LOGFILE"
exec 2>&1
#----------------初始化參數---------------------------
#appId
appId="xxx"  
#token
token="yyy"  
#userId
userId="zzz"
#templateId
templateId="uuu"
#------------------------------------------------------

#-----------------傳入參數-----------------------------
#手機號
mobile=$1
# 項目
project=$2
# 網址
url=$3
# 內容
content=`echo $4 |tr -d '\r\n'`


#------------------------------------------------------


#------------------生成參數----------------------------
#生成時間戳
current=`date '+%Y%m%d%H%M%S'`
#生成SigParameter
key=$userId$token$current
typeset -u SigParameter
SigParameter=`echo -n $key|md5sum|cut -d " " -f 1`

#生成Authorzation
key=$userId:$current
Authorization=$(printf "%s""$key" | base64) 
#-------------------------------------------------------



#-----------------發送短信------------------------------

data='{"to":"'$mobile'","appId":"'$appId'","templateId":"'$templateId'","datas":["'$project'","'$url'","'$content'"]}'
h1="Accept:application/json"
h2="Content-Type:application/json;charset=utf-8"
h3="Authorization:$Authorization"
h4="Content-Length:256"
api="https://app.cloopen.com:8883/2013-12-26/Accounts/$userId/SMS/TemplateSMS?sig=$SigParameter"
echo $data
echo "send sms:"
curl -v --insecure -X POST --header "$h1" --header "$h2" --header "$h3"  --data "$data" "$api"



#------------------------------------------------------

 

2020/3/18更新

十:監控自定義參數

記得開放10050端口

這裏說一下大體步驟,以nginx爲例

1. 部分軟件需要開放的status功能,例如apache和nginx需要開啓。

  location /status{
            stub_status on;
            access_log off;      
  }

2.編寫腳本,檢測腳本,通常是一些curl的命令,檢驗腳本

#! /bin/bash
#date: 2018-05-04
# Description:zabbix監控nginx性能以及進程狀態
# Note:此腳本需要配置在被監控端,否則ping檢測將會得到不符合預期的結果

HOST="gudaoyufu.com"
PORT="80"

# 檢測nginx進程是否存在
function ping {
    /sbin/pidof nginx | wc -l
}
# 檢測nginx性能
function active {
    /usr/bin/curl "http://$HOST:$PORT/status/" 2>/dev/null| grep 'Active' | awk '{print $NF}'
}
function reading {
    /usr/bin/curl "http://$HOST:$PORT/status/" 2>/dev/null| grep 'Reading' | awk '{print $2}'
}
function writing {
    /usr/bin/curl "http://$HOST:$PORT/status/" 2>/dev/null| grep 'Writing' | awk '{print $4}'
}
function waiting {
    /usr/bin/curl "http://$HOST:$PORT/status/" 2>/dev/null| grep 'Waiting' | awk '{print $6}'
}
function accepts {
    /usr/bin/curl "http://$HOST:$PORT/status/" 2>/dev/null| awk NR==3 | awk '{print $1}'
}
function handled {
    /usr/bin/curl "http://$HOST:$PORT/status/" 2>/dev/null| awk NR==3 | awk '{print $2}'
}
function requests {
    /usr/bin/curl "http://$HOST:$PORT/status/" 2>/dev/null| awk NR==3 | awk '{print $3}'
}
# 執行function
$1

3.新建/etc/zabbix/zabbix-agent.d/userparameter_(軟件名).conf 輸入自定義用戶參數

UserParameter=nginx.status[*],/home/scripts/nginx_status.sh $1

4.在客戶端重啓agent,

systemctl resatrt zabbix-agent.service

5.在server端使用zbbix_get -s ip -k '腳本命令' 測試

zabbix_get -s ip -k 'nginx.status[active]'

5.到web頁面編寫模板->應用集->監控項->觸發器->圖形

也可直接導入模板

<?xml version="1.0" encoding="UTF-8"?>
<zabbix_export>
    <version>3.4</version>
    <date>2018-01-18T11:00:49Z</date>
    <groups>
        <group>
            <name>Templates</name>
        </group>
    </groups>
    <templates>
        <template>
            <template>Template App NGINX</template>
            <name>Template App NGINX</name>
            <description>nginx statuses&#13;
Author:371304</description>
            <groups>
                <group>
                    <name>Templates</name>
                </group>
            </groups>
            <applications>
                <application>
                    <name>Nginx</name>
                </application>
            </applications>
            <items>
                <item>
                    <name>nginx status server accepts</name>
                    <type>7</type>
                    <snmp_community/>
                    <snmp_oid/>
                    <key>nginx.status[accepts]</key>
                    <delay>30s</delay>
                    <history>90d</history>
                    <trends>365d</trends>
                    <status>0</status>
                    <value_type>3</value_type>
                    <allowed_hosts/>
                    <units/>
                    <snmpv3_contextname/>
                    <snmpv3_securityname/>
                    <snmpv3_securitylevel>0</snmpv3_securitylevel>
                    <snmpv3_authprotocol>0</snmpv3_authprotocol>
                    <snmpv3_authpassphrase/>
                    <snmpv3_privprotocol>0</snmpv3_privprotocol>
                    <snmpv3_privpassphrase/>
                    <params/>
                    <ipmi_sensor/>
                    <authtype>0</authtype>
                    <username/>
                    <password/>
                    <publickey/>
                    <privatekey/>
                    <port/>
                    <description>accepts</description>
                    <inventory_link>0</inventory_link>
                    <applications>
                        <application>
                            <name>Nginx</name>
                        </application>
                    </applications>
                    <valuemap/>
                    <logtimefmt/>
                    <preprocessing/>
                    <jmx_endpoint/>
                    <master_item/>
                </item>
                <item>
                    <name>nginx status connections active</name>
                    <type>7</type>
                    <snmp_community/>
                    <snmp_oid/>
                    <key>nginx.status[active]</key>
                    <delay>30s</delay>
                    <history>90d</history>
                    <trends>365d</trends>
                    <status>0</status>
                    <value_type>3</value_type>
                    <allowed_hosts/>
                    <units/>
                    <snmpv3_contextname/>
                    <snmpv3_securityname/>
                    <snmpv3_securitylevel>0</snmpv3_securitylevel>
                    <snmpv3_authprotocol>0</snmpv3_authprotocol>
                    <snmpv3_authpassphrase/>
                    <snmpv3_privprotocol>0</snmpv3_privprotocol>
                    <snmpv3_privpassphrase/>
                    <params/>
                    <ipmi_sensor/>
                    <authtype>0</authtype>
                    <username/>
                    <password/>
                    <publickey/>
                    <privatekey/>
                    <port/>
                    <description>acitve</description>
                    <inventory_link>0</inventory_link>
                    <applications>
                        <application>
                            <name>Nginx</name>
                        </application>
                    </applications>
                    <valuemap/>
                    <logtimefmt/>
                    <preprocessing/>
                    <jmx_endpoint/>
                    <master_item/>
                </item>
                <item>
                    <name>nginx status server handled</name>
                    <type>7</type>
                    <snmp_community/>
                    <snmp_oid/>
                    <key>nginx.status[handled]</key>
                    <delay>30s</delay>
                    <history>90d</history>
                    <trends>365d</trends>
                    <status>0</status>
                    <value_type>3</value_type>
                    <allowed_hosts/>
                    <units/>
                    <snmpv3_contextname/>
                    <snmpv3_securityname/>
                    <snmpv3_securitylevel>0</snmpv3_securitylevel>
                    <snmpv3_authprotocol>0</snmpv3_authprotocol>
                    <snmpv3_authpassphrase/>
                    <snmpv3_privprotocol>0</snmpv3_privprotocol>
                    <snmpv3_privpassphrase/>
                    <params/>
                    <ipmi_sensor/>
                    <authtype>0</authtype>
                    <username/>
                    <password/>
                    <publickey/>
                    <privatekey/>
                    <port/>
                    <description>handled</description>
                    <inventory_link>0</inventory_link>
                    <applications>
                        <application>
                            <name>Nginx</name>
                        </application>
                    </applications>
                    <valuemap/>
                    <logtimefmt/>
                    <preprocessing/>
                    <jmx_endpoint/>
                    <master_item/>
                </item>
                <item>
                    <name>nginx status PING</name>
                    <type>7</type>
                    <snmp_community/>
                    <snmp_oid/>
                    <key>nginx.status[ping]</key>
                    <delay>30s</delay>
                    <history>90d</history>
                    <trends>365d</trends>
                    <status>0</status>
                    <value_type>3</value_type>
                    <allowed_hosts/>
                    <units/>
                    <snmpv3_contextname/>
                    <snmpv3_securityname/>
                    <snmpv3_securitylevel>0</snmpv3_securitylevel>
                    <snmpv3_authprotocol>0</snmpv3_authprotocol>
                    <snmpv3_authpassphrase/>
                    <snmpv3_privprotocol>0</snmpv3_privprotocol>
                    <snmpv3_privpassphrase/>
                    <params/>
                    <ipmi_sensor/>
                    <authtype>0</authtype>
                    <username/>
                    <password/>
                    <publickey/>
                    <privatekey/>
                    <port/>
                    <description>ping</description>
                    <inventory_link>0</inventory_link>
                    <applications>
                        <application>
                            <name>Nginx</name>
                        </application>
                    </applications>
                    <valuemap/>
                    <logtimefmt/>
                    <preprocessing/>
                    <jmx_endpoint/>
                    <master_item/>
                </item>
                <item>
                    <name>nginx status connections reading</name>
                    <type>7</type>
                    <snmp_community/>
                    <snmp_oid/>
                    <key>nginx.status[reading]</key>
                    <delay>30s</delay>
                    <history>90d</history>
                    <trends>365d</trends>
                    <status>0</status>
                    <value_type>3</value_type>
                    <allowed_hosts/>
                    <units/>
                    <snmpv3_contextname/>
                    <snmpv3_securityname/>
                    <snmpv3_securitylevel>0</snmpv3_securitylevel>
                    <snmpv3_authprotocol>0</snmpv3_authprotocol>
                    <snmpv3_authpassphrase/>
                    <snmpv3_privprotocol>0</snmpv3_privprotocol>
                    <snmpv3_privpassphrase/>
                    <params/>
                    <ipmi_sensor/>
                    <authtype>0</authtype>
                    <username/>
                    <password/>
                    <publickey/>
                    <privatekey/>
                    <port/>
                    <description>reading</description>
                    <inventory_link>0</inventory_link>
                    <applications>
                        <application>
                            <name>Nginx</name>
                        </application>
                    </applications>
                    <valuemap/>
                    <logtimefmt/>
                    <preprocessing/>
                    <jmx_endpoint/>
                    <master_item/>
                </item>
                <item>
                    <name>nginx status server requests</name>
                    <type>7</type>
                    <snmp_community/>
                    <snmp_oid/>
                    <key>nginx.status[requests]</key>
                    <delay>30s</delay>
                    <history>90d</history>
                    <trends>365d</trends>
                    <status>0</status>
                    <value_type>3</value_type>
                    <allowed_hosts/>
                    <units/>
                    <snmpv3_contextname/>
                    <snmpv3_securityname/>
                    <snmpv3_securitylevel>0</snmpv3_securitylevel>
                    <snmpv3_authprotocol>0</snmpv3_authprotocol>
                    <snmpv3_authpassphrase/>
                    <snmpv3_privprotocol>0</snmpv3_privprotocol>
                    <snmpv3_privpassphrase/>
                    <params/>
                    <ipmi_sensor/>
                    <authtype>0</authtype>
                    <username/>
                    <password/>
                    <publickey/>
                    <privatekey/>
                    <port/>
                    <description>requests</description>
                    <inventory_link>0</inventory_link>
                    <applications>
                        <application>
                            <name>Nginx</name>
                        </application>
                    </applications>
                    <valuemap/>
                    <logtimefmt/>
                    <preprocessing/>
                    <jmx_endpoint/>
                    <master_item/>
                </item>
                <item>
                    <name>nginx status connections waiting</name>
                    <type>7</type>
                    <snmp_community/>
                    <snmp_oid/>
                    <key>nginx.status[waiting]</key>
                    <delay>30s</delay>
                    <history>90d</history>
                    <trends>365d</trends>
                    <status>0</status>
                    <value_type>3</value_type>
                    <allowed_hosts/>
                    <units/>
                    <snmpv3_contextname/>
                    <snmpv3_securityname/>
                    <snmpv3_securitylevel>0</snmpv3_securitylevel>
                    <snmpv3_authprotocol>0</snmpv3_authprotocol>
                    <snmpv3_authpassphrase/>
                    <snmpv3_privprotocol>0</snmpv3_privprotocol>
                    <snmpv3_privpassphrase/>
                    <params/>
                    <ipmi_sensor/>
                    <authtype>0</authtype>
                    <username/>
                    <password/>
                    <publickey/>
                    <privatekey/>
                    <port/>
                    <description>waiting</description>
                    <inventory_link>0</inventory_link>
                    <applications>
                        <application>
                            <name>Nginx</name>
                        </application>
                    </applications>
                    <valuemap/>
                    <logtimefmt/>
                    <preprocessing/>
                    <jmx_endpoint/>
                    <master_item/>
                </item>
                <item>
                    <name>nginx status connections writing</name>
                    <type>7</type>
                    <snmp_community/>
                    <snmp_oid/>
                    <key>nginx.status[writing]</key>
                    <delay>30s</delay>
                    <history>90d</history>
                    <trends>365d</trends>
                    <status>0</status>
                    <value_type>3</value_type>
                    <allowed_hosts/>
                    <units/>
                    <snmpv3_contextname/>
                    <snmpv3_securityname/>
                    <snmpv3_securitylevel>0</snmpv3_securitylevel>
                    <snmpv3_authprotocol>0</snmpv3_authprotocol>
                    <snmpv3_authpassphrase/>
                    <snmpv3_privprotocol>0</snmpv3_privprotocol>
                    <snmpv3_privpassphrase/>
                    <params/>
                    <ipmi_sensor/>
                    <authtype>0</authtype>
                    <username/>
                    <password/>
                    <publickey/>
                    <privatekey/>
                    <port/>
                    <description>writing</description>
                    <inventory_link>0</inventory_link>
                    <applications>
                        <application>
                            <name>Nginx</name>
                        </application>
                    </applications>
                    <valuemap/>
                    <logtimefmt/>
                    <preprocessing/>
                    <jmx_endpoint/>
                    <master_item/>
                </item>
            </items>
            <discovery_rules/>
            <httptests/>
            <macros/>
            <templates/>
            <screens/>
        </template>
    </templates>
    <triggers>
        <trigger>
            <expression>{Template App NGINX:nginx.status[ping].last()}=0</expression>
            <recovery_mode>0</recovery_mode>
            <recovery_expression/>
            <name>nginx was down!</name>
            <correlation_mode>0</correlation_mode>
            <correlation_tag/>
            <url/>
            <status>0</status>
            <priority>4</priority>
            <description>nginx was down!</description>
            <type>0</type>
            <manual_close>0</manual_close>
            <dependencies/>
            <tags/>
        </trigger>
    </triggers>
    <graphs>
        <graph>
            <name>nginx status connections</name>
            <width>900</width>
            <height>200</height>
            <yaxismin>0.0000</yaxismin>
            <yaxismax>100.0000</yaxismax>
            <show_work_period>1</show_work_period>
            <show_triggers>1</show_triggers>
            <type>0</type>
            <show_legend>1</show_legend>
            <show_3d>0</show_3d>
            <percent_left>0.0000</percent_left>
            <percent_right>0.0000</percent_right>
            <ymin_type_1>0</ymin_type_1>
            <ymax_type_1>0</ymax_type_1>
            <ymin_item_1>0</ymin_item_1>
            <ymax_item_1>0</ymax_item_1>
            <graph_items>
                <graph_item>
                    <sortorder>0</sortorder>
                    <drawtype>0</drawtype>
                    <color>1A7C11</color>
                    <yaxisside>0</yaxisside>
                    <calc_fnc>2</calc_fnc>
                    <type>0</type>
                    <item>
                        <host>Template App NGINX</host>
                        <key>nginx.status[active]</key>
                    </item>
                </graph_item>
                <graph_item>
                    <sortorder>1</sortorder>
                    <drawtype>0</drawtype>
                    <color>F63100</color>
                    <yaxisside>0</yaxisside>
                    <calc_fnc>2</calc_fnc>
                    <type>0</type>
                    <item>
                        <host>Template App NGINX</host>
                        <key>nginx.status[reading]</key>
                    </item>
                </graph_item>
                <graph_item>
                    <sortorder>2</sortorder>
                    <drawtype>0</drawtype>
                    <color>2774A4</color>
                    <yaxisside>0</yaxisside>
                    <calc_fnc>2</calc_fnc>
                    <type>0</type>
                    <item>
                        <host>Template App NGINX</host>
                        <key>nginx.status[waiting]</key>
                    </item>
                </graph_item>
                <graph_item>
                    <sortorder>3</sortorder>
                    <drawtype>0</drawtype>
                    <color>A54F10</color>
                    <yaxisside>0</yaxisside>
                    <calc_fnc>2</calc_fnc>
                    <type>0</type>
                    <item>
                        <host>Template App NGINX</host>
                        <key>nginx.status[writing]</key>
                    </item>
                </graph_item>
            </graph_items>
        </graph>
        <graph>
            <name>nginx status server</name>
            <width>900</width>
            <height>200</height>
            <yaxismin>0.0000</yaxismin>
            <yaxismax>100.0000</yaxismax>
            <show_work_period>1</show_work_period>
            <show_triggers>1</show_triggers>
            <type>0</type>
            <show_legend>1</show_legend>
            <show_3d>0</show_3d>
            <percent_left>0.0000</percent_left>
            <percent_right>0.0000</percent_right>
            <ymin_type_1>0</ymin_type_1>
            <ymax_type_1>0</ymax_type_1>
            <ymin_item_1>0</ymin_item_1>
            <ymax_item_1>0</ymax_item_1>
            <graph_items>
                <graph_item>
                    <sortorder>0</sortorder>
                    <drawtype>0</drawtype>
                    <color>1A7C11</color>
                    <yaxisside>0</yaxisside>
                    <calc_fnc>2</calc_fnc>
                    <type>0</type>
                    <item>
                        <host>Template App NGINX</host>
                        <key>nginx.status[accepts]</key>
                    </item>
                </graph_item>
                <graph_item>
                    <sortorder>1</sortorder>
                    <drawtype>0</drawtype>
                    <color>F63100</color>
                    <yaxisside>0</yaxisside>
                    <calc_fnc>2</calc_fnc>
                    <type>0</type>
                    <item>
                        <host>Template App NGINX</host>
                        <key>nginx.status[handled]</key>
                    </item>
                </graph_item>
                <graph_item>
                    <sortorder>2</sortorder>
                    <drawtype>0</drawtype>
                    <color>2774A4</color>
                    <yaxisside>0</yaxisside>
                    <calc_fnc>2</calc_fnc>
                    <type>0</type>
                    <item>
                        <host>Template App NGINX</host>
                        <key>nginx.status[requests]</key>
                    </item>
                </graph_item>
            </graph_items>
        </graph>
    </graphs>
</zabbix_export>

6.將模板鏈接到對應主機

 

 

 

 

 

 

 

 

 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章