Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

api refactor/vminfo remove @hostname, TODO:server get uuid of instanc…

…eid and save uuid to cassandra
  • Loading branch information...
commit 2316301c1e4ddb74110a8aec3000bed7ea9fb421 1 parent be050c3
@hokerffb hokerffb authored
View
149 bin/api-client
@@ -28,12 +28,19 @@ from kanyun.client import api_client
from kanyun.common import const
-#def show_result(rs, cf_str):
-# for i in rs:
-# print "%s" % (i)
-# print "%d results of cf=%s" % (len(rs), cf_str)
-# print "spend", time.clock(), "seconds"
-#
+def show_result(rs):
+ if rs is None:
+ return
+ print rs
+ if rs.has_key("data"):
+ rs = rs['data']
+ if rs is None:
+ return
+ for k, i in rs.iteritems():
+ print "{%s:%s}" % (k, i)
+ print "%d results" % (len(rs))
+ print "spend", time.clock(), "seconds"
+
def show_result2(rs, row_id, cf_str, scf_str):
if rs is None:
@@ -48,63 +55,21 @@ def show_result2(rs, row_id, cf_str, scf_str):
print "%d results of key=%s,cf=%s,scf=%s" % (len(rs), row_id, cf_str, scf_str)
print "spend", time.clock(), "seconds"
-
-#def show_result3(rs, row_id):
-# for r in rs:
-# for k, i in r.iteritems():
-# print "%s %d results" % (k, len(i))
-# print i
-# print "%d results of key=%s" % (len(rs), row_id)
-# print "spend", time.clock(), "seconds"
-#
-#
-#def show_result4(rs, row_id='', cf_str='', scf_str='',
-# statistic=0, time_from=0, time_to=0):
-# strs = "Result"
-# if const.statistic_str.has_key(statistic):
-# strs = const.statistic_str[statistic]
-#
-# timestr1 = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(float(time_from)))
-# if int(time_to) == 0:
-# timestr2 = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
-# else:
-# timestr2 = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(float(time_to)))
-# print 'statistics info of %s(cf=%s)' % (row_id, cf_str)
-# print 'time range:%s --> %s ' % (timestr1, timestr2)
-# print strs + ":"
-# if rs is None:
-# print '\tno result'
-# else:
-# rs = OrderedDict(sorted(rs.items(), key=lambda t: t[0]))
-# for k, i in rs.iteritems():
-# timestr = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(float(k)))
-# print "\t", timestr, "=", i
-# print "spend", time.clock(), "seconds"
-#
-#def list_instance_ip():
-# # achieve in listip
-# print 'use listip instead'
-# return
-
-
def main():
if len(sys.argv) == 2:
if sys.argv[1] in ['--help', "-h", "?"]:
print "usage:"
print "\tapi_client"
-# print "\tapi_client <cf>"
-# print "\tapi_client -l"
-# print "\tapi_client -k <id>"
-# print "\tapi_client <id> <cf> <scf>"
- print "\tapi_client <id> <cf> <scf> " \
- "<[sum]|[max]|[min]|[avg]|[sam]> <period> " \
+ print "\tapi_client -l <metric>"
+ print "\tapi_client <id> vmnetwork <IP> <option> <period> " \
"<time_from> <time_to>"
+ print "\tapi_client <id> <cf> <scf> <option> <period> " \
+ "<time_from> <time_to>"
+ print "option:"
+ print "\t[sum]|[max]|[min]|[avg]|[sam]"
+ print "metric:"
+ print "\t[vmnetwork]|[cpu]|[mem_max]|[mem_free]|[nic_read]|[blk_read]"
print "example:"
-# print "\tapi_client vmnetwork"
-# print "\tapi_client -k instance-0000002"
-# print "\tapi_client instance-0000002 vmnetwork 10.0.0.2"
-# print "\tapi_client instance-00000012@lx12 cpu"
-# print "\tapi_client instance-00000012@lx12 mem mem_free"
print "\tapi-client instance-00000001@pyw.novalocal " \
"cpu total sum 5 " \
"2012-02-20T12:12:12 2012-06-20T12:12:12"
@@ -120,72 +85,12 @@ def main():
apiclient = api_client.ApiClient(cfg['api_host'], cfg['api_port'])
time.clock()
- if len(sys.argv) == 4:
-# row_id, cf_str, scf_str = sys.argv[1], sys.argv[2], sys.argv[3]
-# rs = api_client.invoke_getbykey2(apiclient.socket, row_id, cf_str, scf_str)
-# show_result2(rs, row_id, cf_str, scf_str)
+ if len(sys.argv) == 3:
+ if sys.argv[1] == '-l':
+ metric = sys.argv[2]
+ rs = apiclient.list_instaces(metric=metric)
+ show_result(rs)
return
-# elif len(sys.argv) == 2 and sys.argv[1] == '-l':
-# list_instance_ip()
-# return
-# elif len(sys.argv) == 2:
-# cf_str = sys.argv[1]
-# rs = api_client.invoke_getInstacesList(apiclient.socket, cf_str)
-# show_result(rs, cf_str)
-# elif len(sys.argv) == 3 and sys.argv[1] == '-k':
-# row_id = sys.argv[2]
-# rs = apiclient.invoke_getbykey(apiclient.socket, row_id)
-# show_result3(rs, row_id)
-# return
-# elif len(sys.argv) == 3:
-# if sys.argv[2] == 'nic' or sys.argv[2] == 'blk':
-# row_id, cf_str, scf_str = sys.argv[1], sys.argv[2], sys.argv[2] + '_incoming'
-# rs = api_client.invoke_getbykey2(apiclient.socket, row_id, cf_str, scf_str)
-# show_result2(rs, row_id, cf_str, scf_str)
-#
-# row_id, cf_str, scf_str = sys.argv[1], sys.argv[2], sys.argv[2] + '_outgoing'
-# rs = api_client.invoke_getbykey2(apiclient.socket, row_id, cf_str, scf_str)
-# show_result2(rs, row_id, cf_str, scf_str)
-# return
-# elif sys.argv[2] == 'blk':
-# row_id, cf_str, scf_str = sys.argv[1], sys.argv[2], sys.argv[2] + '_read'
-# rs = api_client.invoke_getbykey2(apiclient.socket, row_id, cf_str, scf_str)
-# show_result2(rs, row_id, cf_str, scf_str)
-#
-# row_id, cf_str, scf_str = sys.argv[1], sys.argv[2], sys.argv[2] + '_write'
-# rs = api_client.invoke_getbykey2(apiclient.socket, row_id, cf_str, scf_str)
-# show_result2(rs, row_id, cf_str, scf_str)
-# return
-# elif sys.argv[2] == 'mem':
-# row_id, cf_str, scf_str = sys.argv[1], sys.argv[2], sys.argv[2] + '_free'
-# rs = api_client.invoke_getbykey2(apiclient.socket, row_id, cf_str, scf_str)
-# show_result2(rs, row_id, cf_str, scf_str)
-#
-# row_id, cf_str, scf_str = sys.argv[1], sys.argv[2], sys.argv[2] + '_max'
-# rs = api_client.invoke_getbykey2(apiclient.socket, row_id, cf_str, scf_str)
-# show_result2(rs, row_id, cf_str, scf_str)
-# return
-# else:
-# row_id, cf_str, scf_str = sys.argv[1], sys.argv[2], 'total'
-# rs = api_client.invoke_getbykey2(apiclient.socket, row_id, cf_str, scf_str)
-# show_result2(rs, row_id, cf_str, scf_str)
-# return
-# elif len(sys.argv) == 8:
-# row_id, cf_str, scf_str = sys.argv[1], sys.argv[2], sys.argv[3]
-# statistic, period = int(sys.argv[4]), int(sys.argv[5])
-# time_from, time_to = int(sys.argv[6]), int(sys.argv[7])
-# apiclient.set_param(row_id, cf_str, scf_str, statistic, period, time_from, time_to)
-# rs = apiclient.get_result(statistic)
-# show_result4(rs, row_id, cf_str, scf_str, statistic, time_from, time_to)
-# return
-# elif len(sys.argv) == 7:
-# row_id, cf_str, scf_str = sys.argv[1], sys.argv[2], sys.argv[3]
-# statistic, period = int(sys.argv[4]), int(sys.argv[5])
-# time_from, time_to = int(sys.argv[6]), 0
-# apiclient.set_param(row_id, cf_str, scf_str, statistic, period, time_from, time_to)
-# rs = apiclient.get_result(statistic)
-# show_result4(rs, row_id, cf_str, scf_str, statistic, time_from, time_to)
-# return
elif len(sys.argv) == 8:
row_id, cf_str, scf_str = sys.argv[1], sys.argv[2], sys.argv[3]
statistic, period = sys.argv[4], int(sys.argv[5])
@@ -202,7 +107,7 @@ def main():
'timestamp_to': time_to,
}
})
- show_result2(rs, "instance-00000001@pyw.novalocal", "cpu", "total")
+ show_result2(rs, row_id=row_id, cf_str=cf_str, scf_str=scf_str)
return
else:
rs = apiclient.query_usage_report({
View
75 bin/kanyun-api
@@ -30,76 +30,24 @@ from kanyun.server import api_server
from kanyun.database.cassadb import CassaDb
-def on_message_receive(api, socket, msg_type, msg_uuid, msg):
- if msg['method'] == 'query_usage_report':
- #[u'S', u'instance-00000001@pyw.novalocal', u'cpu', u'total', 0, 5, 1332897600, 0]
+def on_message_receive(api, method, arg):
+ if method == 'query_usage_report':
print '*' * 60
print "query_usage_report:", msg['args']
(rs, count, _) = api.query_usage_report(msg['args'], **(msg['args']))
- ret = {'code': 0,
- 'message': 'success',
- 'data':rs
- }
- socket.send_multipart(
- [msg_type, msg_uuid, json.dumps(ret)]
- )
- elif msg['method'] == 'update_alert_policy':
+ return rs
+ elif method == 'list_instance':
+ cf_str = arg['metric']
+ rs = api.get_instances_list(cf_str)
+ return rs
+ elif method == 'update_alert_policy':
print '*' * 60
print "update_alert_policy:", msg['args']
- ret = {'code': 0,
- 'message': 'success',
- 'data':None
- }
- socket.send_multipart(
- [msg_type, msg_uuid, json.dumps(ret)]
- )
- elif msg[0] == 'G':
- print '*' * 60
- print "recv:", msg
- row_id = msg[1]
- cf_str = msg[2]
- scf_str = msg[3]
- rs, count, _ = api.get_by_key(row_id, cf_str, scf_str)
- ret = {'code': 0,
- 'message': 'success',
- 'data':rs
- }
- socket.send_multipart(
- [msg_type, msg_uuid, json.dumps(ret)]
- )
- elif msg[0] == 'K':
- print '*' * 60
- print "recv:", msg
- row_id = msg[1]
- cf_str = msg[2]
- rs, count, _ = api.get_by_instance_id(row_id, cf_str)
- ret = {'code': 0,
- 'message': 'success',
- 'data':rs
- }
- socket.send_multipart(
- [msg_type, msg_uuid, json.dumps(ret)]
- )
- elif msg[0] == 'L':
- cf_str = msg[1]
- rs = api.get_instances_list(cf_str)
- ret = {'code': 0,
- 'message': 'success',
- 'data':rs
- }
- socket.send_multipart(
- [msg_type, msg_uuid, json.dumps(ret)]
- )
+ return None
else:
- socket.send_multipart(
- [msg_type, msg_uuid, json.dumps(
- {'code': 500,
- 'message': 'Request failed.',
- 'data':None,
- })]
- )
+ return None
if __name__ == '__main__':
@@ -122,7 +70,8 @@ if __name__ == '__main__':
continue
if not msg.has_key('args'):
continue
- on_message_receive(api, socket, msg_type, msg_uuid, msg)
+ resp = on_message_receive(api, msg['method'], msg['args'])
+ socket.send_multipart([msg_type, msg_uuid, json.dumps(resp)])
except Exception, e:
traceback.print_exc()
print "[EXP]", message
View
26 kanyun/client/api_client.py
@@ -33,6 +33,13 @@
[u'S', u'instance-00000001@pyw.novalocal', u'cpu', u'total', 0, 5, 1332897600, 0]
"""
+PROTOCOL_REQUEST = {
+ 'method': '',
+ 'args': {
+
+ }
+ }
+
param_tmpl = {
'method': 'query_usage_report',
'args': {
@@ -148,13 +155,22 @@ def query_usage_report(self, msg):
json.dumps(msg)])
r_msg_type, r_msg_uuid, r_msg_body = self.socket.recv_multipart()
result = json.loads(r_msg_body)
- print result
return result
-# if result['code'] == 500:
-# raise Exception()
-# else:
-# return result['load_balancer_ids']
+ def list_instaces(self, metric):
+ data = PROTOCOL_REQUEST
+ data['method'] = "list_instance"
+ data['args']['metric'] = metric
+ resp = self.send(data)
+ return resp
################## End public API interface ########################
+ def send(self, data):
+ msg_type = 'kanyun'
+ msg_uuid = str(uuid.uuid4())
+ self.socket.send_multipart([msg_type, msg_uuid,
+ json.dumps(data)])
+ r_msg_type, r_msg_uuid, r_msg_body = self.socket.recv_multipart()
+ result = json.loads(r_msg_body)
+ return result
def set_param(self, key=u'', cf_str=u'', scf_str=u'',
statistic='avg', period=5, time_from=None, time_to=None):
View
18 kanyun/database/cassadb.py
@@ -39,21 +39,29 @@ def insert(self, cf_str, key, values):
cf.insert(key, values)
- def get_range(self, cf_str):
+ def get_range(self, cf_str, row_count=None):
"""get_range([start][, finish][, columns][, column_start][, column_finish]
[, column_reversed][, column_count][, row_count][, include_timestamp]
[, super_column][, read_consistency_level][, buffer_size][, filter_empty])"""
rs = None
- cf = self.get_cf(cf_str)
+ cf = self.get_cf(cf_str, new=True)
if not cf is None:
try:
- rs = cf.get_range()
+ if row_count is None:
+ rs = list(cf.get_range())
+ else:
+ rs = list(cf.get_range(row_count=row_count))
except pycassa.cassandra.c10.ttypes.NotFoundException:
pass
return rs
+ def get_range2(self, cf_str, row_count=None):
+ pool = pycassa.ConnectionPool("data")
+ cf = pycassa.ColumnFamily(pool, cf_str)
+ return cf.get_range()
+
def get(self, cf_str, key, super_column,
column_start, column_finish, column_count = 20000):
"""get(key[, columns][, column_start][, column_finish][, column_count]
@@ -108,8 +116,10 @@ def getbykey2(self, cf_str, key, super_column, column_count):
return rs
########### private #########################################
- def get_cf(self, cf_str):
+ def get_cf(self, cf_str, new=False):
"""[private]"""
+ if new:
+ return pycassa.ColumnFamily(self.db, cf_str)
if not self.cfs.has_key(cf_str):
try:
self.cfs[cf_str] = pycassa.ColumnFamily(self.db, cf_str)
View
4 kanyun/server/api_server.py
@@ -211,8 +211,10 @@ def get_instances_list(self, cf_str):
time_from = time_to - 24 * 60 * 60
db = self.get_db()
- rs = db.get_range(cf_str)
+ rs = db.get_range2(cf_str, row_count=20)
+ return list(rs)
if not rs is None:
+ print rs
for i in rs:
ret.append(i[0])
View
7 kanyun/worker/plugin_agent.py
@@ -74,8 +74,11 @@ def collect_info(self):
infos_by_dom_name = dict()
for dom_id in self.conn.listDomainsID():
dom_conn = self.conn.lookupByID(dom_id)
- dom_key = "%s@%s" % (dom_conn.name(), self.hostname)
+# dom_key = "%s@%s" % (dom_conn.name(), self.hostname)
+ dom_key = dom_conn.name()
dom_xml = dom_conn.XMLDesc(0)
+ print "-" * 60
+ print dom_key
# get infos
infos = list()
# get domain's cpu, memory info
@@ -139,7 +142,7 @@ def _collect_cpu_mem_info(self, dom_id, dom_conn):
#%CPU = 100 * cpu_time_diff / (t * nr_cores * 10^9)
#print "%d * %f / (%d * 1 * %d)" % (100.0, self.diffs[dom_id].get_diff(), self.diffs[dom_id].get_time_pass(), 1e9)
cpu = 100.0 * self.diffs[dom_id].get_diff() / (self.diffs[dom_id].get_time_pass() * 1 * 1e9)
- print dom_id, 'cpu usage:', cpu, '%, cpu_time:', dom_cpu_time, "mem:", mem_free, "/", dom_max_mem_kb
+ print "dom_id:",dom_id, 'cpu:', cpu, '%, cpu_time:', dom_cpu_time, "mem:", mem_free, "/", dom_max_mem_kb
# NOTE(lzyeval): libvirt currently can only see total of all vcpu time
# return [('cpu', 'total', (timestamp, dom_cpu_time)),
# ('mem', 'total', (timestamp, dom_max_mem_kb, dom_memory_kb))]
Please sign in to comment.
Something went wrong with that request. Please try again.