內網服務器之間高頻率檢驗延時信息,java
多進程,可同時異步監控多個地址,不會互相影響。python
建立文件夾(能夠替換爲你本身的,我這裏就用咱們生產環境的標準目錄了)
mkdir /data/w/falcon-agent/tmp
複製wiki裏的代碼至ping.py腳本並替換腳本內須要監控的地址
啓動腳本
python ping.py &
關閉腳本(不必關閉)
ps -ef|grep ping|awk -F
'[ ]+'
'{print $2}'
|xargs kill
/data/w/falcon-agent/tmp目錄下會按照機器名+時間戳的方式進行日誌記錄,默認清除30天以前的ping日誌
#!/bin/python #coding: utf-8 import multiprocessing import time import os #替換掉須要ping的機器 ping_list = [ "productu1.baseinfo.w.cn1", "productu2.baseinfo.w.cn1", "priceu1.baseinfo.w.cn1", "priceu2.baseinfo.w.cn1" ] def ping_worker(hostname): while True: time.sleep(1) os.system("find /data/w/falcon-agent/tmp -mtime +30 -name '*.log'|xargs rm -f") os.system("echo $(date) >> {}.`date +%F`.log".format(hostname)) os.system("echo $(ping -c 1 {}|grep -A 3 time=) >> {}.$(date +%F).log".format(hostname,hostname)) for hostname in ping_list: process = multiprocessing.Process(target = ping_worker, args = (hostname,)) process.start()
分析指定日期日誌,並展現延時大於10毫秒的ping記錄服務器
#!/bin/python #coding: utf-8 import os from sys import argv logs = [] try: script, first = argv except ValueError: print "請傳入一個日期參數,例如 python scripts.py 2017-12-20" else: logs = os.popen("ls *{}.log".format(first)).read().split('\n')[:-1] def worker(*logs): for log in logs: times = os.popen("cat %s| awk '{print $8}' | awk -F '=' '{if($2>10)print $2}'" % (log)).read().split('\n')[:-1] for time in times: output = os.popen("cat %s|grep -B 1 'time=%s'" % (log, time)).read() print log print output worker(*logs)