package main
import (
"fmt"
"net/http"
"runtime"
"github.com/gin-gonic/gin"
"github.com/shirou/gopsutil/cpu"
"github.com/shirou/gopsutil/disk"
"github.com/shirou/gopsutil/host"
"github.com/shirou/gopsutil/load"
"github.com/shirou/gopsutil/mem"
"github.com/shirou/gopsutil/net"
"github.com/shirou/gopsutil/process"
)
type Disk struct {
Total int `json:"总共Total"`
Free int `json:"剩余Free"`
Used int `json:"使用Used"`
}
func main() {
router := gin.Default()
router.LoadHTMLGlob("templates/**/*")
//获取主机名
nInfo, _ := host.Info()
//获取当前负载情况
infoc, _ := load.Avg()
//
v, _ := mem.VirtualMemory()
//cpu详情
b, _ := cpu.Times(true)
//进程详情
pi, _ := process.Pids()
//进程统计
bc := len(pi) //获取元素的个数
//磁盘1
parts, _ := disk.Partitions(true)
for _, part := range parts {
//fmt.Printf("part:%v\n", part.String())
diskInfo, _ := disk.Usage(part.Mountpoint)
//磁盘2
//state := diskstate.DiskUsage("/")
//网卡
netinfo, _ := net.IOCounters(true)
for index, s := range netinfo {
fmt.Printf("网卡使用情况: %v:%v\n send:%v\n recv:%v\n", index, s, s.BytesSent, s.BytesRecv)
router.GET("/", indes)
router.GET("/sys/index", func(c *gin.Context) {
c.HTML(http.StatusOK, "sys/index.tmpl", gin.H{
"title1": nInfo.Hostname,
"title2": infoc.String(),
"title3": v.UsedPercent,
"title4": v.Available / 1024 / 1024,
"title5": v.Total / 1024 / 1024 / 1024,
"title6": runtime.NumCPU(), //cpu物理cpu统计
"title7": b,
"title8": bc, //进程统计
"title9": pi, //进程详情
"title10": diskInfo,
"title11": netinfo,
//"title11": state.All / diskstate.MB, state.Free / diskstate.MB, state.Available / diskstate.MB, state.Used / diskstate.MB, 100 * state.Used / state.All,
"title20": Disk{
Used: int(diskInfo.Used / 1024 / 1024 / 1024),
Free: int(diskInfo.Free / 1024 / 1024 / 1024),
Total: int(diskInfo.Total / 1024 / 1024 / 1024),
},
})
})
router.Run(":8080")
}
}
}
func indes(ctx *gin.Context) {
nInfo2, _ := host.Info()
ctx.JSON(200, gin.H{
"hostname": nInfo2.Hostname,
})
}
模板
[root@dockser sys]# cat templates/sys/index.tmpl
{{ define "sys/index.tmpl" }}
<html><h1>
<p> 主机名:{{ .title1 }} <p/>
<p> 当前负载情况:{{ .title2 }} <p/>
<p> 已经使用内存:{{ .title3}}% <p/>
<p> 闲置可用内存:{{ .title4 }}MB <p/>
<p> 内存总共:{{ .title5 }}G <p/>
<p> 物理CPU:{{ .title6 }}个 <p/>
<p> CPU详细:{{ .title7 }} <p/>
<p> 当前进程:{{ .title8 }}个 <p/>
<p> 当前进程详情:{{ .title9 }} <p/>
<p> 当前磁盘1:{{ .title10 }}G <p/>
<p> 当前磁盘:{{ .title20 }}G <p/>
<p> 当前网卡:{{ .title11 }}G <p/>
</h1>
<p>Using sys/index.tmpl</p>
</html>
{{ end }}
目录结构
tree templates/
templates/
└── sys
└── index.tmpl
1 directory, 1 file
docker阶级构建的方式
tee Dockerfile<<-'EOF'
FROM golang:1.17.6-alpine3.14
ENV GOPROXY https://goproxy.cn
WORKDIR /data
RUN go get -d -v github.com/gin-gonic/gin
RUN go get -d -v github.com/shirou/gopsutil/cpu
RUN go get -d -v github.com/shirou/gopsutil/disk
RUN go get -d -v github.com/shirou/gopsutil/host
RUN go get -d -v github.com/shirou/gopsutil/load
RUN go get -d -v github.com/shirou/gopsutil/mem
RUN go get -d -v github.com/shirou/gopsutil/net
RUN go get -d -v github.com/shirou/gopsutil/process
COPY . .
RUN go mod init app
RUN go mod tidy
RUN CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -o app .
FROM alpine:latest
ENV TZ='Asia/Shanghai'
ENV TIMEZONE Asia/Shanghai
RUN echo "https://mirrors.ustc.edu.cn/alpine/latest-stable/main/" > /etc/apk/repositories && \
echo "https://mirrors.ustc.edu.cn/alpine/latest-stable/community/" >> /etc/apk/repositories && \
apk update && apk upgrade && \
apk add tzdata bash-doc bash && \
ln -snf /usr/share/zoneinfo/$TIMEZONE /etc/localtime && \
echo $TIMEZONE > /etc/timezone
WORKDIR /data
COPY templates /data/templates
COPY --from=0 /data/app .
CMD ["./app"]
EXPOSE 8080
EOF
- @@@现成镜像
registry.cn-shenzhen.aliyuncs.com/jbjb/logs:ginv03
k8s
tee gin-web.yaml <<'EOF'
apiVersion: apps/v1
kind: Deployment
metadata:
name: mygin-v1
labels:
app: mygin
spec:
replicas: 3
selector:
matchLabels:
app: mygin
template:
metadata:
labels:
app: mygin
spec:
containers:
- name: mygin
image: registry.cn-shenzhen.aliyuncs.com/jbjb/logs:ginv03
resources:
requests:
memory: 256Mi
ports:
- containerPort: 8080
---
apiVersion: v1
kind: Service
metadata:
name: mygin-v1
labels:
app: mygin
spec:
type: NodePort
ports:
- port: 8080
targetPort: 8080
selector:
app: mygin
---
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: gin-app
annotations:
kubernetes.io/ingress.class: nginx
kubernetes.io/elb.port: '80'
spec:
rules:
- host: jb.jbjb2.com
http:
paths:
- path: '/'
pathType: Prefix
backend:
service:
name: mygin-v1
port:
number: 8080
EOF
while true; do curl jb.jbjb2.com -w '\n'; sleep 1; done
{"hostname":"mygin-v1-5785df5d68-zzn9m"}
{"hostname":"mygin-v1-5785df5d68-t6rsc"}
{"hostname":"mygin-v1-5785df5d68-t6rsc"}
{"hostname":"mygin-v1-5785df5d68-j2jkm"}
{"hostname":"mygin-v1-5785df5d68-zzn9m"}
{"hostname":"mygin-v1-5785df5d68-j2jkm"}
{"hostname":"mygin-v1-5785df5d68-zzn9m"}
{"hostname":"mygin-v1-5785df5d68-zzn9m"}
{"hostname":"mygin-v1-5785df5d68-zzn9m"}
{"hostname":"mygin-v1-5785df5d68-t6rsc"}
{"hostname":"mygin-v1-5785df5d68-zzn9m"}
{"hostname":"mygin-v1-5785df5d68-j2jkm"}