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
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 $@ $<

@ -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" \

@ -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")

@ -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"`

@ -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()

Loading…
Cancel
Save