page3_handler.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. package main
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "net/http"
  6. "os"
  7. "os/exec"
  8. "strconv"
  9. "strings"
  10. "time"
  11. )
  12. func downloadTimeLog(w http.ResponseWriter, r *http.Request) {
  13. if r.Method != http.MethodGet {
  14. http.Error(w, "method not allowed", http.StatusMethodNotAllowed)
  15. return
  16. }
  17. filename := fmt.Sprintf("time.log.%s.tar.gz", time.Now().Format("20060102150405"))
  18. tmpFile := fmt.Sprintf("/tmp/%d.tar.gz", time.Now().UnixNano())
  19. defer os.Remove(tmpFile)
  20. cmd := exec.Command(
  21. "tar",
  22. "--warning=no-file-changed",
  23. "-czf",
  24. tmpFile,
  25. "-C",
  26. "/opt/yfkj/timesyncd.service/log",
  27. ".",
  28. )
  29. out, err := cmd.CombinedOutput()
  30. if err != nil {
  31. if exitErr, ok := err.(*exec.ExitError); !ok || exitErr.ExitCode() != 1 {
  32. http.Error(w, string(out), http.StatusInternalServerError)
  33. return
  34. }
  35. }
  36. w.Header().Set("Content-Type", "application/gzip")
  37. w.Header().Set("Content-Disposition", fmt.Sprintf(`attachment; filename="%s"`, filename))
  38. http.ServeFile(w, r, tmpFile)
  39. }
  40. func restartTimeService(w http.ResponseWriter, r *http.Request) {
  41. if r.Method != http.MethodPost {
  42. http.Error(w, "method not allowed", http.StatusMethodNotAllowed)
  43. return
  44. }
  45. cmd := exec.Command("systemctl", "restart", "yfkj-timesyncd.service")
  46. if out, err := cmd.CombinedOutput(); err != nil {
  47. http.Error(w, string(out)+err.Error(), http.StatusInternalServerError)
  48. return
  49. }
  50. w.Header().Set("Content-Type", "application/json")
  51. w.Write([]byte(`{"status":"ok"}`))
  52. }
  53. func timeStatusHandler(w http.ResponseWriter, r *http.Request) {
  54. var t struct {
  55. SystemTime string `json:"system_time"`
  56. TimeSource string `json:"time_source"`
  57. TimeSynced string `json:"time_synced"`
  58. }
  59. callErr := callRPCResult(r.Context(), 7001, "core.getTimeSyncStatus", nil, &t)
  60. sync := noValue
  61. if t.TimeSynced == "true" {
  62. sync = "🟢正常"
  63. }
  64. timeSource := noValue
  65. systemTime := time.Now().Format("2006-01-02 15:04:05")
  66. if callErr == nil {
  67. timeSource = "🟢" + strings.ToUpper(t.TimeSource)
  68. systemTime = t.SystemTime
  69. }
  70. boot, uptime := noValue, noValue
  71. if b, err := os.ReadFile("/proc/uptime"); err == nil {
  72. if fields := strings.Fields(string(b)); len(fields) > 0 {
  73. if sec, err := strconv.ParseFloat(fields[0], 64); err == nil {
  74. d := time.Duration(sec) * time.Second
  75. boot = time.Now().Add(-d).Format("2006-01-02 15:04:05")
  76. day := int(d.Hours() / 24)
  77. if day > 0 {
  78. uptime = fmt.Sprintf("%d天 %02d:%02d:%02d",
  79. int(d.Hours()/24), int(d.Hours())%24, int(d.Minutes())%60, int(d.Seconds())%60)
  80. } else {
  81. uptime = fmt.Sprintf("%02d:%02d:%02d",
  82. int(d.Hours())%24, int(d.Minutes())%60, int(d.Seconds())%60)
  83. }
  84. }
  85. }
  86. }
  87. w.Header().Set("Content-Type", "application/json; charset=utf-8")
  88. json.NewEncoder(w).Encode(map[string]string{
  89. "var1": map[bool]string{true: "🟢正常", false: noValue}[callErr == nil],
  90. "var2": sync,
  91. "var3": timeSource,
  92. "var4": systemTime,
  93. "var5": boot,
  94. "var6": uptime,
  95. })
  96. }