Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

fix static result bug.

  • Loading branch information...
commit 2ca76f790b97b1b87545d3fbcb79739b0c6d693c 1 parent 0fabebf
@hokerffb hokerffb authored
View
44 bin/api-client
@@ -5,8 +5,10 @@ import time
import ConfigParser
import json
import zmq
+from collections import OrderedDict
from kanyun.client import api_client
+from kanyun.common import const
# Author: Peng Yuwei<yuwei5@staff.sina.com.cn> 2012-3-27
# Last update: Peng Yuwei<yuwei5@staff.sina.com.cn> 2012-4-5
@@ -25,7 +27,27 @@ def show_result3(rs, row_id):
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]
+
+ rs = OrderedDict(sorted(rs.items(), key=lambda t: t[0]))
+
+ 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 + ":"
+ 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'
@@ -56,8 +78,10 @@ def main():
context = zmq.Context()
- api_socket = context.socket(zmq.REQ)
- api_socket.connect("tcp://%(api_host)s:%(api_port)s" % cfg)
+ 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]
@@ -110,10 +134,22 @@ def main():
show_result2(rs, row_id, cf_str, scf_str)
return
elif len(sys.argv) == 8:
- 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])
+ 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:
- 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')
+ 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
View
27 bin/listip
@@ -2,6 +2,8 @@
# coding: utf-8
import sys
+import traceback
+import os
import subprocess
import shlex
from nova import context
@@ -34,16 +36,24 @@ def list_instance_ip():
print line[1], info['address']
ret[line[1]] = info['address']
break
+ print len(ret), 'instances.'
return ret
def main():
- cmd1 = """sudo iptables -t raw -A PREROUTING -s %s -m comment --comment '%s'"""
- cmd2 = """sudo iptables -t raw -I PREROUTING -s %s -m comment --comment '%s'"""
setup = False # auto setup iptables
if len(sys.argv) > 1:
if sys.argv[1] == '-a':
setup = True
+ elif sys.argv[1] == '?' or sys.argv[1] == "--help" or sys.argv[1] == "-h":
+ print "Show instance's ip address"
+ print 'usage: listip [-a]'
+ print '\t[-a] : auto set iptables policy.'
+ print
+ return
+
+ cmd1 = """sudo iptables -t raw -A PREROUTING -s %s -m comment --comment '%s'"""
+ cmd2 = """sudo iptables -t raw -I PREROUTING -s %s -m comment --comment '%s'"""
utils.default_flagfile()
try:
flags.FLAGS(sys.argv[:2])
@@ -57,19 +67,22 @@ def main():
for k, i in ret.iteritems():
cmd = cmd1 % (i, k)
try:
- subprocess.call(cmd)
+ os.system(cmd)
except:
has_error = True
print cmd
cmd = cmd2 % (i, k)
try:
- subprocess.call(cmd)
+ os.system(cmd)
except:
has_error = True
print cmd
- if has_error:
- print 'Error.'
-
+ traceback.print_stack()
+ if has_error:
+ print 'Auto set iptables policy failed.'
+ else:
+ print "Use 'sudo iptables -L -n' to look the policy."
+
if __name__ == '__main__':
main()
View
12 kanyun/client/api_client.py
@@ -120,6 +120,14 @@ def __init__(self, api_host = '127.0.0.1', api_port = '5556'):
self.context = zmq.Context()
self.socket = self.context.socket(zmq.REQ)
self.socket.connect("tcp://%s:%s" % (api_host, api_port))
+ def set_param(self, key=u'', cf_str=u'', scf_str=u'', statistic=0, period=5, time_from=0, time_to=0):
+ self.cf_str = cf_str
+ self.scf_str = scf_str
+ self.statistic = statistic
+ self.period = period
+ self.time_from = time_from
+ self.time_to = time_to
+ self.key = key
def invoke(self):
param = ['S', self.key, self.cf_str, self.scf_str, int(self.statistic), int(self.period), int(self.time_from), int(self.time_to)]
r = invoke(self.socket, param)
@@ -140,6 +148,10 @@ def get_average(self):
self.period = STATISTIC.AVERAGE
r = self.invoke()
return None if r is None else r.values()[0]
+ def get_result(self, s):
+ self.statistic = s
+ r = self.invoke()
+ return r
def getbykey(self, key, cf_str=None, scf_str=None):
if cf_str is None or scf_str is None:
return invoke_getbykey(self.socket, key)
View
8 kanyun/common/const.py
@@ -22,3 +22,11 @@ class STATISTIC:
MINIMUM = 2
AVERAGE = 3
SAMPLES = 4
+
+statistic_str = dict()
+statistic_str[STATISTIC.SUM] = "SUM"
+statistic_str[STATISTIC.MAXIMUM] = "MAXIMUM"
+statistic_str[STATISTIC.MINIMUM] = "MINIMUM"
+statistic_str[STATISTIC.AVERAGE] = "AVERAGE"
+statistic_str[STATISTIC.SAMPLES] = "SAMPLES"
+
View
2  kanyun/database/cassadb.py
@@ -40,7 +40,7 @@ def get_range(self, cf_str):
pass
return rs
-
+
def get(self, cf_str, key, super_column, column_start, column_finish, column_count = 20000):
"""get(key[, columns][, column_start][, column_finish][, column_count]
[, column_reversed][, include_timestamp][, super_column][, read_consistency_level])
View
29 kanyun/server/api_server.py
@@ -12,6 +12,7 @@
import time
import types
import json
+import logging
import traceback
import ConfigParser
import zmq
@@ -41,6 +42,11 @@
[u'S', u'instance-00000001@pyw.novalocal', u'cpu', u'total', 0, 5, 1332897600, 0]
"""
+logger = logging.getLogger()
+handler = logging.FileHandler("/tmp/api-server.log")
+logger.addHandler(handler)
+logger.setLevel(logging.NOTSET)
+
class Statistics():
def __init__(self):
@@ -122,6 +128,7 @@ def get_db():
def api_getdata(row_id, cf_str, scf_str, time_from=0, time_to=0):
"""
+ param type: UnicodeType and IntType
return: recordset, count, bool(count > limit?)
"""
if not type(row_id) is types.UnicodeType \
@@ -136,13 +143,15 @@ def api_getdata(row_id, cf_str, scf_str, time_from=0, time_to=0):
if time_to == 0:
time_to = time.time()
- rs = db.get(cf_str, row_id, super_column=scf_str, column_start=time_from, column_finish=int(float(time_to)), column_count=20000)
+ rs = db.get(cf_str, row_id, super_column=scf_str, column_start=time_from, column_finish=time_to, column_count=20000)
count = 0 if rs is None else len(rs)
return rs, count, False if (count == 20000) else True
def analyize_data(rs, period, statistic):
- """[private func]analyize the data"""
+ """[private func]analyize the data
+ period: minutes
+ """
if rs is None or not type(period) is types.IntType or not type(statistic) is types.IntType:
return None
t = 0
@@ -156,12 +165,12 @@ def analyize_data(rs, period, statistic):
if t == 0:
print '\tget first value'
st.clean()
- t = key
+ t = timestmp
key_time = time.gmtime(timestmp)
- if key >= t + period:
- print '\tnext'
+ if timestmp >= t + period*60:
+ print '\tnext', key, ">=", t, "+", period
st.clean()
- t = key
+ t = timestmp
key_time = time.gmtime(timestmp)
st.update(float(value))
key2 = time.mktime((key_time.tm_year, key_time.tm_mon, key_time.tm_mday, key_time.tm_hour, key_time.tm_min,0,0,0,0))
@@ -210,7 +219,7 @@ def api_getbyInstanceID(row_id, cf_str):
def api_getbykey(row_id, cf_str, scf_str, limit=20000):
"""
- example:cf=vmnetwork,scf=10.0.0.1,key=instance-0000002
+ example:cf=u'vmnetwork',scf=u'10.0.0.1',key=u'instance-0000002'
return: recordset, count, bool(count > limit?)
"""
if not type(row_id) is types.UnicodeType \
@@ -244,7 +253,11 @@ def api_statistic(row_id, cf_str, scf_str, statistic, period=5, time_from=0, tim
if not rs is None and count > 0:
buf = analyize_data(rs, 1, statistic)
ret = analyize_data(buf, period, statistic)
- ret_len = 0 if ret is None else len(ret)
+ if ret is None:
+ ret_len = 0
+ else:
+ ret = OrderedDict(sorted(ret.items(), key=lambda t: t[0]))
+ ret_len = len(ret)
print ret_len, "result."
else:
print "no result."
View
8 kanyun/worker/plugin_agent.py
@@ -57,7 +57,7 @@ def update(self, value):
self.count += 1
if self.first:
self.first = False
- self.diff = 0
+ self.diff = 0.0
self.previous = value
self.time_pass = time.time() - self.previous_time
self.previous_time = time.time()
@@ -150,9 +150,9 @@ def _collect_cpu_mem_info(self, dom_id, dom_conn):
if not self.diffs.has_key(dom_id):
self.diffs[dom_id] = Diff()
self.diffs[dom_id].update(dom_cpu_time)
- #%CPU = 100 * cpu_time_diff / (t * nr_cores * 10e9)
- print "%d * %f / (%d * 1 * %d)" % (100.0, self.diffs[dom_id].get_diff(), self.diffs[dom_id].get_time_pass(), 10e9)
- cpu = 100.0 * self.diffs[dom_id].get_diff() / (self.diffs[dom_id].get_time_pass() * 1 * 10e9)
+ #%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
# NOTE(lzyeval): libvirt currently can only see total of all vcpu time
# return [('cpu', 'total', (timestamp, dom_cpu_time)),
View
30 tests/test_cassadb.py
@@ -38,27 +38,25 @@ def ObjectTest(self):
column_start, column_finish = None, None
column_count = 0
- # TODO:how to mox the class?how to mox the constructor func of class?
-# self.mox.StubOutWithMock(db, 'insert')
-# db.insert().AndReturn('')
-#
-# self.mox.StubOutWithMock(cassadb, 'CassaDb')
-# cassadb.CassaDb().AndReturn(cassadbMox())
-# self.mox.ReplayAll()
-#
-# db = cassadb.CassaDb()
-# db.insert(cf_str, key, values)
-# db.get_range(cf_str)
-# db.get(cf_str, key, super_column, column_start, column_finish, column_count = 20000)
-# db.getbykey(cf_str, key)
-# db.getbykey2(cf_str, key, super_column, column_count)
-# db.get_cf(cf_str)
- self.mox.VerifyAll()
+ def GetTest(self):
+ cf_str = "cpu"
+ key = "instance-00000001@pyw.novalocal"
+ super_column = 'total'
+ column_start, column_finish = '', ''
+ column_count = 5
+ column_reversed = True
+
+ pool = pycassa.ConnectionPool('data', server_list=['127.0.0.1'])
+ cf = pycassa.ColumnFamily(pool, cf_str)
+ rs = cf.get(key=key, super_column=super_column, column_start=column_start, column_finish=column_finish, column_reversed=True, column_count=column_count)
+ print rs
+
if __name__ == '__main__':
print 'Unit test of worker.'
DBTestSuite = unittest.TestSuite()
DBTestSuite.addTest(CassaDBTest("ObjectTest"))
+ DBTestSuite.addTest(CassaDBTest("GetTest"))
runner = unittest.TextTestRunner()
runner.run(DBTestSuite)
Please sign in to comment.
Something went wrong with that request. Please try again.