[监控基础]Shell查询 数据库/HTTP接口 数据 写入 HTML文件 发送邮件

数据库版

连接数据库,输入查询指令获取数据,写入文件,发送邮件

#!/bin/bash

timenow=`date +%Y%m%d%H`

filePath='./mail.html'

html_input(){
echo "<tr>
		<td>$1</td>
		<td>$2</td>
		<td>$3</td>
		<td>$4</td>
	</tr>" >> $filePath
}

set_info(){

echo "
<!DOCTYPE html>
<html lang='zh-CN'>
<head>
    <meta charset='UTF-8'>
    <meta http-equiv='content-type' content='text/html; charset=utf-8'>
    <title>DB info</title>
    <style type='text/css'>
        th, td{
            text-align:center;
            vertical-align:middle;
        }
    </style>
</head>
<body>
    <table border='1' width='500'>
      <tr>
        <th>Tradetime</th>
        <th>Uin</th>
        <th>Fofferid</th>
        <th>ErrMsg</th>
      </tr>"> $filePath

query_total_num_sql="select * from database.table;"

res=$(/usr/bin/mysql -h服务器地址 -u账号 -p密码 -P端口 -s -e "${query_total_num_sql}");

echo $res|while read  line 
    do
        str1=`echo $line |awk -F '"' '{print $1}'`
        str2=`echo $line |awk -F '"' '{print $2}'`
        str3=`echo $line |awk -F '"' '{print $3}'`
        str4=`echo $line |awk -F '"' '{print $4}'`
        html_input $str1 $str2 $str3 $str4
      done
echo "</table></body>" >> $filePath
}

set_info

if [ 1 -gt 0 ]; then
    sendmail2 'hyperdai' 'hyperdai' '' test $filePath --html
fi

Http接口版

获取Json数据,并分解解析,写文件

#!/bin/bash

timenow=`date +%Y%m%d%H`

filePath='./mail.html'

html_input(){
echo "<tr>
		<td>$1</td>
		<td>$2</td>
		<td>$3</td>
		<td>$4</td>
	</tr>" >> $filePath
}

set_info(){

echo "
<!DOCTYPE html>
<html lang='zh-CN'>
<head>
    <meta charset='UTF-8'>
    <meta http-equiv='content-type' content='text/html; charset=utf-8'>
    <title>DB info</title>
    <style type='text/css'>
        th, td{
            text-align:center;
            vertical-align:middle;
        }
    </style>
</head>
<body>
    <table border='1' width='500'>
      <tr>
        <th>Tradetime</th>
        <th>Uin</th>
        <th>Fofferid</th>
        <th>ErrMsg</th>
      </tr>"> $filePath

res=`curl '你的网址'`

echo $res|tr '[]' '\n'|tr  '}' '\n'|grep 'statis_date'|while read  line 
    do
        str1=`echo $line |awk -F '"' '{print $4}'`
        str2=`echo $line |awk -F '"' '{print $8}'`
        str3=`echo $line |awk -F '"' '{print $12}'`
        str4=`echo $line |awk -F '"' '{print $16}'`
        html_input $str1 $str2 $str3 $str4
      done
echo "</table></body>" >> $filePath
}

set_info

if [ 1 -gt 0 ]; then
    sendmail2 'hyperdai' 'hyperdai' test $filePath --html
fi

效果

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/deaidai/article/details/92090680