代码展示
package main
import (
"bufio"
"fmt"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
"log"
"math"
"net/http"
"os"
"strconv"
"strings"
)
type netflow struct {
//flowSend prometheus.Gauge
//flowReceive prometheus.Gauge
//label *prometheus.CounterVec
flowSend *prometheus.CounterVec
flowReceive *prometheus.CounterVec
}
func NewMetrics(reg prometheus.Registerer) *netflow {
m := &netflow{
//flowSend: prometheus.NewGauge(prometheus.GaugeOpts{
// Name: "flowSend",
// Help: "Current flowSend of the Host.",
//}),
//flowReceive: prometheus.NewGauge(prometheus.GaugeOpts{
// Name: "flowReceive",
// Help: "Current flowReceive of the Host.",
//}),
flowSend: prometheus.NewCounterVec(
prometheus.CounterOpts{
Name: "flowSend",
Help: "Current flowSend of the Host.",
},
[]string{"host"},
),
flowReceive: prometheus.NewCounterVec(
prometheus.CounterOpts{
Name: "flowReceive",
Help: "Current flowReceive of the Host.",
},
[]string{"host"},
),
}
reg.MustRegister(m.flowSend)
reg.MustRegister(m.flowReceive)
return m
}
func main() {
// Create a non-global registry.
reg := prometheus.NewRegistry()
// Create new metrics and register them using the custom registry.
m := NewMetrics(reg)
// 打开文件
file, err := os.Open("D:\\zjh\\github\\node_exporter-master\\node_exporter-master\\test\\test.txt")
if err != nil {
fmt.Println(err)
return
}
defer func(file *os.File) {
err := file.Close()
if err != nil {
}
}(file)
// 创建一个新的扫描器
scanner := bufio.NewScanner(file)
//fmt.Println(scanner.Scan())
//fmt.Println("---------------")
// 遍历文件中的每一行
var count = 1
var groupnum = 0
//all_netflow := make(map[string]string)
//send_rece_arr := make([]float64, 6)
var send_flow = ""
for scanner.Scan() {
// 获取当前行
line := scanner.Text()
//line := strings.TrimSpace(line0)
//fmt.Println(line)
// 从当前行中获取切片
slice0 := strings.Split(line, " ")
// 打印切片
//fmt.Println("----------------")
if (count % 2) != 0 {
groupnum++
//发送
send_arr := make([]string, 7)
var scount = 0
//fmt.Printf("%d 组发送流量:%s\n", groupnum, slice0)
for _, v := range slice0 {
if v != "" {
//fmt.Println(v)
send_arr[scount] = v
scount++
}
}
//fmt.Println(send_arr)
//fmt.Println(send_arr[1], send_arr[len(send_arr)-2])
send_flow = send_arr[len(send_arr)-2]
} else {
//all_netflow[send_arr[0]+string("_send")] = send_arr[len(send_arr)-2]
//fmt.Printf("%d 组接收流量:%s\n", groupnum, slice0)
//for i := 0; i < len(slice0); i++ {
// if slice0[i] != "" {
// fmt.Println(i, slice0[i], "--")
// }
//}
//接收
rece_arr := make([]string, 6)
var rcount = 0
for _, v := range slice0 {
if v != "" {
//fmt.Println(v)
rece_arr[rcount] = v
rcount++
}
}
rece_flow := rece_arr[len(rece_arr)-2]
m.flowSend.With(prometheus.Labels{"host": rece_arr[0]}).Add(transformtokb(send_flow))
m.flowReceive.With(prometheus.Labels{"host": rece_arr[0]}).Add(transformtokb(rece_flow))
send_flow = strconv.FormatFloat(transformtokb(send_flow), 'f', 2, 64)
rece_flow = strconv.FormatFloat(transformtokb(rece_flow), 'f', 2, 64)
//all_netflow[rece_arr[0]] = send_flow + "|" + rece_flow
send_flow = ""
}
count++
}
//fmt.Println(all_netflow)
//m0 := make(map[string]float64)
// Set values for the new created metrics.
//m.flowSend.Set(65.3)
//m.flowReceive.Set(65.3)
//m.label.With(prometheus.Labels{"device": "/dev/sda"}).Add(4.1)
//m.label.With(prometheus.Labels{"device": "/dev/sdb"}).Add(4.1)
m.flowSend.With(prometheus.Labels{"host": "192.168.10.11"}).Add(4.6)
m.flowReceive.With(prometheus.Labels{"host": "192.168.10.11"}).Add(4.6)
// Expose metrics and custom registry via an HTTP server
// using the HandleFor function. "/metrics" is the usual endpoint for that.
http.Handle("/metrics", promhttp.HandlerFor(reg, promhttp.HandlerOpts{Registry: reg}))
log.Fatal(http.ListenAndServe(":8080", nil))
}
// 对b/Kb/Mb数据进行转换
func transformtokb(flow string) float64 {
//if strings.Split(flow, "Kb")[0] != "" {
// fmt.Println(strings.Split(flow, "Kb")[0])
//} else if strings.Split(flow, "b")[0] != "" {
// fmt.Println(strings.Split(flow, "b")[0])
//} else if strings.Split(flow, "Mb")[0] != "" {
// fmt.Println(strings.Split(flow, "Mb")[0])
//}
//if strings.Contains(flow, "Kb") {
// newstr := strings.ReplaceAll(flow, "Kb", "")
// fmt.Println(newstr)
//} else if strings.Contains(flow, "b") {
// newstr := strings.ReplaceAll(flow, "b", "")
// fmt.Println(newstr)
//} else if strings.Contains(flow, "Mb") {
// newstr := strings.ReplaceAll(flow, "Mb", "")
// fmt.Println(newstr)
//}
// strconv.Itoa() 将 int 转换为 string
//• strconv.Atoi() 将 string 转换为 int
//• strconv.FormatFloat() 将 float64 转换为 string
//• strconv.ParseFloat() 将 string 转换为 float64
newstr := ""
for _, v := range flow {
if v == 'K' {
//fmt.Println(newstr)
strInt, err := strconv.ParseFloat(newstr, 2)
if err != nil {
fmt.Println("error")
}
finalResult := float64(strInt)
return finalResult
//break
} else if v == 'b' {
//fmt.Println(newstr)
strInt, err := strconv.ParseFloat(newstr, 2)
if err != nil {
fmt.Println("error")
}
//fmt.Println(str_int / 1024)
//var final_result=0.0
//final_result :=
finalResult := math.Round(float64(strInt/1024)*1000) / 1000
return finalResult
//break
} else if v == 'M' {
//fmt.Println(newstr)
strInt, err := strconv.ParseFloat(newstr, 2)
if err != nil {
fmt.Println("error")
}
//fmt.Println(str_int * 1024)
//final_result := float64(str_int * 1024)
finalResult := math.Round(float64(strInt*1024)*1000) / 1000
return finalResult
}
newstr += string(v)
}
return 0
}
test.txt内容
1 192.168.10.99 => 7.52Kb 7.52Kb 7.52Kb 1.88KB
192.168.10.102 <= 1.39Kb 1.39Kb 1.39Kb 355B
2 192.168.10.99 => 2.56Kb 2.56Kb 2.56Kb 656B
10.10.2.136 <= 5.22Kb 5.22Kb 5.22Kb 1.30KB
3 192.168.10.99 => 2.47Kb 2.47Kb 2.47Kb 632B
10.10.2.138 <= 4.38Kb 4.38Kb 4.38Kb 1.10KB
4 192.168.10.99 => 3.05Kb 3.05Kb 3.05Kb 780B
192.168.10.104 <= 2.40Kb 2.40Kb 2.40Kb 615B
5 192.168.10.99 => 3.62Kb 3.62Kb 3.62Kb 926B
192.168.10.101 <= 404b 404b 404b 101B
6 192.168.10.99 => 464b 464b 464b 116B
192.168.10.103 <= 288b 288b 288b 72B