-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathokerr-poster.py
executable file
·66 lines (51 loc) · 1.71 KB
/
okerr-poster.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
57
58
59
60
61
62
63
64
65
66
#!/usr/bin/env python
import sys
import redis
import requests
import time
import argparse
import logging
def get_redis():
return redis.Redis(unix_socket_path='/var/run/redis/redis.sock', decode_responses=True)
def loop(redis):
while True:
name = redis.lpop('http_post_list')
if name:
log.debug("processing {}".format(name))
d = redis.hgetall(name)
url = d['url']
data = dict(data=d['data'])
try:
r = requests.post(url, data = data, timeout=args.timeout)
except requests.exceptions.RequestException as e:
log.info("err while report to {}: {}".format(url, e))
else:
log.info("reported ({}) to {}".format(r.status_code, url))
finally:
redis.delete(name)
else:
log.debug("nothing in http_post_list")
time.sleep(1)
def main():
global log, args
parser = argparse.ArgumentParser(description='okerr HTTP POSTer.')
parser.add_argument('-v', dest='verbose', action='store_true',
default=False, help='verbose mode')
parser.add_argument('-t', dest='timeout', type=int, default=3,
help='timeout in seconds')
args = parser.parse_args()
#signal.signal(signal.SIGINT, sighandler)
logging.basicConfig(
format='%(asctime)s %(message)s',
datefmt='%Y-%m-%d %H:%M:%S',
level=logging.INFO)
log = logging.getLogger('okerr')
if args.verbose:
log.setLevel(logging.DEBUG)
log.debug('Verbose mode')
err = logging.StreamHandler(sys.stderr)
log.addHandler(err)
log.info('Okerr HTTP POSTer started')
r = get_redis()
loop(r)
main()