Files
silk/server-go/internal/handler/storage.go
T

71 lines
2.0 KiB
Go

package handler
import (
"encoding/json"
"net/http"
"os/exec"
"silk-server-go/internal/middleware"
"github.com/gin-gonic/gin"
"gorm.io/gorm"
)
// RegisterStorageRoutes 注册存储状态路由
func RegisterStorageRoutes(rg *gin.RouterGroup, db *gorm.DB) {
rg.GET("/storage/ceph", middleware.RequirePermission(db, "video:read"), getCephStorage())
}
// getCephStorage 获取 Ceph 存储使用情况
func getCephStorage() gin.HandlerFunc {
return func(c *gin.Context) {
// 1. ceph df --format json
dfCmd := exec.Command("ceph", "df", "--format", "json")
dfOutput, err := dfCmd.Output()
if err != nil {
c.JSON(http.StatusServiceUnavailable, gin.H{"error": "ceph df 失败: " + err.Error()})
return
}
var dfData map[string]interface{}
if err := json.Unmarshal(dfOutput, &dfData); err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": "解析 ceph df 失败: " + err.Error()})
return
}
// 2. ceph osd tree --format json
treeCmd := exec.Command("ceph", "osd", "tree", "--format", "json")
treeOutput, err := treeCmd.Output()
if err != nil {
c.JSON(http.StatusServiceUnavailable, gin.H{"error": "ceph osd tree 失败: " + err.Error()})
return
}
var treeData map[string]interface{}
if err := json.Unmarshal(treeOutput, &treeData); err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": "解析 ceph osd tree 失败: " + err.Error()})
return
}
// 3. ceph health --format json
healthCmd := exec.Command("ceph", "health", "--format", "json")
healthOutput, err := healthCmd.Output()
if err != nil {
c.JSON(http.StatusServiceUnavailable, gin.H{"error": "ceph health 失败: " + err.Error()})
return
}
var healthData map[string]interface{}
if err := json.Unmarshal(healthOutput, &healthData); err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": "解析 ceph health 失败: " + err.Error()})
return
}
c.JSON(http.StatusOK, gin.H{
"df": dfData,
"osdTree": treeData,
"health": healthData,
})
}
}