diff --git a/scalpels/agents/server.py b/scalpels/agents/server.py index 97356fb..ef730cb 100644 --- a/scalpels/agents/server.py +++ b/scalpels/agents/server.py @@ -23,8 +23,8 @@ class TraceEndpoint(object): def tracer_list(self, ctx): # TODO db_api # XXX ctx required? - from scalpels.cli.actions.start import agents_map - return agents_map + from scalpels.cli.utils import traces_map + return traces_map def start_tracers(self, ctx, tracers): print locals() diff --git a/scalpels/cli/actions/load.py b/scalpels/cli/actions/load.py index 437b7ab..986e4e7 100644 --- a/scalpels/cli/actions/load.py +++ b/scalpels/cli/actions/load.py @@ -15,7 +15,7 @@ def run(config): continue loadcall(config) -def get_creds_from_env(): +def _get_creds_from_env(): user = os.environ.get("OS_USERNAME") pw = os.environ.get("OS_PASSWORD") tenant = os.environ.get("OS_TENANT_NAME") @@ -23,7 +23,7 @@ def get_creds_from_env(): return (user, pw, tenant, auth_url) def nova_boot_bulk(): - creds = get_creds_from_env() + creds = _get_creds_from_env() if None in creds: raise ValueError("can't find all necessary creds from env: %s" % creds) nova = client.Client(2, *creds) diff --git a/scalpels/cli/actions/report.py b/scalpels/cli/actions/report.py index dd05419..da3b83d 100644 --- a/scalpels/cli/actions/report.py +++ b/scalpels/cli/actions/report.py @@ -3,54 +3,14 @@ # Author: Kun Huang from scalpels.cli.api import api as agent_api -from prettytable import PrettyTable -from mako.lookup import TemplateLookup -from scalpels import templates -import os +from scalpels.cli.utils import generate_multiple_result_html +from scalpels.cli.utils import pprint_result -def pprint_result(result): - print "" % result["uuid"] - t = PrettyTable(["timestamp", "%s (%s)" % (result["name"], result["unit"])]) - for data in result["data"]: - t.add_row([data[0], data[1][:100]]) - print t - -LOWEST=8 - -def generate_result_html(result): - if result.rtype == "stream": - tmpl_dir = os.path.dirname(templates.__file__) - lookup = TemplateLookup(directories=[tmpl_dir]) - t = lookup.get_template("line-chart.mako") - print t.render(**result.__dict__) - -def generate_multiple_result_html(results): - tmpl_dir = os.path.dirname(templates.__file__) - lookup = TemplateLookup(directories=[tmpl_dir]) - t = lookup.get_template("multi-line-chart.mako") - d = {"results": results} - print t.render(**d) - def run(config): - uuid = config.get("uuid") - last = config.get("last") + task = agent_api.try_get_task_from_config(config) - if last and uuid: - raise ValueError("can't assign last and uuid togther") - elif not last and not uuid: - task = agent_api.get_latest_task() - elif last: - task = agent_api.get_latest_task() - elif uuid and len(uuid) < LOWEST: - print "at least %d to find a task" % LOWEST - return - else: - # len(uuid) > LOWEST - task = agent_api.get_task(uuid, fuzzy=True) - - print "command report: %s" % config - print "task: <%s>" % task["uuid"] + print "reporting task: <%s>" % task["uuid"] rets = [] for ret_uuid in task["results"]: ret = agent_api.get_result(ret_uuid) diff --git a/scalpels/cli/actions/start.py b/scalpels/cli/actions/start.py index e180da9..f52210d 100644 --- a/scalpels/cli/actions/start.py +++ b/scalpels/cli/actions/start.py @@ -29,19 +29,6 @@ def _parse_agents_from_file(config): parsed_agents.add(ag["name"]) return parsed_agents -# TODO this map should be saved in a config file -# TODO refar to pre/exec/post -agents_map = { - "mysql": "bash %s/mysql-live.sh", #XXX doesn't work now, needs works on interapt pipeline - "rabbit": "python %s/rbt-trace.py", - "rpc": "bash %s/port-input-traffic.sh 5672", - "traffic": "bash %s/device-input-traffic.sh eth0", - "oslolock": "stap %s/oslo-lock.stp", # with sudo, need add current user to stapdev group - "modelsave": "stap %s/model-save.stp", # with sudo, need add current user to stapdev group - "sqlaexec": "stap %s/sqla-exec.stp", # with sudo, need add current user to stapdev group - "rpccount": "stap %s/rpc-count.stp", # with sudo, need add current user to stapdev group -} - def run(config): print "command start: %s" % config agents = _parse_agents_from_args(config) diff --git a/scalpels/cli/actions/stop.py b/scalpels/cli/actions/stop.py index 2cda4f0..ee43bb9 100644 --- a/scalpels/cli/actions/stop.py +++ b/scalpels/cli/actions/stop.py @@ -2,29 +2,11 @@ #-*- coding:utf-8 -*- # Author: Kun Huang -from scalpels.db import api as db_api from scalpels.cli.api import api as agent_api -LOWEST=8 - -def get_last_task(): - last_task = db_api.task_get_last() - return last_task def run(config): - uuid = config.get("uuid") - last = config.get("last") + task = agent_api.try_get_task_from_config(config) - if last and uuid: - raise ValueError("can't assign last and uuid togther") - elif not last and not uuid: - task = agent_api.get_latest_task() - elif last: - task = agent_api.get_latest_task() - elif uuid and len(uuid) < LOWEST: - print "at least %d to find a task" % LOWEST - return - else: - # len(uuid) > LOWEST - task = agent_api.get_task(uuid, fuzzy=True) + print "stopping task: <%s>" % task["uuid"] agent_api.stop_task(task["uuid"]) diff --git a/scalpels/cli/api.py b/scalpels/cli/api.py index 7c181a0..b17dadd 100644 --- a/scalpels/cli/api.py +++ b/scalpels/cli/api.py @@ -4,6 +4,7 @@ from scalpels.cli.rpcapi import rpcapi +from scalpels.cli.utils import UUID_LOWEST_SUPPORT class API(object): def __init__(self): @@ -19,6 +20,8 @@ class API(object): rpcapi.stop_task(uuid=uuid) def get_task(self, uuid, fuzzy=False): + if fuzzy and len(uuid) < UUID_LOWEST_SUPPORT: + raise ValueError("fuzzy uuid query must get %s length" % UUID_LOWEST_SUPPORT) return rpcapi.get_task(uuid=uuid, fuzzy=fuzzy) def get_latest_task(self): @@ -30,4 +33,15 @@ class API(object): def get_all_results(self): return rpcapi.get_all_results() + def try_get_task_from_config(self, config): + uuid = config.get("uuid") + last = config.get("last") + + if last and uuid: + raise ValueError("can't assign last and uuid togther") + elif uuid: + return self.get_task(uuid, fuzzy=True) + else: # no matter whether last is set + return self.get_latest_task() + api = API() diff --git a/scalpels/cli/utils.py b/scalpels/cli/utils.py new file mode 100644 index 0000000..800bfb6 --- /dev/null +++ b/scalpels/cli/utils.py @@ -0,0 +1,45 @@ +#!/usr/bin/env python +#-*- coding:utf-8 -*- +# Author: Kun Huang + +from mako.lookup import TemplateLookup +from prettytable import PrettyTable +from scalpels import templates +import os + +UUID_LOWEST_SUPPORT = 8 + +# TODO this map should be saved in a config file +# TODO refar to pre/exec/post +tracers_map = { + "mysql": "bash %s/mysql-live.sh", #XXX doesn't work now, needs works on interapt pipeline + "rabbit": "python %s/rbt-trace.py", + "rpc": "bash %s/port-input-traffic.sh 5672", + "traffic": "bash %s/device-input-traffic.sh eth0", + "oslolock": "stap %s/oslo-lock.stp", # with sudo, need add current user to stapdev group + "modelsave": "stap %s/model-save.stp", # with sudo, need add current user to stapdev group + "sqlaexec": "stap %s/sqla-exec.stp", # with sudo, need add current user to stapdev group + "rpccount": "stap %s/rpc-count.stp", # with sudo, need add current user to stapdev group +} + + +def generate_result_html(result): + if result.rtype == "stream": + tmpl_dir = os.path.dirname(templates.__file__) + lookup = TemplateLookup(directories=[tmpl_dir]) + t = lookup.get_template("line-chart.mako") + print t.render(**result.__dict__) + +def generate_multiple_result_html(results): + tmpl_dir = os.path.dirname(templates.__file__) + lookup = TemplateLookup(directories=[tmpl_dir]) + t = lookup.get_template("multi-line-chart.mako") + d = {"results": results} + print t.render(**d) + +def pprint_result(result): + print "" % result["uuid"] + t = PrettyTable(["timestamp", "%s (%s)" % (result["name"], result["unit"])]) + for data in result["data"]: + t.add_row([data[0], data[1][:100]]) + print t diff --git a/scripts/agent.py b/scripts/agent.py index ae46e7f..e5f1a9f 100755 --- a/scripts/agent.py +++ b/scripts/agent.py @@ -18,7 +18,7 @@ python /agent.py mysql def read_from_ag(ag): # wrong impl. here, need read from config or db instead - from scalpels.cli.actions.start import agents_map + from scalpels.cli.utils import tracers_map as agents_map data_dir = db_api.setup_config_get()["data_dir"].rstrip("/") return agents_map.get(ag) % data_dir