Permalink
Browse files

Initial commit

  • Loading branch information...
0 parents commit 8f37cad2bb8825e7e2de255bbd9801dc859ecd4a @silas silas committed Jun 2, 2010
Showing with 108 additions and 0 deletions.
  1. +21 −0 LICENSE
  2. +8 −0 README.md
  3. +79 −0 scribe_log
21 LICENSE
@@ -0,0 +1,21 @@
+The MIT License
+
+Copyright (c) 2009 Silas Sewell
+
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in
+all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+THE SOFTWARE.
@@ -0,0 +1,8 @@
+scribe_log
+==========
+
+scribe_log is a simple Python script to Pipe log data to Scribe.
+
+### Licenses
+
+This work is licensed under the MIT License (see the LICENSE file).
@@ -0,0 +1,79 @@
+#!/usr/bin/env python
+
+import os, time, sys
+from optparse import OptionParser
+from thrift.transport import TTransport, TSocket
+from thrift.transport.TTransport import TTransportException
+from thrift.protocol import TBinaryProtocol
+from scribe import scribe
+
+class ScribeLogError(Exception): pass
+
+def tail_file(file):
+ while True:
+ where = file.tell()
+ line = file.readline()
+ if not line:
+ time.sleep(1.0)
+ file.seek(where)
+ else:
+ yield line
+
+def handle(file, category, host='127.0.0.1', port=1463, prefix='', postfix=''):
+ result = 0
+
+ socket = TSocket.TSocket(host=host, port=port)
+ transport = TTransport.TFramedTransport(socket)
+ protocol = TBinaryProtocol.TBinaryProtocol(trans=transport, strictRead=False, strictWrite=False)
+ client = scribe.Client(iprot=protocol, oprot=protocol)
+
+ transport.open()
+
+ try:
+ log_file = open(file)
+ try:
+ log_file.seek(0, 2)
+ for line in tail_file(log_file):
+ log_entry = scribe.LogEntry(dict(category=category, message=prefix+line+postfix))
+ result = client.Log(messages=[log_entry])
+ finally:
+ log_file.close()
+ except IOError, e:
+ raise ScribeLogError('IOError: %s' % e)
+
+ try: transport.close()
+ except: pass
+
+ if result == scribe.ResultCode.OK:
+ pass
+ elif result == scribe.ResultCode.TRY_LATER:
+ raise ScribeLogError('Scribe Error: TRY LATER')
+ else:
+ raise ScribeLogError('Scribe Error: Unknown error code (%s)' % result)
+
+if __name__ == '__main__':
+ parser = OptionParser()
+ parser.add_option('--file', dest='file', help='file to tail into Scribe', metavar='FILE')
+ parser.add_option('--category', dest='category', help='Scribe category', metavar='CATEGORY')
+ parser.add_option('--host', default='127.0.0.1', dest='host', help='destination Scribe host server', metavar='HOST')
+ parser.add_option('--port', default=1463, dest='port', help='destination Scribe port', metavar='PORT', type='int')
+ parser.add_option('--prefix', default='', dest='prefix', help='add to the beginning of each log line', metavar='PREFIX')
+ parser.add_option('--postfix', default='', dest='postfix', help='add to the end of each log line', metavar='POSTFIX')
+
+ (options, args) = parser.parse_args()
+
+ if options.file and options.category:
+ try:
+ handle(file=options.file,
+ category=options.category,
+ host=options.host,
+ port=options.port,
+ prefix=options.prefix,
+ postfix=options.postfix)
+ except KeyboardInterrupt:
+ sys.exit(0)
+ except (ScribeLogError, TTransportException), e:
+ print >> sys.stderr, e
+ sys.exit(1)
+ else:
+ parser.print_help()

0 comments on commit 8f37cad

Please sign in to comment.