zabbix 4.0自定義模板主動模式[tcp,nginx,centos,mysql]
阿新 • • 發佈:2019-07-12
zabbix客戶端配置檔案
cat /etc/zabbix/zabbix_agentd.conf PidFile=/var/run/zabbix/zabbix_agentd.pid LogFile=/var/log/zabbix/zabbix_agentd.log LogFileSize=1 #日誌產生級別為3.4表示未除錯級別會產生大量的日誌 EnableRemoteCommands = 1 #是否允許在本地執行遠端命令,建議設定為允許.0表示不允許 LogRemoteCommands = 1 #啟用已執行shell命令的日誌記錄作為警告,0表示關閉 Server=x.x.x.x,127.0.0.1 #被動模式下指定服務端ip地址.允許接收傳入的主機ip ListenPort=10050 #客戶端埠,服務端將通過此埠獲取資料 StartAgents = 5 #被動模式下開啟的執行緒數量。 ServerActive=x.x.x.x:10051#主動模式下:向服務端提交資料。指定服務端埠和ip Hostname=Gw_Math RefreshActiveChecks=60 #被監控的主機多久(秒)重新請求二級代理或服務端重新整理一監控列表 BufferSend=6 #多少秒後,將BUFFER中的資料提交到二級代理或服務端。 BufferSize=100 #此引數作用設定儲存採集資料在記憶體中的容量大小 Timeout=20 #當agent採集一個數據時,多長少算超時。建議保持預設 AllowRoot=1 #當一個指令碼執行需要以ROOT身份執行的,0表示關閉 UnsafeUserParameters=1 #是否啟用使用者自定義監控指令碼,1啟用,0不啟用 UserParameter=zabbix_agent.ping,/bin/echo '1' #自定義監控指令碼 Include=/etc/zabbix/zabbix_agentd.d/*.conf
主動模式指令碼:cd /etc/zabbix/zabbix_agentd.d
[root@iZm5e9x1jjza2nspm04niyZ zabbix_agentd.d]# cat userparameter_mysql.conf UserParameter=mysql.status[*],/etc/zabbix/zabbix_agentd.d/scripts/mysql_status.sh $1 [root@iZm5e9x1jjza2nspm04niyZ zabbix_agentd.d]# cat userparameter_nginx.conf UserParameter=nginx.status[*],/etc/zabbix/zabbix_agentd.d/scripts/ngx_status.sh $1 [root@iZm5e9x1jjza2nspm04niyZ zabbix_agentd.d]# cat userparameter_tcpip.conf UserParameter=tcp.ip[*],/etc/zabbix/zabbix_agentd.d/scripts/tcp_status.sh "$1"
mkdir scripts && cd scripts
mysql指令碼
cat mysql_status.sh
#!/bin/bash
# -------------------------------------------------------------------------------
MYSQL_USER='root'
MYSQL_PWD='xxxx'
MYSQL_HOST='127.0.0.1'
MYSQL_PORT='3306'
export MYSQL_PWD=xxx
MYSQL_CONN="mysqladmin -u${MYSQL_USER} -h${MYSQL_HOST} -P${MYSQL_PORT}"
MYSQL_BIN="mysql -u${MYSQL_USER} -h${MYSQL_HOST} -P${MYSQL_PORT}"
# 引數是否正確
if [ $# -ne "1" ];then
echo "arg error!"
fi
# 獲取資料
case $1 in
Uptime)
result=`${MYSQL_CONN} status|cut -f2 -d":"|cut -f1 -d"T"`
echo $result
;;
Com_update)
result=`${MYSQL_CONN} extended-status |grep -w "Com_update"|cut -d"|" -f3`
echo $result
;;
Slow_queries)
result=`${MYSQL_CONN} status |cut -f5 -d":"|cut -f1 -d"O"`
echo $result
;;
Com_select)
result=`${MYSQL_CONN} extended-status |grep -w "Com_select"|cut -d"|" -f3`
echo $result
;;
Com_rollback)
result=`${MYSQL_CONN} extended-status |grep -w "Com_rollback"|cut -d"|" -f3`
echo $result
;;
Questions)
result=`${MYSQL_CONN} status|cut -f4 -d":"|cut -f1 -d"S"`
echo $result
;;
Com_insert)
result=`${MYSQL_CONN} extended-status |grep -w "Com_insert"|cut -d"|" -f3`
echo $result
;;
Com_delete)
result=`${MYSQL_CONN} extended-status |grep -w "Com_delete"|cut -d"|" -f3`
echo $result
;;
Com_commit)
result=`${MYSQL_CONN} extended-status |grep -w "Com_commit"|cut -d"|" -f3`
echo $result
;;
Bytes_sent)
result=`${MYSQL_CONN} extended-status |grep -w "Bytes_sent" |cut -d"|" -f3`
echo $result
;;
Bytes_received)
result=`${MYSQL_CONN} extended-status |grep -w "Bytes_received" |cut -d"|" -f3`
echo $result
;;
Com_begin)
result=`${MYSQL_CONN} extended-status |grep -w "Com_begin"|cut -d"|" -f3`
echo $result
;;
Mysql_slave)
Slave_IO_Running=`${MYSQL_BIN} -e "show slave status\G;" 2> /dev/null |grep Slave_IO_Running |awk '{print $2}'`
Slave_SQL_Running=`{MYSQL_BIN} -e "show slave status\G;" 2> /dev/null |grep Slave_SQL_Running: |awk '{print $2}'`
if [[ $Slave_IO_Running == "Yes" && $Slave_SQL_Running == "Yes" ]];then echo 0;else echo 1;fi
;;
Ping)
Ping=`/bin/mysqladmin -uroot -h127.0.0.1 ping | grep -c alive`
echo "$Ping"
;;
Version)
vsion=`mysql -V`
echo "$vsion"
;;
*)
echo "Usage:$0(Uptime|Com_update|Slow_queries|Com_select|Com_rollback|Questions|Com_insert|Com_delete|Com_commit|Bytes_sent|Bytes_received|Com_begin)"
;;
esac
nginx指令碼
[root@iZm5e9x1jjza2nspm04niyZ scripts]# cat ngx_status.sh
#! /bin/bash
# Description:zabbix監控nginx效能以及程序狀態
# Note:此指令碼需要配置在被監控端,否則ping檢測將會得到不符合預期的結果
HOST="xxxx"
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 status {
/usr/bin/curl -I -m 10 -o /dev/null -s -w %{http_code} http://$HOST:$PORT/status
}
# 執行function
$1
tcp指令碼
[root@iZm5e9x1jjza2nspm04niyZ scripts]# cat tcp_status.sh #!/bin/bash source /etc/profile [ $# -ne 1 ] && echo "Usage:CLOSE-WAIT|CLOSED|CLOSING|ESTAB|FIN-WAIT-1|FIN-WAIT-2|LAST-ACK|LISTEN|SYN-RECV SYN-SENT|TIME-WAIT" && exit 1 tcp_status_fun(){ TCP_STAT=$1 ss -ant | awk 'NR>1 {++s[$1]} END {for(k in s) print k,s[k]}' > /etc/zabbix/zabbix_agentd.d/scripts/.ssn.txt TCP_STAT_VALUE=$(grep "$TCP_STAT" /etc/zabbix/zabbix_agentd.d/scripts/.ssn.txt | cut -d ' ' -f2) if [ -z "$TCP_STAT_VALUE" ];then TCP_STAT_VALUE=0 fi echo $TCP_STAT_VALUE } tcp_status_fun $1;
cat zbx_export_templates .xml
<?xml version="1.0" encoding="UTF-8"?> <zabbix_export> <version>4.2</version> <date>2019-07-11T11:38:15Z</date> <groups> <group> <name>Templates</name> </group> <group> <name>Templates/Applications</name> </group> <group> <name>Templates/Databases</name> </group> <group> <name>Templates/Operating systems</name> </group> </groups> <templates> <template> <template>Mt_Math_Centos_Template</template> <name>Mt_Math_Centos_Template</name> <description/> <groups> <group> <name>Templates/Operating systems</name> </group> </groups> <applications> <application> <name>CPU</name> </application> <application> <name>Disk</name> </application> <application> <name>Filesystems</name> </application> <application> <name>General</name> </application> <application> <name>Memory</name> </application> <application> <name>Network interfaces</name> </application> <application> <name>OS</name> </application> <application> <name>Performance</name> </application> <application> <name>Processes</name> </application> <application> <name>Security</name> </application> <application> <name>zabbix_agentd狀態</name> </application> </applications> <items> <item> <name>開啟檔案的最大數(sysctl.conf)</name> <type>7</type> <snmp_community/> <snmp_oid/> <key>kernel.maxfiles</key> <delay>1h</delay> <history>1w</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>It could be increased by using sysctrl utility or modifying file /etc/sysctl.conf.</description> <inventory_link>0</inventory_link> <applications> <application> <name>OS</name> </application> </applications> <valuemap/> <logtimefmt/> <preprocessing/> <jmx_endpoint/> <timeout>3s</timeout> <url/> <query_fields/> <posts/> <status_codes>200</status_codes> <follow_redirects>1</follow_redirects> <post_type>0</post_type> <http_proxy/> <headers/> <retrieve_mode>0</retrieve_mode> <request_method>0</request_method> <output_format>0</output_format> <allow_traps>0</allow_traps> <ssl_cert_file/> <ssl_key_file/> <ssl_key_password/> <verify_peer>0</verify_peer> <verify_host>0</verify_host> <master_item/> </item> <item> <name>系統允許的最大程序數</name> <type>7</type> <snmp_community/> <snmp_oid/> <key>kernel.maxproc</key> <delay>1h</delay> <history>1w</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>It could be increased by using sysctrl utility or modifying file /etc/sysctl.conf.</description> <inventory_link>0</inventory_link> <applications> <application> <name>OS</name> </application> </applications> <valuemap/> <logtimefmt/> <preprocessing/> <jmx_endpoint/> <timeout>3s</timeout> <url/> <query_fields/> <posts/> <status_codes>200</status_codes> <follow_redirects>1</follow_redirects> <post_type>0</post_type> <http_proxy/> <headers/> <retrieve_mode>0</retrieve_mode> <request_method>0</request_method> <output_format>0</output_format> <allow_traps>0</allow_traps> <ssl_cert_file/> <ssl_key_file/> <ssl_key_password/> <verify_peer>0</verify_peer> <verify_host>0</verify_host> <master_item/> </item> <item> <name>eth0入口流量</name> <type>7</type> <snmp_community/> <snmp_oid/> <key>net.if.in[eth0,bytes]</key> <delay>30s</delay> <history>30d</history> <trends>365d</trends> <status>0</status> <value_type>3</value_type> <allowed_hosts/> <units>B/s</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/> <inventory_link>0</inventory_link> <applications> <application> <name>Network interfaces</name> </application> </applications> <valuemap/> <logtimefmt/> <preprocessing> <step> <type>10</type> <params/> <error_handler>0</error_handler> <error_handler_params/> </step> </preprocessing> <jmx_endpoint/> <timeout>3s</timeout> <url/> <query_fields/> <posts/> <status_codes>200</status_codes> <follow_redirects>1</follow_redirects> <post_type>0</post_type> <http_proxy/> <headers/> <retrieve_mode>0</retrieve_mode> <request_method>0</request_method> <output_format>0</output_format> <allow_traps>0</allow_traps> <ssl_cert_file/> <ssl_key_file/> <ssl_key_password/> <verify_peer>0</verify_peer> <verify_host>0</verify_host> <master_item/> </item> <item> <name>eth1入口流量</name> <type>7</type> <snmp_community/> <snmp_oid/> <key>net.if.in[eth1,bytes]</key> <delay>30s</delay> <history>90d</history> <trends>365d</trends> <status>0</status> <value_type>3</value_type> <allowed_hosts/> <units>B/s</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/> <inventory_link>0</inventory_link> <applications> <application> <name>Network interfaces</name> </application> </applications> <valuemap/> <logtimefmt/> <preprocessing> <step> <type>10</type> <params/> <error_handler>0</error_handler> <error_handler_params/> </step> </preprocessing> <jmx_endpoint/> <timeout>3s</timeout> <url/> <query_fields/> <posts/> <status_codes>200</status_codes> <follow_redirects>1</follow_redirects> <post_type>0</post_type> <http_proxy/> <headers/> <retrieve_mode>0</retrieve_mode> <request_method>0</request_method> <output_format>0</output_format> <allow_traps>0</allow_traps> <ssl_cert_file/> <ssl_key_file/> <ssl_key_password/> <verify_peer>0</verify_peer> <verify_host>0</verify_host> <master_item/> </item> <item> <name>eth0出口流量</name> <type>7</type> <snmp_community/> <snmp_oid/> <key>net.if.out[eth0,bytes]</key> <delay>30s</delay> <history>90d</history> <trends>365d</trends> <status>0</status> <value_type>3</value_type> <allowed_hosts/> <units>B/s</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/> <inventory_link>0</inventory_link> <applications> <application> <name>Network interfaces</name> </application> </applications> <valuemap/> <logtimefmt/> <preprocessing> <step> <type>10</type> <params/> <error_handler>0</error_handler> <error_handler_params/> </step> </preprocessing> <jmx_endpoint/> <timeout>3s</timeout> <url/> <query_fields/> <posts/> <status_codes>200</status_codes> <follow_redirects>1</follow_redirects> <post_type>0</post_type> <http_proxy/> <headers/> <retrieve_mode>0</retrieve_mode> <request_method>0</request_method> <output_format>0</output_format> <allow_traps>0</allow_traps> <ssl_cert_file/> <ssl_key_file/> <ssl_key_password/> <verify_peer>0</verify_peer> <verify_host>0</verify_host> <master_item/> </item> <item> <name>eth1出口流量</name> <type>7</type> <snmp_community/> <snmp_oid/> <key>net.if.out[eth1,bytes]</key> <delay>30s</delay> <history>90d</history> <trends>365d</trends> <status>0</status> <value_type>3</value_type> <allowed_hosts/> <units>B/s</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/> <inventory_link>0</inventory_link> <applications> <application> <name>Network interfaces</name> </application> </applications> <valuemap/> <logtimefmt/> <preprocessing> <step> <type>10</type> <params/> <error_handler>0</error_handler> <error_handler_params/> </step> </preprocessing> <jmx_endpoint/> <timeout>3s</timeout> <url/> <query_fields/> <posts/> <status_codes>200</status_codes> <follow_redirects>1</follow_redirects> <post_type>0</post_type> <http_proxy/> <headers/> <retrieve_mode>0</retrieve_mode> <request_method>0</request_method> <output_format>0</output_format> <allow_traps>0</allow_traps> <ssl_cert_file/> <ssl_key_file/> <ssl_key_password/> <verify_peer>0</verify_peer> <verify_host>0</verify_host> <master_item/> </item> <item> <name>處於執行狀態的程序數</name> <type>7</type> <snmp_community/> <snmp_oid/> <key>proc.num[,,run]</key> <delay>1m</delay> <history>1w</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>Number of processes in running state.</description> <inventory_link>0</inventory_link> <applications> <application> <name>Processes</name> </application> </applications> <valuemap/> <logtimefmt/> <preprocessing/> <jmx_endpoint/> <timeout>3s</timeout> <url/> <query_fields/> <posts/> <status_codes>200</status_codes> <follow_redirects>1</follow_redirects> <post_type>0</post_type> <http_proxy/> <headers/> <retrieve_mode>0</retrieve_mode> <request_method>0</request_method> <output_format>0</output_format> <allow_traps>0</allow_traps> <ssl_cert_file/> <ssl_key_file/> <ssl_key_password/> <verify_peer>0</verify_peer> <verify_host>0</verify_host> <master_item/> </item> <item> <name>程序總數</name> <type>7</type> <snmp_community/> <snmp_oid/> <key>proc.num[]</key> <delay>1m</delay> <history>1w</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>Total number of processes in any state.</description> <inventory_link>0</inventory_link> <applications> <application> <name>Processes</name> </application> </applications> <valuemap/> <logtimefmt/> <preprocessing/> <jmx_endpoint/> <timeout>3s</timeout> <url/> <query_fields/> <posts/> <status_codes>200</status_codes> <follow_redirects>1</follow_redirects> <post_type>0</post_type> <http_proxy/> <headers/> <retrieve_mode>0</retrieve_mode> <request_method>0</request_method> <output_format>0</output_format> <allow_traps>0</allow_traps> <ssl_cert_file/> <ssl_key_file/> <ssl_key_password/> <verify_peer>0</verify_peer>