From 3ec61813cf3bcc52af1f3b2cda7829565bdae4c5 Mon Sep 17 00:00:00 2001 From: Kun Huang Date: Fri, 23 Oct 2015 04:56:29 +0800 Subject: [PATCH] read data from subprocess, but it's bad for shell scripts --- scalpels/cli/actions/start.py | 14 +++++++++----- scripts/mysql-live.sh | 30 ++++++++++++++++++------------ 2 files changed, 27 insertions(+), 17 deletions(-) diff --git a/scalpels/cli/actions/start.py b/scalpels/cli/actions/start.py index 5e10aaa..c2b9036 100644 --- a/scalpels/cli/actions/start.py +++ b/scalpels/cli/actions/start.py @@ -7,6 +7,7 @@ import json from scalpels.db import api as db_api import subprocess import time +import signal def _parse_agents_from_args(config): parsed_agents = set() @@ -32,8 +33,9 @@ def _parse_agents_from_file(config): return parsed_agents # TODO this map should be saved in a config file +# TODO refar to pre/exec/post agents_map = { - "mysql": "", + "mysql": "bash /opt/stack/scalpels/scripts/mysql-live.sh", "rabbit": "", "traffic": "", "rpctraffic": "", @@ -47,13 +49,15 @@ def run(config): for ag in agents: ag_exec = agents_map.get(ag) if ag_exec: - ag_p = subprocess.Popen(ag_exec, stdout=subprocess.PIPE) + ag_p = subprocess.Popen(ag_exec.split(), stdout=subprocess.PIPE) running_agents.append(ag_p) - time.sleep(15) + time.sleep(5) data = [] for ag_p in running_agents: - stdout = ag_p.communicate()[0] - ag_p.terminate() + # shell scripts has depend child which can't be killed by subprocess' API + # it should be ag_p.kill() + os.system("pkill -P %s" % ag_p.pid) + stdout = ag_p.stdout.read() data.append(stdout) rets = [] ret = db_api.result_create(data) diff --git a/scripts/mysql-live.sh b/scripts/mysql-live.sh index c2b92c0..4a0e4d9 100755 --- a/scripts/mysql-live.sh +++ b/scripts/mysql-live.sh @@ -11,7 +11,20 @@ log_switch_var=general_log log_file=/tmp/mysqllive.log old_log_file=`mysql -e "SELECT @@$log_file_var" | grep -v $log_file_var | grep -v '\-\-\-\-\-'` old_log_switch=`mysql -e "SELECT @@$log_switch_var" | grep -v $log_switch_var | grep -v '\-\-\-\-\-'` -trap ':' INT + +reset () { + echo ------------------------------------- + echo reset $log_file_var to $old_log_file + echo reset $log_switch_var to $old_log_switch + mysql -e "SET GLOBAL $log_switch_var = $old_log_switch;" + mysql -e "SET GLOBAL $log_file_var = '$old_log_file';" + + echo remove $log_file + echo ------------------------------------- + sudo rm $log_file +} + +trap "reset" SIGINT SIGTERM echo ------------------------------------- echo reserve $log_file_var: $log_file @@ -21,8 +34,11 @@ echo ------------------------------------- mysql -e "SET GLOBAL $log_file_var = '$log_file';" mysql -e "SET GLOBAL $log_switch_var = ON;" +sleep 1 +sudo chmod +r $log_file + # TODO use awk /reg/ statement instead -sudo tailf $log_file | awk '{ +tailf $log_file | awk '{ if ( $1 + 0 != $1 ) # TODO cat this line on its above line print $0; @@ -37,13 +53,3 @@ else { $1=$2=""; print $0; print ""} } ' - -echo ------------------------------------- -echo reset $log_file_var to $old_log_file -echo reset $log_switch_var to $old_log_switch -mysql -e "SET GLOBAL $log_switch_var = $old_log_switch;" -mysql -e "SET GLOBAL $log_file_var = '$old_log_file';" - -echo remove $log_file -echo ------------------------------------- -sudo rm $log_file