El script de shell cuenta los datos de la tabla mysql, los convierte al formato influx y los informa para su monitoreo.

El script de shell es el siguiente

#! /bin/sh
resultSum=$(mysql -h 10.87.18.20 -P 3306 -uroot -ptest@1234 -Dmonitor -e "SELECT stability_result.ip_addr,
count(case when stability_result.run_result = 0 then 0 end) AS '成功总数',
count(case when stability_result.run_result != 0 then 0 end) AS '失败总数',
count(case when stability_result.run_result = 101 then 101 end) AS '登录失败',
count(case when stability_result.run_result IN (103,102) then 103 end) AS '应用:访问应用失败',
count(case when stability_result.run_result IN (105,104) then 105 end) AS '应用:上传下载失败'
FROM stability_result GROUP BY stability_result.ip_addr" |awk 'NR == 1 {next} {print}')

#echo $resultSum >/opt/categraf/winsum.txt
#result=`cat /opt/categraf/winsum.txt`
flag=1
echo -n "" > /opt/categraf/winsum.txt
for v in ${resultSum[@]}
do
# 输出样例:atrustMonitor,winIp=10.87.16.4 successSum=5752,failSum=30,failLoginSum=1,failVisitSum=29,failUpdownSum=25
    if [ $(( $flag % 6 )) == 1 ]
    then
        echo -n "atrustMonitor,winIp="$v >> /opt/categraf/winsum.txt
    fi
    if [ $(( $flag % 6 )) == 2 ]
    then
        echo -n " successSum="$v >> /opt/categraf/winsum.txt
    fi
    if [ $(( $flag % 6 )) == 3 ]
    then
        echo -n ",failSum="$v >> /opt/categraf/winsum.txt
    fi
    if [ $(( $flag % 6 )) == 4 ]
    then
        echo -n ",failLoginSum="$v >> /opt/categraf/winsum.txt
    fi
    if [ $(( $flag % 6 )) == 5 ]
    then
        echo -n ",failVisitSum="$v >> /opt/categraf/winsum.txt
    fi
    if [ $(( $flag % 6 )) == 0 ]
    then
        echo ",failUpdownSum="$v >> /opt/categraf/winsum.txt
    fi
    flag=`expr $flag + 1`
done
resultSum1=$(mysql -h 10.87.18.20 -P 3306 -uroot -ptest@1234 -Dmonitor -e "SELECT count(*) 
from (SELECT stability_result.ip_addr,count(*) from stability_result GROUP BY stability_result.ip_addr) AS newtable" |awk 'NR == 1 {next} {print}')
echo "atrustWinCount winCount="$resultSum1 >> /opt/categraf/winsum.txt

Estadísticas de datos bajo cada condición:

SELECT stability_result.ip_addr,
count(case when stability_result.run_result = 0 then 0 end) AS '成功总数',
count(case when stability_result.run_result != 0 then 0 end) AS '失败总数',
count(case when stability_result.run_result = 101 then 101 end) AS '登录失败',
count(case when stability_result.run_result IN (103,102) then 103 end) AS '应用:访问应用失败',
count(case when stability_result.run_result IN (105,104) then 105 end) AS '应用:上传下载失败'
FROM stability_result GROUP BY stability_result.ip_addr

Sintaxis de la declaración SQL de ejecución de Shell:

mysql -h 10.87.18.20 -P 3306 -uroot -ptest@1234 -D数据库名称 -e “sql语句”

Supongo que te gusta

Origin blog.csdn.net/Mr_wilson_liu/article/details/129188083
Recomendado
Clasificación