|
|
@@ -0,0 +1,106 @@
|
|
|
+package main
|
|
|
+
|
|
|
+import (
|
|
|
+ "encoding/json"
|
|
|
+ "fmt"
|
|
|
+ "net/http"
|
|
|
+ "os"
|
|
|
+ "os/exec"
|
|
|
+ "strconv"
|
|
|
+ "strings"
|
|
|
+ "time"
|
|
|
+)
|
|
|
+
|
|
|
+func downloadTimeLog(w http.ResponseWriter, r *http.Request) {
|
|
|
+ if r.Method != http.MethodGet {
|
|
|
+ http.Error(w, "method not allowed", http.StatusMethodNotAllowed)
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ filename := fmt.Sprintf("time.log.%s.tar.gz", time.Now().Format("20060102150405"))
|
|
|
+ tmpFile := fmt.Sprintf("/tmp/%d.tar.gz", time.Now().UnixNano())
|
|
|
+
|
|
|
+ defer os.Remove(tmpFile)
|
|
|
+
|
|
|
+ cmd := exec.Command(
|
|
|
+ "tar",
|
|
|
+ "--warning=no-file-changed",
|
|
|
+ "-czf",
|
|
|
+ tmpFile,
|
|
|
+ "-C",
|
|
|
+ "/opt/yfkj/timesyncd.service/log",
|
|
|
+ ".",
|
|
|
+ )
|
|
|
+
|
|
|
+ out, err := cmd.CombinedOutput()
|
|
|
+ if err != nil {
|
|
|
+ if exitErr, ok := err.(*exec.ExitError); !ok || exitErr.ExitCode() != 1 {
|
|
|
+ http.Error(w, string(out), http.StatusInternalServerError)
|
|
|
+ return
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ w.Header().Set("Content-Type", "application/gzip")
|
|
|
+ w.Header().Set("Content-Disposition", fmt.Sprintf(`attachment; filename="%s"`, filename))
|
|
|
+
|
|
|
+ http.ServeFile(w, r, tmpFile)
|
|
|
+}
|
|
|
+
|
|
|
+func restartTimeService(w http.ResponseWriter, r *http.Request) {
|
|
|
+ if r.Method != http.MethodPost {
|
|
|
+ http.Error(w, "method not allowed", http.StatusMethodNotAllowed)
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ cmd := exec.Command("systemctl", "restart", "yfkj-timesyncd.service")
|
|
|
+
|
|
|
+ if out, err := cmd.CombinedOutput(); err != nil {
|
|
|
+ http.Error(w, string(out)+err.Error(), http.StatusInternalServerError)
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ w.Header().Set("Content-Type", "application/json")
|
|
|
+ w.Write([]byte(`{"status":"ok"}`))
|
|
|
+}
|
|
|
+
|
|
|
+func timeStatusHandler(w http.ResponseWriter, r *http.Request) {
|
|
|
+ var t struct {
|
|
|
+ SystemTime string `json:"system_time"`
|
|
|
+ TimeSource string `json:"time_source"`
|
|
|
+ TimeSynced string `json:"time_synced"`
|
|
|
+ }
|
|
|
+
|
|
|
+ _ = callRPCResult(r.Context(), 7001, "core.getTimeSyncStatus", nil, &t)
|
|
|
+
|
|
|
+ sync := noValue
|
|
|
+ if t.TimeSynced == "true" {
|
|
|
+ sync = "已同步"
|
|
|
+ }
|
|
|
+
|
|
|
+ boot, uptime := noValue, noValue
|
|
|
+ if b, err := os.ReadFile("/proc/uptime"); err == nil {
|
|
|
+ if fields := strings.Fields(string(b)); len(fields) > 0 {
|
|
|
+ if sec, err := strconv.ParseFloat(fields[0], 64); err == nil {
|
|
|
+ d := time.Duration(sec) * time.Second
|
|
|
+ boot = time.Now().Add(-d).Format("2006-01-02 15:04:05")
|
|
|
+ day := int(d.Hours() / 24)
|
|
|
+ if day > 0 {
|
|
|
+ uptime = fmt.Sprintf("%d天 %02d:%02d:%02d",
|
|
|
+ int(d.Hours()/24), int(d.Hours())%24, int(d.Minutes())%60, int(d.Seconds())%60)
|
|
|
+ } else {
|
|
|
+ uptime = fmt.Sprintf("%02d:%02d:%02d",
|
|
|
+ int(d.Hours())%24, int(d.Minutes())%60, int(d.Seconds())%60)
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ w.Header().Set("Content-Type", "application/json; charset=utf-8")
|
|
|
+ json.NewEncoder(w).Encode(map[string]string{
|
|
|
+ "var1": sync,
|
|
|
+ "var2": strings.ToUpper(t.TimeSource),
|
|
|
+ "var3": t.SystemTime,
|
|
|
+ "var4": boot,
|
|
|
+ "var5": uptime,
|
|
|
+ })
|
|
|
+}
|