很早就想寫一些shell相關(guān)得東西了旺遮,平時會用到比較多的shell相關(guān)的東西呵俏,但是一般都去百度或者google去了,一直覺得不夠系統(tǒng)范嘱,因此就從日常雜記開始,把日常用到的一些腳本記錄下來開始吧员魏。
監(jiān)控工作通知socket的連接狀態(tài):
tail -f /data/micro_servers/logs/inner_api.log | grep 'fail!response' |
while read msg ;
do python /home/lc/send_wechat.py 'lc@email.com,cy@email.com' '工作通知socket斷開,\n請檢查服務(wù)推送消息是否正常' $msg;
done
監(jiān)控是否有消息推送失敗
tail -f /data/micro_servers/logs/inner_api.log | grep '推送失敗' | while read msg ;
do python /home/lc/send_wechat.py 'lc@email.com,cy@email.com' '工作通知推送失敗,\n請關(guān)注服務(wù)推送消息是否正常' $msg;
done
發(fā)送微信的python腳本
#!/usr/bin/python
# -*- coding: utf-8 -*-
import hashlib
import json
import urllib2
import sys
def getMD5(data):
hash_md5 = hashlib.md5(data)
return hash_md5.hexdigest()
# 調(diào)用接口發(fā)送企業(yè)微信消息
def send_wechat(emails, message):
# emails = ['lc@email.com', 'cy@email.com']
businessId = 'monitor'
salt = 'monitor_slat'
url = 'http://{ip}:{port}/message/inform'
params = {}
params['businessId'] = businessId
params['recipients'] = emails
params['content'] = message
params['sign'] = getMD5(businessId + message + salt)
headers = {'Content-Type': 'application/json'}
request = urllib2.Request(url=url, data=json.dumps(params), headers=headers)
response = urllib2.urlopen(request)
print response.getcode() # 請求狀態(tài)丑蛤,200為成功
print response.read() # 返回的body
if __name__ == "__main__":
if len(sys.argv) < 3:
print("args error, need more argv like 'xxx@emails.com,xxx@emails.com'")
sys.exit(-1)
emails = sys.argv[1].split(",")
message = sys.argv[2]
send_wechat(emails, message)
日志統(tǒng)計腳本:
#!/bin/bash
# log statistics
currentTimeLog=/data//logs/info-$(date "+%Y-%m-%d" -d "1 days ago").log
IFS=$'\n'
cat $currentTimeLog | grep "收到請求:" | grep "null" | awk '{a[$8]=a[$8]+1}END{for (i in a)print i,"請求次數(shù):",a[i]}' | sort -n -t ":" -k2 -r > /home/lc/logStatistics-$(date "+%Y-%m-%d" -d "1 days ago").log
if [ "$?" == 0 ];then
cat /home/lc/logStatistics-$(date "+%Y-%m-%d" -d "1 days ago").log | mail -s "外部接口訪問情況-非網(wǎng)關(guān)訪問" lc@emails.com lg@emails.com
fi