-
Notifications
You must be signed in to change notification settings - Fork 1
/
monitorNginx.py
53 lines (40 loc) · 1.41 KB
/
monitorNginx.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#coding=utf8
import re
import os
import sys
import time
#nginxLogFile = r'/data/logs/nginx-ssl/openrestyaccess.log'
nginxLogFile = r'/tmp/openrestyaccess.log'
def detailIpList(ipList):
printList = sorted([(number, IP) for IP,number in ipList.items()],reverse=True)[:15]
for printSortedList in printList:
print printSortedList[0], ':', printSortedList[1]
def detailMessege(messegeList):
printList = sorted([(number, messege) for messege, number in messegeList.items()], reverse=True)[:15]
for prinSortedMessege in printList:
print prinSortedMessege[0], ':', prinSortedMessege[1]
def main():
ipList = {}
messegeList = {}
logFile = open(nginxLogFile, 'r')
for everyLine in logFile.readlines():
everyIp = everyLine.split()[0]
if everyIp in ipList:
ipList[everyIp] += 1
else:
ipList[everyIp] = 1
everyMessege = everyLine.split('\"')[1]
if everyMessege in messegeList:
messegeList[everyMessege] += 1
else:
messegeList[everyMessege] = 1
detailIpList(ipList)
print '===================================' + os.linesep
detailMessege(messegeList)
#everyTime = re.split(r"[\[\]]", everyLine)
if __name__ == '__main__' :
if not os.path.exists(nginxLogFile):
print 'nginx log file is not exist !!!' + os.linesep
exit()
main()