很早就想写一些shell相关得东西了,平时会用到比较多的shell相关的东西,但是一般都去百度或者google去了,一直觉得不够系统,因此就从日常杂记开始,把日常用到的一些脚本记录下来开始吧。
监控工作通知socket的连接状态:
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请检查服务推送消息是否正常' $msg;
done
监控是否有消息推送失败
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请关注服务推送消息是否正常' $msg;
done
发送微信的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()
# 调用接口发送企业微信消息
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() # 请求状态,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)
日志统计脚本:
#!/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,"请求次数:",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 "外部接口访问情况-非网关访问" lc@emails.com lg@emails.com
fi