Exemplo n.º 1
0
func (h *RpcHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
	oa := CheckAuthorized(w, r, false)
	if oa == nil {
		return
	}

	sURL := r.URL.Path[5:]
	//log.Println("rpc:", sURL)
	mParams := r.URL.Query()
	if sURL == "shutdown" {
		DoShutdown()
		return
	}

	if sURL == "backupget" {
		file := mParams.Get("file")
		w.Header().Set("Content-Disposition", "attachment; filename="+file)
		http.ServeFile(w, r, "backup/"+file)
		return
	}

	if sURL == "backupload" {
		m, h, err := r.FormFile("backup")
		if err != nil {
			http.Error(w, err.Error(), http.StatusNotFound)
			return
		}
		defer m.Close()
		sFile := "backup/" + h.Filename
		dst, err := os.Create(sFile)
		if err != nil {
			http.Error(w, err.Error(), http.StatusInternalServerError)
			return
		}
		_, err = io.Copy(dst, m)
		dst.Close()
		if err != nil {
			http.Error(w, err.Error(), http.StatusInternalServerError)
			return
		}

		finfo, _ := os.Stat(sFile)

		w.WriteHeader(200)
		w.Write([]byte("Uploaded: " + humanize.Bytes(uint64(finfo.Size()))))
		return
	}

	var err error
	var data interface{}
	defer func() {
		var result = map[string]interface{}{}
		if err == nil {
			result["result"] = true
			if data != nil {
				result["data"] = data
			}
		} else {
			log.Println("rpc error:", sURL, err)
			result["result"] = false
			result["error"] = err.Error()
		}
		msg, _ := json.Marshal(result)
		w.Write(msg)
	}()

	if sURL == "start" {
		err = ServerStart()
		return
	}
	if sURL == "stop" {
		err = ServerStop()
		return
	}
	if sURL == "flush" {
		err = ServerFlush()
		return
	}
	if sURL == "deploy" {
		err = DoDeployServer()
		return
	}
	if sURL == "dobackup" {
		err = DoBackup()
		return
	}
	if sURL == "backuplist" {
		data, err = GetBackupList()
		return
	}
	if sURL == "backupdel" {
		file := mParams.Get("file")
		if len(file) < 1 {
			err = errors.New("invalid file:" + file)
			return
		}
		sFile := "backup/" + file
		err = os.RemoveAll(sFile)
		return
	}
	if sURL == "backuprestore" {
		file := mParams.Get("file")
		if len(file) < 1 {
			err = errors.New("invalid file:" + file)
			return
		}
		sFile := "backup/" + file
		err = DoBackupRestore(sFile)
		return
	}
	if sURL == "activate" {
		//err = srv.DoActivateVersion(true, mParams.Get("version"))
		return
	}
	if sURL == "deactivate" {
		//err = srv.DoActivateVersion(false, mParams.Get("version"))
		return
	}
	if sURL == "autorestart" {
		val := mParams.Get("val")
		v := false
		if val == "true" {
			v = true
		}
		//log.Println("autorestart", val)
		SetAutoRestart(v)
		return
	}
	if sURL == "files" {
		//d := map[string]interface{}{}
		//d["files"], err = srv.GetVersionFiles(mParams.Get("version"))
		//data = d
		return
	}
	if sURL == "db" {
		data, err = db.DoAdminRpc(mParams)
		return
	}

	if sURL == "status" {
		d := map[string]interface{}{}
		if len(mParams.Get("log")) > 0 {
			t, _ := strconv.Atoi(mParams.Get("logt"))
			log, t := srv.GetLog(t)
			d["log"] = log
			d["logt"] = t
		}
		if len(mParams.Get("deploy")) > 0 {
			d["deploy"] = GetDeployInfo()
		}
		if len(mParams.Get("server")) > 0 {
			d["server"] = ServerStatus()
		}
		if len(mParams.Get("stats")) > 0 {
			d["stats"] = srv.GetStatsCur()
		}
		if len(mParams.Get("system")) > 0 {
			sys := map[string]interface{}{}

			var mem perf.Mem
			err = mem.Get()
			sys["mem_load"] = mem.Load
			sys["mem_total"] = mem.Total
			sys["mem_used"] = mem.Used
			sys["mem_pagetotal"] = mem.PageTotal
			sys["mem_pageused"] = mem.PageUsed
			sys["mem_cache"] = mem.Cache

			uptCtrl := time.Since(timeStart)
			uptSys, _ := perf.GetUptime()
			//log.Println("x:", uptSys, uptCtrl)
			sys["upt_system"] = uptSys / time.Millisecond
			sys["upt_ctrl"] = uptCtrl / time.Millisecond

			var cpu perf.Cpu
			err = cpu.Get()
			sys["cpu_loadtotal"] = cpu.LoadTotal
			sys["cpu_loadsys"] = cpu.LoadSys

			d["system"] = sys
		}

		data = d
		return
	}

	log.Println("invalid rpc:", sURL)
}
Exemplo n.º 2
0
func StatsRun() {
	tickSec := time.NewTicker(time.Second)
	tickMin := time.NewTicker(time.Minute)
	tickHou := time.NewTicker(time.Hour)
	defer tickSec.Stop()
	defer tickMin.Stop()
	defer tickHou.Stop()

	var sCurLoadSys int
	var sCurLoadTot int
	var sCurLoadMem int
	var sCurRqCount int
	var sCurClientRPCs int
	var sCurClientPtlChg int
	var sCurClientErrors int
	var sCurRqDur int
	var sCurMin StatsEntry
	var cpu perf.Cpu
	var mem perf.Mem
	var iCurMinCpu = 0
	var iCurMinMem = 0
	var iLastMin = -1
	var iLastHou = -1
	var iLastRqCount int64 = -1
	var iLastRqDur int64 = -1
	var iLastClientRPCs int64 = -1
	var iLastClientErrors int64 = -1
	var iLastClientPtlChg int64 = -1

	iLastMin = statsToday.LastMinute()
	if iLastMin != -1 {
		tLast := statsToday.MinutesHour[iLastMin].Time()
		now := time.Now()
		dist := now.Sub(tLast)
		//log.Println(">>>>>>>>>>>>>>>>> lm:", iLastMin, tLast.Hour(), now.Hour(), dist)
		if tLast.YearDay() != now.YearDay() || dist > time.Hour*24 {
			StatsCycleDay()
		}
		if tLast.Hour() != now.Hour() || dist > time.Hour {
			statsToday.ClearMinutesHour()
		}
	}

	for {
		select {
		case <-tickSec.C:
			if cpu.Get() == nil {
				iCurMinCpu++
				sCurLoadSys += int(cpu.LoadSys * 100)
				if int8(cpu.LoadSys*100) > sCurMin.LoadSysMax {
					sCurMin.LoadSysMax = int8(cpu.LoadSys * 100)
				}
				sCurLoadTot += int(cpu.LoadTotal * 100)
				if int8(cpu.LoadTotal*100) > sCurMin.LoadTotalMax {
					sCurMin.LoadTotalMax = int8(cpu.LoadTotal * 100)
				}
			}
			if mem.Get() == nil {
				iCurMinMem++
				sCurLoadMem += int(mem.Load)

				if int8(mem.Load) > sCurMin.LoadMemMax {
					sCurMin.LoadMemMax = int8(mem.Load)
				}
			}

			if SrvRequestCount > iLastRqCount && iLastRqCount != -1 {
				sCurRqCount += int(SrvRequestCount - iLastRqCount)
				sCurRqDur += int((SrvRequestDur - iLastRqDur) / 1000)
			}
			iLastRqCount = SrvRequestCount
			iLastRqDur = SrvRequestDur

			if SrvClientRPCs > iLastClientRPCs && iLastClientRPCs != -1 {
				sCurClientRPCs += int(SrvClientRPCs - iLastClientRPCs)
			}
			if SrvClientPtlChg > iLastClientPtlChg && iLastClientPtlChg != -1 {
				sCurClientPtlChg += int(SrvClientPtlChg - iLastClientPtlChg)
			}
			if SrvClientErrors > iLastClientErrors && iLastClientErrors != -1 {
				sCurClientErrors += int(SrvClientErrors - iLastClientErrors)
			}
			iLastClientRPCs = SrvClientRPCs
			iLastClientPtlChg = SrvClientPtlChg
			iLastClientErrors = SrvClientErrors

			now := time.Now()
			min := now.Minute()
			hou := now.Hour()

			if min != iLastMin {
				//flush minute
				iLastMin = min
				sCurLoadSys = 0
				sCurLoadTot = 0
				sCurLoadMem = 0
				iCurMinCpu = 0
				iCurMinMem = 0
				sCurRqCount = 0
				sCurRqDur = 0
				sCurClientRPCs = 0
				sCurClientPtlChg = 0
				sCurClientErrors = 0
				sCurMin = StatsEntry{}
				//statsToday.MinutesHour[min] = StatsEntry{}
				StatsSave()
			}
			if hou != iLastHou {
				//flush hour
				if iLastHou != -1 {
					statsToday.ClearMinutesHour()
				}
				if iLastHou > hou {
					StatsCycleDay()
				}
				iLastHou = hou
			}

			cur := &statsToday.MinutesHour[min]
			if iCurMinCpu > 0 {
				cur.LoadSysCur = int8(sCurLoadSys / iCurMinCpu)
				cur.LoadSysMax = sCurMin.LoadSysMax
				cur.LoadTotalCur = int8(sCurLoadTot / iCurMinCpu)
				cur.LoadTotalMax = sCurMin.LoadTotalMax
			}
			if iCurMinMem > 0 {
				cur.LoadMemCur = int8(sCurLoadMem / iCurMinMem)
				cur.LoadMemMax = sCurMin.LoadMemMax
			}
			cur.RqCount = sCurRqCount
			cur.RqDur = sCurRqDur
			cur.RPCs = sCurClientRPCs
			cur.PortChg = sCurClientPtlChg
			cur.CErr = sCurClientErrors
			cur.Timestamp = uint64(now.UnixNano()) / (1000 * 1000)

			//log.Println(">>>>>>>>>>>>>>>>> :", cur.RqCount, cur.RqDur)
			//log.Println(">>>>>>>>>>>>>>>>>:", now, cur.Time())

			statsToday.CompactHour(hou)

		case <-tickMin.C:
		case <-tickHou.C:
		case <-quit:
			return
		}
	}
}