-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
126 lines (101 loc) · 2.76 KB
/
main.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
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
122
123
124
125
126
package main
import (
"flag"
"fmt"
"github.com/silenteh/monitoring/config"
"github.com/silenteh/monitoring/queue"
"github.com/silenteh/monitoring/server"
"log"
"os"
"os/signal"
"runtime"
"runtime/pprof"
"time"
)
var cpuprofile = flag.String("cpuprofile", "", "write cpu profile to file")
var memprofile = flag.String("memprofile", "", "write memory profile to this file")
func main() {
//read the command line parameters
flag.Parse()
// READ THE CONFIG FILES
appConfig := config.LoadAppConfig()
if appConfig.Key == "" && appConfig.Secret == "" {
log.Fatal("Cannot open the config.json !!!")
return
}
// registration channel
// the app does not continue if the server is not registered with the backend
registrationChannel := make(chan config.ServerConfig, 1)
server.Register(registrationChannel)
serverConfig := <-registrationChannel // here it blocks !
if serverConfig.ServerId == "" {
log.Fatal("The server failed to register...")
return
}
if *cpuprofile != "" {
f, err := os.Create(*cpuprofile)
if err != nil {
log.Fatal(err)
}
pprof.StartCPUProfile(f)
defer pprof.StopCPUProfile()
}
// run with multiple cores
//nproc := cpu.Count()
//runtime.GOMAXPROCS(nproc)
runtime.MemProfileRate = 1
//var responseChannel chan string
responseChannel := make(chan string)
queue.Consume(responseChannel)
//httpserver.Start()
// this is NOT needed because we store it with the server registration and this does not change unless
// the server is rebooted
// on monitoring start the server updates the informations
//queue.Produce(queue.SYSINFO, responseChannel)
queue.Produce(queue.LOADINFO, responseChannel)
queue.Produce(queue.CPULOAD, responseChannel)
queue.Produce(queue.DISKINFO, responseChannel)
queue.Produce(queue.NETINFO, responseChannel)
tickerInterval := appConfig.PollingTiker
if tickerInterval < 15 {
tickerInterval = 15
}
ticker := time.NewTicker(time.Duration(tickerInterval) * time.Second)
quit := make(chan struct{})
go func() {
for {
select {
case <-ticker.C:
queue.Produce(queue.LOADINFO, responseChannel)
queue.Produce(queue.CPULOAD, responseChannel)
queue.Produce(queue.DISKINFO, responseChannel)
queue.Produce(queue.NETINFO, responseChannel)
fmt.Printf("%s \n", time.Now())
case <-quit:
ticker.Stop()
fmt.Println("Closed channel")
for i := 0; i < 10; i++ {
fmt.Println("OK")
}
return
}
}
}()
if *memprofile != "" {
time.Sleep(60 * time.Second)
f, err := os.Create(*memprofile)
if err != nil {
log.Fatal(err)
}
pprof.WriteHeapProfile(f)
defer f.Close()
//return
}
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt, os.Kill)
// Block until a signal is received.
s := <-c
close(quit)
close(c)
fmt.Println("Got signal:", s)
}