Repository navigation
Expand file tree
/
Copy pathmain.go
More file actions
156 lines (124 loc) · 4.91 KB
/
Copy pathmain.go
File metadata and controls
156 lines (124 loc) · 4.91 KB
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
package main
import (
"encoding/json"
"fmt"
"net/http"
"os"
"strconv"
"time"
"github.com/go-logr/logr"
"github.com/go-logr/zapr"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/spf13/cobra"
"go.uber.org/zap"
)
var (
host string
interval time.Duration
logger logr.Logger
)
func scheduleMetricsRefresh(interval time.Duration) {
ticker := time.NewTicker(interval)
defer ticker.Stop()
for {
select {
case <-ticker.C:
fetchAndUpdateMetrics()
}
}
}
func fetchAndUpdateMetrics() {
logger.Info("Refreshing metrics from APIs")
// Fetch provisioning data
provisioningURL := fmt.Sprintf("http://%s/rest/v1/system/gateway/provisioning", host)
var provRes ProvisioningResponse
if err := fetchJSON(provisioningURL, &provRes); err == nil {
ipv4Lease.Set(provRes.Provisioning.IPv4.LeaseTime)
logger.V(1).Info("Setting metric", "provisioning_ipv4_lease_time", provRes.Provisioning.IPv4.LeaseTime)
ipv4Expire.Set(provRes.Provisioning.IPv4.ExpireTime)
logger.V(1).Info("Setting metric", "provisioning_ipv4_expire_time", provRes.Provisioning.IPv4.ExpireTime)
ipv6Lease.Set(provRes.Provisioning.IPv6.LeaseTime)
logger.V(1).Info("Setting metric", "provisioning_ipv6_lease_time", provRes.Provisioning.IPv6.LeaseTime)
ipv6Expire.Set(provRes.Provisioning.IPv6.ExpireTime)
logger.V(1).Info("Setting metric", "provisioning_ipv6_expire_time", provRes.Provisioning.IPv6.ExpireTime)
if provRes.Provisioning.DSLite.Enable {
dsLiteEnable.Set(1)
} else {
dsLiteEnable.Set(0)
}
logger.V(1).Info("Setting metric", "provisioning_dsLite_enable", provRes.Provisioning.DSLite.Enable)
}
// Fetch PON state
ponStateURL := fmt.Sprintf("http://%s/rest/v1/pon/state", host)
var ponStateRes PonStateResponse
if err := fetchJSON(ponStateURL, &ponStateRes); err == nil {
ponUpTime.Set(ponStateRes.Pon.UpTime)
logger.V(1).Info("Setting metric", "pon_status_up_time", ponStateRes.Pon.UpTime)
ponAccess.Set(boolToFloat(ponStateRes.Pon.AccessAllowed))
logger.V(1).Info("Setting metric", "pon_status_access_allowed", ponStateRes.Pon.AccessAllowed)
}
// Fetch PON status
ponStatusURL := fmt.Sprintf("http://%s/rest/v1/pon/status", host)
var ponStatusRes PonStatusResponse
if err := fetchJSON(ponStatusURL, &ponStatusRes); err == nil {
ponFirewall.Set(boolToFloat(ponStatusRes.Status.Firewall))
logger.V(1).Info("Setting metric", "pon_status_firewall", ponStatusRes.Status.Firewall)
ponIPData.Set(boolToFloat(ponStatusRes.Status.IPData == "Up"))
logger.V(1).Info("Setting metric", "pon_status_ip_data", ponStatusRes.Status.IPData)
ponIPVoip.Set(boolToFloat(ponStatusRes.Status.IPVoip == "Up"))
logger.V(1).Info("Setting metric", "pon_status_ip_voip", ponStatusRes.Status.IPVoip)
transceiverTemp.Set(parseStringToFloat(ponStatusRes.Interface.TransceiverTemp))
logger.V(1).Info("Setting metric", "pon_interface_transceiver_temp", ponStatusRes.Interface.TransceiverTemp)
transceiverVoltage.Set(parseStringToFloat(ponStatusRes.Interface.TransceiverVoltage))
logger.V(1).Info("Setting metric", "pon_interface_transceiver_voltage", ponStatusRes.Interface.TransceiverVoltage)
laserBiasCurrent.Set(parseStringToFloat(ponStatusRes.Interface.LaserBiasCurrent))
logger.V(1).Info("Setting metric", "pon_interface_laser_bias_current", ponStatusRes.Interface.LaserBiasCurrent)
transmitOpticalLevel.Set(parseStringToFloat(ponStatusRes.Interface.TransmitOpticalLevel))
logger.V(1).Info("Setting metric", "pon_interface_transmit_optical_level", ponStatusRes.Interface.TransmitOpticalLevel)
opticalSignalLevel.Set(parseStringToFloat(ponStatusRes.Interface.OpticalSignalLevel))
logger.V(1).Info("Setting metric", "pon_interface_optical_signal_level", ponStatusRes.Interface.OpticalSignalLevel)
}
}
func fetchJSON(url string, target interface{}) error {
resp, err := http.Get(url)
if err != nil {
logger.Error(err, "Failed to fetch URL", "url", url)
return err
}
defer resp.Body.Close()
return json.NewDecoder(resp.Body).Decode(target)
}
func boolToFloat(b bool) float64 {
if b {
return 1
}
return 0
}
func parseStringToFloat(s string) float64 {
f, _ := strconv.ParseFloat(s, 64)
return f
}
func main() {
zapLog, _ := zap.NewDevelopment()
logger = zapr.NewLogger(zapLog)
var rootCmd = &cobra.Command{
Use: "vmhub5_exporter",
Short: "Prometheus exporter for VMHub5 router",
Run: func(cmd *cobra.Command, args []string) {
logger.Info("Starting exporter")
// registerMetrics()
http.Handle("/metrics", promhttp.Handler())
// Initial metrics fetch
fetchAndUpdateMetrics()
go scheduleMetricsRefresh(interval)
logger.Info("Exporter running on :8080")
http.ListenAndServe(":8080", nil)
},
}
rootCmd.Flags().StringVarP(&host, "host", "H", "192.168.0.1", "Router host or IP")
rootCmd.Flags().DurationVarP(&interval, "interval", "i", 10*time.Second, "Refresh interval")
if err := rootCmd.Execute(); err != nil {
logger.Error(err, "Failed to execute command")
os.Exit(1)
}
}