shell脚本每天自动统计网站访问日志并发送到钉钉
本脚本是根据centos.bz在2012年写的shell脚本每天自动统计网站访问日志来简单修改了下,用了这么多年从邮箱接收变到钉钉接收,一直想把这个脚本在钉钉上的输出改得好看点,今天周6有空就来改了下.
脚本内容:
cat /root/webbak/wdts_summary.sh
#!/bin/bash PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin export PATH function notify(){ curl "https://oapi.dingtalk.com/robot/send?access_token=$dingdingtoken×tamp=$timestamp&sign=$sign" -H 'Content-Type: application/json' -d "{ 'msgtype': 'markdown', 'markdown': { 'title': 'nginx访问概况', 'text': '# nginx访问概况\n\n- 报告生成时间:${maketime}\n- 总访问量:${total_visit}\n- 总带宽:${total_bandwidth}M\n- 独立访客:${total_unique}\n\n### 访问IP统计\n| IP地址 | PV |\n| ------ | --- |\n${ip_table}\n\n### 访问URL统计\n${url_num}\n\n### 来源页面统计\n| PV | 页面 |\n| ---- | --- |\n${referer}\n\n### 404统计\n| PV | URL |\n| --- | --- |\n${notfound}\n\n### 蜘蛛统计\n| 蜘蛛 | PV |\n| --- | --- |\n${spider}\n\n### 搜索引擎来源统计\n| 搜索引擎 | PV |\n| ------ | --- |\n${search}' } }" } getkey=$(python /root/webbak/ding_secret.py) timestamp=${getkey:0:13} sign=$(echo "${getkey:13:100}" | tr -d '\n') dingdingtoken="xxxxxxxxxxxxxx" #这里改成你自己钉钉的token log_path="/data/nginx/logs/blog.slogra.com.access.log" domain="blog.slogra.com" maketime=$(date +%Y-%m-%d"_"%H":"%M) logdate=$(date -d "yesterday" +%Y-%m-%d) total_visit=$(wc -l ${log_path} | awk '{print $1}') total_bandwidth=$(awk -v total=0 '{total+=$10}END{print total/1024/1024}' ${log_path}) total_unique=$(awk '{ip[$1]++}END{print asort(ip)}' ${log_path}) ip_table=$(awk '{ip[$1]++}END{for (k in ip){print "|",k,"|",ip[k],"|\n"}}' ${log_path} | sort -rn -k 3 | head -10) url_table=$(awk '{url[$7]++}END{for (k in url){print "| "url[k]" | "k" |"}}' ${log_path} | sort -rn | head -10) url_num="| PV | URL |\n| --- | --- |\n${url_table}" referer=$(awk -v domain=$domain '($11 !~ /http:\/\/[^/]*'"$domain"'/) && ($11 !~ /^"-?"$/){url[$11]++}END{for (k in url){print "| "url[k]" | "k" |\n"}}' ${log_path} | sort -rn | head -n 10) notfound=$(awk '$9 == 404 {url[$7]++}END{for (k in url){print "|",url[k],"|",k,"|\n"}}' ${log_path} | sort -rn | head -10) spider=$(awk -F'"' '$6 ~ /Baiduspider/ {spider["baiduspider"]++} $6 ~ /Googlebot/ {spider["googlebot"]++}END{for (k in spider){printf("| %s | %s |\n", k, spider[k])}}' ${log_path} | sort -rn | head -10) search=$(awk -F'"' '{if($4 ~ /http:\/\/www\.baidu\.com/) search["百度搜索"]++ ; if($4 ~ /http:\/\/www\.google\.com/) search["谷歌搜索"]++}END{for (k in search){print "| " k " | " search[k] " |"}}' ${log_path} | sort -rnk3 | head -10) notify
python2的钉钉加签脚本:
cat /root/webbak/ding_secret.py
#!/usr/bin/env python # -*- coding:utf-8 -*- import time import hmac import hashlib import base64 import urllib timestamp = int(time.time() * 1000) secret = 'xxxxxxxxxxxxxxxxxx' #这里改成你自己的钉钉加签密钥 string_to_sign = '%s\n%s' % (timestamp, secret) hmac_code = hmac.new(secret.encode(), string_to_sign.encode(), digestmod=hashlib.sha256).digest() sign = urllib.quote_plus(base64.b64encode(hmac_code)) print(timestamp) print(sign)
如果你们不喜欢python2写的话,下面这个是python3写的钉钉加签脚本:
cat /root/webbak/ding_secret.py
#!/usr/bin/env python # -*- coding:utf-8 -*- import time import hmac import hashlib import base64 import urllib.parse timestamp = round(time.time() * 1000) secret = 'xxxxxxxxxxxxxxxxxxxx' ##这里改成你自己的钉钉加签密钥 secret_enc = secret.encode('utf-8') string_to_sign = f'{timestamp}\n{secret}' string_to_sign_enc = string_to_sign.encode('utf-8') hmac_code = hmac.new(secret_enc, string_to_sign_enc, digestmod=hashlib.sha256).digest() sign = urllib.parse.quote_plus(base64.b64encode(hmac_code)) print(timestamp) print(sign)
至于你们具体用哪个,就看你们自己了.
下面的脚本运行后的钉钉输出图:
好了,说真的这么多年过去了,像这样好的脚本越来越少了.
评论: