Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

fix query interface data format

  • Loading branch information...
commit a9bd21c0d212c2ee1f0ba5aaf47ffb165e8aec8f 1 parent 2316301
Peng Yuwei hokerffb authored
21 bin/kanyun
View
@@ -26,12 +26,18 @@ from collections import OrderedDict
from kanyun.worker.worker import *
from kanyun.worker.plugin_traffic_accounting import *
from kanyun.worker import plugin_agent
+from kanyun.common.nova_tools import *
+from kanyun.common.app import *
def show_usage():
print "usage:"
print "\tkanyun <param> [option]"
+ print "usage:"
print "\t -n : list network traffic, option=time-interval"
print "\t -vm : list virtualmachine info, option=time-interval"
+ print "\t -vi <uuid> : get vm info"
+ print "example:"
+ print "\t kanyun -vi instance-00000166"
def vm(interval=60):
while True:
@@ -47,6 +53,12 @@ def network(interval=60):
time.sleep(interval)
+def vminfo(app, instanceid):
+ tool = NovaTools(app)
+ id = tool.get_id(instanceid)
+ rs = tool.get_instances(id=id)
+ print rs
+
def main():
if len(sys.argv) == 1:
show_usage()
@@ -56,6 +68,11 @@ def main():
show_usage()
return
+ app = App(conf="kanyun.conf", log='/tmp/kanyun.log')
+ cfg = app.get_cfg('kanyun')
+ if cfg is None:
+ cfg = dict({"api_host":"0.0.0.0", "api_port":"5400"})
+
if len(sys.argv) == 2:
if sys.argv[1] == '-n':
network(interval=60)
@@ -69,9 +86,11 @@ def main():
if sys.argv[1] == '-n':
network(interval=int(option))
return
- if sys.argv[1] == '-vm':
+ elif sys.argv[1] == '-vm':
vm(interval=int(option))
return
+ elif sys.argv[1] == '-vi':
+ print vminfo(app=app, instanceid=option)
else:
return
else:
15 bin/kanyun-api
View
@@ -65,12 +65,19 @@ if __name__ == '__main__':
while True:
msg_type, msg_uuid, message = socket.recv_multipart()
try:
+ data = dict()
msg = json.loads(message)
- if not msg.has_key('method'):
+ if not msg.has_key('method') or not msg.has_key("args"):
+ socket.send_multipart([msg_type, msg_uuid, json.dumps(data)])
continue
- if not msg.has_key('args'):
- continue
- resp = on_message_receive(api, msg['method'], msg['args'])
+ data = on_message_receive(api, msg['method'], msg['args'])
+ if data is None:
+ data = dict()
+ print "response:", data
+ resp = {'code': 200,
+ 'message': 'Success',
+ 'data': data,
+ }
socket.send_multipart([msg_type, msg_uuid, json.dumps(resp)])
except Exception, e:
traceback.print_exc()
63 kanyun/common/nova_tools.py
View
@@ -0,0 +1,63 @@
+#!/usr/bin/env python
+# vim: tabstop=4 shiftwidth=4 softtabstop=4
+#
+# Copyright 2012 Sina Corporation
+# All Rights Reserved.
+# Author: YuWei Peng <pengyuwei@gmail.com>
+#
+# 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
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# 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 ConfigParser
+import json
+from collections import OrderedDict
+from sqlalchemy import *
+from sqlalchemy.orm import sessionmaker
+
+from kanyun.common.app import *
+
+class NovaTools():
+ def __init__(self, app):
+ self.app = app
+ self.cfg = self.app.get_cfg("DEFAULT")
+ self.db = create_engine(self.cfg["sql_connection"])
+ self.db.echo = False
+ metadata = MetaData(self.db)
+ self.instances = Table('instances', metadata, autoload=True)
+
+ def get_id(self, instance):
+ if len(instance) < 10:
+ return None
+ return int(instance[9:], 16)
+
+ def get_instances(self, uuid=None, id=None):
+ "return format: (id, uuid, display_name) or None"
+ if not uuid is None:
+ stmt = self.instances.select(self.instances.c.uuid == uuid)
+ elif not id is None:
+ stmt = self.instances.select(self.instances.c.id == id)
+ else:
+ return None
+ rs = stmt.execute()
+ if not rs is None:
+ row = rs.first()
+ return (row[4], row[34], row[26])
+ return None
+
+if __name__ == '__main__':
+ app = App(conf="/etc/nova/nova.conf", log='/tmp/kanyun.log')
+ tool = NovaTools(app)
+ rs = tool.get_instances(uuid='76d6f296-68b8-4a21-9a20-9f46ff48d6ef')
+ print rs
+ print tool.get_id("instance-00000138")
5 kanyun/server/api_server.py
View
@@ -151,7 +151,8 @@ def get_data(self, row_id, cf_str, scf_str, time_from=0, time_to=0):
count = 0 if rs is None else len(rs)
ret = rs, count, False if (count == 20000) else True
- self.buf.save(bufkey, ret)
+ if not rs is None:
+ self.buf.save(bufkey, ret)
return ret
@@ -313,7 +314,7 @@ def query_usage_report(self, args, **kwargs):
ret_len = 0
result = ret, ret_len, all_data
- if (time.time() - time_to > 120):
+ if (not result is None and time.time() - time_to > 120):
self.buf.cleanup()
self.buf.save(bufkey, result)
return result
12 kanyun/server/plugin_agent_srv.py
View
@@ -113,7 +113,14 @@ def parse_multi(db, raw_cf_str, instance_id, value, keypath, scf_str):
print '\t%s=%s saved\n\t%s=%s saved' % (prekey1, str(val1), prekey2, str(val2))
#print '\tkey=%s, cf=%s/%s 2 records saved' % (instance_id, cf_str1, cf_str2)
-
+def get_uuid(instance_id):
+ # 1.open nova.mysql
+ # 2.get id from instance_id
+ # 3.select uuid from instances where id=id
+ # 4. return uuid
+
+ return instance_id
+
def plugin_decoder_agent(db, data):
"""decoder the agent data, and save into cassandra database.
# db: cassandra ConnectionPool
@@ -144,6 +151,9 @@ def plugin_decoder_agent(db, data):
val1 = 0
val2 = 0
for instance_id, data in data.iteritems():
+ # TODO:translate instance_id to instance_uuid
+ instance_id = get_uuid(instance_id)
+
keypath = '/' # use for previous_data's key
keypath += instance_id
# print '***** instance=%s:%d ColumnFamilys *****' % (instance_id, len(cfs))
Please sign in to comment.
Something went wrong with that request. Please try again.