介绍项目:仿写监控系统资源项目
主要功能:监控服务资源可视化
开发环境 :windows+idea+influxdb+granfa
主要技术:时序性数据库influxdb 和可视化平台granfa
项目描述 实时监控本机资源,展示成可视化页面。
负责模块:监控cpu 内存等指数写到influxdb数据库,然后用granfa平台展示
influxdb模块
首先我们要先下载influxdb
注意事项:
先influxdb.conf
dir 然后修改文件路径 修改meta文件路径 自己设定
"E:/influxdb-1.8.4_windows_amd64/meta"
这是我自己的
默认端口8086 想改的花可以自己改
然后先双击influxd.exe
启动之后就算我们的goland项目
goland第三方库
"github.com/influxdata/influxdb1-client/v2"
import (
"fmt"
client "github.com/influxdata/influxdb1-client/v2"
"log"
"time"
)
// influxdb demo
func connInflux() client.Client {
cli, err := client.NewHTTPClient(client.HTTPConfig{
Addr: "http://127.0.0.1:8086",
Username: "admin",
Password: "",
})
if err != nil {
log.Fatal(err)
}
return cli
}
// query
func queryDB(cli client.Client, cmd string) (res []client.Result, err error) {
q := client.Query{
Command: cmd,
Database: "test",
}
if response, err := cli.Query(q); err == nil {
if response.Error() != nil {
return res, response.Error()
}
res = response.Results
} else {
return res, err
}
return res, nil
}
// insert
func writesPoints(cli client.Client) {
bp, err := client.NewBatchPoints(client.BatchPointsConfig{
Database: "test",
Precision: "s", //精度,默认ns
})
if err != nil {
log.Fatal(err)
}
tags := map[string]string{"cpu": "ih-cpu"}
fields := map[string]interface{}{
"idle": 201.1,
"system": 43.3,
"user": 86.6,
}
pt, err := client.NewPoint("cpu_usage", tags, fields, time.Now())
if err != nil {
log.Fatal(err)
}
bp.AddPoint(pt)
err = cli.Write(bp)
if err != nil {
log.Fatal(err)
}
log.Println("insert success")
}
func main() {
conn := connInflux()
fmt.Println(conn)
// insert
writesPoints(conn)
// 获取10条数据并展示
qs := fmt.Sprintf("SELECT * FROM %s LIMIT %d", "cpu_usage", 10)
res, err := queryDB(conn, qs)
if err != nil {
log.Fatal(err)
}
for _, row := range res[0].Series[0].Values {
for j, value := range row {
log.Printf("j:%d value:%v\n", j, value)
}
}
测试influxdb的demo
本次我们用的时cpu的监控。
package main
import (
"fmt"
client "github.com/influxdata/influxdb1-client/v2"
"github.com/shirou/gopsutil/cpu"
"log"
"time"
)
var (
cli client.Client
)
// influxdb demo
func initConnInflux() (err error) {
cli, err = client.NewHTTPClient(client.HTTPConfig{
Addr: "http://127.0.0.1:8086",
Username: "admin",
Password: "",
})
return
}
// insert
func writesPoints(percent int64) {
bp, err := client.NewBatchPoints(client.BatchPointsConfig{
Database: "test",
Precision: "s", //精度,默认ns
})
if err != nil {
log.Fatal(err)
}
tags := map[string]string{"cpu": "cpu0"}
fields := map[string]interface{}{
"cpu_percent": percent,
}
pt, err := client.NewPoint("cpu_percent", tags, fields, time.Now())
if err != nil {
log.Fatal(err)
}
bp.AddPoint(pt)
err = cli.Write(bp)
if err != nil {
log.Fatal(err)
}
log.Println("insert success")
}
func getCpuInfo() {
//cpu使用率
percent, _ := cpu.Percent(time.Second, false)
fmt.Printf("cpu percent:%v\n", percent)
writesPoints(int64(percent[0]))
}
func main() {
err := initConnInflux()
if err != nil {
fmt.Printf("connect to influxDB failed,err:%v", err)
return
}
//每一名秒钟的getCpuInfo
for {
getCpuInfo()
time.Sleep(time.Second)
}
}