Skip to content

Commit e1c31b4

Browse files
committed
adding system cpu and memory metrics
1 parent f9e9925 commit e1c31b4

File tree

5 files changed

+87
-1
lines changed

5 files changed

+87
-1
lines changed

cache/redis.go

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package cache
2+
3+
import (
4+
"fmt"
5+
6+
"gopkg.in/redis.v5"
7+
)
8+
9+
var Client *redis.Client
10+
11+
/**
12+
* redis client
13+
*/
14+
func RedisConnect() {
15+
Client = redis.NewClient(&redis.Options{
16+
Addr: "localhost:6379",
17+
Password: "", // no password set
18+
DB: 0, // use default DB
19+
})
20+
21+
pong, err := Client.Ping().Result()
22+
fmt.Println(pong, err)
23+
}

debug

461 KB
Binary file not shown.

main.go

+6-1
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,21 @@ import (
88
//
99
// sw "github.com/myname/myrepo/go"
1010
//
11+
1112
"log"
1213
"net/http"
1314

15+
cache "server/cache"
1416
sw "server/mylib"
17+
sy "server/system"
1518
)
1619

1720
func main() {
1821
log.Printf("Server started")
1922

2023
router := sw.NewRouter()
21-
24+
sy.Iterator()
25+
sy.CPUIterator()
26+
cache.RedisConnect()
2227
log.Fatal(http.ListenAndServe(":8080", router))
2328
}

system/cpu.go

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package system
2+
3+
import (
4+
"fmt"
5+
"time"
6+
7+
"github.com/shirou/gopsutil/cpu"
8+
)
9+
10+
/**
11+
This will read system memory
12+
**/
13+
func readcpu() []float64 {
14+
v, _ := cpu.Percent(0, false)
15+
16+
// almost every return value is a struct
17+
// fmt.Printf("Total: %v, Free:%v, UsedPercent:%f%%\n", v., v.Free, v.UsedPercent)
18+
19+
// convert to JSON. String() is also implemented
20+
// fmt.Println("this is cool", v)
21+
return v
22+
}
23+
24+
func CPUIterator() {
25+
c := time.Tick(time.Second)
26+
for now := range c {
27+
fmt.Println(now, readcpu())
28+
}
29+
}

system/memory.go

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package system
2+
3+
import (
4+
"fmt"
5+
"time"
6+
7+
"github.com/shirou/gopsutil/mem"
8+
)
9+
10+
/**
11+
This will read system memory
12+
**/
13+
func readmemory() *mem.VirtualMemoryStat {
14+
v, _ := mem.VirtualMemory()
15+
16+
// almost every return value is a struct
17+
fmt.Printf("Total: %v, Free:%v, UsedPercent:%f%%\n", v.Total, v.Free, v.UsedPercent)
18+
19+
// convert to JSON. String() is also implemented
20+
fmt.Println(v)
21+
return v
22+
}
23+
24+
func Iterator() {
25+
c := time.Tick(time.Second)
26+
for now := range c {
27+
fmt.Print(now, readmemory())
28+
}
29+
}

0 commit comments

Comments
 (0)