-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
131 lines (122 loc) · 4.03 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"
"net/http"
"os"
"time"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
)
var (
station string
address string
help bool
verbose bool
timeout, backofftime int
failfast bool
localaddr string
humidity = prometheus.NewGauge(prometheus.GaugeOpts{
Namespace: "nws",
Name: "humidity",
Help: "humidity gauge percentage",
})
temperature = prometheus.NewGauge(prometheus.GaugeOpts{
Namespace: "nws",
Name: "temperature",
Help: "temperature in celsius",
})
dewpoint = prometheus.NewGauge(prometheus.GaugeOpts{
Namespace: "nws",
Name: "dewpoint",
Help: "dewpoint in celsius",
})
winddirection = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Namespace: "nws",
Name: "wind_direction",
Help: "wind direction in degrees",
},
[]string{"Direction"},
)
windspeed = prometheus.NewGauge(prometheus.GaugeOpts{
Namespace: "nws",
Name: "wind_speed",
Help: "wind speed in kilometers per hour",
})
barometricpressure = prometheus.NewGauge(prometheus.GaugeOpts{
Namespace: "nws",
Name: "barometric_pressure",
Help: "barometric pressure in pascals",
})
sealevelpressure = prometheus.NewGauge(prometheus.GaugeOpts{
Namespace: "nws",
Name: "sealevel_pressure",
Help: "sealevel pressure in pascals",
})
visibility = prometheus.NewGauge(prometheus.GaugeOpts{
Namespace: "nws",
Name: "visibility",
Help: "visibility in meters",
})
)
func init() {
flag.StringVar(&station, "station", "KPHL", "nws address")
flag.StringVar(&localaddr, "localaddr", ":8080", "The address to listen on for HTTP requests")
flag.StringVar(&address, "addr", "api.weather.gov", "nws address")
flag.BoolVar(&help, "help", false, "help info")
flag.BoolVar(&verbose, "verbose", false, "verbose logging")
flag.IntVar(&timeout, "timeout", 10, "timeout in seconds")
flag.IntVar(&backofftime, "backofftime", 100, "backofftime in seconds")
flag.BoolVar(&failfast, "failfast", false, "Exit quickly on errors")
flag.Parse()
prometheus.MustRegister(humidity)
prometheus.MustRegister(temperature)
prometheus.MustRegister(dewpoint)
prometheus.MustRegister(winddirection)
prometheus.MustRegister(windspeed)
prometheus.MustRegister(barometricpressure)
prometheus.MustRegister(sealevelpressure)
prometheus.MustRegister(visibility)
}
func main() {
if help {
flag.Usage()
os.Exit(1)
}
log.Printf("Starting up, retrieving from %s at station %s", address, station)
log.Printf("Serving on http://%s/metrics...", localaddr)
// start scrape loop
go func() {
for {
response, err := RetrieveCurrentObservation(station, address, timeout)
if err != nil {
if failfast {
log.Fatalf("error: %v", err)
}
log.Printf("Problem retrieving from: %s at station %s: %s", address, station, err)
backoffseconds := (time.Duration(backofftime) * time.Second)
log.Printf("Waiting %v seconds, next scrape at %s", backofftime, time.Now().Add(backoffseconds))
time.Sleep(time.Duration(backofftime) * time.Second)
continue
}
humidity.Set(response.Properties.RelativeHumidity.Value)
temperature.Set(response.Properties.Temperature.Value)
dewpoint.Set(response.Properties.Dewpoint.Value)
winddirection.WithLabelValues(
CardinalDirection(response.Properties.WindDirection.Value)).Set(
response.Properties.WindDirection.Value)
windspeed.Set(response.Properties.WindSpeed.Value)
barometricpressure.Set(response.Properties.BarometricPressure.Value)
sealevelpressure.Set(response.Properties.SeaLevelPressure.Value)
visibility.Set(response.Properties.Visibility.Value)
if verbose {
log.Printf("Waiting %v seconds, next scrape at %s", backofftime, time.Now().Add(
time.Duration(backofftime)*time.Second).String())
}
time.Sleep(time.Duration(backofftime) * time.Second)
}
}()
http.Handle("/metrics", promhttp.Handler())
log.Fatal(http.ListenAndServe(localaddr, nil))
}