惜风不起、唯有努力!
nginx 访问前10 ip和次数写入数据库脚本

nginx 访问前10 ip和次数写入数据库脚本

#!/bin/bash
#user: yp

NGINX_LOG=/var/log/nginx/access.log
DBNAME=nginx
DBPWD=123456
DBUSER=yp
DBHOST=127.0.0.1
DBPORT=13306

function main(){
    local clientip=($(awk '{print $1}' ${NGINX_LOG} | sort | uniq -c | sort -nr -k1 | head -n 10))
    for i in {1..20..2}
        do
        j=`expr ${i}-1`
        mysql -u${DBUSER} -p${DBPWD} -h ${DBHOST} -P ${DBPORT} ${DBNAME} -e 'INSERT INTO client_ip (ip_addr,ip_max_values, status_datetime) VALUES ("'${clientip[$i]}'", "'${clientip[$j]}'", NOW());'
    done
}
main

发表回复

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