Skip to content
This repository was archived by the owner on Aug 24, 2024. It is now read-only.

Commit

Permalink
Initial agent ping implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
nint8835 committed Feb 26, 2024
1 parent faac78d commit f22fc00
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 2 deletions.
16 changes: 14 additions & 2 deletions cmd/agent_run.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
package cmd

import (
"fmt"
"os"
"os/signal"
"syscall"

"github.com/spf13/cobra"

"github.com/nint8835/instatus-cluster-monitor/pkg/agent"
"github.com/nint8835/instatus-cluster-monitor/pkg/config"
)

Expand All @@ -16,7 +19,16 @@ var agentRunCmd = &cobra.Command{
agentCfg, err := config.LoadAgentConfig()
checkError(err, "failed to load agent configuration")

fmt.Printf("%#+v\n", agentCfg)
agentInst, err := agent.New(agentCfg)
checkError(err, "failed to create agent")

agentInst.Start()

sc := make(chan os.Signal, 1)
signal.Notify(sc, syscall.SIGINT, syscall.SIGTERM, os.Interrupt, os.Kill)
<-sc

agentInst.Stop()
},
}

Expand Down
77 changes: 77 additions & 0 deletions pkg/agent/agent.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package agent

import (
"bytes"
"encoding/json"
"net/http"
"time"

"github.com/rs/zerolog/log"

"github.com/nint8835/instatus-cluster-monitor/pkg/config"
"github.com/nint8835/instatus-cluster-monitor/pkg/server"
)

type Agent struct {
config *config.AgentConfig

stopChan chan struct{}
}

func (a *Agent) run() {
ticker := time.NewTicker(a.config.PingFrequency)
defer ticker.Stop()

log.Info().Msg("Starting agent")

for {
select {
case <-a.stopChan:
log.Debug().Msg("Stopping agent")
return
case <-ticker.C:
log.Debug().Msg("Pinging")

reqBody := server.PingBody{
// TODO: Select identifier
Identifier: "Host",
}

bodyBytes, err := json.Marshal(reqBody)
if err != nil {
log.Error().Err(err).Msg("Error marshalling body")
continue
}

req, err := http.NewRequest(http.MethodPost, a.config.ServerAddress, bytes.NewReader(bodyBytes))
req.Header.Set("Authorization", "Bearer "+a.config.SharedConfig.SharedSecret)
req.Header.Set("Content-Type", "application/json")

if err != nil {
log.Error().Err(err).Msg("Error creating request")
continue
}

_, err = http.DefaultClient.Do(req)
if err != nil {
log.Error().Err(err).Msg("Error sending request")
continue
}
}
}
}

func (a *Agent) Stop() {
a.stopChan <- struct{}{}
}

func (a *Agent) Start() {
go a.run()
}

func New(c *config.AgentConfig) (*Agent, error) {
return &Agent{
config: c,
stopChan: make(chan struct{}),
}, nil
}
7 changes: 7 additions & 0 deletions pkg/config/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,17 @@ package config

import (
"fmt"
"time"
)

type AgentConfig struct {
SharedConfig

// PingFrequency is the amount of time between pings
PingFrequency time.Duration `split_words:"true" default:"1m"`

// ServerAddress is the address of the server to ping
ServerAddress string `split_words:"true" required:"true"`
}

func LoadAgentConfig() (*AgentConfig, error) {
Expand Down

0 comments on commit f22fc00

Please sign in to comment.