Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tree: c19c022d0a
Fetching contributors…

Cannot retrieve contributors at this time

executable file 161 lines (138 sloc) 5.271 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 isALL=(ALL:ALL) ALL 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
from kanyun.worker.plugin_traffic_accounting import get_traffic_accounting_info
from kanyun.worker import plugin_agent
from kanyun.server.plugin_agent_srv import plugin_decoder_agent
from kanyun.common.nova_tools import NovaTools
from kanyun.common.app import App
from kanyun.database.cassadb import CassaDb
import random
def show_usage():
print "usage:"
print "\tkanyun <param> [option]"
print "param:"
print "\t -n : list network traffic, option=time-interval"
print "\t -vm : list virtualmachine info, option=time-interval"
print "\t -vi <id> : get vm info"
print "\t -cmd <instance_id> <instance_ip> <public_interface>"
print "\t -data <count> <instance-id>"
print "example:"
print "\t kanyun-manager -vi instance-00000166"
print "\t kanyun-manager -data 9 instance-00000017"
def vm(interval=60):
while True:
info = plugin_agent.plugin_call()
if not info is None and len(info) > 0:
print info
time.sleep(interval)
def network(interval=60):
while True:
for k, info in get_traffic_accounting_info().iteritems():
print k, info
time.sleep(interval)
def vminfo(app, instanceid):
tool = NovaTools(app)
id = tool.get_id(instanceid)
rs = tool.get_instances(id=id)
return rs
def show_iptables_cmd(instance_id, instance_ip, public_interface):
cmd1 = """sudo iptables -t filter -N nova-compute-f-inst-%s"""
cmd2 = """sudo iptables -I FORWARD -s %s -j nova-compute-f-inst-%s"""
cmd3 = """sudo iptables -A nova-compute-f-inst-%s -o %s -m comment --comment " %s %s accounting rule " """
print cmd1 % instance_id
print cmd2 % (instance_ip, instance_id)
print cmd3 % (instance_id, public_interface, instance_id, instance_ip)
print "Use 'sudo iptables -n -L|grep Chain|grep nova-compute' to look the policy."
def create_test_data(app, count=1, key="instance-00000001@pyw.novalocal"):
# insert test data
cfg = app.get_cfg('server')
tool = NovaTools(app)
db = CassaDb('data', cfg['db_host'])
for i in range(0, count):
data = {key:
[
["cpu", "total", [1343794332, random.randint(0, 100)]],
["mem", "total", [1343794332, 131072, 131072]],
["nic", "vnet0", [1343794332, 21795245, 5775663]],
["blk", "vda", [1343794332, 474624, 4851712]],
["blk", "vdb", [1343794332, 122880, 0]]
]
}
plugin_decoder_agent(tool, db, data)
#plugin_traffic_accounting(app, db, data)
def main():
if len(sys.argv) == 1:
show_usage()
return
if len(sys.argv) == 2:
if sys.argv[1] in ['--help', "-h"]:
print "sudo iptables-save -t filter -c|grep 'accounting rule'"
print "Use 'sudo virsh list' to look the list of instances."
print "Use 'sudo virsh dumpxml <id>|grep IP' to look the ipaddress of instance."
show_usage()
return
elif sys.argv[1] in ["?"]:
show_usage()
return
app = App(conf="kanyun.conf", name='kanyun')
if len(sys.argv) == 2:
if sys.argv[1] == '-n':
network(interval=60)
return
if sys.argv[1] == '-vm':
vm(interval=60)
return
if sys.argv[1] == '-data':
key = None
count = 1
if len(sys.argv) > 2:
count = int(sys.argv[2])
if len(sys.argv) > 3:
key = sys.argv[3]
create_test_data(app, count=count, key=key)
elif len(sys.argv) == 3:
option = sys.argv[2]
if sys.argv[1] == '-n':
network(interval=int(option))
return
elif sys.argv[1] == '-vm':
vm(interval=int(option))
return
elif sys.argv[1] == '-vi':
rs = vminfo(app=app, instanceid=option)
print "id : ", rs[0]
print "uuid : ", rs[1]
print "display_name: ", rs[2]
else:
return
elif len(sys.argv) == 5:
if sys.argv[1] == '-cmd':
instance_id = sys.argv[2]
instance_ip = sys.argv[3]
public_interface = sys.argv[4]
show_iptables_cmd(
instance_id=instance_id,
instance_ip=instance_ip,
public_interface=public_interface)
else:
pass
if __name__ == '__main__':
main()
Jump to Line
Something went wrong with that request. Please try again.