-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
121 lines (105 loc) · 2.24 KB
/
main.go
File metadata and controls
121 lines (105 loc) · 2.24 KB
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
package main
import (
"context"
"encoding/json"
"flag"
"fmt"
"net/http"
"os"
"os/signal"
"sync"
"time"
"github.com/jinzhu/gorm"
_ "github.com/jinzhu/gorm/dialects/sqlite"
"github.com/shirou/gopsutil/mem"
"github.com/sirupsen/logrus"
)
var (
logger = logrus.New()
endpoint string
db *gorm.DB
transport = &http.Transport{
MaxIdleConns: 10,
IdleConnTimeout: 30 * time.Second,
DisableCompression: true,
}
client = &http.Client{
Timeout: time.Second * 10,
Transport: transport,
}
)
// SystemData sturcture.
type SystemData struct {
gorm.Model
CPULoad uint64
MEMFree uint64
}
func init() {
logger.SetFormatter(&logrus.JSONFormatter{})
}
// collectData collect data from system every 1 second.
func collectData(ctx context.Context, wg *sync.WaitGroup) {
ticker := time.NewTicker(time.Second * 1)
for {
select {
case <-ctx.Done():
ticker.Stop()
wg.Done()
return
case <-ticker.C:
m, _ := mem.VirtualMemory()
db.Create(&SystemData{CPULoad: 10000, MEMFree: m.Free})
}
}
}
// sendData send data to endpoint every 10 seconds and clean DB if success.
func sendData(ctx context.Context, wg *sync.WaitGroup) {
var data []SystemData
ticker := time.NewTicker(time.Second * 10)
for {
select {
case <-ctx.Done():
ticker.Stop()
wg.Done()
return
case <-ticker.C:
db.Find(&data)
d, _ := json.Marshal(&data)
// client.Post
fmt.Printf("%s\n", d)
db.Delete(&data)
data = data[:0]
}
}
}
func main() {
var err error
exit := make(chan os.Signal, 1)
var wg sync.WaitGroup
flag.StringVar(&endpoint, "endpoint", "", "Address of remote server, which recive logs.")
flag.Parse()
// Prepare database connection.
db, err = gorm.Open("sqlite3", "./db.db")
if err != nil {
logger.Errorln(err)
}
db.DB().SetMaxIdleConns(0)
db.AutoMigrate(&SystemData{})
ctx, cancel := context.WithCancel(context.Background())
signal.Notify(exit, os.Interrupt)
wg.Add(1)
go collectData(ctx, &wg)
wg.Add(1)
go sendData(ctx, &wg)
logger.Infoln("I'am working...")
<-exit
logger.Infoln("Got exit signal. Stoping...")
// Cancel collectionData() executing.
cancel()
// Wait until collectData() do everything.
wg.Wait()
db.Close()
// Say BYE! everyONE.
logger.Infoln("BYE!")
os.Exit(0)
}