add machineId, gofmt

pull/3/head
Simon Let 7 years ago
parent 4d8d7efa4d
commit abcd44e108
  1. 360
      collect/resh-collect.go
  2. 1
      common/resh-common.go

@ -1,192 +1,200 @@
package main package main
import ( import (
"bytes" "bytes"
"encoding/json" "encoding/json"
"os" "flag"
"os/exec" "github.com/BurntSushi/toml"
"os/user" common "github.com/curusarn/resh/common"
"path/filepath" "io/ioutil"
"flag" "log"
"log" "net/http"
"net/http" "os"
"strconv" "os/exec"
"strings" "os/user"
common "github.com/curusarn/resh/common" "path/filepath"
"github.com/BurntSushi/toml" "strconv"
"strings"
) )
func main() { func main() {
usr, _ := user.Current() usr, _ := user.Current()
dir := usr.HomeDir dir := usr.HomeDir
configPath := filepath.Join(dir, "/.config/resh.toml") configPath := filepath.Join(dir, "/.config/resh.toml")
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.Fatal("Error reading config:", err) log.Fatal("Error reading config:", err)
} }
cmdLine := flag.String("cmdLine", "", "command line") cmdLine := flag.String("cmdLine", "", "command line")
exitCode := flag.Int("exitCode", -1, "exit code") exitCode := flag.Int("exitCode", -1, "exit code")
// posix variables // posix variables
cols := flag.Int("cols", -1, "$COLUMNS") cols := flag.Int("cols", -1, "$COLUMNS")
lines := flag.Int("lines", -1, "$LINES") lines := flag.Int("lines", -1, "$LINES")
home := flag.String("home", "", "$HOME") home := flag.String("home", "", "$HOME")
lang := flag.String("lang", "", "$LANG") lang := flag.String("lang", "", "$LANG")
lcAll := flag.String("lcAll", "", "$LC_ALL") lcAll := flag.String("lcAll", "", "$LC_ALL")
login := flag.String("login", "", "$LOGIN") login := flag.String("login", "", "$LOGIN")
path := flag.String("path", "", "$PATH") path := flag.String("path", "", "$PATH")
pwd := flag.String("pwd", "", "$PWD - present working directory") pwd := flag.String("pwd", "", "$PWD - present working directory")
shell := flag.String("shell", "", "$SHELL") shell := flag.String("shell", "", "$SHELL")
term := flag.String("term", "", "$TERM") term := flag.String("term", "", "$TERM")
// non-posix // non-posix
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") 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")
ostype := flag.String("ostype", "", "$OSTYPE") ostype := flag.String("ostype", "", "$OSTYPE")
machtype := flag.String("machtype", "", "$MACHTYPE") machtype := flag.String("machtype", "", "$MACHTYPE")
// before after // before after
timezoneBefore := flag.String("timezoneBefore", "", "") timezoneBefore := flag.String("timezoneBefore", "", "")
timezoneAfter := flag.String("timezoneAfter", "", "") timezoneAfter := flag.String("timezoneAfter", "", "")
rtb := flag.String("realtimeBefore", "-1", "before $EPOCHREALTIME") rtb := flag.String("realtimeBefore", "-1", "before $EPOCHREALTIME")
rta := flag.String("realtimeAfter", "-1", "after $EPOCHREALTIME") rta := flag.String("realtimeAfter", "-1", "after $EPOCHREALTIME")
rtsess := flag.String("realtimeSession", "-1", rtsess := flag.String("realtimeSession", "-1",
"on session start $EPOCHREALTIME") "on session start $EPOCHREALTIME")
rtsessboot := flag.String("realtimeSessSinceBoot", "-1", rtsessboot := flag.String("realtimeSessSinceBoot", "-1",
"on session start $EPOCHREALTIME") "on session start $EPOCHREALTIME")
flag.Parse() flag.Parse()
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 { if err != nil {
log.Fatal("Flag Parsing error (1):", err) log.Fatal("Flag Parsing error (1):", err)
} }
realtimeSessSinceBoot, err := strconv.ParseFloat(*rtsessboot, 64) realtimeSessSinceBoot, err := strconv.ParseFloat(*rtsessboot, 64)
if err != nil { if err != nil {
log.Fatal("Flag Parsing error (2):", err) 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
timezoneBeforeOffset := getTimezoneOffsetInSeconds(*timezoneBefore) timezoneBeforeOffset := getTimezoneOffsetInSeconds(*timezoneBefore)
timezoneAfterOffset := getTimezoneOffsetInSeconds(*timezoneAfter) timezoneAfterOffset := getTimezoneOffsetInSeconds(*timezoneAfter)
realtimeBeforeLocal := realtimeBefore + timezoneBeforeOffset realtimeBeforeLocal := realtimeBefore + timezoneBeforeOffset
realtimeAfterLocal := realtimeAfter + timezoneAfterOffset realtimeAfterLocal := realtimeAfter + timezoneAfterOffset
rec := common.Record{
rec := common.Record{ // core
// core CmdLine: *cmdLine,
CmdLine: *cmdLine, ExitCode: *exitCode,
ExitCode: *exitCode,
// posix
// posix Cols: *cols,
Cols: *cols, Lines: *lines,
Lines: *lines,
Home: *home,
Home: *home, Lang: *lang,
Lang: *lang, LcAll: *lcAll,
LcAll: *lcAll, Login: *login,
Login: *login, Path: *path,
Path: *path, Pwd: *pwd,
Pwd: *pwd, Shell: *shell,
Shell: *shell, Term: *term,
Term: *term,
// non-posix
// non-posix Pid: *pid,
Pid: *pid, SessionPid: *sessionPid,
SessionPid: *sessionPid, WindowId: *windowId,
WindowId: *windowId, Host: *host,
Host: *host, Hosttype: *hosttype,
Hosttype: *hosttype, Ostype: *ostype,
Ostype: *ostype, Machtype: *machtype,
Machtype: *machtype, Shlvl: *shlvl,
Shlvl: *shlvl,
// before after
// before after TimezoneBefore: *timezoneBefore,
TimezoneBefore: *timezoneBefore, TimezoneAfter: *timezoneAfter,
TimezoneAfter: *timezoneAfter,
RealtimeBefore: realtimeBefore,
RealtimeBefore: realtimeBefore, RealtimeAfter: realtimeAfter,
RealtimeAfter: realtimeAfter, RealtimeBeforeLocal: realtimeBeforeLocal,
RealtimeBeforeLocal: realtimeBeforeLocal, RealtimeAfterLocal: realtimeAfterLocal,
RealtimeAfterLocal: realtimeAfterLocal,
RealtimeDuration: realtimeDuration,
RealtimeDuration: realtimeDuration, RealtimeSinceSessionStart: realtimeSinceSessionStart,
RealtimeSinceSessionStart: realtimeSinceSessionStart, RealtimeSinceBoot: realtimeSinceBoot,
RealtimeSinceBoot: realtimeSinceBoot,
GitWorkTree: getGitDir(),
GitWorkTree: getGitDir(), MachineId: getMachineId(),
} }
sendRecord(rec, strconv.Itoa(config.Port)) sendRecord(rec, strconv.Itoa(config.Port))
} }
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("send err 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("send err 2", err) log.Fatal("send err 2", err)
} }
req.Header.Set("Content-Type", "application/json") req.Header.Set("Content-Type", "application/json")
client := &http.Client{} client := &http.Client{}
_, err = client.Do(req) _, err = client.Do(req)
if err != nil { if err != nil {
log.Fatal("resh-daemon is not running :(") log.Fatal("resh-daemon is not running :(")
} }
}
func getMachineId() string {
dat, err := ioutil.ReadFile("/etc/machine-id")
if err != nil {
log.Fatal("failed to open /etc/machine-id")
}
return string(dat)
} }
func getGitDir() string { func getGitDir() string {
// assume we are in pwd // assume we are in pwd
gitWorkTree := os.Getenv("GIT_WORK_TREE") gitWorkTree := os.Getenv("GIT_WORK_TREE")
if gitWorkTree != "" { if gitWorkTree != "" {
return gitWorkTree return gitWorkTree
} }
// we should look up the git directory ourselves // we should look up the git directory ourselves
// OR leave it to resh daemon to not slow down user // OR leave it to resh daemon to not slow down user
out, err := exec.Command("git", "rev-parse", "--show-toplevel").Output() out, err := exec.Command("git", "rev-parse", "--show-toplevel").Output()
if err != nil { if err != nil {
if exitError, ok := err.(*exec.ExitError); ok { if exitError, ok := err.(*exec.ExitError); ok {
if exitError.ExitCode() == 128 { if exitError.ExitCode() == 128 {
return "" return ""
} }
log.Fatal("git cmd failed") log.Fatal("git cmd failed")
} else { } else {
log.Fatal("git cmd failed w/o exit code") log.Fatal("git cmd failed w/o exit code")
} }
} }
return strings.TrimSuffix(string(out), "\n") return strings.TrimSuffix(string(out), "\n")
} }
func getTimezoneOffsetInSeconds(zone string) float64 { func getTimezoneOffsetInSeconds(zone string) float64 {
hours_mins := strings.Split(zone, ":") hours_mins := strings.Split(zone, ":")
hours, err := strconv.Atoi(hours_mins[0]) hours, err := strconv.Atoi(hours_mins[0])
if err != nil { if err != nil {
log.Println("err while parsing hours in timezone offset:", err) log.Println("err while parsing hours in timezone offset:", err)
return -1 return -1
} }
mins, err := strconv.Atoi(hours_mins[1]) mins, err := strconv.Atoi(hours_mins[1])
if err != nil { if err != nil {
log.Println("err while parsing mins in timezone offset:", err) log.Println("err while parsing mins in timezone offset:", err)
return -1 return -1
} }
secs := ( (hours * 60) + mins ) * 60 secs := ((hours * 60) + mins) * 60
return float64(secs) return float64(secs)
} }

@ -42,6 +42,7 @@ type Record struct {
//Logs []string `json: "logs"` //Logs []string `json: "logs"`
GitWorkTree string `json:"gitWorkTree"` GitWorkTree string `json:"gitWorkTree"`
MachineId string `json:"machineId"`
} }
type Config struct { type Config struct {

Loading…
Cancel
Save