Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tree: f5301d3ae2
Fetching contributors…

Cannot retrieve contributors at this time

executable file 182 lines (157 sloc) 7.445 kb
#!/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
import zmq
from collections import OrderedDict
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)
def show_result2(rs, row_id, cf_str, scf_str):
for k, i in rs.iteritems():
print "{%s:%s}" % (k, i)
print "%d results of key=%s,cf=%s,scf=%s" % (len(rs), row_id, cf_str, scf_str)
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)
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
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> <statistic> <period> <time_from> [time_to]"
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-0000002 vmnetwork 10.0.0.2 0 5 1332897600 0"
return
config = ConfigParser.ConfigParser()
config.read("kanyun.conf")
cfg = dict(config.items('client'))
context = zmq.Context()
apiclient = api_client.ApiClient(cfg['api_host'], cfg['api_port'])
api_socket = apiclient.socket
# api_socket = context.socket(zmq.REQ)
# api_socket.connect("tcp://%(api_host)s:%(api_port)s" % cfg)
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(api_socket, row_id, cf_str, scf_str)
show_result2(rs, row_id, cf_str, scf_str)
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(api_socket, cf_str)
show_result(rs, cf_str)
elif len(sys.argv) == 3 and sys.argv[1] == '-k':
row_id = sys.argv[2]
rs = api_client.invoke_getbykey(api_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(api_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(api_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(api_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(api_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(api_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(api_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(api_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)
# rs = api_client.invoke_statistics(api_socket, sys.argv[1], sys.argv[2], sys.argv[3],sys.argv[4], sys.argv[5], sys.argv[6], sys.argv[7])
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)
# rs = api_client.invoke_statistics(api_socket, sys.argv[1], sys.argv[2], sys.argv[3],sys.argv[4], sys.argv[5], sys.argv[6], '0')
return
if __name__ == '__main__':
main()
Jump to Line
Something went wrong with that request. Please try again.