-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
executable file
·131 lines (116 loc) · 2.51 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
package main
import (
"flag"
"log"
"math/rand"
"net/http"
"time"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/prometheus/client_golang/prometheus/push"
"github.com/prometheus/procfs"
)
func pullmetrics() {
addr := flag.String("addr", ":5198", "The address to listen on for HTTP requests.")
flag.Parse()
var (
gauge = prometheus.NewGauge(prometheus.GaugeOpts{
Name: "sun_temperature",
Help: "current sun temperature",
})
counter = prometheus.NewCounterVec(
prometheus.CounterOpts{
Name: "blink_total",
Help: "blink count",
},
[]string{"star"},
)
)
reg := prometheus.NewRegistry()
reg.MustRegister(gauge)
reg.MustRegister(counter)
handlerFunc := func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
counter.With(prometheus.Labels{"star": "venus"}).Inc()
gauge.Set(rand.Float64() * 100)
next.ServeHTTP(w, r)
})
}
mux := http.NewServeMux()
mux.Handle("/metrics", handlerFunc(promhttp.HandlerFor(reg, promhttp.HandlerOpts{})))
mux.Handle("/metrics2", promhttp.Handler())
server := &http.Server{
Addr: *addr,
Handler: mux,
}
log.Fatal(server.ListenAndServe())
}
func pushmetrics() {
var (
gauge = prometheus.NewGauge(prometheus.GaugeOpts{
Name: "sun_temperature",
Help: "current sun temperature",
})
counter = prometheus.NewCounterVec(
prometheus.CounterOpts{
Name: "blink_total",
Help: "blink count",
},
[]string{"star"},
)
)
pusher := push.New("http://192.168.126.128:9091", "sky_observer").Collector(gauge).Collector(counter)
for {
counter.With(prometheus.Labels{"star": "venus"}).Inc()
gauge.Set(rand.Float64() * 100)
if err := pusher.Push(); err != nil {
log.Fatal(err)
return
}
time.Sleep(time.Second)
}
}
func getproc() {
fs, err := procfs.NewDefaultFS()
if err != nil {
log.Fatal(err)
return
}
cpus, err := fs.CPUInfo()
if err != nil {
log.Fatal(err)
return
}
log.Println(len(cpus))
log.Println(cpus)
mem, err := fs.Meminfo()
if err != nil {
log.Fatal(err)
return
}
log.Println(mem)
proc, err := fs.Self()
if err != nil {
log.Fatal(err)
return
}
fds, err := proc.FileDescriptorTargets()
if err != nil {
log.Fatal(err)
return
}
log.Println(fds)
st, err := proc.NewStat()
if err != nil {
log.Fatal(err)
return
}
log.Println(st)
}
func main() {
log.SetFlags(log.LstdFlags | log.Lshortfile)
go pullmetrics()
go pushmetrics()
go getproc()
select {}
}