-
Notifications
You must be signed in to change notification settings - Fork 0
/
upload_robot_logs.py
executable file
·92 lines (70 loc) · 2.87 KB
/
upload_robot_logs.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
"""
Script used to upload robot log messages to statusdb. Works either with stdin or a file.
Denis Moreno, Joel Gruselius
Scilifelab
2016-03-04
"""
import logging
import logging.handlers
import argparse
import sys
import datetime
import couchdb
import yaml
import socket
def save_to_statusdb(db, message, args):
data={'message':message}
data['timestamp']=datetime.datetime.now().isoformat()
data['instrument_name']=args.name
db.save(data)
def read_message(args):
if args.input_file:
#read from input file
with open(args.input_file, 'rb') as inp:
message=inp.read()
else:
#read from stdin
message=sys.stdin.read().rstrip()
return message
def setupServer(conf):
db_conf = conf['statusdb']
url="http://{0}:{1}@{2}:{3}".format(db_conf['username'], db_conf['password'], db_conf['url'], db_conf['port'])
return couchdb.Server(url)
def setupLog(name, logfile, log_level=logging.INFO, max_size=209715200, nb_files=5):
mainlog = logging.getLogger(name)
mainlog.setLevel(level=log_level)
mfh = logging.handlers.RotatingFileHandler(logfile, maxBytes=max_size, backupCount=nb_files)
mft = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
mfh.setFormatter(mft)
mainlog.addHandler(mfh)
return mainlog
def main(args):
mainlog=setupLog(name="upload_robot_logs", logfile=args.logfile, nb_files=1)
mainlog.info("Starting uploading from instrument {}".format(args.name))
#read the configuration
with open(args.conf) as conf_file:
conf=yaml.load(conf_file)
couch=setupServer(conf)
db=couch[conf['statusdb']['instrument_logs_db']]
message=read_message(args)
mainlog.info("Read message : {}".format(message))
try:
save_to_statusdb(db, message, args)
except Exception:
mainlog.error("Failed to upload to statusdb : {}".format(sys.exc_info()[0]))
else:
mainlog.info("Uploaded message {} from {} to statusdb".format(message, args.name))
if __name__=="__main__":
usage = "Usage: upload_robot_logs.py [options]"
parser = argparse.ArgumentParser(description='Upload any message to statusdb.', usage=usage)
parser.add_argument("-f", "--file", dest = "input_file", help = ("file",
" to read. its contents will be uploaded to satusdb as a message"))
parser.add_argument("-n", "--name", dest = "name", help = (
"name of the instrument. Default is the host name"), default=socket.gethostname())
parser.add_argument("-l", "--logfile", dest = "logfile", help = ("log file",
" that will be used. Default is ./statusdb_upload.log "), default="statusdb_upload.log")
parser.add_argument("-c", "--conf", dest="conf",
default='statusdb.yaml',
help = "Config file. Default: ./statusdb.yaml")
args = parser.parse_args()
main(args)