Skip to content


Subversion checkout URL

You can clone with
Download ZIP
tree: 4901bc32cf
Fetching contributors…

Cannot retrieve contributors at this time

executable file 118 lines (105 sloc) 3.726 kB
#!/usr/bin/env python
# vim: tabstop=4 shiftwidth=4 softtabstop=4
# Copyright 2012 Sina Corporation
# All Rights Reserved.
# Author: YuWei Peng <>
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
import sys
import time
import signal
import traceback
import ConfigParser
import json
import zmq
from kanyun.common.const import *
from import *
from kanyun.server import api_server
from kanyun.database.cassadb import CassaDb
def on_message_receive(api, method, args):
if method == 'query_usage_report':
print '*' * 60
print "query_usage_report:", args
(rs, count, _) = api.query_usage_report(args, **args)
return rs
elif method == 'get_data':
row_id = args['id']
cf_str = args['metric']
scf_str = args.setdefault("metric_param", "total")
time_from = int(args['timestamp_from'])
time_to = int(args['timestamp_to'])
print '*' * 60
rs, _, _ = api.get_data(row_id, cf_str, scf_str, time_from, time_to)
return rs
elif method == 'list_instance':
cf_str = args['metric']
rs = api.get_instances_list(cf_str)
return rs
elif method == 'update_alert_policy':
print '*' * 60
print "update_alert_policy:", msg['args']
return None
return None
if __name__ == '__main__':
app = App(conf="kanyun.conf", name="api")
logger = app.get_logger()
cfg = app.get_cfg('api')
api = api_server.ApiServer(db_host=cfg['db_host'])
context = zmq.Context()
socket = context.socket(zmq.REP)
socket.bind("tcp://%(api_host)s:%(api_port)s" % cfg)
print "listen tcp://%(api_host)s:%(api_port)s" % cfg
while True:
msg_type, msg_uuid, message = socket.recv_multipart()
data = dict()
msg = json.loads(message)
method = msg.setdefault("method", None)
args = msg.setdefault("args", None)
if method is None or args is None:
socket.send_multipart([msg_type, msg_uuid, json.dumps(data)])
continue + str(args))
data = on_message_receive(api, method, args)
if data is None:
data = dict()
resp = {'code': 200,
'message': 'Success',
'data': data,
socket.send_multipart([msg_type, msg_uuid, json.dumps(resp)])
except Exception, e:
print "[EXP]", message
print "[EXP]", e
[msg_type, msg_uuid, json.dumps(
{'code': 500,
'message': 'Request failed.',
'method': 'query_usage_report',
'args': {
'metric': 'network',
'statistic': 'sum',
'period': 5,
'timestamp_from': '2012-02-20T12:12:12',
'timestamp_to': '2012-02-22T12:12:12',
Jump to Line
Something went wrong with that request. Please try again.