From 3ed3c05a30c007bf1926f089e89acaafc95096be Mon Sep 17 00:00:00 2001 From: Simon Let Date: Thu, 16 May 2019 20:46:48 +0200 Subject: [PATCH] add a lot of metadata --- bashrc.sh | 63 +++++++++++++++++++++++++++++--- collect/resh-collect.go | 81 ++++++++++++++++++++++++++++++++++++++--- common/resh-common.go | 36 ++++++++++++++++-- 3 files changed, 165 insertions(+), 15 deletions(-) diff --git a/bashrc.sh b/bashrc.sh index 6f415a5..33855b3 100644 --- a/bashrc.sh +++ b/bashrc.sh @@ -1,22 +1,73 @@ PATH=$PATH:~/.resh/bin +export __RESH_RT_SESSION=$EPOCHREALTIME +export __RESH_RT_SESS_SINCE_BOOT=$(cat /proc/uptime | cut -d' ' -f1) #resh-daemon & disown preexec() { + # core __RESH_COLLECT=1 - __RESH_PWD="$PWD" __RESH_CMDLINE="$1" - __RESH_RT_BEFORE=$EPOCHREALTIME + + # posix + __RESH_COLS="$COLUMNS" + __RESH_HOME="$HOME" + __RESH_LANG="$LANG" + __RESH_LC_ALL="$LC_ALL" + # other LC ? + __RESH_LINES="$LINES" + __RESH_LOGIN="$LOGNAME" + __RESH_PATH="$PATH" + __RESH_PWD="$PWD" + __RESH_SHELL="$SHELL" + __RESH_TERM="$TERM" + + # non-posix + __RESH_PID="$BASHPID" # current pid + __RESH_SESSION_PID="$$" # pid of original shell + __RESH_WINDOWID="$WINDOWID" # session + __RESH_HOST="$HOSTNAME" + __RESH_HOSTTYPE="$HOSTTYPE" + __RESH_OSTYPE="$OSTYPE" + __RESH_MACHTYPE="$MACHTYPE" + + # time + __RESH_TZ_BEFORE="$TZ" # posix + __RESH_SECS_UTC_BEFORE=$(date +%s -u) + __RESH_RT_BEFORE="$EPOCHREALTIME" } precmd() { __RESH_EXIT_CODE=$? __RESH_RT_AFTER=$EPOCHREALTIME + __RESH_SECS_UTC_AFTER=$(date +%s -u) + __RESH_TZ_AFTER="$TZ" if [ ! -z ${__RESH_COLLECT+x} ]; then - resh-collect -cmd "$__RESH_CMDLINE" -status $__RESH_EXIT_CODE \ - -pwd "$PWD" \ - -realtimeBefore $__RESH_RT_BEFORE \ - -realtimeAfter $__RESH_RT_AFTER + resh-collect -cmdLine "$__RESH_CMDLINE" -exitCode "$__RESH_EXIT_CODE" \ + -cols "$__RESH_COLS" \ + -home "$__RESH_HOME" \ + -lang "$__RESH_LANG" \ + -lcAll "$__RESH_LC_ALL" \ + -lines "$__RESH_LINES" \ + -login "$__RESH_LOGIN" \ + -path "$__RESH_PATH" \ + -pwd "$__RESH_PWD" \ + -shell "$__RESH_SHELL" \ + -term "$__RESH_TERM" \ + -pid "$__RESH_PID" -sessionPid "$__RESH_SESSION_PID" \ + -windowId "$__RESH_WINDOWID" \ + -host "$__RESH_HOST" \ + -hosttype "$__RESH_HOSTTYPE" \ + -ostype "$__RESH_OSTYPE" \ + -machtype "$__RESH_MACHTYPE" \ + -realtimeBefore "$__RESH_RT_BEFORE" \ + -realtimeAfter "$__RESH_RT_AFTER" \ + -secsUtcBefore "$__RESH_SECS_UTC_BEFORE" \ + -secsUtcAfter "$__RESH_SECS_UTC_AFTER" \ + -realtimeSession "$__RESH_RT_SESSION" \ + -realtimeSessSinceBoot "$__RESH_RT_SESS_SINCE_BOOT" \ + -timezoneBefore "$__RESH_TZ_BEFORE" \ + -timezoneAfter "$__RESH_TZ_AFTER" fi unset __RESH_COLLECT } diff --git a/collect/resh-collect.go b/collect/resh-collect.go index ce97c0e..ed964f0 100644 --- a/collect/resh-collect.go +++ b/collect/resh-collect.go @@ -27,16 +27,54 @@ func main() { return } - cmdLine := flag.String("cmd", "", "command line") - exitCode := flag.Int("status", -1, "exit code") - pwd := flag.String("pwd", "", "present working directory") + cmdLine := flag.String("cmdLine", "", "command line") + exitCode := flag.Int("exitCode", -1, "exit code") + + // posix variables + cols := flag.Int("cols", -1, "$COLUMNS") + lines := flag.Int("lines", -1, "$LINES") + + home := flag.String("home", "", "$HOME") + lang := flag.String("lang", "", "$LANG") + lcAll := flag.String("lcAll", "", "$LC_ALL") + login := flag.String("login", "", "$LOGIN") + path := flag.String("path", "", "$PATH") + pwd := flag.String("pwd", "", "$PWD - present working directory") + shell := flag.String("shell", "", "$SHELL") + term := flag.String("term", "", "$TERM") + + // non-posix + pid := flag.Int("pid", -1, "$PID") + sessionPid := flag.Int("sessionPid", -1, "$$") + windowId := flag.Int("windowId", -1, "$WINDOWID - session id") + + host := flag.String("host", "", "$HOSTNAME") + hosttype := flag.String("hosttype", "", "$HOSTTYPE") + ostype := flag.String("ostype", "", "$OSTYPE") + machtype := flag.String("machtype", "", "$MACHTYPE") + + // before after + timezoneBefore := flag.String("timezoneBefore", "", "before $TZ") + timezoneAfter := flag.String("timezoneAfter", "", "after $TZ") + + secondsUtcBefore := flag.Int("secsUtcBefore", -1, "secs utc") + secondsUtcAfter := flag.Int("secsUtcAfter", -1, "secs utc") rtb := flag.String("realtimeBefore", "-1", "before $EPOCHREALTIME") rta := flag.String("realtimeAfter", "-1", "after $EPOCHREALTIME") + rtsess := flag.String("realtimeSession", "-1", + "on session start $EPOCHREALTIME") + rtsessboot := flag.String("realtimeSessSinceBoot", "-1", + "on session start $EPOCHREALTIME") flag.Parse() realtimeAfter, err := strconv.ParseFloat(*rta, 64) realtimeBefore, err := strconv.ParseFloat(*rtb, 64) + realtimeSessionStart, err := strconv.ParseFloat(*rtsess, 64) + realtimeSessSinceBoot, err := strconv.ParseFloat(*rtsessboot, 64) realtimeDuration := realtimeAfter - realtimeBefore + realtimeSinceSessionStart := realtimeBefore - realtimeSessionStart + realtimeSinceBoot := realtimeSessSinceBoot + realtimeSinceSessionStart + if err != nil { log.Fatal("Flag Parsing error:", err) } @@ -45,14 +83,45 @@ func main() { } rec := common.Record{ + // core CmdLine: *cmdLine, - Pwd: *pwd, - GitWorkTree: getGitDir(), - Shell: os.Getenv("SHELL"), ExitCode: *exitCode, + + // posix + Cols: *cols, + Lines: *lines, + + Home: *home, + Lang: *lang, + LcAll: *lcAll, + Login: *login, + Path: *path, + Pwd: *pwd, + Shell: *shell, + Term: *term, + + // non-posix + Pid: *pid, + SessionPid: *sessionPid, + WindowId: *windowId, + Host: *host, + Hosttype: *hosttype, + Ostype: *ostype, + Machtype: *machtype, + + // before after + TimezoneBefore: *timezoneBefore, + TimezoneAfter: *timezoneAfter, + RealtimeBefore: realtimeBefore, RealtimeAfter: realtimeAfter, + SecondsUtcBefore: *secondsUtcBefore, + SecondsUtcAfter: *secondsUtcAfter, RealtimeDuration: realtimeDuration, + RealtimeSinceSessionStart: realtimeSinceSessionStart, + RealtimeSinceBoot: realtimeSinceBoot, + + GitWorkTree: getGitDir(), } sendRecord(rec, strconv.Itoa(config.Port)) } diff --git a/common/resh-common.go b/common/resh-common.go index 8c51d01..e56c66d 100644 --- a/common/resh-common.go +++ b/common/resh-common.go @@ -1,15 +1,45 @@ package common type Record struct { + // core CmdLine string `json:"cmdLine"` - Pwd string `json:"pwd"` - GitWorkTree string `json:"gitWorkTree"` - Shell string `json:"shell"` ExitCode int `json:"exitCode"` + + // posix + Cols int `json:"cols"` + Lines int `json:"lines"` + Home string `json:"home"` + Lang string `json:"lang"` + LcAll string `json:"lcAll"` + Login string `json:"login"` + Path string `json:"path"` + Pwd string `json:"pwd"` + Shell string `json:"shell"` + Term string `json:"term"` + + // non-posix"` + Pid int `json:"pid"` + SessionPid int `json:"sessionPid"` + WindowId int `json:"windowId"` + Host string `json:"host"` + Hosttype string `json:"hosttype"` + Ostype string `json:"ostype"` + Machtype string `json:"machtype"` + + // before after + TimezoneBefore string `json:"timezoneBefore"` + TimezoneAfter string `json:"timezoneAfter"` + RealtimeBefore float64 `json:"realtimeBefore"` RealtimeAfter float64 `json:"realtimeAfter"` + SecondsUtcBefore int `json:"secondsUtcBefore"` + SecondsUtcAfter int `json:"secondsUtcAfter"` RealtimeDuration float64 `json:"realtimeDuration"` + RealtimeSinceSessionStart float64 `json:"realtimeSinceSessionStart"` + RealtimeSinceBoot float64 `json:"realtimeSinceBoot"` //Logs []string `json: "logs"` + + GitWorkTree string `json:"gitWorkTree"` } type Config struct {