| 301 |
ashish |
1 |
#!/usr/bin/python
|
|
|
2 |
|
|
|
3 |
## Copyright (c) 2007-2008 Facebook
|
|
|
4 |
##
|
|
|
5 |
## Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
6 |
## you may not use this file except in compliance with the License.
|
|
|
7 |
## You may obtain a copy of the License at
|
|
|
8 |
##
|
|
|
9 |
## http://www.apache.org/licenses/LICENSE-2.0
|
|
|
10 |
##
|
|
|
11 |
## Unless required by applicable law or agreed to in writing, software
|
|
|
12 |
## distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
13 |
## WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
14 |
## See the License for the specific language governing permissions and
|
|
|
15 |
## limitations under the License.
|
|
|
16 |
##
|
|
|
17 |
## See accompanying file LICENSE or visit the Scribe site at:
|
|
|
18 |
## http://developers.facebook.com/scribe/
|
|
|
19 |
|
|
|
20 |
|
|
|
21 |
'''scribe_cat: A simple script for sending messages to scribe.'''
|
|
|
22 |
|
|
|
23 |
import sys
|
|
|
24 |
from scribe import scribe
|
|
|
25 |
from thrift.transport import TTransport, TSocket
|
|
|
26 |
from thrift.protocol import TBinaryProtocol
|
|
|
27 |
|
|
|
28 |
if len(sys.argv) == 2:
|
|
|
29 |
category = sys.argv[1]
|
|
|
30 |
host = '127.0.0.1'
|
|
|
31 |
port = 1463
|
|
|
32 |
elif len(sys.argv) == 4 and sys.argv[1] == '-h':
|
|
|
33 |
category = sys.argv[3]
|
|
|
34 |
host_port = sys.argv[2].split(':')
|
|
|
35 |
host = host_port[0]
|
|
|
36 |
if len(host_port) > 1:
|
|
|
37 |
port = int(host_port[1])
|
|
|
38 |
else:
|
|
|
39 |
port = 1463
|
|
|
40 |
else:
|
|
|
41 |
sys.exit('usage (message is stdin): scribe_cat [-h host[:port]] category')
|
|
|
42 |
|
|
|
43 |
log_entry = scribe.LogEntry(category=category, message=sys.stdin.read())
|
|
|
44 |
|
|
|
45 |
socket = TSocket.TSocket(host=host, port=port)
|
|
|
46 |
transport = TTransport.TFramedTransport(socket)
|
|
|
47 |
protocol = TBinaryProtocol.TBinaryProtocol(trans=transport, strictRead=False, strictWrite=False)
|
|
|
48 |
client = scribe.Client(iprot=protocol, oprot=protocol)
|
|
|
49 |
|
|
|
50 |
transport.open()
|
|
|
51 |
result = client.Log(messages=[log_entry])
|
|
|
52 |
transport.close()
|
|
|
53 |
|
|
|
54 |
if result == scribe.ResultCode.OK:
|
|
|
55 |
sys.exit()
|
|
|
56 |
elif result == scribe.ResultCode.TRY_LATER:
|
|
|
57 |
print >> sys.stderr, "TRY_LATER"
|
|
|
58 |
sys.exit(84) # 'T'
|
|
|
59 |
else:
|
|
|
60 |
sys.exit("Unknown error code.")
|