add is_running status to tracers

Change-Id: Ia197bd256e05be6c20f3c4456af996788891417c
This commit is contained in:
Kun Huang 2015-11-18 11:43:11 +08:00
parent ff99607004
commit b13adc44a6
10 changed files with 54 additions and 13 deletions

View File

@ -22,9 +22,11 @@ class TraceEndpoint(object):
def tracer_list(self, ctx): def tracer_list(self, ctx):
tracers = db_api.tracer_list() tracers = db_api.tracer_list()
ret = {} ret = list()
for tr in tracers: for tr in tracers:
ret[tr.name] = tr.template ret.append({"name":tr.name,
"tpl":tr.template,
"running":tr.is_running})
return ret return ret
def start_tracers(self, ctx, tracers): def start_tracers(self, ctx, tracers):
@ -43,6 +45,11 @@ class TraceEndpoint(object):
db_api.register_tracer(name=tracer_opts["name"], template=tracer_opts["tpl"]) db_api.register_tracer(name=tracer_opts["name"], template=tracer_opts["tpl"])
print "[LOG] registering tracer %(name)s: %(tpl)s" % tracer_opts print "[LOG] registering tracer %(name)s: %(tpl)s" % tracer_opts
def set_tracer_stat(self, ctx, tracer, running):
running=bool(running)
print "[LOG] setting tracer: %s running: %s" % (tracer, running)
db_api.tracer_update(tracer, running=running)
class TaskEndpoint(object): class TaskEndpoint(object):
target = oslo_messaging.Target(topic="test", version='1.0') target = oslo_messaging.Target(topic="test", version='1.0')

View File

@ -8,7 +8,7 @@ from prettytable import PrettyTable
def run(config): def run(config):
tracers = agent_api.get_tracer_list() tracers = agent_api.get_tracer_list()
t = PrettyTable(["tracer", "tracer template"]) t = PrettyTable(["tracer", "tracer template", "is running"])
for tracer, script in tracers.items(): for tr in tracers:
t.add_row([tracer, script]) t.add_row([tr["name"],tr["tpl"],tr["running"]])
print t print t

View File

@ -57,5 +57,8 @@ class API(object):
def get_config(self): def get_config(self):
return rpcapi.get_config() return rpcapi.get_config()
def set_tracer_stat(self, tracer, running):
rpcapi.set_tracer_stat(tracer=tracer, running=running)
api = API() api = API()

View File

@ -37,5 +37,8 @@ class RPCAPI(object):
def get_config(self, ctxt={}): def get_config(self, ctxt={}):
return self._client.call(ctxt, "get_config") return self._client.call(ctxt, "get_config")
def set_tracer_stat(self, ctxt={}, tracer=None, running=None):
self._client.cast(ctxt, "set_tracer_stat", tracer=tracer, running=running)
transport = messaging.get_transport(cfg.CONF) transport = messaging.get_transport(cfg.CONF)
rpcapi = RPCAPI(transport) rpcapi = RPCAPI(transport)

View File

@ -3,9 +3,13 @@
# Author: Kun Huang <academicgareth@gmail.com> # Author: Kun Huang <academicgareth@gmail.com>
from scalpels.agents.server import server from scalpels.agents.server import server
from oslo_log import log as logging
from oslo_config import cfg
def main(): def main():
# TODO handle stop later # TODO handle stop later
logging.register_options(cfg.CONF)
logging.setup(cfg.CONF, "scalpels")
server.start() server.start()
server.wait() server.wait()

View File

@ -30,15 +30,16 @@ def read_from_ag(ag):
# wrong impl. here, need read from config or db instead # wrong impl. here, need read from config or db instead
config = agent_api.get_config() config = agent_api.get_config()
tracers = agent_api.get_tracer_list() tracers = agent_api.get_tracer_list()
if ag not in tracers.keys(): for tr in tracers:
raise ValueError("tracer %s is not found" % ag) if tr["name"] == ag:
tpl = tracers[ag] return tr["tpl"] % config
return tpl % config raise ValueError("tracer %s is not found" % ag)
def handle_int(signal, frame): def handle_int(signal, frame):
print "[LOG] xxx is interupted" print "[LOG] xxx is interupted"
stop_tracer() stop_tracer()
save_result_to_task() save_result_to_task()
agent_api.set_tracer_stat(ag, running=False)
sys.exit(0) sys.exit(0)
def stop_tracer(): def stop_tracer():
@ -83,8 +84,11 @@ def main():
worker = subprocess.Popen(cmd.split(), stdout=subprocess.PIPE) worker = subprocess.Popen(cmd.split(), stdout=subprocess.PIPE)
worker_pid = worker.pid worker_pid = worker.pid
# this time, we got the pid, so tracer is running
agent_api.set_tracer_stat(ag, running=True)
while True: while True:
t = worker.stdout.readline() t = worker.stdout.readline()
if not len(t): if not len(t):
break break
_t = (time.time(), t.strip()) _t = (time.time(), t.strip())

View File

@ -68,3 +68,12 @@ def update_config(data_opts):
def get_config(): def get_config():
return IMPL.get_config() return IMPL.get_config()
def tracer_get(tracer):
"""
param tracer: tracer name, like, 'rpc'
"""
return IMPL.tracer_get(tracer)
def tracer_update(tracer, running):
return IMPL.tracer_update(tracer, running)

View File

@ -139,3 +139,15 @@ def update_config(data_opts):
def get_config(): def get_config():
config = model_query(models.Setup).first() config = model_query(models.Setup).first()
return config.config return config.config
def tracer_get(tracer):
tracer = model_query(models.Tracer).filter_by(name=tracer).first()
return tracer
def tracer_update(tracer_name, running):
session = get_session()
tracer = model_query(models.Tracer, session=session).filter_by(name=tracer_name).first()
tracer.update({"is_running":running})
tracer.save(session=session)
print "[LOG] %s db update running %s" % (tracer.name, running)
return tracer

View File

@ -7,7 +7,7 @@ import json
from sqlalchemy import types as sqa_types from sqlalchemy import types as sqa_types
from scalpels.db.sqlalchemy import BASE from scalpels.db.sqlalchemy import BASE
from sqlalchemy import (Column, Integer, String) from sqlalchemy import (Column, Integer, String, Boolean)
from oslo_db.sqlalchemy import models as oslodbmodels from oslo_db.sqlalchemy import models as oslodbmodels
@ -56,5 +56,4 @@ class Tracer(BASE, ScalpelsBase):
id = Column(Integer, primary_key=True, autoincrement=True) id = Column(Integer, primary_key=True, autoincrement=True)
name = Column(String(20), nullable=False, unique=True) name = Column(String(20), nullable=False, unique=True)
template = Column(String(20), nullable=False) template = Column(String(20), nullable=False)
# yes for running, no for not is_running = Column(Boolean(), default=False)
status = Column(String(20), default=lambda : "No", nullable=False)

View File

@ -1,7 +1,7 @@
#!/bin/bash -x #!/bin/bash -x
# Author: Kun Huang <academicgareth@gmail.com> # Author: Kun Huang <academicgareth@gmail.com>
sca-manage setup -f sca-manage db-create -f
sca-manage setup -d rpcport=5672 -t name=rpc -t tpl="bash %(tracer_path)s/port-input-traffic.sh %(rpcport)s" sca-manage setup -d rpcport=5672 -t name=rpc -t tpl="bash %(tracer_path)s/port-input-traffic.sh %(rpcport)s"
sca-manage setup -t name=mysql -t tpl="bash %(tracer_path)s/mysql-live.sh" sca-manage setup -t name=mysql -t tpl="bash %(tracer_path)s/mysql-live.sh"
sca-manage setup -t name=rabbit -t tpl="python %(tracer_path)s/rbt-trace.py" sca-manage setup -t name=rabbit -t tpl="python %(tracer_path)s/rbt-trace.py"