Skip to content

Commit

Permalink
Merge pull request #133 from mfreeman451/updates/fixing_leak
Browse files Browse the repository at this point in the history
Updates/fixing leak
  • Loading branch information
mfreeman451 authored Jan 28, 2025
2 parents ddf7937 + 103ff27 commit 85689a6
Show file tree
Hide file tree
Showing 13 changed files with 617 additions and 281 deletions.
34 changes: 17 additions & 17 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,45 +31,45 @@ ServiceRadar can be installed via direct downloads from GitHub releases.
Install these components on your monitored host:
```bash
# Download and install core components
curl -LO https://github.com/mfreeman451/serviceradar/releases/download/1.0.3/serviceradar-agent_1.0.8.deb \
-O https://github.com/mfreeman451/serviceradar/releases/download/1.0.3/serviceradar-poller_1.0.8.deb
curl -LO https://github.com/mfreeman451/serviceradar/releases/download/1.0.3/serviceradar-agent_1.0.9.deb \
-O https://github.com/mfreeman451/serviceradar/releases/download/1.0.3/serviceradar-poller_1.0.9.deb

sudo dpkg -i serviceradar-agent_1.0.8.deb serviceradar-poller_1.0.8.deb
sudo dpkg -i serviceradar-agent_1.0.9.deb serviceradar-poller_1.0.9.deb
```

On a separate machine (recommended) or the same host:
```bash
# Download and install cloud service
curl -LO https://github.com/mfreeman451/serviceradar/releases/download/1.0.3/serviceradar-cloud_1.0.8.deb
sudo dpkg -i serviceradar-cloud_1.0.8.deb
curl -LO https://github.com/mfreeman451/serviceradar/releases/download/1.0.3/serviceradar-cloud_1.0.9.deb
sudo dpkg -i serviceradar-cloud_1.0.9.deb
```

#### Optional: Dusk Node Monitoring
If you're running a [Dusk](https://dusk.network/) node and want specialized monitoring:
```bash
curl -LO https://github.com/mfreeman451/serviceradar/releases/download/1.0.8/serviceradar-dusk-checker_1.0.8.deb
sudo dpkg -i serviceradar-dusk-checker_1.0.8.deb
curl -LO https://github.com/mfreeman451/serviceradar/releases/download/1.0.9/serviceradar-dusk-checker_1.0.9.deb
sudo dpkg -i serviceradar-dusk-checker_1.0.9.deb
```

#### Distributed Setup
For larger deployments where components run on different hosts:

1. On monitored hosts:
```bash
curl -LO https://github.com/mfreeman451/serviceradar/releases/download/1.0.8/serviceradar-agent_1.0.8.deb
sudo dpkg -i serviceradar-agent_1.0.8.deb
curl -LO https://github.com/mfreeman451/serviceradar/releases/download/1.0.9/serviceradar-agent_1.0.9.deb
sudo dpkg -i serviceradar-agent_1.0.9.deb
```

2. On monitoring host:
```bash
curl -LO https://github.com/mfreeman451/serviceradar/releases/download/1.0.3/serviceradar-poller_1.0.8.deb
sudo dpkg -i serviceradar-poller_1.0.8.deb
curl -LO https://github.com/mfreeman451/serviceradar/releases/download/1.0.3/serviceradar-poller_1.0.9.deb
sudo dpkg -i serviceradar-poller_1.0.9.deb
```

3. On cloud host:
```bash
curl -LO https://github.com/mfreeman451/serviceradar/releases/download/1.0.3/serviceradar-cloud_1.0.8.deb
sudo dpkg -i serviceradar-cloud_1.0.8.deb
curl -LO https://github.com/mfreeman451/serviceradar/releases/download/1.0.3/serviceradar-cloud_1.0.9.deb
sudo dpkg -i serviceradar-cloud_1.0.9.deb
```

## Architecture
Expand Down Expand Up @@ -171,19 +171,19 @@ cd serviceradar

1. **Agent Installation** (on monitored hosts):
```bash
sudo dpkg -i serviceradar-dusk-checker_1.0.8.deb # For Dusk nodes
sudo dpkg -i serviceradar-dusk-checker_1.0.9.deb # For Dusk nodes
# or
sudo dpkg -i serviceradar-agent_1.0.8.deb # For other hosts
sudo dpkg -i serviceradar-agent_1.0.9.deb # For other hosts
```

2. **Poller Installation** (on any host in your network):
```bash
sudo dpkg -i serviceradar-poller_1.0.8.deb
sudo dpkg -i serviceradar-poller_1.0.9.deb
```

3. **Cloud Installation** (on a reliable host):
```bash
sudo dpkg -i serviceradar-cloud_1.0.8.deb
sudo dpkg -i serviceradar-cloud_1.0.9.deb
```

## Configuration
Expand Down
2 changes: 1 addition & 1 deletion buildAll.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/bin/bash

VERSION=${VERSION:-1.0.8}
VERSION=${VERSION:-1.0.9}


./setup-deb-poller.sh
Expand Down
16 changes: 10 additions & 6 deletions pkg/agent/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,16 @@ type Duration time.Duration

// SweepConfig represents sweep service configuration from JSON.
type SweepConfig struct {
Networks []string `json:"networks"`
Ports []int `json:"ports"`
SweepModes []models.SweepMode `json:"sweep_modes"`
Interval Duration `json:"interval"`
Concurrency int `json:"concurrency"`
Timeout Duration `json:"timeout"`
MaxTargets int
MaxGoroutines int
BatchSize int
MemoryLimit int64
Networks []string `json:"networks"`
Ports []int `json:"ports"`
SweepModes []models.SweepMode `json:"sweep_modes"`
Interval Duration `json:"interval"`
Concurrency int `json:"concurrency"`
Timeout Duration `json:"timeout"`
}

// CheckerConfig represents the configuration for a checker.
Expand Down
101 changes: 35 additions & 66 deletions pkg/cloud/api/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ package api
import (
"embed"
"encoding/json"
"fmt"
"io/fs"
"log"
"net/http"
Expand Down Expand Up @@ -60,6 +59,7 @@ type APIServer struct {
router *mux.Router
nodeHistoryHandler func(nodeID string) ([]NodeHistoryPoint, error)
metricsManager metrics.MetricCollector
knownPollers []string
}

func NewAPIServer(metricsManager metrics.MetricCollector) *APIServer {
Expand Down Expand Up @@ -141,71 +141,10 @@ func (s *APIServer) SetNodeHistoryHandler(handler func(nodeID string) ([]NodeHis
s.nodeHistoryHandler = handler
}

func (*APIServer) handleSweepService(svc *ServiceStatus) {
sweepData, err := parseSweepDetails(svc.Details)
if err != nil {
log.Printf("Error parsing sweep details: %v", err)
return
}

processHosts(sweepData)
}

// parseSweepDetails unmarshals the sweep details JSON into a map.
func parseSweepDetails(details []byte) (map[string]interface{}, error) {
var sweepData map[string]interface{}
if err := json.Unmarshal(details, &sweepData); err != nil {
return nil, fmt.Errorf("failed to unmarshal sweep details: %w", err)
}

return sweepData, nil
}

// processHosts processes the hosts in the sweep data.
func processHosts(sweepData map[string]interface{}) {
hosts, ok := sweepData["hosts"].([]interface{})
if !ok {
return
}

for _, h := range hosts {
host, ok := h.(map[string]interface{})
if !ok {
continue
}

processICMPStatus(host)
}
}

// processICMPStatus processes the ICMP status for a host.
func processICMPStatus(host map[string]interface{}) {
icmpStatus, ok := host["icmp_status"].(map[string]interface{})
if !ok {
return
}

// Log ICMP round-trip time if it exists
if rt, exists := icmpStatus["round_trip"].(float64); exists {
log.Printf("Host %v ICMP RTT: %.2fms",
host["host"],
float64(rt)/float64(time.Millisecond))
}

// Log the full ICMP status
log.Printf("Host %v ICMP status: %+v", host["host"], icmpStatus)
}

func (s *APIServer) UpdateNodeStatus(nodeID string, status *NodeStatus) {
s.mu.Lock()
defer s.mu.Unlock()

for _, svc := range status.Services {
if svc.Type == "sweep" {
s.handleSweepService(&svc)
}
}

s.nodes[nodeID] = status
}

Expand Down Expand Up @@ -261,19 +200,34 @@ func (s *APIServer) getSystemStatus(w http.ResponseWriter, _ *http.Request) {

func (s *APIServer) getNodes(w http.ResponseWriter, _ *http.Request) {
s.mu.RLock()
defer s.mu.RUnlock()

// Preallocate the slice with the correct length
nodes := make([]*NodeStatus, 0, len(s.nodes))

for _, node := range s.nodes {
log.Printf("Node %s services:", node.NodeID)
nodes = append(nodes, node)
// Append all map values to the slice
for id, node := range s.nodes {
// Only include known pollers
for _, known := range s.knownPollers {
if id == known {
nodes = append(nodes, node)
break
}
}
}
s.mu.RUnlock()

// Encode and send the response
if err := s.encodeJSONResponse(w, nodes); err != nil {
http.Error(w, "Internal server error", http.StatusInternalServerError)
}
}

func (s *APIServer) SetKnownPollers(knownPollers []string) {
s.mu.Lock()
defer s.mu.Unlock()
s.knownPollers = knownPollers
}

func (s *APIServer) getNodeByID(nodeID string) (*NodeStatus, bool) {
s.mu.RLock()
defer s.mu.RUnlock()
Expand All @@ -298,6 +252,21 @@ func (s *APIServer) getNode(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
nodeID := vars["id"]

// Check if it's a known poller
isKnown := false

for _, known := range s.knownPollers {
if nodeID == known {
isKnown = true
break
}
}

if !isKnown {
http.Error(w, "Node not found", http.StatusNotFound)
return
}

node, exists := s.getNodeByID(nodeID)
if !exists {
log.Printf("Node %s not found", nodeID)
Expand Down
Loading

0 comments on commit 85689a6

Please sign in to comment.