This repository has been archived by the owner on Jan 2, 2019. It is now read-only.
/
idlelog.go
87 lines (73 loc) · 1.97 KB
/
idlelog.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
package main
import (
"log"
"fmt"
"github.com/BurntSushi/xgb"
"github.com/BurntSushi/xgb/screensaver"
"github.com/BurntSushi/xgb/xproto"
"github.com/stuart-warren/idlelog/env"
"github.com/stuart-warren/idlelog/file"
"github.com/stuart-warren/idlelog/net"
"time"
)
var (
timeSinceInput time.Duration
)
func init() {
}
func debugLog(isIdle bool) {
if isIdle {
fmt.Printf("%s %d %s@%s is idle\n", env.TimestampString, env.EpochString, env.User, env.Host)
} else {
fmt.Printf("%s %d %s@%s is active\n", env.TimestampString, env.EpochString, env.User, env.Host)
}
}
func main() {
x, err := xgb.NewConn()
if err != nil {
log.Fatal(err)
}
defer x.Close()
screen := xproto.Setup(x).DefaultScreen(x)
err = screensaver.Init(x)
if err != nil {
log.Fatal(err)
}
for true {
env.DateString = time.Now().Local().Format(env.DATE_FORMAT)
env.TimestampString = time.Now().Local().Format(env.TIMESTAMP_FORMAT)
env.EpochString = time.Now().Unix()
env.LogFile = fmt.Sprintf("%s/%s.log", *env.LogDir, env.DateString)
env.LogJsonFile = fmt.Sprintf("%s/%s.json", *env.LogJsonDir, env.DateString)
env.LogCsvFile = fmt.Sprintf("%s/%s.csv", *env.LogCsvDir, env.DateString)
info, err := screensaver.QueryInfo(x, xproto.Drawable(screen.Root)).Reply()
if err != nil {
log.Fatal(err)
}
timeSinceInput, _ = time.ParseDuration(fmt.Sprintf("%dms", info.MsSinceUserInput))
var isIdle bool
if timeSinceInput > *env.TimeForIdle {
isIdle = true
}
// call reporting methods here
if *env.Debug {
debugLog(isIdle)
}
if *env.LogDir != "" {
file.WriteLog(isIdle)
}
if *env.LogJsonDir != "" {
file.WriteJsonLog(isIdle)
}
if *env.LogCsvDir != "" {
file.WriteCsvLog(isIdle)
}
if *env.Graphite {
net.SendGraphite(isIdle)
}
if *env.OpenTsdb {
net.SendOpenTsdb(isIdle)
}
time.Sleep(*env.SleepDuration)
}
}