diff --git a/Makefile b/Makefile index 90bc284..69750be 100644 --- a/Makefile +++ b/Makefile @@ -13,9 +13,8 @@ install: build | $(HOME)/.resh $(HOME)/.resh/bin $(HOME)/.config/resh echo '[[ -f ~/.resh/bashrc ]] && source ~/.resh/bashrc' >> ~/.bashrc grep '[[ -f ~/.bash-preexec.sh ]] && source ~/.bash-preexec.sh' ~/.bashrc ||\ echo '[[ -f ~/.bash-preexec.sh ]] && source ~/.bash-preexec.sh' >> ~/.bashrc - #-pkill resh-daemon - #resh-daemon & disown - + -pkill resh-daemon + nohup resh-daemon &>/dev/null & disown resh-daemon: daemon/resh-daemon.go common/resh-common.go go build -o $@ $< diff --git a/bashrc.sh b/bashrc.sh index 873ef32..7e51e3b 100644 --- a/bashrc.sh +++ b/bashrc.sh @@ -2,7 +2,7 @@ PATH=$PATH:~/.resh/bin export __RESH_RT_SESSION=$EPOCHREALTIME export __RESH_RT_SESS_SINCE_BOOT=$(cat /proc/uptime | cut -d' ' -f1) -#resh-daemon & disown +nohup resh-daemon &>/dev/null & disown preexec() { # core @@ -30,6 +30,7 @@ preexec() { __RESH_HOSTTYPE="$HOSTTYPE" __RESH_OSTYPE="$OSTYPE" __RESH_MACHTYPE="$MACHTYPE" + __RESH_SHLVL="$SHLVL" # time __RESH_TZ_BEFORE=$(date +%:z) @@ -59,6 +60,7 @@ precmd() { -hosttype "$__RESH_HOSTTYPE" \ -ostype "$__RESH_OSTYPE" \ -machtype "$__RESH_MACHTYPE" \ + -shlvl "$__RESH_SHLVL" \ -realtimeBefore "$__RESH_RT_BEFORE" \ -realtimeAfter "$__RESH_RT_AFTER" \ -realtimeSession "$__RESH_RT_SESSION" \ diff --git a/collect/resh-collect.go b/collect/resh-collect.go index 9a1ea26..a37e5a9 100644 --- a/collect/resh-collect.go +++ b/collect/resh-collect.go @@ -23,8 +23,7 @@ func main() { var config common.Config if _, err := toml.DecodeFile(configPath, &config); err != nil { - log.Println("Error reading config", err) - return + log.Fatal("Error reading config:", err) } cmdLine := flag.String("cmdLine", "", "command line") @@ -47,6 +46,7 @@ func main() { pid := flag.Int("pid", -1, "$PID") sessionPid := flag.Int("sessionPid", -1, "$$") windowId := flag.Int("windowId", -1, "$WINDOWID - session id") + shlvl := flag.Int("shlvl", -1, "$SHLVL") host := flag.String("host", "", "$HOSTNAME") hosttype := flag.String("hosttype", "", "$HOSTTYPE") @@ -68,7 +68,13 @@ func main() { realtimeAfter, err := strconv.ParseFloat(*rta, 64) realtimeBefore, err := strconv.ParseFloat(*rtb, 64) realtimeSessionStart, err := strconv.ParseFloat(*rtsess, 64) + if err != nil { + log.Fatal("Flag Parsing error (1):", err) + } realtimeSessSinceBoot, err := strconv.ParseFloat(*rtsessboot, 64) + if err != nil { + log.Fatal("Flag Parsing error (2):", err) + } realtimeDuration := realtimeAfter - realtimeBefore realtimeSinceSessionStart := realtimeBefore - realtimeSessionStart realtimeSinceBoot := realtimeSessSinceBoot + realtimeSinceSessionStart @@ -79,13 +85,6 @@ func main() { realtimeAfterLocal := realtimeAfter + timezoneAfterOffset - if err != nil { - log.Fatal("Flag Parsing error:", err) - } - if err != nil { - log.Fatal("Flag Parsing error:", err) - } - rec := common.Record{ // core CmdLine: *cmdLine, @@ -112,6 +111,7 @@ func main() { Hosttype: *hosttype, Ostype: *ostype, Machtype: *machtype, + Shlvl: *shlvl, // before after TimezoneBefore: *timezoneBefore, @@ -134,13 +134,13 @@ func main() { func sendRecord(r common.Record, port string) { recJson, err := json.Marshal(r) if err != nil { - log.Fatal("1 ", err) + log.Fatal("send err 1", err) } req, err := http.NewRequest("POST", "http://localhost:" + port + "/record", bytes.NewBuffer(recJson)) if err != nil { - log.Fatal("2 ", err) + log.Fatal("send err 2", err) } req.Header.Set("Content-Type", "application/json") diff --git a/common/resh-common.go b/common/resh-common.go index b22d1db..0b621a1 100644 --- a/common/resh-common.go +++ b/common/resh-common.go @@ -25,6 +25,7 @@ type Record struct { Hosttype string `json:"hosttype"` Ostype string `json:"ostype"` Machtype string `json:"machtype"` + Shlvl int `json:"shlvl"` // before after TimezoneBefore string `json:"timezoneBefore"` diff --git a/daemon/resh-daemon.go b/daemon/resh-daemon.go index 16a64b5..d28e2af 100644 --- a/daemon/resh-daemon.go +++ b/daemon/resh-daemon.go @@ -11,20 +11,27 @@ import ( "os/user" "strconv" "strings" - //"syscall" "net/http" common "github.com/curusarn/resh/common" "github.com/BurntSushi/toml" ) - - func main() { usr, _ := user.Current() dir := usr.HomeDir pidfilePath := filepath.Join(dir, ".resh/resh.pid") configPath := filepath.Join(dir, ".config/resh.toml") outputPath := filepath.Join(dir, ".resh/history.json") + logPath := filepath.Join(dir, ".resh/daemon.log") + + f, err := os.OpenFile(logPath, os.O_RDWR | os.O_CREATE | os.O_APPEND, 0644) + if err != nil { + log.Fatal("Error opening file:", err) + } + defer f.Close() + + log.SetOutput(f) + log.SetPrefix(strconv.Itoa(os.Getpid()) + " | ") var config common.Config if _, err := toml.DecodeFile(configPath, &config); err != nil { @@ -36,7 +43,7 @@ func main() { log.Println("Error while checking if the daemon is runnnig", err) } if res { - log.Println("Daemon is already runnnig - exiting!") + log.Println("Daemon is already running - exiting!") return } _, err = os.Stat(pidfilePath) @@ -61,7 +68,7 @@ func main() { func statusHandler(w http.ResponseWriter, r *http.Request) { w.Write([]byte("OK\n")) - log.Printf("Status OK\n") + log.Println("Status OK") } type recordHandler struct { @@ -96,7 +103,7 @@ func (h *recordHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { log.Printf("Error while writing: %v, %s\n", record, err) return } - log.Printf("Received: %v\n", record) + log.Println("Received: ", record.CmdLine) // fmt.Println("cmd:", r.CmdLine) // fmt.Println("pwd:", r.Pwd) @@ -134,7 +141,7 @@ func isDaemonRunning(port int) (bool, error) { resp, err := http.Get(url) if err != nil { log.Println("Error while checking daemon status - " + - "it's probably not runnig!", err) + "it's probably not running!", err) return false, err } defer resp.Body.Close()