Bootstrap

shell脚本执行mysql语句,将执行结果发送到钉钉报警群

shell中执行mysql语句,将执行结果发送到报警群

#!/bin/bash
redLine="0"
echo " 警戒线: $redLine"
libraryHost="你的数据库地址"
libraryDB="mysql -u你的用户名 -p你的密码 -h${libraryHost} --default-character-set=utf8 -A -N"

crmHost="你的数据库地址"
crmDB="mysql -uwww -p你的密码 -h${crmHost} --default-character-set=utf8 -A -N"

offset="0 1 2 3"
limit=40
for o in ${offset[@]}
do
    taskAssignRelationQuery="SELECT DISTINCT(userId) FROM crm.task_assistant_relation WHERE createTime>='2020-04-01 00:00:00' limit $tmpOffset, $limit;"
    taskAssignRelationResult="$($crmDB -e "$taskAssignRelationQuery")"
    taskRecordIdsByAssignQuery="SELECT a.userTaskRecordId FROM crm.task_assistant_relation a WHERE a.userId=uid;"
    #insight
    assignNameQuery="SELECT user_name FROM insight.p_user WHERE user_id=uid;"
    assignEmailQuery="SELECT user_login_name FROM insight.p_user WHERE user_id=uid;"
    #library
    noModifyCountQuery="SELECT COUNT(1) FROM library.course_user_task_record WHERE modify_status=0 AND id IN(rids);"
    content=""
    for i in ${taskAssignRelationResult[@]}
    do
         #助教名字
         query1="${assignNameQuery/uid/$i}"
         assignNameResult="$($crmDB -e "$query1")"
         query3="${assignEmailQuery/uid/$i}"
         assignEmailResult="$($crmDB -e "$query3")"
         echo $assignEmailResult

         query2=${taskRecordIdsByAssignQuery/uid/$i}
         taskRecordIdsByAssignResult="$($crmDB -e "$query2")"
         if [ ${#taskRecordIdsByAssignResult[@]} = "0" ]
         then
             content2="助教老师:     $assignNameResult \n还没有分配作业"
             curl "https://oapi.dingtalk.com/robot/send?access_token=你的token" -H 'Content-Type:application/json' -d "{\"msgtype\":\"text\",\"text\":{\"content\":\"$content2\"}}"
             continue
         fi
         array=($taskRecordIdsByAssignResult)
         len=${#array[@]}
         echo "task record id size: $len"

         taskRecordIds=""
         for rid in ${taskRecordIdsByAssignResult[@]}
         do
            taskRecordIds="$taskRecordIds,$rid"
         done
         taskRecordIds=${taskRecordIds/,/""}
         query3=${noModifyCountQuery/rids/$taskRecordIds}
         echo $query3
         noModifyCountResult=$($libraryDB -e "$query3")
         echo $noModifyCountResult
         if [ $noModifyCountResult = "0" ]
         then
             continue
         fi
         emptyString=""
         if [ "$content"  == "$emptyString" ];then
            content="助教老师:($assignNameResult) 已分配作业数量: ($len) 未批改数量: ($noModifyCountResult) \n"
         else
            content="$content 助教老师:($assignNameResult) 已分配作业数量: ($len) 未批改数量: ($noModifyCountResult) \n"
         fi
    done
    curl "https://oapi.dingtalk.com/robot/send?access_token=你的token" -H 'Content-Type:application/json' -d "{\"msgtype\":\"text\",\"text\":{\"content\":\"$content\"}}"
done

noAuditCourseQuery="SELECT * FROM (SELECT DISTINCT(b.name), 'ljx' char1, COUNT(1) count, '#' char2 FROM library.course_user_task_record a JOIN library.course_lesson b ON a.lesson_id=b.id WHERE a.modify_status=2 AND b.course_type_id IN(3,8) GROUP BY b.name) a ORDER BY count desc;"
noAuditCourseResult=$($libraryDB -e "$noAuditCourseQuery")
echo $noAuditCourseResult
noAuditCourseResult=${noAuditCourseResult//[ ,  ,
]/}
echo $noAuditCourseResult
OLD_IFS="$IFS"
IFS="#"
noAuditCourseArray=($noAuditCourseResult)
content2=""
allNoAuditCount=0
for nac in ${noAuditCourseArray[@]}
do
    IFS="ljx"
    dataArray=($nac)
    IFS="$OLD_IFS"
    desc=""
    for index in " ${!dataArray[@]}"
    do
        if [ $index -eq 0 ];then
           desc="课程:  【${dataArray[0]}】"
        elif [ $index -eq 1 ];then
           desc="$desc  未审核数量:     ${dataArray[3]}"
           count=${dataArray[3]}
           allNoAuditCount=$((allNoAuditCount+count))
        fi
    done
    emptyString=""
    if [ "$content2"  == "$emptyString" ];then
        content2="$content2$desc"
    else
        content2="$content2 \n$desc"
    fi
done
content2="$content2 \n未审核总量:       $allNoAuditCount"
curl "https://oapi.dingtalk.com/robot/send?access_token=你的token" -H 'Content-Type:application/json' -d "{\"msgtype\":\"text\",\"text\":{\"content\":\"$content2\"}}"

 

;