page3_handler.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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. _ = callRPCResult(r.Context(), 7001, "core.getTimeSyncStatus", nil, &t)
  60. sync := noValue
  61. if t.TimeSynced == "true" {
  62. sync = "已同步"
  63. }
  64. boot, uptime := noValue, noValue
  65. if b, err := os.ReadFile("/proc/uptime"); err == nil {
  66. if fields := strings.Fields(string(b)); len(fields) > 0 {
  67. if sec, err := strconv.ParseFloat(fields[0], 64); err == nil {
  68. d := time.Duration(sec) * time.Second
  69. boot = time.Now().Add(-d).Format("2006-01-02 15:04:05")
  70. day := int(d.Hours() / 24)
  71. if day > 0 {
  72. uptime = fmt.Sprintf("%d天 %02d:%02d:%02d",
  73. int(d.Hours()/24), int(d.Hours())%24, int(d.Minutes())%60, int(d.Seconds())%60)
  74. } else {
  75. uptime = fmt.Sprintf("%02d:%02d:%02d",
  76. int(d.Hours())%24, int(d.Minutes())%60, int(d.Seconds())%60)
  77. }
  78. }
  79. }
  80. }
  81. w.Header().Set("Content-Type", "application/json; charset=utf-8")
  82. json.NewEncoder(w).Encode(map[string]string{
  83. "var1": sync,
  84. "var2": strings.ToUpper(t.TimeSource),
  85. "var3": t.SystemTime,
  86. "var4": boot,
  87. "var5": uptime,
  88. })
  89. }