This repository has been archived by the owner on May 1, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
92 lines (79 loc) · 1.67 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
package main
import (
"fmt"
"github.com/prometheus/client_golang/prometheus/promhttp"
"io/ioutil"
"log"
"net/http"
"os"
"strconv"
"strings"
"time"
)
var hostname = getHostname()
func main() {
mux := http.NewServeMux()
go uptime()
go cpuTempMeasurement()
mux.Handle("/metrics", promhttp.Handler())
port := os.Getenv("PORT")
if port == "" {
port = "8080"
}
srv := &http.Server{
Addr: ":" + port,
Handler: mux,
IdleTimeout: time.Minute,
ReadTimeout: 10 * time.Second,
WriteTimeout: 10 * time.Second,
}
log.Printf("starting server on %s", srv.Addr)
err := srv.ListenAndServe()
log.Fatal(err)
}
func uptime() {
for {
uptimeSeconds.WithLabelValues(hostname).Inc()
time.Sleep(1 * time.Second)
}
}
func cpuTempMeasurement() {
for {
content, err := ioutil.ReadFile(os.Getenv("FILE"))
if err != nil {
fmt.Println(err)
}
fmt.Print(string(content))
cpuTempCelsius.WithLabelValues(hostname).Set(getCpuTemp())
time.Sleep(15 * time.Second)
}
}
func getCpuTemp() float64 {
cpuTempRaw := getCpuTempFromFile()
cpuTemp := parseRawCpuTemp(cpuTempRaw)
return cpuTemp
}
func parseRawCpuTemp(cpuTempRaw string) float64 {
cpuTemp1 := strings.TrimPrefix(cpuTempRaw, "temp=")
cpuTemp2 := strings.TrimSuffix(cpuTemp1, "'C\n")
cpuTempString, err := strconv.ParseFloat(cpuTemp2, 64)
if err != nil {
fmt.Println(err)
}
return cpuTempString
}
func getCpuTempFromFile() string {
content, err := ioutil.ReadFile(os.Getenv("FILE"))
if err != nil {
fmt.Println(err)
}
return string(content)
}
func getHostname() string {
hostname, err := os.Hostname()
if err != nil {
fmt.Println(err)
}
fmt.Println("Hostname: " + hostname)
return hostname
}