知识点
ajax定时刷新
json转换,ajax接收json
go语言的float64,和转换成string
———————————————————–
index.html
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="description" content="">
<title>Go语言与ajax示例</title>
<!-- Bootstrap core CSS -->
<link href="assets/dist/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
<div class="container themed-container">
<header class="d-flex justify-content-center py-3">
<p class="h2">系统信息</p>
</header>
<p class="mt-4" id="cpu">Cpu: </p>
<p class="mt-4" id="disk">Disk: </p>
<p class="mt-4" id="mem">Memory: </p>
<p class="mt-4" id="speed">网络: </p>
</div>
<script src="assets/dist/js/bootstrap.bundle.min.js"></script>
<script src="assets/dist/js/jquery-3.6.0.min.js"></script>
<script src="assets/dist/js/loli.js"></script>
</body>
</html>
js部分
$(function (){
setInterval(myajax, 2000);
function myajax(){
$.get('/ajax',function(data){
$('#cpu').text("Cpu: "+data.Cpuused+"%")
$('#disk').text("Disk: "+data.Diskused+"% used "+data.Diskfree+"G/"+ data.Disktotal+"G")
$('#mem').text("Memory: "+data.Memused+"% used "+data.Memavailable+"G/"+ data.Memtotal+"G")
$('#speed').text("网络: 下载: "+data.Netdown+" | 上传: "+data.Netup)
});
};
});
js设置每2秒刷新一次
main.go
package main
import (
"encoding/json"
"fmt"
"net/http"
"strconv"
"text/template"
"time"
"github.com/shirou/gopsutil/cpu"
"github.com/shirou/gopsutil/disk"
"github.com/shirou/gopsutil/mem"
"github.com/shirou/gopsutil/net"
)
type Sys struct {
Cpuused float64
Memused float64
Memavailable float64
Memtotal float64
Disktotal float64
Diskfree float64
Diskused float64
Netsend float64
Netrecv float64
Netup string
Netdown string
}
func OnAjax(res http.ResponseWriter, req *http.Request) {
s := getstatus()
d, _ := json.Marshal(s)
res.Header().Set("Content-type", "application/json;charset=utf-8")
fmt.Fprintln(res,string(d))
}
func index(w http.ResponseWriter, r *http.Request) {
t, _ := template.ParseFiles("index.html") //找模板
t.Execute(w, nil) //执行模板
}
func main() {
http.Handle("/assets/", http.StripPrefix("/assets/", http.FileServer(http.Dir("assets"))))
http.HandleFunc("/", index) //函数也是一种变量
http.HandleFunc("/ajax", OnAjax) //函数也是一种变量
http.ListenAndServe(":8082", nil)
}
func toG(i uint64) float64 {
return float64(i) / 1024 / 1024 / 1024
}
func toKB(i uint64) float64 {
return float64(i) / 1024
}
func toMB(i uint64) float64 {
return float64(i) / 1024 / 1024
}
func Decimal(value float64) float64 {//float64保留2位小数
value, _ = strconv.ParseFloat(fmt.Sprintf("%.2f", value), 64)
return value
}
func getNetSpeed() (string, string) {
// 读取所有网卡网速
Net, _ := net.IOCounters(true)
// 定义网速保存变量
var rx, tx uint64
// 循环网络信息
for _, nv := range Net {
// 去除多余信息
if "eth0" == nv.Name {
// 加上网速信息
rx += nv.BytesRecv
tx += nv.BytesSent
}
}
// 暂停一秒
time.Sleep(time.Second)
// 重新读取网络信息
Net, _ = net.IOCounters(true)
// 网络信息保存变量
var rx2, tx2 uint64
// 循环网络信息
for _, nv := range Net {
// 去除多余信息
if "eth0" == nv.Name {
// 加上网速信息
rx2 += nv.BytesRecv
tx2 += nv.BytesSent
}
}
// 两次相减
if toKB(rx2-rx) > 1024 || toKB(tx2-tx) > 1024 {
down:= strconv.FormatFloat(toMB(rx2 - rx),'f',2,64)+"MB/S"
up:= strconv.FormatFloat(toMB(tx2 - tx),'f',2,64)+"MB/S"
return down, up
} else {
down:= strconv.FormatFloat(toKB(rx2 - rx),'f',2,64)+"KB/S"
up:= strconv.FormatFloat(toKB(tx2 - tx),'f',2,64)+"KB/S"
return down, up
}
}
func getstatus() Sys {
s := Sys{}
m, _ := mem.VirtualMemory()
c, _ := cpu.Percent(time.Second, false)
d, _ := disk.Usage("/")
n, _ := net.IOCounters(true)
s.Netsend = Decimal(toG(n[1].BytesSent))
s.Netrecv = Decimal(toG(n[1].BytesRecv))
s.Diskused = Decimal(d.UsedPercent)
s.Diskfree = Decimal(toG(d.Free))
s.Disktotal = Decimal(toG(d.Total))
s.Memused = Decimal(m.UsedPercent)
s.Memtotal = Decimal(toG(m.Total))
s.Memavailable = Decimal(toG(m.Available))
s.Cpuused = Decimal(c[0])
s.Netdown, s.Netup = getNetSpeed()
return s
}
代码并不具有通用性,我只获取了自己vps的信息,比如网卡选的是eth0