Add top_sender script
Benjamin Renard

Benjamin Renard commited on 2016-01-20 17:55:12
Showing 1 changed files, with 59 additions and 0 deletions.

... ...
@@ -0,0 +1,59 @@
1
+#!/usr/bin/python
2
+
3
+from optparse import OptionParser
4
+parser = OptionParser()
5
+
6
+parser.add_option('-d',
7
+                  '--debug',
8
+                  action="store_true",
9
+                  dest="debug")
10
+
11
+parser.add_option('-v',
12
+                  '--verbose',
13
+                  action="store_true",
14
+                  dest="verbose")
15
+
16
+(options, args) = parser.parse_args()
17
+
18
+import logging
19
+if options.debug:
20
+        loglevel=logging.DEBUG
21
+elif options.verbose:
22
+        loglevel=logging.INFO
23
+else:
24
+        loglevel=logging.WARNING
25
+
26
+logging.basicConfig(level=loglevel,format='%(asctime)s - %(levelname)s - %(message)s', datefmt='%m/%d/%Y %I:%M:%S %p')
27
+
28
+import subprocess
29
+import re
30
+
31
+mailq_p=subprocess.Popen(['mailq'], stdout=subprocess.PIPE)
32
+mailq=mailq_p.communicate()[0]
33
+
34
+senders={}
35
+current_sender=None
36
+for line in mailq.split('\n'):
37
+	m=re.match('^([A-Z0-9]+)\*?[ \t].* ([^ ]+)$',line)
38
+	if m:
39
+		sender=m.group(2)
40
+		logging.debug("New mailid of %s" % sender)
41
+		if sender not in senders:
42
+			senders[sender]={'count': 0, 'mailid': []}
43
+		senders[sender]['mailid'].append(m.group(1))
44
+		current_sender=sender
45
+		continue
46
+
47
+	m=re.match('^ *([^ ]+@.+)$',line)
48
+	if m:
49
+		if current_sender is None:
50
+			print "??? : %s" % line
51
+			continue
52
+		logging.debug("New mail for %s" % current_sender)
53
+	
54
+		senders[current_sender]['count']+=1
55
+
56
+top_senders=senders.keys()
57
+top_senders=sorted(top_senders, key=lambda sender: senders[sender]['count'],reverse=True)
58
+for s in top_senders:
59
+	print "%s\t%s" % (senders[s]['count'],s)
0 60