自动化运维

system_check.sh

#!/bin/bash

#写html样式 完成度 100%
function start_html(){
cat > echo.html << EOF
<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
<script language="JavaScript">
setTimeout(function(){location.reload()},60000); //指定60秒刷新一次
</script>
        <title>自动化运维</title>
        <style type="text/css">
            * {
               margin: 0;
               padding: 0;
               }

            #parent >li> span{background: #999999;display: block;width: 1900px;border:2px solid #ECEEF2;}
            li {line-height: 50px;display: block;}
            li  p{
                    display: inline-block;
                    width: 0px;
                    height: 0px;
                    border-left: 5px solid transparent;
                    border-right: 5px solid transparent;
                    border-top: 5px solid#2f2f2f;
                      }
             li>ul{display: none;}
             li>ul>li{border: 1px solid #DEDEDE;width: 1999px;}
             #parent span:hover + ul{display: block;}
             #parent span:hover >p{
                     display: inline-block;
                    width: 0px;
                    height: 0px;
                    border-top: 5px solid transparent;
                    border-bottom: 5px solid transparent;
                    border-left: 5px solid#2f2f2f;}
        </style>
    </head>
    <body>
        <ul id="parent">
EOF
}
start_html 


#i系统信息 完成度 100%
function system_ip(){
touch system_ip.txt
ip a |\
grep "global" |\
awk '{print $2}' |\
awk -F/ '{print "<li>系统信息 服务器ip "$1"</li>"}' > system_ip.txt
if [ ! -f "pub_ip.txt" ];then
curl -s ifconfig.me > pub_ip.txt
fi
}
function system_disk(){
df -hT | awk '{print "<li>系统信息 服务器磁盘 "$0"</li>"}' > system_disk.txt
}
function system_top(){
top -n 1 | awk '{print "<li>系统信息 服务器top "$0"</li>"}' | sed "s/                                       //g" > system_top.txt
}
function system_info_html(){
#system_ip
#system_disk
#system_top
echo "<li>" >> echo.html
    echo "<span>" >> echo.html
        echo "`cat pub_ip.txt` 主机 `hostname` 系统信息  `cat /var/log/check_user_history.log | wc -l` 条" >> echo.html
    echo "</span>" >> echo.html
        echo "<ul>" >> echo.html
            cat system_ip.txt   >> echo.html
            cat system_disk.txt >> echo.html
            cat system_top.txt  >> echo.html
            tac /var/log/check_user_history.log                                                       > ssh_history.txt
            while read line ; do echo "<li>系统信息 ssh登录操作记录 $line </li>"  >> echo.html ; done < ssh_history.txt
        echo "</ul>" >> echo.html
echo "</li>" >> echo.html    
}
system_info_html

#宝塔安全检查 完成度 100%
function bt_check(){
touch bt_check.txt
if [ ! -d "/www/server/panel/data/warning/result/" ];then
    echo "<li>宝塔安全检查 报错 路径 /www/server/panel/data/warning/result/ 丢失 </li>"
else
    for i in `ls /www/server/panel/data/warning/result/`; 
    do 
        cat /www/server/panel/data/warning/result/$i |\
        grep "false" |\
        jq |\
        awk -F"\"" '{print $2}' |\
        grep -v ^$ |\
        awk '{print "<li>宝塔安全检查 "$0"</li>"}'
    done 
fi
}
function bt_check_html(){
bt_check > bt_check_html.txt
echo "<li>" >> echo.html
    echo "<span>" >> echo.html
        echo "`cat pub_ip.txt` 主机 `hostname` 宝塔安全检查 `cat bt_check_html.txt | wc -l` 条" >> echo.html
    echo "</span>" >> echo.html
        echo "<ul>" >> echo.html
            cat bt_check_html.txt >> echo.html
        echo "</ul>" >> echo.html
echo "</li>" >> echo.html
}
bt_check_html

#宝塔操作日志 完成度 100%
function bt_history(){
touch bt_history.txt
if [ ! -f "/www/server/panel/data/default.db" ];then
    echo "<li>宝塔操作日志数据库 报错 /www/server/panel/data/default.db 丢失 </li>"
else
    tail /www/server/panel/data/default.db | sed -b "s/'/\n/g"          > bt_history.txt
    while read line ; do echo "<li>宝塔操作日志 $line </li>" ; done     < bt_history.txt
fi
}
function bt_history_html(){
bt_history > bt_history_html.txt
echo "<li>" >> echo.html
    echo "<span>" >> echo.html
        echo "`cat pub_ip.txt` 主机 `hostname` 宝塔操作日志 `cat bt_history.txt | wc -l` 条" >> echo.html
    echo "</span>" >> echo.html
        echo "<ul>" >> echo.html
            tac bt_history_html.txt >> echo.html
        echo "</ul>" >> echo.html
echo "</li>" >> echo.html
}
bt_history_html

#宝塔web登录 完成度 100%
function bt_login(){
if [ ! -f "bt_history_html.txt" ];then
    echo "<li>宝塔 bt_history 函数未运行 报错 bt_history_html.txt 丢失 </li>"
else
    tac bt_history_html.txt | grep 登录成功
fi
}
function bt_login_html(){
bt_login > bt_login_html.txt
echo "<li>" >> echo.html
    echo "<span>" >> echo.html
        echo "`cat pub_ip.txt` 主机 `hostname` 宝塔后台web登录成功 `cat bt_login_html.txt | wc -l` 条" >> echo.html
    echo "</span>" >> echo.html
        echo "<ul>" >> echo.html    
            cat bt_login_html.txt >> echo.html
        echo "</ul>" >> echo.html
echo "</li>" >> echo.html
}
bt_login_html

#宝塔终端成功登录到SSH服务器 完成度 100%
function bt_login_ssh(){
if [ ! -f "bt_history_html.txt" ];then
    echo "<li>宝塔 bt_history 函数未运行 报错 bt_history_html.txt 丢失 </li>"
else
    tac bt_history_html.txt | grep 宝塔终端成功登录到SSH服务器
fi
}
function bt_login_ssh_html(){
bt_login_ssh > bt_login_ssh_html.txt
echo "<li>" >> echo.html
    echo "<span>" >> echo.html
        echo "`cat pub_ip.txt` 主机 `hostname` 宝塔终端成功登录到SSH服务器 `cat bt_login_ssh_html.txt | wc -l` 条" >> echo.html
    echo "</span>" >> echo.html
        echo "<ul>" >> echo.html    
            cat bt_login_ssh_html.txt >> echo.html
        echo "</ul>" >> echo.html
echo "</li>" >> echo.html
}
bt_login_ssh_html


#宝塔 nginx 已配置域名 完成度 100%
function bt_nginx_www(){
touch bt_nginx_www.txt
if [ ! -d "/www/server/panel/vhost/nginx/" ];then
    echo "<li>宝塔 nginx 未配置域名</li>" 
else
    find /www/server/panel/vhost/nginx/ -name "*.conf" |\
    grep -Ev "*default.conf" |\
    xargs cat |\
    grep server_name |\
    grep -Ev "#" |\
    grep -Ev "127.0.0.1" |\
    sed 's/server_name/ /g' |\
    sed 's/;/ /g' |\
    sed "s/<\/li>//g" |\
    sed 's/ /\n/g ' |\
    sed '/^\s*$/d' | \
    awk '{print "<li> 宝塔 nginx 已配置域名 "$0"</li>"}'
fi
}
function bt_nginx_www_html(){
bt_nginx_www > bt_nginx_www.txt
echo "<li>" >> echo.html
    echo "<span>" >> echo.html
        echo "`cat pub_ip.txt` 主机 `hostname` 宝塔 nginx 已配置域名 `cat bt_nginx_www.txt | wc -l` 条" >> echo.html
    echo "</span>" >> echo.html
        echo "<ul>" >> echo.html
            cat bt_nginx_www.txt >> echo.html
        echo "</ul>" >> echo.html
echo "</li>" >> echo.html
}
bt_nginx_www_html

#宝塔 apache 已配置域名 完成度 100%
function bt_apache_www(){
touch bt_apache_www.txt
if [ ! -d "/www/server/panel/vhost/apache/" ];then
    echo "<li>宝塔 apache 未配置域名</li>"
else
    find /www/server/panel/vhost/apache/ -name "*.conf" |\
    grep -Ev "*default.conf" |\
    xargs cat |\
    grep ServerAlias |\
    awk '{print $2}' |\
    sed 's/ /\n/g ' |\
    sed '/^\s*$/d' |\
    awk '{print "<li> 宝塔 apache 已配置域名 "$0"</li>"}'
fi
}
function bt_apache_www_html(){
bt_apache_www > bt_apache_www.txt
echo "<li>" >> echo.html
    echo "<span>" >> echo.html
        echo "`cat pub_ip.txt` 主机 `hostname` 宝塔 apache 已配置域名 `cat bt_apache_www.txt | wc -l` 条" >> echo.html
    echo "</span>" >> echo.html
        echo "<ul>" >> echo.html
            cat bt_apache_www.txt >> echo.html
    echo "</ul>" >> echo.html
echo "</li>" >> echo.html
}
bt_apache_www_html

#宝塔域名错误日志 完成度 100%
function bt_wwwlog_error(){
touch bt_wwwlog_error.txt
if [ ! -d "/www/wwwlogs/" ];then
    echo "<li>域名错误日志!!! 报错 路径 /www/wwwlogs/ 丢失 </li>"
else
find /www/wwwlogs/ -mtime -1 -type f -name "*.error.log"                            > bt_wwwlog_error.txt
while read line ; do echo "<li>错误日志!!! $line 内容 `tail -1 $line` </li>" ; done < bt_wwwlog_error.txt
fi
}
function bt_wwwlog_error_html(){
echo "<li>" >> echo.html
    echo "<span>" >> echo.html
        echo "`cat pub_ip.txt` 主机 `hostname` 域名错误日志!!! `cat bt_wwwlog_error.txt | wc -l` 条" >> echo.html
    echo "</span>" >> echo.html
        echo "<ul>" >> echo.html
            bt_wwwlog_error >> echo.html
        echo "</ul>" >> echo.html
echo "</li>" >> echo.html
}
bt_wwwlog_error_html

#宝塔 mysql 慢日志 完成度 100%
function bt_mysql_slow(){
touch bt_mysql_slow.txt
if [ ! -d "www/server/data" ];then
    echo "<li>mysql-slow.log 报错 路径 www/server/data 丢失 </li>"
else
    find /www/server/data/ -mmin -60 -type f -name "mysql-slow.log"                               > bt_mysql_slow.txt
    while read line ; do echo "<li>mysql-slow.log 慢日志!!! $line 内容 `tail $line` </li>" ; done < bt_mysql_slow.txt
fi
}
function bt_mysql_slow_html(){
bt_mysql_slow > bt_mysql_slow_html.txt
echo "<li>" >> echo.html
    echo "<span>" >> echo.html
        echo "`cat pub_ip.txt` 主机 `hostname` mysql-slow.log 慢日志 `cat bt_mysql_slow.txt | wc -l` 条" >> echo.html
    echo "</span>" >> echo.html
        echo "<ul>" >> echo.html
             cat bt_mysql_slow_html.txt >> echo.html
        echo "</ul>" >> echo.html
echo "</li>" >> echo.html
}
bt_mysql_slow_html

#宝塔域名日志 完成度 100%
function bt_wwwlog(){
touch bt_wwwlog.txt
if [ ! -d "/www/wwwlogs/" ];then
    echo "<li>网站正常日志 报错 路径 /www/wwwlogs/ 丢失 </li>"
else
    find /www/wwwlogs/ -mmin -1 -type f -name "*.log" | grep -v "error.log"          > bt_wwwlog.txt
    while read line ; do echo "<li>正常日志 $line 内容 `tail -1 $line` </li>" ; done < bt_wwwlog.txt
fi
}
function bt_wwwlog_html(){
bt_wwwlog > bt_wwwlog_html.txt
echo "<li>" >> echo.html
    echo "<span>" >> echo.html
        echo "`cat pub_ip.txt` 主机 `hostname` 网站正常日志 `cat bt_wwwlog.txt | wc -l` 条" >> echo.html
    echo "</span>" >> echo.html
        echo "<ul>" >> echo.html
            cat bt_wwwlog_html.txt >> echo.html
        echo "</ul>" >> echo.html
echo "</li>" >> echo.html
}
bt_wwwlog_html

#系统核心配置文件监控 完成度 100%
function etc_change(){
touch change_etc.txt
if [ ! -d "/etc/" ];then
    echo "<li>系统核心配置文件 报错 路径 /etc/ 丢失 </li>"
else
    find /etc/ -mtime -7 -type f -name "*.conf"                                 > change_etc.txt
    while read line ; do echo "<li>7天内变动过的系统配置文件 $line</li>" ; done < change_etc.txt
fi
}
function etc_change_html(){
etc_change > etc_change_html.txt
echo "<li>" >> echo.html
    echo "<span>" >> echo.html
        echo "`cat pub_ip.txt` 主机 `hostname` 系统核心配置文件变动 `cat change_etc.txt | wc -l` 条" >> echo.html
    echo "</span>" >> echo.html
        echo "<ul>" >> echo.html
            cat etc_change_html.txt >> echo.html
        echo "</ul>" >> echo.html
echo "</li>" >> echo.html
}
etc_change_html

#定时任务列表 完成度 100%
function crontab_list(){
touch crontab_list.txt
crontab -l                                         > crontab_list.txt
while read line ; do echo "<li> $line</li>" ; done < crontab_list.txt
}
function crontab_list_html(){
crontab_list > crontab_list_html.txt
echo "<li>" >> echo.html
    echo "<span>" >> echo.html
        echo "`cat pub_ip.txt` 主机 `hostname` 定时任务crontab `cat crontab_list.txt | wc -l` 条" >> echo.html
    echo "</span>" >> echo.html
        echo "<ul>" >> echo.html
            cat crontab_list_html.txt >> echo.html
        echo "</ul>" >> echo.html
echo "</li>" >> echo.html
}
crontab_list_html

#网站 SSL 证书 完成度 100%   
function www_ssl(){
touch www_ssl.txt
if [ ! -f ssl-cert-check ];then
    wget https://raw.githubusercontent.com/Matty9191/ssl-cert-check/master/ssl-cert-check
    chmod +x ssl-cert-check
fi

if [ ! -d "/www/server/panel/vhost/cert/" ];then
    echo "<li>网站SSL 报错 路径 /www/server/panel/vhost/cert/ 丢失 </li>"
else
    for i in `tree -fi /www/server/panel/vhost/cert/ | grep full` ; do bash ssl-cert-check -c $i ; done > www_ssl.txt
    while read line ; do echo "<li> $line </li>" ; done                                                 < www_ssl.txt
fi
}
function www_ssl_html(){
www_ssl > www_ssl_html.txt
cat www_ssl.txt |\
grep FILE |\
awk '{print "<li>"$1"     签发机构"$2"  签发时间 "$3" "$4" "$5" 到期天数 "$6"</li>"}' > www_ssl_html.txt

echo "<li>" >> echo.html
    echo "<span>" >> echo.html
        echo "`cat pub_ip.txt` 主机 `hostname` 证书SSL `cat www_ssl.txt | grep FILE | wc -l` 条" >> echo.html
    echo "</span>" >> echo.html
        echo "<ul>" >> echo.html
            cat www_ssl_html.txt >> echo.html
        echo "</ul>" >> echo.html
echo "</li>" >> echo.html
}
www_ssl_html

#闭合html 完成度 100%
function end_html(){
echo '</body>' >> echo.html
echo '</html>' >> echo.html
}
end_html


##宝塔bot报警
function telegram_bot(){
PUB_IP="$(curl -s ifconfig.me)"

NOW=$(date "+%Y-%m-%d %H:%M:%S")
ERROR="$1 主机 `hostname`时间 $NOW `cat pub_ip.txt`/linux/echo.html"
curl -s \
  -X POST \
  -F "chat_id=-446223495" \
  -F "text=${ERROR}" \
  -o /dev/null --silent \
  -w %{http_code} \
  "https://api.telegram.org/bot1964244239:AAFIY_CqMoCR0V7dN04ADrjJxZ-rfdrAcXU/sendMessage"
}
#宝塔后台操作报警
function bt_web(){
echo > bt_web.txt
if [ ! -f "/www/server/panel/data/default.db" ];then
    echo "<li>宝塔操作日志数据库 报错 /www/server/panel/data/default.db 丢失 </li>"
else
    date "+%Y-%m-%d %H:%M" > bt_web_time.txt
    tail /www/server/panel/data/default.db |\
    sed -b "s/'/\n/g" |\
    grep -a "`cat bt_web_time.txt`" > bt_web.txt
fi
if test -s bt_web.txt; then
    telegram_bot 宝塔操作中
else
    echo "宝塔无操作"
fi
}
bt_web 
#宝塔后台登录成功报警
function bt_web_login(){
echo > bt_web_login.txt
if [ ! -f "bt_login_html.txt" ];then
    echo "<li>宝塔 bt_login_html 函数未运行 报错 bt_login_html.txt 丢失 </li>"
else
    date "+%Y-%m-%d %H:%M" > bt_web_login_time.txt
    cat bt_login_html.txt |\
    grep -a "`cat bt_web_login_time.txt`" > bt_web_login.txt
fi
if test -s bt_web_login.txt ; then
    telegram_bot 宝塔后台web登录成功
else
    echo "无宝塔后台web登录成功"
fi
}
bt_web_login 
#宝塔终端操作报警
function bt_ssh(){
echo > bt_ssh.txt
if [ ! -f "/www/server/panel/logs/terminal.log" ];then
    echo "<li>宝塔ssh终端日志 报错 /www/server/panel/logs/terminal.log 丢失 </li>"
else
    date "+%Y-%m-%d %H:%M" > bt_ssh_time.txt
    tail /www/server/panel/logs/terminal.log |\
    grep -a "`cat bt_ssh_time.txt`" > bt_ssh.txt
fi
if test -s bt_ssh.txt ; then
    telegram_bot 链接宝塔ssh终端
else
    echo "无链接宝塔ssh终端"
fi
}
bt_ssh 
#宝塔终端成功登录到SSH服务器报警
function bt_ssh_login(){
echo > bt_ssh_login.txt
if [ ! -f "bt_login_ssh_html.txt" ];then
    echo "<li>宝塔 bt_login_ssh_html 函数未运行 报错 bt_login_ssh_html.txt 丢失 </li>"
else
    date "+%Y-%m-%d %H:%M" > bt_ssh_login_time.txt
    cat bt_login_ssh_html.txt |\
    grep -a "`cat bt_ssh_login_time.txt`" > bt_ssh_login.txt
fi
if test -s bt_ssh_login.txt ; then
    telegram_bot 宝塔终端成功登录到SSH服务器
else
    echo "无宝塔终端成功登录到SSH服务器"
fi
}
bt_ssh_login
#第二版
###########################################################
#域名 DNS 记录 完成度 50%
function dns_list(){
    echo 100
}

#域名 DNS 污染检查  完成度 50%
function dns_list_check(){
echo 1 
}
    
#域名 被墙检查 国内 完成度 20%
function www_go_china_ok(){
    echo 127
}

#域名 被墙检查 香港 完成度 20%
function www_go_hk_ok(){
    echo 100
}

#域名 收录 统计 完成度 100%
function www_baidu_top(){
    echo 111
}
function www_sogou_top(){
    echo 111
}
function www_360_top(){
    echo 111
}
function www_toutiao_top(){
    echo 111
}

#网站 排名 统计 完成度 0%
function www_top(){
    echo 1111
}

    
#第二版
###########################################################   
# 新功能规划中
    
    
    
    





Last updated