@@ -25,160 +25,158 @@ const (
2525 GB = 1024 * MB
2626)
2727
28- var (
29- //Version string
30- //expectDiskFsTypes = []string{
31- // "apfs", "ext4", "ext3", "ext2", "f2fs", "reiserfs", "jfs", "btrfs",
32- // "fuseblk", "zfs", "simfs", "ntfs", "fat32", "exfat", "xfs", "fuse.rclone",
33- //}
34- excludeNetInterfaces = []string {
35- "lo" , "tun" , "docker" , "veth" , "br-" , "vmbr" , "vnet" , "kube" ,
36- }
37- //getMacDiskNo = regexp.MustCompile(`\/dev\/disk(\d)s.*`)
38- )
39-
40- var (
41- netInSpeed , netOutSpeed , netInTransfer , netOutTransfer , lastUpdateNetStats uint64
42- cachedBootTime time.Time
43- )
28+ var excludeNetInterfaces = []string {
29+ "lo" , "tun" , "docker" , "veth" , "br-" , "vmbr" , "vnet" , "kube" ,
30+ }
4431
4532type ServerMonitor struct {
4633 api.Api
4734}
4835
4936// GetHourDiffer 获取相差时间
5037func GetHourDiffer (startTime , endTime string ) int64 {
51- var hour int64
5238 t1 , err1 := time .ParseInLocation ("2006-01-02 15:04:05" , startTime , time .Local )
5339 t2 , err2 := time .ParseInLocation ("2006-01-02 15:04:05" , endTime , time .Local )
54- if err1 == nil && err2 == nil && t1 .Before (t2 ) {
55- diff := t2 .Unix () - t1 .Unix () //
56- hour = diff / 3600
57- return hour
58- } else {
59- return hour
40+ if err1 != nil || err2 != nil || ! t1 .Before (t2 ) {
41+ return 0
6042 }
43+ return (t2 .Unix () - t1 .Unix ()) / 3600
6144}
6245
6346// ServerInfo 获取系统信息
64- // @Summary 系统信息
65- // @Description 获取JSON
66- // @Tags 系统信息
67- // @Success 200 {object} response.Response "{"code": 200, "data": [...]}"
68- // @Router /api/v1/server-monitor [get]
69- // @Security Bearer
7047func (e ServerMonitor ) ServerInfo (c * gin.Context ) {
7148 e .Context = c
7249
50+ osInfo := getOSInfo ()
51+ memInfo := getMemoryInfo ()
52+ swapInfo := getSwapInfo ()
53+ cpuInfo := getCPUInfo ()
54+ diskInfo := getDiskInfo ()
55+ netInfo := getNetworkInfo ()
56+
57+ bootTime , _ := host .BootTime ()
58+ cachedBootTime := time .Unix (int64 (bootTime ), 0 )
59+
60+ e .Custom (gin.H {
61+ "code" : 200 ,
62+ "os" : osInfo ,
63+ "mem" : memInfo ,
64+ "cpu" : cpuInfo ,
65+ "disk" : diskInfo ,
66+ "net" : netInfo ,
67+ "swap" : swapInfo ,
68+ "location" : "Aliyun" ,
69+ "bootTime" : GetHourDiffer (cachedBootTime .Format ("2006-01-02 15:04:05" ), time .Now ().Format ("2006-01-02 15:04:05" )),
70+ })
71+ }
72+
73+ func getOSInfo () map [string ]interface {} {
7374 sysInfo , _ := host .Info ()
74- osDic := make (map [string ]interface {}, 0 )
75- osDic ["goOs" ] = runtime .GOOS
76- osDic ["arch" ] = runtime .GOARCH
77- osDic ["mem" ] = runtime .MemProfileRate
78- osDic ["compiler" ] = runtime .Compiler
79- osDic ["version" ] = runtime .Version ()
80- osDic ["numGoroutine" ] = runtime .NumGoroutine ()
81- osDic ["ip" ] = pkg .GetLocalHost ()
82- osDic ["projectDir" ] = pkg .GetCurrentPath ()
83- osDic ["hostName" ] = sysInfo .Hostname
84- osDic ["time" ] = time .Now ().Format ("2006-01-02 15:04:05" )
75+ return map [string ]interface {}{
76+ "goOs" : runtime .GOOS ,
77+ "arch" : runtime .GOARCH ,
78+ "mem" : runtime .MemProfileRate ,
79+ "compiler" : runtime .Compiler ,
80+ "version" : runtime .Version (),
81+ "numGoroutine" : runtime .NumGoroutine (),
82+ "ip" : pkg .GetLocalHost (),
83+ "projectDir" : pkg .GetCurrentPath (),
84+ "hostName" : sysInfo .Hostname ,
85+ "time" : time .Now ().Format ("2006-01-02 15:04:05" ),
86+ }
87+ }
8588
89+ func getMemoryInfo () map [string ]interface {} {
8690 memory , _ := mem .VirtualMemory ()
87- memDic := make ( map [string ]interface {}, 0 )
88- memDic [ "used" ] = memory .Used / MB
89- memDic [ "total" ] = memory .Total / MB
90-
91- fmt . Println ( "mem" , int ( memory . Total / memory . Used * 100 ))
92- memDic [ "percent" ] = pkg . Round ( memory . UsedPercent , 2 )
91+ return map [string ]interface {}{
92+ "used" : memory .Used / MB ,
93+ "total" : memory .Total / MB ,
94+ "percent" : pkg . Round ( memory . UsedPercent , 2 ),
95+ }
96+ }
9397
94- swapDic := make (map [string ]interface {}, 0 )
95- swapDic ["used" ] = memory .SwapTotal - memory .SwapFree
96- swapDic ["total" ] = memory .SwapTotal
98+ func getSwapInfo () map [string ]interface {} {
99+ memory , _ := mem .VirtualMemory ()
100+ return map [string ]interface {}{
101+ "used" : memory .SwapTotal - memory .SwapFree ,
102+ "total" : memory .SwapTotal ,
103+ }
104+ }
97105
98- cpuDic := make ( map [string ]interface {}, 0 )
99- cpuDic [ " cpuInfo" ] , _ = cpu .Info ()
106+ func getCPUInfo () map [string ]interface {} {
107+ cpuInfo , _ : = cpu .Info ()
100108 percent , _ := cpu .Percent (0 , false )
101- cpuDic ["percent" ] = pkg .Round (percent [0 ], 2 )
102- cpuDic ["cpuNum" ], _ = cpu .Counts (false )
109+ cpuNum , _ := cpu .Counts (false )
110+ return map [string ]interface {}{
111+ "cpuInfo" : cpuInfo ,
112+ "percent" : pkg .Round (percent [0 ], 2 ),
113+ "cpuNum" : cpuNum ,
114+ }
115+ }
103116
104- //服务器磁盘信息
105- disklist := make ([]disk.UsageStat , 0 )
106- //所有分区
117+ func getDiskInfo () map [string ]interface {} {
107118 var diskTotal , diskUsed , diskUsedPercent float64
119+ diskList := make ([]disk.UsageStat , 0 )
120+
108121 diskInfo , err := disk .Partitions (true )
109122 if err == nil {
110123 for _ , p := range diskInfo {
111124 diskDetail , err := disk .Usage (p .Mountpoint )
112125 if err == nil {
113126 diskDetail .UsedPercent , _ = strconv .ParseFloat (fmt .Sprintf ("%.2f" , diskDetail .UsedPercent ), 64 )
114- diskDetail .Total = diskDetail .Total / 1024 / 1024
115- diskDetail .Used = diskDetail .Used / 1024 / 1024
116- diskDetail .Free = diskDetail .Free / 1024 / 1024
117- disklist = append (disklist , * diskDetail )
118-
127+ diskDetail .Total /= MB
128+ diskDetail .Used /= MB
129+ diskDetail .Free /= MB
130+ diskList = append (diskList , * diskDetail )
119131 }
120132 }
121133 }
122134
123135 d , _ := disk .Usage ("/" )
124-
125136 diskTotal = float64 (d .Total / GB )
126137 diskUsed = float64 (d .Used / GB )
127138 diskUsedPercent , _ = strconv .ParseFloat (fmt .Sprintf ("%.2f" , d .UsedPercent ), 64 )
128139
129- diskDic := make (map [string ]interface {}, 0 )
130- diskDic ["total" ] = diskTotal
131- diskDic ["used" ] = diskUsed
132- diskDic ["percent" ] = diskUsedPercent
133-
134- bootTime , _ := host .BootTime ()
135- cachedBootTime = time .Unix (int64 (bootTime ), 0 )
140+ return map [string ]interface {}{
141+ "total" : diskTotal ,
142+ "used" : diskUsed ,
143+ "percent" : diskUsedPercent ,
144+ }
145+ }
136146
137- TrackNetworkSpeed ()
138- netDic := make (map [string ]interface {}, 0 )
139- netDic ["in" ] = pkg .Round (float64 (netInSpeed / KB ), 2 )
140- netDic ["out" ] = pkg .Round (float64 (netOutSpeed / KB ), 2 )
141- e .Custom (gin.H {
142- "code" : 200 ,
143- "os" : osDic ,
144- "mem" : memDic ,
145- "cpu" : cpuDic ,
146- "disk" : diskDic ,
147- "net" : netDic ,
148- "swap" : swapDic ,
149- "location" : "Aliyun" ,
150- "bootTime" : GetHourDiffer (cachedBootTime .Format ("2006-01-02 15:04:05" ), time .Now ().Format ("2006-01-02 15:04:05" )),
151- })
147+ func getNetworkInfo () map [string ]interface {} {
148+ netInSpeed , netOutSpeed := trackNetworkSpeed ()
149+ return map [string ]interface {}{
150+ "in" : pkg .Round (float64 (netInSpeed / KB ), 2 ),
151+ "out" : pkg .Round (float64 (netOutSpeed / KB ), 2 ),
152+ }
152153}
153154
154- func TrackNetworkSpeed ( ) {
155- var innerNetInTransfer , innerNetOutTransfer uint64
155+ func trackNetworkSpeed () ( uint64 , uint64 ) {
156+ var netInSpeed , netOutSpeed , netInTransfer , netOutTransfer , lastUpdateNetStats uint64
156157 nc , err := net .IOCounters (true )
157158 if err == nil {
158159 for _ , v := range nc {
159160 if isListContainsStr (excludeNetInterfaces , v .Name ) {
160161 continue
161162 }
162- innerNetInTransfer += v .BytesRecv
163- innerNetOutTransfer += v .BytesSent
163+ netInTransfer += v .BytesRecv
164+ netOutTransfer += v .BytesSent
164165 }
165166 now := uint64 (time .Now ().Unix ())
166167 diff := now - lastUpdateNetStats
167168 if diff > 0 {
168- netInSpeed = (innerNetInTransfer - netInTransfer ) / diff
169- fmt .Println ("netInSpeed" , netInSpeed )
170- netOutSpeed = (innerNetOutTransfer - netOutTransfer ) / diff
171- fmt .Println ("netOutSpeed" , netOutSpeed )
169+ netInSpeed = (netInTransfer - netInTransfer ) / diff
170+ netOutSpeed = (netOutTransfer - netOutTransfer ) / diff
172171 }
173- netInTransfer = innerNetInTransfer
174- netOutTransfer = innerNetOutTransfer
175172 lastUpdateNetStats = now
176173 }
174+ return netInSpeed , netOutSpeed
177175}
178176
179177func isListContainsStr (list []string , str string ) bool {
180- for i := 0 ; i < len ( list ); i ++ {
181- if strings .Contains (str , list [ i ] ) {
178+ for _ , item := range list {
179+ if strings .Contains (str , item ) {
182180 return true
183181 }
184182 }
0 commit comments