统计granfa监控面板配置的监控是否齐全,以防缺漏
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
"reflect"
"regexp"
"strings"
"github.com/tealeg/xlsx"
)
// func TestTealeg() {
// excel := xlsx.NewFile()
// // 新建一个sheet
// sheet, err := excel.AddSheet("Sheet1")
// if err != nil {
// // t.Fatal(err)
// fmt.Print("jxl err1", err)
// return
// }
// // 创建首行
// headerRow := sheet.AddRow()
// // 设置行高
// headerRow.SetHeightCM(0.5)
// // // 填充行中的单元格
// // headerRow.AddCell().Value = "姓名"
// // headerRow.AddCell().Value = "年龄"
// valList := [][]string{
// {"张三", "13"},
// {"李四", "14"},
// {"王五", "15"},
// }
// for _, line := range valList {
// row := sheet.AddRow()
// row.SetHeightCM(0.5)
// for _, v := range line {
// row.AddCell().Value = v
// }
// }
// // 持久化到磁盘
// if err := excel.Save("username.xlsx"); err != nil {
// // t.Fatal(err)
// fmt.Print("jxl err", err)
// }
// }
// 如果`json:"code"`去掉.会以字段名称为解析内容
type Result struct {
Jsonrpc int `json:"jsonrpc"`
Result string `json:"result"`
Id string `json:"id"`
}
func getAuth(cookie string) string {
client := &http.Client{}
var data = strings.NewReader(`{"jsonrpc":"2.0","method":"user.login","params":{"user":"admin","password":"hik7ujm"},"id":1}`)
req, err := http.NewRequest("POST", "http://*****m/api/datasources/proxy/2", data)
if err != nil {
log.Fatal(err)
}
req.Header.Set("Connection", "keep-alive")
req.Header.Set("Accept", "application/json, text/plain, */*")
req.Header.Set("X-Grafana-Org-Id", "1")
req.Header.Set("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.77 Safari/537.36")
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Origin", "http://*****m")
req.Header.Set("Referer", "http://*****m/d/sCb3BAP7k/*****?orgId=1&from=1653918700943&to=1655128300943")
req.Header.Set("Accept-Language", "zh-CN,zh;q=0.9")
req.Header.Set("Cookie", cookie)
resp, err := client.Do(req)
if err != nil {
log.Fatal(err)
return err.Error()
}
defer resp.Body.Close()
bodyText, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Fatal(err)
return err.Error()
}
var res2 Result
_ = json.Unmarshal(bodyText, &res2)
return res2.Result
}
type Group struct {
Groupid string `json:"groupid"`
Name string `json:"name"`
// groupid:800800000000733 name:addvalue_fdfs_tracker
}
type GroupResult struct {
Jsonrpc int `json:"jsonrpc"`
Result []Group `json:"result"`
Id string `json:"id"`
}
func getHostGroup(cookie, auth string) ([]Group, string) {
client := &http.Client{}
var data = strings.NewReader(`{"jsonrpc":"2.0","method":"hostgroup.get","params":{"output":["name"],"sortfield":"name","real_hosts":true},"id":1,"auth":"` + auth + `"}`)
req, err := http.NewRequest("POST", "http://*****m/api/datasources/proxy/2", data)
if err != nil {
log.Fatal(err)
}
req.Header.Set("Connection", "keep-alive")
req.Header.Set("Accept", "application/json, text/plain, */*")
req.Header.Set("X-Grafana-Org-Id", "1")
req.Header.Set("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.77 Safari/537.36")
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Origin", "http://*****m")
req.Header.Set("Referer", "http://*****/from=1653918700943&to=1655128300943")
req.Header.Set("Accept-Language", "zh-CN,zh;q=0.9")
req.Header.Set("Cookie", cookie)
resp, err := client.Do(req)
if err != nil {
log.Fatal(err)
}
defer resp.Body.Close()
bodyText, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Fatal(err)
}
// map2 := make(map[string]interface{})
var groupResult GroupResult
// var groupList []Group
_ = json.Unmarshal(bodyText, &groupResult)
groupList := groupResult.Result
var result []Group
var groupIDList []string
// var groupIDListStr = '['
for _, group := range groupList {
// refValue := reflect.ValueOf(value)
// a(x*)b(y|z)c
var pattern = regexp.MustCompile(`(k|K)afka`)
var pattern2 = regexp.MustCompile(`zookeeper`)
if pattern.MatchString(group.Name) && !pattern2.MatchString(group.Name) {
result = append(result, group)
groupIDList = append(groupIDList, group.Groupid)
}
}
// var str = fmt.Sprintf("%s")
groupIDListStr := "[\"" + strings.Join(groupIDList, "\",\"") + "\"]"
// data, _ := json.Marshal(groupIDList)
// fmt.Println(c)
// fmt.Print(groupIDListStr)
// fmt.Printf(string(groupIDList))
return result, groupIDListStr
}
type Host struct {
Hostid string `json:"hostid"`
Name string `json:"name"`
// groupid:800800000000733 name:addvalue_fdfs_tracker
}
type HostResult struct {
Jsonrpc int `json:"jsonrpc"`
Result []Host `json:"result"`
Id string `json:"id"`
}
func getHostIPList(cookie, groupids string) ([]Host, string) {
client := &http.Client{}
jsonData := `{"jsonrpc":"2.0","method":"host.get","params":{"output":["name","host"],"sortfield":"name","groupids":` + groupids + `},"id":1,"auth":"eab92c6224cd9a7a04db7fd9f048ffd6"}`
// fmt.Print(jsonData)
// return
var data = strings.NewReader(jsonData)
req, err := http.NewRequest("POST", "http://*****m/api/datasources/proxy/2", data)
if err != nil {
log.Fatal(err)
}
req.Header.Set("Connection", "keep-alive")
req.Header.Set("Accept", "application/json, text/plain, */*")
req.Header.Set("X-Grafana-Org-Id", "1")
req.Header.Set("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.77 Safari/537.36")
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Origin", "http://*****m")
req.Header.Set("Referer", "http://*****m/d/sCb3BAP7k/*****?orgId=1&from=1653918700943&to=1655128300943")
req.Header.Set("Accept-Language", "zh-CN,zh;q=0.9")
req.Header.Set("Cookie", cookie)
resp, err := client.Do(req)
if err != nil {
log.Fatal(err)
}
defer resp.Body.Close()
bodyText, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Fatal(err)
}
var hostResult HostResult
// var groupList []Group
_ = json.Unmarshal(bodyText, &hostResult)
hostList := hostResult.Result
// var result []Host
var hostIDList []string
// var groupIDListStr = '['
for _, host := range hostList {
// result = append(result, host)
hostIDList = append(hostIDList, host.Hostid)
}
// var str = fmt.Sprintf("%s")
hostIDListStr := "[\"" + strings.Join(hostIDList, "\",\"") + "\"]"
// fmt.Printf("%s\n", bodyText)
return hostList, hostIDListStr
}
// {"itemid":"800800002605042",
// "name":"zk_znode_count",
// "key_":"proc.num[,,,zk_znode_count]",
// "value_type":"3",
// "hostid":"800800000021893",
// "status":"0","state":"0",
// "hosts":[{"hostid":"800800000021893","name":"pop_kafka_zookeeper 10.209.38.15"}]},
type Item struct {
Itemid string `json:"itemid"`
Name string `json:"name"`
Key_ string `json:"key_"`
Hostid string `json:"hostid"`
Hosts []Host `json:"hosts"`
// groupid:800800000000733 name:addvalue_fdfs_tracker
}
type ItemResult struct {
Jsonrpc int `json:"jsonrpc"`
Result []Item `json:"result"`
Id string `json:"id"`
}
func getItem(cookie string, hostStr string) ([]Item, string) {
client := &http.Client{}
var data = strings.NewReader(`{"jsonrpc":"2.0","method":"item.get","params":{"output":
["name","host","key_","value_type","hostid","status","state"],"sortfield":"name","webitems":true,
"filter":{"value_type":[0,3]},"selectHosts":["hostid","name"],"hostids":` + hostStr + `},"id":1,"auth":"eab92c6224cd9a7a04db7fd9f048ffd6"}`)
req, err := http.NewRequest("POST", "http://*****m/api/datasources/proxy/2", data)
if err != nil {
log.Fatal(err)
}
req.Header.Set("Connection", "keep-alive")
req.Header.Set("Accept", "application/json, text/plain, */*")
req.Header.Set("X-Grafana-Org-Id", "1")
req.Header.Set("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.77 Safari/537.36")
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Origin", "http://*****m")
req.Header.Set("Referer", "http://*****m/d/sCb3BAP7k/*****?orgId=1&from=1653918700943&to=1655128300943")
req.Header.Set("Accept-Language", "zh-CN,zh;q=0.9")
req.Header.Set("Cookie", cookie)
resp, err := client.Do(req)
if err != nil {
log.Fatal(err)
}
defer resp.Body.Close()
bodyText, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Fatal(err)
}
var itemResult ItemResult
// var groupList []Group
_ = json.Unmarshal(bodyText, &itemResult)
itemList := itemResult.Result
var result []Item
var itemIDList []string
// var groupIDListStr = '['
ItemMap := make(map[string][]Item)
excel := xlsx.NewFile()
// 新建一个sheet
sheet, err := excel.AddSheet("Sheet1")
if err != nil {
log.Fatal(err)
}
// 创建首行
headerRow := sheet.AddRow()
// 设置行高
headerRow.SetHeightCM(0.5)
row := sheet.AddRow()
row.SetHeightCM(0.5)
row.AddCell().Value = "itemId"
row.AddCell().Value = "Name"
row.AddCell().Value = "Key_"
row.AddCell().Value = "Hostid"
row.AddCell().Value = "HostName"
row.AddCell().Value = "HostIp"
// 持久化到磁盘
for _, host := range itemList {
// result = append(result, host)
// vfs.fs.size[/data,pfree]
// if host.Key_ == "system.cpu.util[,iowait]" || host.Key_ == "vfs.fs.size[/,free]" || host.Key_ == "vfs.fs.size[/data,free]" || host.Key_ == "Free disk space on /data (percentage)" || host.Key_ == "system.cpu.util[,idle]" || host.Name == "/ free percentage" || host.Name == "/data free percentage"|| host.Name == "/data free percentage" || host.Name == "Agent ping" || host.Name == "docker cpu usage" || host.Name == "docker_mem" || host.Name == "Free disk space on /data (percentage)" || host.Name == "Free disk space on $1" || host.Name == "Free disk space on / (percentage)" || host.Name == "CPU $2 time" || host.Name == "mem_idle" || host.Name == "Total memory"|| host.Name == "Available memory" {
row := sheet.AddRow()
row.SetHeightCM(0.5)
t := reflect.TypeOf(host) // 注意,obj不能为指针类型,否则会报:panic recovered: reflect: NumField of non-struct type
v := reflect.ValueOf(host)
for k := 0; k < t.NumField(); k++ {
// fmt.Print(t.Field(k).Name)
if t.Field(k).Name == "Hosts" {
hostItems := v.Field(k).Interface().([]Host)
var hostItem Host
if len(hostItems) > 0 {
hostItem = hostItems[0]
}
arr := strings.Split(hostItem.Name, " ")
if len((arr)) == 1 {
row.AddCell().Value = ""
row.AddCell().Value = arr[0]
} else if len((arr)) == 2 {
row.AddCell().Value = arr[0]
row.AddCell().Value = arr[1]
}
// row.AddCell().Value = hostItem.Name
} else {
str := fmt.Sprintf("%s", v.Field(k).Interface())
// fmt.Printf("%s -- %v \n", t.Field(k).Tag, v.Field(k).Interface())
row.AddCell().Value = str
}
}
if ItemMap[host.Key_] == nil {
ItemMap[host.Key_] = make([]Item, 0)
}
itemIDList = append(itemIDList, host.Itemid)
ItemMap[host.Key_] = append(ItemMap[host.Key_], host)
result = append(result, host)
// }
}
if err := excel.Save("item.xlsx"); err != nil {
fmt.Print(" err", err)
}
// var str = fmt.Sprintf("%s", ItemMap)
itemIDListStr := "[\"" + strings.Join(itemIDList, "\",\"") + "\"]"
return itemList, itemIDListStr
}
type Metric struct {
Itemid string `json:"itemid"`
Clock string `json:"clock"`
Num string `json:"num"`
Value_min string `json:"value_min"`
Value_avg string `json:"value_avg"`
Value_max string `json:"value_max"`
}
type metricResult struct {
Jsonrpc int `json:"jsonrpc"`
Result []Metric `json:"result"`
Id string `json:"id"`
}
func getMetric(cookie string, itemIdStr string) {
client := &http.Client{}
var data = strings.NewReader(`{"jsonrpc":"2.0","method":"trend.get","params":{"output":"extend","trend":"0","itemids":` + itemIdStr + `,"sortfield":"clock","sortorder":"ASC","time_from":1653918701,"time_till":1655128301},"id":1,"auth":"942ef6e204a06f885f0aa1c0bac968b0"}`)
req, err := http.NewRequest("POST", "http://*****m/api/datasources/proxy/2", data)
if err != nil {
log.Fatal(err)
}
req.Header.Set("Connection", "keep-alive")
req.Header.Set("Accept", "application/json, text/plain, */*")
req.Header.Set("X-Grafana-Org-Id", "1")
req.Header.Set("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.77 Safari/537.36")
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Origin", "http://*****m")
req.Header.Set("Referer", "http://*****m/d/sCb3BAP7k/*****?orgId=1&refresh=1m&from=1653918700943&to=1655128300943")
req.Header.Set("Accept-Language", "zh-CN,zh;q=0.9")
req.Header.Set("Cookie", cookie)
resp, err := client.Do(req)
if err != nil {
log.Fatal(err)
}
defer resp.Body.Close()
bodyText, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Fatal(err)
}
var metricResult metricResult
// var groupList []Group
_ = json.Unmarshal(bodyText, &metricResult)
var str = fmt.Sprintf("%s", metricResult.Result)
//_ = ioutil.WriteFile("./output_metric.txt", []byte(string(str)), 0666)
// fmt.Printf("%s\n", bodyText)
}
func getHistory() {
client := &http.Client{}
var data = strings.NewReader(`{"jsonrpc":"2.0","method":"history.get","params":{"output":"extend","history":"0","itemids":
["800800001433856"],"sortfield":"clock","sortorder":"ASC","time_from":1655188930,"time_till":1655189230},
"id":1,"auth":"942ef6e204a06f885f0aa1c0bac968b0"}`)
req, err := http.NewRequest("POST", "http://*****m/api/datasources/proxy/2", data)
if err != nil {
log.Fatal(err)
}
req.Header.Set("Connection", "keep-alive")
req.Header.Set("Accept", "application/json, text/plain, */*")
req.Header.Set("X-Grafana-Org-Id", "1")
req.Header.Set("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.77 Safari/537.36")
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Origin", "http://*****m")
req.Header.Set("Referer", "http://*****m/d/sCb3BAP7k/*****?orgId=1&refresh=1m&from=now-5m&to=now")
req.Header.Set("Accept-Language", "zh-CN,zh;q=0.9")
req.Header.Set("Cookie", "grafana_session=7ccf32cdc5fa4870908335f39f962421")
resp, err := client.Do(req)
if err != nil {
log.Fatal(err)
}
defer resp.Body.Close()
bodyText, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Fatal(err)
}
fmt.Printf("%s\n", bodyText)
}
func main() {
//从浏览器查看session
var session = "grafana_session=16cfd36979*****6b"
auth := getAuth(session)
_, groupStr := getHostGroup(session, auth)
_, hostListStr := getHostIPList(session, groupStr)
// _, itemIDListStr := getItem(session, hostListStr)
_, _ = getItem(session, hostListStr)
// getMetric(session, itemIDListStr)
// TestTealeg()
// fmt.Printf("%s\n", string(auth))
}
// 监控指标写入了item.xlsx文件中
item.xlsx 文件内容如下:
Name | Key_ | Hostid | HostName | HostIp |
---|---|---|---|---|
/ free percentage | vfs.fs.size[/,pfree] | 800800000019431 | host2 | 10.97.×.× |