log to file, start daemon in bashrc, add shlvl var to metadata

pull/3/head
Simon Let 7 years ago
parent 28aa6895f6
commit 4d8d7efa4d
  1. 5
      Makefile
  2. 4
      bashrc.sh
  3. 22
      collect/resh-collect.go
  4. 1
      common/resh-common.go
  5. 21
      daemon/resh-daemon.go

@ -13,9 +13,8 @@ install: build | $(HOME)/.resh $(HOME)/.resh/bin $(HOME)/.config/resh
echo '[[ -f ~/.resh/bashrc ]] && source ~/.resh/bashrc' >> ~/.bashrc echo '[[ -f ~/.resh/bashrc ]] && source ~/.resh/bashrc' >> ~/.bashrc
grep '[[ -f ~/.bash-preexec.sh ]] && source ~/.bash-preexec.sh' ~/.bashrc ||\ grep '[[ -f ~/.bash-preexec.sh ]] && source ~/.bash-preexec.sh' ~/.bashrc ||\
echo '[[ -f ~/.bash-preexec.sh ]] && source ~/.bash-preexec.sh' >> ~/.bashrc echo '[[ -f ~/.bash-preexec.sh ]] && source ~/.bash-preexec.sh' >> ~/.bashrc
#-pkill resh-daemon -pkill resh-daemon
#resh-daemon & disown nohup resh-daemon &>/dev/null & disown
resh-daemon: daemon/resh-daemon.go common/resh-common.go resh-daemon: daemon/resh-daemon.go common/resh-common.go
go build -o $@ $< go build -o $@ $<

@ -2,7 +2,7 @@
PATH=$PATH:~/.resh/bin PATH=$PATH:~/.resh/bin
export __RESH_RT_SESSION=$EPOCHREALTIME export __RESH_RT_SESSION=$EPOCHREALTIME
export __RESH_RT_SESS_SINCE_BOOT=$(cat /proc/uptime | cut -d' ' -f1) export __RESH_RT_SESS_SINCE_BOOT=$(cat /proc/uptime | cut -d' ' -f1)
#resh-daemon & disown nohup resh-daemon &>/dev/null & disown
preexec() { preexec() {
# core # core
@ -30,6 +30,7 @@ preexec() {
__RESH_HOSTTYPE="$HOSTTYPE" __RESH_HOSTTYPE="$HOSTTYPE"
__RESH_OSTYPE="$OSTYPE" __RESH_OSTYPE="$OSTYPE"
__RESH_MACHTYPE="$MACHTYPE" __RESH_MACHTYPE="$MACHTYPE"
__RESH_SHLVL="$SHLVL"
# time # time
__RESH_TZ_BEFORE=$(date +%:z) __RESH_TZ_BEFORE=$(date +%:z)
@ -59,6 +60,7 @@ precmd() {
-hosttype "$__RESH_HOSTTYPE" \ -hosttype "$__RESH_HOSTTYPE" \
-ostype "$__RESH_OSTYPE" \ -ostype "$__RESH_OSTYPE" \
-machtype "$__RESH_MACHTYPE" \ -machtype "$__RESH_MACHTYPE" \
-shlvl "$__RESH_SHLVL" \
-realtimeBefore "$__RESH_RT_BEFORE" \ -realtimeBefore "$__RESH_RT_BEFORE" \
-realtimeAfter "$__RESH_RT_AFTER" \ -realtimeAfter "$__RESH_RT_AFTER" \
-realtimeSession "$__RESH_RT_SESSION" \ -realtimeSession "$__RESH_RT_SESSION" \

@ -23,8 +23,7 @@ func main() {
var config common.Config var config common.Config
if _, err := toml.DecodeFile(configPath, &config); err != nil { if _, err := toml.DecodeFile(configPath, &config); err != nil {
log.Println("Error reading config", err) log.Fatal("Error reading config:", err)
return
} }
cmdLine := flag.String("cmdLine", "", "command line") cmdLine := flag.String("cmdLine", "", "command line")
@ -47,6 +46,7 @@ func main() {
pid := flag.Int("pid", -1, "$PID") pid := flag.Int("pid", -1, "$PID")
sessionPid := flag.Int("sessionPid", -1, "$$") sessionPid := flag.Int("sessionPid", -1, "$$")
windowId := flag.Int("windowId", -1, "$WINDOWID - session id") windowId := flag.Int("windowId", -1, "$WINDOWID - session id")
shlvl := flag.Int("shlvl", -1, "$SHLVL")
host := flag.String("host", "", "$HOSTNAME") host := flag.String("host", "", "$HOSTNAME")
hosttype := flag.String("hosttype", "", "$HOSTTYPE") hosttype := flag.String("hosttype", "", "$HOSTTYPE")
@ -68,7 +68,13 @@ func main() {
realtimeAfter, err := strconv.ParseFloat(*rta, 64) realtimeAfter, err := strconv.ParseFloat(*rta, 64)
realtimeBefore, err := strconv.ParseFloat(*rtb, 64) realtimeBefore, err := strconv.ParseFloat(*rtb, 64)
realtimeSessionStart, err := strconv.ParseFloat(*rtsess, 64) realtimeSessionStart, err := strconv.ParseFloat(*rtsess, 64)
if err != nil {
log.Fatal("Flag Parsing error (1):", err)
}
realtimeSessSinceBoot, err := strconv.ParseFloat(*rtsessboot, 64) realtimeSessSinceBoot, err := strconv.ParseFloat(*rtsessboot, 64)
if err != nil {
log.Fatal("Flag Parsing error (2):", err)
}
realtimeDuration := realtimeAfter - realtimeBefore realtimeDuration := realtimeAfter - realtimeBefore
realtimeSinceSessionStart := realtimeBefore - realtimeSessionStart realtimeSinceSessionStart := realtimeBefore - realtimeSessionStart
realtimeSinceBoot := realtimeSessSinceBoot + realtimeSinceSessionStart realtimeSinceBoot := realtimeSessSinceBoot + realtimeSinceSessionStart
@ -79,13 +85,6 @@ func main() {
realtimeAfterLocal := realtimeAfter + timezoneAfterOffset realtimeAfterLocal := realtimeAfter + timezoneAfterOffset
if err != nil {
log.Fatal("Flag Parsing error:", err)
}
if err != nil {
log.Fatal("Flag Parsing error:", err)
}
rec := common.Record{ rec := common.Record{
// core // core
CmdLine: *cmdLine, CmdLine: *cmdLine,
@ -112,6 +111,7 @@ func main() {
Hosttype: *hosttype, Hosttype: *hosttype,
Ostype: *ostype, Ostype: *ostype,
Machtype: *machtype, Machtype: *machtype,
Shlvl: *shlvl,
// before after // before after
TimezoneBefore: *timezoneBefore, TimezoneBefore: *timezoneBefore,
@ -134,13 +134,13 @@ func main() {
func sendRecord(r common.Record, port string) { func sendRecord(r common.Record, port string) {
recJson, err := json.Marshal(r) recJson, err := json.Marshal(r)
if err != nil { if err != nil {
log.Fatal("1 ", err) log.Fatal("send err 1", err)
} }
req, err := http.NewRequest("POST", "http://localhost:" + port + "/record", req, err := http.NewRequest("POST", "http://localhost:" + port + "/record",
bytes.NewBuffer(recJson)) bytes.NewBuffer(recJson))
if err != nil { if err != nil {
log.Fatal("2 ", err) log.Fatal("send err 2", err)
} }
req.Header.Set("Content-Type", "application/json") req.Header.Set("Content-Type", "application/json")

@ -25,6 +25,7 @@ type Record struct {
Hosttype string `json:"hosttype"` Hosttype string `json:"hosttype"`
Ostype string `json:"ostype"` Ostype string `json:"ostype"`
Machtype string `json:"machtype"` Machtype string `json:"machtype"`
Shlvl int `json:"shlvl"`
// before after // before after
TimezoneBefore string `json:"timezoneBefore"` TimezoneBefore string `json:"timezoneBefore"`

@ -11,20 +11,27 @@ import (
"os/user" "os/user"
"strconv" "strconv"
"strings" "strings"
//"syscall"
"net/http" "net/http"
common "github.com/curusarn/resh/common" common "github.com/curusarn/resh/common"
"github.com/BurntSushi/toml" "github.com/BurntSushi/toml"
) )
func main() { func main() {
usr, _ := user.Current() usr, _ := user.Current()
dir := usr.HomeDir dir := usr.HomeDir
pidfilePath := filepath.Join(dir, ".resh/resh.pid") pidfilePath := filepath.Join(dir, ".resh/resh.pid")
configPath := filepath.Join(dir, ".config/resh.toml") configPath := filepath.Join(dir, ".config/resh.toml")
outputPath := filepath.Join(dir, ".resh/history.json") 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 var config common.Config
if _, err := toml.DecodeFile(configPath, &config); err != nil { 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) log.Println("Error while checking if the daemon is runnnig", err)
} }
if res { if res {
log.Println("Daemon is already runnnig - exiting!") log.Println("Daemon is already running - exiting!")
return return
} }
_, err = os.Stat(pidfilePath) _, err = os.Stat(pidfilePath)
@ -61,7 +68,7 @@ func main() {
func statusHandler(w http.ResponseWriter, r *http.Request) { func statusHandler(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("OK\n")) w.Write([]byte("OK\n"))
log.Printf("Status OK\n") log.Println("Status OK")
} }
type recordHandler struct { 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) log.Printf("Error while writing: %v, %s\n", record, err)
return return
} }
log.Printf("Received: %v\n", record) log.Println("Received: ", record.CmdLine)
// fmt.Println("cmd:", r.CmdLine) // fmt.Println("cmd:", r.CmdLine)
// fmt.Println("pwd:", r.Pwd) // fmt.Println("pwd:", r.Pwd)
@ -134,7 +141,7 @@ func isDaemonRunning(port int) (bool, error) {
resp, err := http.Get(url) resp, err := http.Get(url)
if err != nil { if err != nil {
log.Println("Error while checking daemon status - " + log.Println("Error while checking daemon status - " +
"it's probably not runnig!", err) "it's probably not running!", err)
return false, err return false, err
} }
defer resp.Body.Close() defer resp.Body.Close()

Loading…
Cancel
Save