4
0

system.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. // system.go — GET /api/system/info (SPEC-65 D65-6): datos REALES del
  2. // proceso/servidor (nunca hardcodeados), autenticado (cualquier usuario).
  3. // Las lecturas de /proc y /etc tienen fallback amable ("" / 0) si no son
  4. // legibles (p.ej. contenedor sin /proc completo); los parseadores son
  5. // funciones puras para testearlas con fixtures.
  6. package httpapi
  7. import (
  8. "net/http"
  9. "os"
  10. "runtime"
  11. "strconv"
  12. "strings"
  13. "time"
  14. )
  15. // systemInfo es la respuesta de GET /api/system/info.
  16. type systemInfo struct {
  17. Version string `json:"version"` // versión de la app (const Version, la de update.go)
  18. GoVersion string `json:"goVersion"` // runtime.Version()
  19. OS string `json:"os"` // runtime.GOOS
  20. Arch string `json:"arch"` // runtime.GOARCH
  21. Distro string `json:"distro"` // /etc/os-release PRETTY_NAME ("" si no legible)
  22. Kernel string `json:"kernel"` // /proc/sys/kernel/osrelease ("" si no legible)
  23. CPUModel string `json:"cpuModel"` // /proc/cpuinfo "model name" (o "Model" en ARM)
  24. CPUCores int `json:"cpuCores"` // runtime.NumCPU()
  25. MemTotalMb int64 `json:"memTotalMb"` // /proc/meminfo MemTotal → MiB
  26. UptimeS int64 `json:"uptimeS"` // uptime del PROCESO netpulse, segundos
  27. Demo bool `json:"demo"` // DEMO_MODE activo
  28. }
  29. // parseOsReleasePretty extrae PRETTY_NAME de un contenido de os-release
  30. // ("" si ausente). Tolera comillas y espacios.
  31. func parseOsReleasePretty(content string) string {
  32. for _, line := range strings.Split(content, "\n") {
  33. line = strings.TrimSpace(line)
  34. if !strings.HasPrefix(line, "PRETTY_NAME=") {
  35. continue
  36. }
  37. v := strings.TrimPrefix(line, "PRETTY_NAME=")
  38. v = strings.TrimSpace(v)
  39. v = strings.Trim(v, `"`)
  40. return v
  41. }
  42. return ""
  43. }
  44. // parseCPUModel devuelve la primera entrada "model name" (x86) o "Model"
  45. // (ARM/DeviceTree) de un contenido de /proc/cpuinfo ("" si ninguna).
  46. func parseCPUModel(content string) string {
  47. for _, line := range strings.Split(content, "\n") {
  48. key, val, ok := strings.Cut(line, ":")
  49. if !ok {
  50. continue
  51. }
  52. switch strings.TrimSpace(key) {
  53. case "model name", "Model":
  54. return strings.TrimSpace(val)
  55. }
  56. }
  57. return ""
  58. }
  59. // parseMemTotalMB convierte MemTotal (kB) de un contenido de /proc/meminfo a
  60. // MiB (0 si ausente o inválido).
  61. func parseMemTotalMB(content string) int64 {
  62. for _, line := range strings.Split(content, "\n") {
  63. if !strings.HasPrefix(line, "MemTotal:") {
  64. continue
  65. }
  66. fields := strings.Fields(line)
  67. if len(fields) < 2 {
  68. return 0
  69. }
  70. kb, err := strconv.ParseInt(fields[1], 10, 64)
  71. if err != nil {
  72. return 0
  73. }
  74. return kb / 1024
  75. }
  76. return 0
  77. }
  78. // readFileOrEmpty lee un fichero de texto del sistema ("" si no legible).
  79. func readFileOrEmpty(path string) string {
  80. b, err := os.ReadFile(path)
  81. if err != nil {
  82. return ""
  83. }
  84. return string(b)
  85. }
  86. // handleSystemInfo: 200 con los datos del proceso/servidor (sesión).
  87. func (s *server) handleSystemInfo(w http.ResponseWriter, _ *http.Request) {
  88. started := s.started
  89. if started.IsZero() {
  90. started = time.Now() // tests sin Started: uptime ~0 en vez de negativo
  91. }
  92. writeJSON(w, http.StatusOK, systemInfo{
  93. Version: Version,
  94. GoVersion: runtime.Version(),
  95. OS: runtime.GOOS,
  96. Arch: runtime.GOARCH,
  97. Distro: parseOsReleasePretty(readFileOrEmpty("/etc/os-release")),
  98. Kernel: strings.TrimSpace(readFileOrEmpty("/proc/sys/kernel/osrelease")),
  99. CPUModel: parseCPUModel(readFileOrEmpty("/proc/cpuinfo")),
  100. CPUCores: runtime.NumCPU(),
  101. MemTotalMb: parseMemTotalMB(readFileOrEmpty("/proc/meminfo")),
  102. UptimeS: int64(time.Since(started).Seconds()),
  103. Demo: s.adapter.Mode() == "demo",
  104. })
  105. }