forked from cms-sw/cms-bot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
es_hypernews_log.py
executable file
·56 lines (55 loc) · 1.94 KB
/
es_hypernews_log.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
54
55
56
#!/usr/bin/env python
from commands import getstatusoutput
import re ,sys,time , datetime
from es_utils import send_payload
from json import dumps
def cust_strip(str_in):
return str_in.lstrip('/').rstrip(':"')
def prs_tprl(str_in):
str_out = map(str.strip,str_in.split(':'))
val = int(str_out[0].split('/')[0])
key = str_out[1].split(' ')[0]
return key,val
def rm_extra(str_in):
str_out = str_in.split(' ')
return str_out[-4].lstrip('/').rstrip('"'),int(str_out[-2])
def map_int_val(pair):
key , val = pair
return key , int(val)
#get relevant info
match_hn = re.compile('.*\|\/.*emails')
match_tmp = re.compile('.*\|\/.*Time\(s\)')
temp_fails = []
egrps = []
payload = {}
err , cmd_out = getstatusoutput('logwatch --range yesterday --detail 10 --service sendmail')
if err:
sys.exit(1)
for line in cmd_out.split('\n'):
if re.match(match_tmp,line): temp_fails.append(line)
elif 'Messages To Recipients:' in line: msgs_num = line
elif 'Addressed Recipients:' in line : adrpts = line
elif 'Bytes Transferred:' in line: byttr = line
elif re.match(match_hn,line): egrps.append(line)
#process info
yesterday = datetime.date.today() - datetime.timedelta(1)
timestp = int(yesterday.strftime("%s")) * 1000
temp_fails = dict(map(map_int_val,(dict([map(cust_strip,x.split(' ')[-3:-1]) for x in temp_fails])).items()))
msgs_num= map(str.strip,msgs_num.split(':'))
adrpts = map(str.strip, adrpts.split(':'))
byttr = map(str.strip, byttr.split(':'))
egrp_emails = dict(map(rm_extra,[x.strip() for x in egrps]))
#send info
payload['@timestamp'] = timestp
payload['msgs_to_rcpts'] = msgs_num[1]
payload['addressed_rcpts'] = adrpts[1]
payload['byts_transfd'] = byttr[1]
total=0
for k in egrp_emails:
payload['hn-'+k] = egrp_emails[k]
total += egrp_emails[k]
payload['posts'] = total
payload['forums'] = len(egrp_emails)
for k in temp_fails:
payload['fail-'+k] = temp_fails[k]
send_payload("hypernews","mailinfo",timestp, dumps(payload), passwd_file="/data/es/es_secret")