-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
141 lines (122 loc) · 4.97 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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
package main
import (
"encoding/json"
"fmt"
"log"
"time"
// import package collector sesuai dengan module path
"github.com/irvandandung/gomon/collector"
// import package command sesuai dengan module path
"github.com/irvandandung/gomon/pkg/models/command"
//import package gocron
"github.com/go-co-op/gocron"
)
func main() {
configvar, err := GetConfig("./config.yml")
if err != nil {
log.Fatal(err)
}
//inisialisasi gocron
cron := gocron.NewScheduler(time.Local)
//load running code every 15 second
cron.Every(15).Second().Do(func() {
// panggil fungsi getLoad() yang saat ini berada dalam
// package collector untuk mendapatkan informasi load yang
// telah diparsing, dan lakukan pengecekan, jika terdapat error
// kita tampilkan error dan hentikan program
loads, err := collector.GetLoad()
if err != nil {
log.Fatal(err)
}
// membuat line protocol dengan menyertakan data loads
datapoint := fmt.Sprintf("loadavg,hostname=server-1 load1=%.2f,load5=%.2f,load15=%.2f", loads["load1"], loads["load5"], loads["load15"])
// hit api untuk memasukan data loads kedalam database gomondb
data, err := command.ExecCurlInsert("-i", UriConnection(configvar, "write"), datapoint)
if(err == nil){
log.Println(data)
}else{
log.Fatal(err)
cron.Clear()
}
})
//storage running code every 15 second
cron.Every(15).Second().Do(func() {
// panggil collector.GetStorage() yang akan mengembalikan 2 nilai
// map[string]string dan error, map[string]string yang berisi
// mountpoint dengan informasi mountpoint tersebut akan kita simpan
// di variable storages
storages, err := collector.GetStorage()
if err != nil {
log.Fatal(err)
}
for storage := range storages {
// buat s sebagai alias StorageInfo yang berada pada package
// collector
var s collector.StorageInfo
// lakukan unmarshal pada value map, dan simpan hasilnya pada
// StorageInfo, yang sebelumnya kita beri alias s
if err := json.Unmarshal([]byte(storages[storage]), &s); err != nil {
log.Fatal(err)
}
// buat variable datapoint yang berisi nama mountpoint dan informasi
// mengenai total ukuran storage, kapasitas yang tersedia, dan
// kapasitas yang sudah digunakan
datapoint := fmt.Sprintf("diskusage,hostname=server-1,mountpoint=%s size=%d,avail=%d,used=%d", storage, s.Size, s.Avail, s.Used)
// hit api untuk memasukan data diskusage kedalam database gomondb
data, err := command.ExecCurlInsert("-i", UriConnection(configvar, "write"), datapoint)
if(err == nil){
log.Println(data)
}else{
log.Fatal(err)
cron.Clear()
}
}
})
//memory running code every 15 second
cron.Every(15).Second().Do(func() {
// panggil fungsi GetMemory() yang berada pada package collector
// fungsi tsb akan mengembalikan 2 nilai, map[string]uint64 dan error
// map[string]uint64 yang berisi informasi mengenai memori akan
// disimpan pada variable memory
memory, err := collector.GetMemory()
if err != nil {
log.Fatal(err)
}
// buat line protocol yang berisi informasi mengenai memori
memdp := fmt.Sprintf("memory,hostname=server-1 memtotal=%d,memused=%d", memory["MemTotal"], memory["Used"])
// hit api untuk memasukan data memory kedalam database gomondb
data, err := command.ExecCurlInsert("-i", UriConnection(configvar, "write"), memdp)
if(err == nil){
log.Println(data)
}else{
log.Fatal(err)
cron.Clear()
}
})
//cpu
cpustat := &collector.CPUStats{}
//cpu running code every 15 second
cron.Every(15).Second().Do(func() {
cputotal := collector.GetCPU(cpustat)
datapoint := fmt.Sprintf("cpu,hostname=server-1 totaltime=%.1f", cputotal)
// hit api untuk memasukan data diskusage kedalam database gomondb
data, err := command.ExecCurlInsert("-i", UriConnection(configvar, "write"), datapoint)
if(err == nil){
log.Println(data)
}else{
log.Fatal(err)
cron.Clear()
}
})
//start all cron in code
cron.StartBlocking()
}
func UriConnection(configvar *Config, paramQuery string) (uri string){
conf := configvar.Database.Influxdb
baseUrl := fmt.Sprintf("http://%s:%s/", conf.Host, conf.Port)
db := fmt.Sprintf("%s", conf.Database)
username := fmt.Sprintf("%s", conf.Username)
password := fmt.Sprintf("%s", conf.Password)
uri = baseUrl+paramQuery+"?db="+db+"&u="+username+"&p="+password
return uri
}