Skip to content

Commit

Permalink
add INTERNET_CONNECTION_CHECK_PING_ADDRS config
Browse files Browse the repository at this point in the history
  • Loading branch information
blesswinsamuel committed May 9, 2024
1 parent 5717167 commit d43d582
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 12 deletions.
4 changes: 3 additions & 1 deletion cmd/router-monitor/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"net/http"
"os"
"os/signal"
"strings"
"syscall"
"time"

Expand Down Expand Up @@ -42,7 +43,8 @@ func main() {
go func() {
prometheus.MustRegister(ebpfFirewallCollector)
prometheus.MustRegister(routermonitor.NewArpCollector("/proc/net/arp", os.Getenv("DOMAIN_SUFFIX")))
internetChecker := routermonitor.NewInternetChecker(10 * time.Second)
pingAddrs := strings.Split(os.Getenv("INTERNET_CONNECTION_CHECK_PING_ADDRS"), ",")
internetChecker := routermonitor.NewInternetChecker(10*time.Second, pingAddrs)
internetChecker.Register(prometheus.DefaultRegisterer)
go internetChecker.Start(context.Background())

Expand Down
17 changes: 6 additions & 11 deletions internal/routermonitor/internet-check.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,17 @@ import (
)

type internetChecker struct {
interval time.Duration
interval time.Duration
pingAddrs []string

internetConnectionDuration *prometheus.HistogramVec
internetConnectionIsUp *prometheus.GaugeVec
}

func NewInternetChecker(interval time.Duration) *internetChecker {
func NewInternetChecker(interval time.Duration, pingAddrs []string) *internetChecker {
return &internetChecker{
interval: interval,
interval: interval,
pingAddrs: pingAddrs,
internetConnectionDuration: prometheus.NewHistogramVec(prometheus.HistogramOpts{
Name: "router_monitor_internet_connection_duration_seconds",
Help: "",
Expand All @@ -38,18 +40,11 @@ func (collector *internetChecker) Register(registry prometheus.Registerer) {

func (collector *internetChecker) Start(ctx context.Context) {
ticker := time.NewTicker(collector.interval)
addrs := []string{
"1.1.1.1:53",
"64.6.64.6:53",
"8.8.8.8:53",
"208.67.222.222:53",
"9.9.9.9:53",
}
log.Println("Checking if internet connection is up.")
for {
select {
case <-ticker.C:
for _, addr := range addrs {
for _, addr := range collector.pingAddrs {
connectionIsUp := 0
startTime := time.Now()
conn, err := net.DialTimeout("tcp", addr, 2*time.Second)
Expand Down

0 comments on commit d43d582

Please sign in to comment.