2
# vim: tabstop=4 shiftwidth=4 softtabstop=4
4
# Copyright 2010 United States Government as represented by the
5
# Administrator of the National Aeronautics and Space Administration.
8
# Licensed under the Apache License, Version 2.0 (the "License"); you may
9
# not use this file except in compliance with the License. You may obtain
10
# a copy of the License at
12
# http://www.apache.org/licenses/LICENSE-2.0
14
# Unless required by applicable law or agreed to in writing, software
15
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
16
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
17
# License for the specific language governing permissions and limitations
30
import cPickle as pickle
35
class SpoolSentry(object):
36
def __init__(self, spool_dir, sentry_url, key=None):
37
self.spool_dir = spool_dir
38
self.sentry_url = sentry_url
42
for fname in os.listdir(self.spool_dir):
43
if fname == "processed":
46
sourcefile = "%s/%s" % (self.spool_dir, fname)
47
with open(sourcefile) as f:
49
data_from_json = json.loads(fdata)
50
data = self.build_data(data_from_json)
52
destfile = "%s/processed/%s" % (self.spool_dir, fname)
53
shutil.move(sourcefile, destfile)
55
logging.exception("Unable to upload record %s", fname)
58
def build_data(self, filejson):
59
env = {'SERVER_NAME': 'unknown', 'SERVER_PORT': '0000',
60
'SCRIPT_NAME': '/unknown/', 'PATH_INFO': 'unknown'}
62
env = json.loads(filejson['env'])
63
url = "http://%s:%s%s%s" % (env['SERVER_NAME'], env['SERVER_PORT'],
64
env['SCRIPT_NAME'], env['PATH_INFO'])
65
rv = {'logger': filejson['logger'], 'level': logging.ERROR,
66
'server_name': filejson['host'], 'url': url,
67
'message': filejson['message'],
68
'traceback': filejson['traceback']}
71
rv['data']['META'] = env
72
if filejson['request_id']:
73
rv['data']['request_id'] = filejson['request_id']
76
def send_data(self, data):
77
data = {'data': base64.b64encode(pickle.dumps(data).encode('zlib')),
79
req = urllib2.Request(self.sentry_url)
80
res = urllib2.urlopen(req, urllib.urlencode(data))
81
if res.getcode() != 200:
82
raise Exception("Bad HTTP code: %s" % res.getcode())
85
if __name__ == '__main__':
86
sentryurl = 'http://127.0.0.1/sentry/store/'
88
spooldir = '/var/spool/nova'
90
sentryurl = sys.argv[1]
94
spooldir = sys.argv[3]
95
SpoolSentry(spooldir, sentryurl, key).process()