working on system management

This commit is contained in:
Warp Agent
2025-12-26 17:47:20 +00:00
parent 5e63ebc9fe
commit ec0ba85958
19 changed files with 969 additions and 128 deletions

View File

@@ -304,6 +304,13 @@ func (h *Handler) DeleteZFSPool(c *gin.Context) {
return
}
// Invalidate cache for pools list
if h.cache != nil {
cacheKey := "http:/api/v1/storage/zfs/pools:"
h.cache.Delete(cacheKey)
h.logger.Debug("Cache invalidated for pools list", "key", cacheKey)
}
c.JSON(http.StatusOK, gin.H{"message": "ZFS pool deleted successfully"})
}

View File

@@ -7,6 +7,7 @@ import (
"os"
"os/exec"
"path/filepath"
"strconv"
"strings"
"time"
@@ -44,7 +45,8 @@ type ZFSPool struct {
AutoExpand bool `json:"auto_expand"`
ScrubInterval int `json:"scrub_interval"` // days
IsActive bool `json:"is_active"`
HealthStatus string `json:"health_status"` // online, degraded, faulted, offline
HealthStatus string `json:"health_status"` // online, degraded, faulted, offline
CompressRatio float64 `json:"compress_ratio"` // compression ratio (e.g., 1.45x)
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
CreatedBy string `json:"created_by"`
@@ -359,6 +361,26 @@ func (s *ZFSService) getSpareDisks(ctx context.Context, poolName string) ([]stri
return spareDisks, nil
}
// getCompressRatio gets the compression ratio from ZFS
func (s *ZFSService) getCompressRatio(ctx context.Context, poolName string) (float64, error) {
cmd := exec.CommandContext(ctx, "zfs", "get", "-H", "-o", "value", "compressratio", poolName)
output, err := cmd.Output()
if err != nil {
return 1.0, err
}
ratioStr := strings.TrimSpace(string(output))
// Remove 'x' suffix if present (e.g., "1.45x" -> "1.45")
ratioStr = strings.TrimSuffix(ratioStr, "x")
ratio, err := strconv.ParseFloat(ratioStr, 64)
if err != nil {
return 1.0, err
}
return ratio, nil
}
// ListPools lists all ZFS pools
func (s *ZFSService) ListPools(ctx context.Context) ([]*ZFSPool, error) {
query := `
@@ -407,8 +429,17 @@ func (s *ZFSService) ListPools(ctx context.Context) ([]*ZFSPool, error) {
pool.SpareDisks = spareDisks
}
// Get compressratio from ZFS system
compressRatio, err := s.getCompressRatio(ctx, pool.Name)
if err != nil {
s.logger.Warn("Failed to get compressratio", "pool", pool.Name, "error", err)
pool.CompressRatio = 1.0 // Default to 1.0 if can't get ratio
} else {
pool.CompressRatio = compressRatio
}
pools = append(pools, &pool)
s.logger.Debug("Added pool to list", "pool_id", pool.ID, "name", pool.Name)
s.logger.Debug("Added pool to list", "pool_id", pool.ID, "name", pool.Name, "compressratio", pool.CompressRatio)
}
if err := rows.Err(); err != nil {

View File

@@ -218,7 +218,7 @@ func (m *ZFSPoolMonitor) updatePoolStatus(ctx context.Context, poolName string,
return nil
}
// markMissingPoolsOffline marks pools that exist in database but not in system as offline
// markMissingPoolsOffline marks pools that exist in database but not in system as offline or deletes them
func (m *ZFSPoolMonitor) markMissingPoolsOffline(ctx context.Context, systemPools map[string]PoolInfo) error {
// Get all pools from database
rows, err := m.zfsService.db.QueryContext(ctx, "SELECT id, name FROM zfs_pools WHERE is_active = true")
@@ -235,17 +235,13 @@ func (m *ZFSPoolMonitor) markMissingPoolsOffline(ctx context.Context, systemPool
// Check if pool exists in system
if _, exists := systemPools[poolName]; !exists {
// Pool doesn't exist in system, mark as offline
_, err = m.zfsService.db.ExecContext(ctx, `
UPDATE zfs_pools SET
health_status = 'offline',
updated_at = NOW()
WHERE id = $1
`, poolID)
// Pool doesn't exist in system - delete from database (pool was destroyed)
m.logger.Info("Pool not found in system, removing from database", "pool", poolName)
_, err = m.zfsService.db.ExecContext(ctx, "DELETE FROM zfs_pools WHERE id = $1", poolID)
if err != nil {
m.logger.Warn("Failed to mark pool as offline", "pool", poolName, "error", err)
m.logger.Warn("Failed to delete missing pool from database", "pool", poolName, "error", err)
} else {
m.logger.Info("Marked pool as offline (not found in system)", "pool", poolName)
m.logger.Info("Removed missing pool from database", "pool", poolName)
}
}
}