惜风不起、唯有努力!
nginx 状态写入数据库脚本

nginx 状态写入数据库脚本

#!/bin/bash
#user: yp
 
ROUTE=/var/log/nginx/access.log
URL=xxx.xxx.xxx
DBNAME=nginx
DBPWD=123456
DBUSER=yp
DBHOST=127.0.0.1
DBPORT=13306

case $1 in
    accs-status)
    acc_max=`grep "$URL" ${ROUTE}|wc -l`
    mysql -u${DBUSER} -p${DBPWD} -h ${DBHOST} -P ${DBPORT} ${DBNAME} -e \
        "INSERT INTO nginx_status (status_name, status_values, status_datetime) VALUES ('acc-status', "${acc_max}", NOW());"
    ;;
    200-status)
    max=`grep "$URL" ${ROUTE}|awk '{print $9}'|grep "200"|wc -l`
    mysql -u${DBUSER} -p${DBPWD} -h ${DBHOST} -P ${DBPORT} ${DBNAME} -e \
        "INSERT INTO nginx_status (status_name, status_values, status_datetime) VALUES ('200-status', "${max}", NOW());"
    ;;
    304-status)
    max=`grep "$URL" ${ROUTE}|awk '{print $9}'|grep "304"|wc -l`
    mysql -u${DBUSER} -p${DBPWD} -h ${DBHOST} -P ${DBPORT} ${DBNAME} -e \
        "INSERT INTO nginx_status (status_name, status_values, status_datetime) VALUES ('304-status', "${max}", NOW());"
    ;;
    4xx-status)
    max=`grep "$URL" ${ROUTE}|awk '{print $9}'|grep -E "401|402|403|404"|wc -l`
    mysql -u${DBUSER} -p${DBPWD} -h ${DBHOST} -P ${DBPORT} ${DBNAME} -e \
        "INSERT INTO nginx_status (status_name, status_values, status_datetime) VALUES ('4xx-status', "${max}", NOW());"
    ;;
    502-status)
    max=`grep "$URL" ${ROUTE}|awk '{print $9}'|grep "502"|wc -l`
    mysql -u${DBUSER} -p${DBPWD} -h ${DBHOST} -P ${DBPORT} ${DBNAME} -e \
        "INSERT INTO nginx_status (status_name, status_values, status_datetime) VALUES ('502-status', "${max}", NOW());"
    ;;
    *)
    echo -e "\033[32mUsage: sh \$0 {accs-status|200-status|304-status|4xx-status|502-status}\033[0m"
esac

发表回复

您的电子邮箱地址不会被公开。