Configure HAProxy Template on ZABBIX 4
ZABBIX 4 에 HAProxy 템플릿 설정
- HAProxy 2.0 출시 후 변경 내용이 있어 일부 수정 사항이 추가되었습니다.
HAProxy 에 대한 내용은 HAProxy 설치 가이드 포스팅을 참고해 주시기 바랍니다. 이 가이드는 sergiotocalini (Sergio Tocalini Joerg) · GitHub 페이지에서 ZABBIX 용 HAProxy Template 인 habbixy 를 참고하여 작성되었으며 일부 수정 사항을 포함 합니다.
서버 설정
의존성 패키지 설치
[root@172-19-11-6 /]# yum install -y ksh
스크립트 디렉토리 생성
[root@172-19-11-6 /]# mkdir -p /etc/zabbix/bin/habbixy/var
map.index 파일 생성
: vi /etc/zabbix/bin/habbixy/map.index
# index:name:default 1:pxname:@ 2:svname:@ 3:qcur:0 4:qmax:0 5:scur:0 6:smax:0 7:slim:1 8:stot:@ 9:bin:0 10:bout:0 11:dreq:0 12:dresp:0 13:ereq:0 14:econ:0 15:eresp:0 16:wretr:0 17:wredis:0 18:status:UNK 19:weight:0 20:act:0 21:bck:0 22:chkfail:0 23:chkdown:0 24:lastchg:0 25:downtime:0 26:qlimit:0 27:pid:@ 28:iid:@ 29:sid:@ 30:throttle:0 31:lbtot:0 32:tracked:0 33:type:0 34:rate:0 35:rate_lim:@ 36:rate_max:@ 37:check_status:@ 38:check_code:@ 39:check_duration:0 40:hrsp_1xx:@ 41:hrsp_2xx:@ 42:hrsp_3xx:@ 43:hrsp_4xx:@ 44:hrsp_5xx:@ 45:hrsp_other:@ 46:hanafail:@ 47:req_rate:0 48:req_rate_max:@ 49:req_tot:0 50:cli_abrt:0 51:srv_abrt:0 52:comp_in:0 53:comp_out:0 54:comp_byp:0 55:comp_rsp:0 56:lastsess:0 57:last_chk:@ 58:last_agt:@ 59:qtime:0 60:ctime:0 61:rtime:0 62:ttime:0
스크립트 파일 생성
: vi /etc/zabbix/bin/habbixy/habbixy.sh
#!/usr/bin/env ksh PATH=/usr/local/bin:${PATH} IFS_DEFAULT=${IFS} ################################################################################# ################################################################################# # # Variable Definition # --------------------- # APP_NAME=$(basename $0) APP_DIR=$(dirname $0) APP_VER="0.0.1" APP_WEB="http://www.sergiotocalini.com.ar/" APP_TIMESTAMP=`date '+%s'` APP_MAP_INDEX=${APP_DIR}/map.index HAPROXY_CONFIG="/etc/haproxy/haproxy.cfg" # haproxy.cfg 경로 HAPROXY_SOCKET="/var/run/haproxy.sock" # haproxy.sock 경로 (haproxy.cfg에 명시) HAPROXY_CACHE_DIR=${APP_DIR}/var HAPROXY_CACHE_TTL=5 # IN MINUTES # ################################################################################# ################################################################################# # # Load Environment # ------------------ # [[ -f ${APP_DIR}/${APP_NAME%.*}.conf ]] && . ${APP_DIR}/${APP_NAME%.*}.conf # ################################################################################# ################################################################################# # # Function Definition # --------------------- # usage() { echo "Usage: ${APP_NAME%.*} [Options]" echo "" echo "Options:" echo " -a Query arguments." echo " -h Displays this help message." echo " -j Jsonify output." echo " -s ARG(str) Section (default=stat)." echo " -v Show the script version." echo "" echo "Please send any bug reports to sergiotocalini@gmail.com" exit 1 } version() { echo "${APP_NAME%.*} ${APP_VER}" exit 1 } check_params() { [[ -d ${HAPROXY_CACHE_DIR} ]] || mkdir -p ${HAPROXY_CACHE_DIR} } refresh_cache() { type=${1:-'stat'} file=${HAPROXY_CACHE_DIR}/${type}.cache if [[ $(( `stat -c '%Y' "${file}"`+60*${HAPROXY_CACHE_TTL} )) -le ${APP_TIMESTAMP} ]]; then echo "show ${type}" | sudo socat ${HAPROXY_SOCKET} stdio 2>/dev/null > ${file} fi echo "${file}" return 0 } discovery() { svname=${1} if [[ ${svname} =~ (BACKEND|FRONTEND) ]]; then cache=$(refresh_cache 'stat') for item in `cat ${cache} | awk -F"," '$2 ~ /^'${svname}'$/{print}' | cut -d, -f1 | uniq`; do echo ${item} done elif [[ ${svname} == "certs" ]]; then discovery_certs fi } ifArrayHas() { item=${1} shift array=( "${@}" ) for i in ${!array[@]}; do [[ ${array[${i}]} == ${item} ]] && return 0 done return 1 } discovery_certs() { while read line; do IFS=" " params=( ${line} ) IFS=${IFS_DEFAULT} for idx in ${!params[@]}; do if [[ ${params[${idx}]} == 'crt' ]]; then if ! ifArrayHas "${params[$((${idx}+1))]}" "${crt[@]}"; then if [[ -f "${params[$((${idx}+1))]}" ]]; then crt[${#crt[@]}]="${params[$((${idx}+1))]}" fi fi elif [[ ${params[${idx}]} == 'crt-list' ]]; then if ! ifArrayHas "${params[$((${idx}+1))]}" "${crt_list[@]}"; then if [[ -f "${params[$((${idx}+1))]}" ]]; then crt_list[${#crt_list[@]}]="${params[$((${idx}+1))]}" fi fi fi done done < <(grep -E "(^|\s)bind($|\s)" ${HAPROXY_CONFIG} | grep -E " (crt|crt-list) " | awk '{$1=$1};1') for idx in ${!crt_list[@]}; do while read cert; do if ! ifArrayHas "${cert}" "${crt[@]}"; then if [[ -f "${cert}" ]]; then crt[${#crt[@]}]="${cert}" fi fi done < <(cat ${crt_list[${idx}]}) done printf '%s\n' ${crt[@]} } get_cert() { file="${1}" attr="${2}" [ -f ${file} ] || return 1 if [[ ${attr} == 'expires' ]]; then after=`sudo openssl x509 -noout -in ${file} -enddate 2>/dev/null|cut -d'=' -f2` res=$((($(date -d "${after}" +'%s') - $(date +'%s'))/86400)) fi echo "${res:-0}" return 0 } get_cert_text() { crt_file="${1}" [[ -f ${crt_file} ]] || return 1 openssl x509 -noout -in ${crt_file} -text return 0 } get_stat() { pxname=${1} svname=${2} stats=${3} cache=$(refresh_cache 'stat') _STAT=`grep :${stats}: ${APP_MAP_INDEX}` _INDEX=${_STAT%%:*} _DEFAULT=${_STAT##*:} _res="`grep \"${pxname},${svname}\" \"${cache}\" 2>/dev/null`" _res="$(echo $_res | cut -d, -f ${_INDEX})" if [ -z "${_res}" ] && [[ "${_DEFAULT}" != "@" ]]; then echo "${_DEFAULT}" else echo "${_res}" fi } get_info() { attr=${1} cache=$(refresh_cache 'info') _res="`grep -E \"^${attr}:\" \"${cache}\" 2>/dev/null | cut -d: -f 2`" echo "${_res:-0}" } # ################################################################################# ################################################################################# while getopts "s::a:s:uphvj:" OPTION; do case ${OPTION} in h) usage ;; s) SECTION="${OPTARG}" ;; j) JSON=1 IFS=":" JSON_ATTR=(${OPTARG}) IFS=${IFS_DEFAULT} ;; a) ARGS[${#ARGS[*]}]=${OPTARG//p=} ;; v) version ;; \?) exit 1 ;; esac done if [[ ${JSON} -eq 1 ]]; then rval=$(discovery ${ARGS[*]}) echo '{' echo ' "data":[' count=1 while read line; do IFS="|" values=(${line}) output='{ ' for val_index in ${!values[*]}; do output+='"'{#${JSON_ATTR[${val_index}]}}'":"'${values[${val_index}]}'"' if (( ${val_index}+1 < ${#values[*]} )); then output="${output}, " fi done output+=' }' if (( ${count} < `echo ${rval}|wc -l` )); then output="${output}," fi echo " ${output}" let "count=count+1" done <<< ${rval} echo ' ]' echo '}' else if [[ ${SECTION} == 'stat' ]]; then rval=$( get_stat ${ARGS[*]} ) rcode="${?}" elif [[ ${SECTION} == 'info' ]]; then rval=$( get_info ${ARGS[*]} ) rcode="${?}" elif [[ ${SECTION} == 'certs' ]]; then rval=$( get_cert ${ARGS[*]} ) fi echo ${rval:-0} fi exit ${rcode}
추가사항 : haproxy 2.0 이상 버전에서 디스커버리 데이타가 중복이라 나오면서 오류가 발생하면 67번 라인을 아래와 같이 수정해 주시기 바랍니다.
echo "@1 show ${type}" | sudo socat ${HAPROXY_SOCKET} stdio 2>/dev/null > ${file}
UserParameter 파일추가
: vi /etc/zabbix/zabbix_agentd.conf.d/haproxy.conf
UserParameter=habbixy[*],/etc/zabbix/scripts/agentd/habbixy/habbixy.sh -s "$1" -a p="$2" -a p="$3" -a p="$4" -a p="$5" UserParameter=habbixy.discovery[*],/etc/zabbix/scripts/agentd/habbixy/habbixy.sh -a p="$1" -j "$2" -a p="$3" UserParameter=habbixy.version,/etc/zabbix/scripts/agentd/habbixy/habbixy.sh -v short
자빅스 재시작
[root@172-19-11-6 /]# systemctl restart zabbix_agentd
ZABBIX Frontend 설정
템플릿 파일 임포트
Habbixy Template URL
host에 template 적용
적용 후 그래프 확인 (디스커버리 시간문제로 오래 기다리셔야 합니다.)