Initial commit: Nerd Monitor - Cross-platform system monitoring application
Features: - Multi-platform agents (Linux, macOS, Windows - AMD64 & ARM64) - Real-time CPU, RAM, and disk usage monitoring - Responsive web dashboard with live status indicators - Session-based authentication with secure credentials - Stale agent detection and removal (6+ months inactive) - Auto-refresh dashboard (5 second intervals) - 15-second agent reporting intervals - Auto-generated agent IDs from hostnames - In-memory storage (zero database setup) - Minimal dependencies (Chi router + Templ templating) Project Structure: - cmd/: Agent and Server executables - internal/: API, Auth, Stats, Storage, and UI packages - views/: Templ templates for dashboard UI - Makefile: Build automation for all platforms Ready for deployment with comprehensive documentation: - README.md: Full project documentation - QUICKSTART.md: Getting started guide - AGENTS.md: Development guidelines
This commit is contained in:
78
internal/store/store.go
Normal file
78
internal/store/store.go
Normal file
@@ -0,0 +1,78 @@
|
||||
package store
|
||||
|
||||
import (
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"nerd-monitor/internal/stats"
|
||||
)
|
||||
|
||||
// AgentStats represents the latest stats for an agent.
|
||||
type AgentStats struct {
|
||||
ID string
|
||||
Hostname string
|
||||
CPUUsage float64
|
||||
RAMUsage uint64
|
||||
RAMTotal uint64
|
||||
DiskUsage uint64
|
||||
DiskTotal uint64
|
||||
LastSeen time.Time
|
||||
}
|
||||
|
||||
// Store manages agent statistics in memory.
|
||||
type Store struct {
|
||||
mu sync.RWMutex
|
||||
agents map[string]*AgentStats
|
||||
}
|
||||
|
||||
// New creates a new store.
|
||||
func New() *Store {
|
||||
return &Store{
|
||||
agents: make(map[string]*AgentStats),
|
||||
}
|
||||
}
|
||||
|
||||
// UpdateAgent updates or creates agent stats.
|
||||
func (s *Store) UpdateAgent(id string, stat *stats.Stats) {
|
||||
s.mu.Lock()
|
||||
defer s.mu.Unlock()
|
||||
|
||||
s.agents[id] = &AgentStats{
|
||||
ID: id,
|
||||
Hostname: stat.Hostname,
|
||||
CPUUsage: stat.CPUUsage,
|
||||
RAMUsage: stat.RAMUsage,
|
||||
RAMTotal: stat.RAMTotal,
|
||||
DiskUsage: stat.DiskUsage,
|
||||
DiskTotal: stat.DiskTotal,
|
||||
LastSeen: time.Now(),
|
||||
}
|
||||
}
|
||||
|
||||
// GetAgent retrieves agent stats by ID.
|
||||
func (s *Store) GetAgent(id string) *AgentStats {
|
||||
s.mu.RLock()
|
||||
defer s.mu.RUnlock()
|
||||
|
||||
return s.agents[id]
|
||||
}
|
||||
|
||||
// GetAllAgents returns all agents.
|
||||
func (s *Store) GetAllAgents() []*AgentStats {
|
||||
s.mu.RLock()
|
||||
defer s.mu.RUnlock()
|
||||
|
||||
agents := make([]*AgentStats, 0, len(s.agents))
|
||||
for _, agent := range s.agents {
|
||||
agents = append(agents, agent)
|
||||
}
|
||||
return agents
|
||||
}
|
||||
|
||||
// DeleteAgent removes an agent.
|
||||
func (s *Store) DeleteAgent(id string) {
|
||||
s.mu.Lock()
|
||||
defer s.mu.Unlock()
|
||||
|
||||
delete(s.agents, id)
|
||||
}
|
||||
Reference in New Issue
Block a user