シェルスクリプトは以下の通り
#! /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
各条件におけるデータの統計:
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
シェル実行 SQL ステートメントの構文:
mysql -h 10.87.18.20 -P 3306 -uroot -ptest@1234 -D数据库名称 -e “sql语句”