您好,登錄后才能下訂單哦!
本篇內(nèi)容主要講解“go根據(jù)配置文件操作influxdb并插入數(shù)據(jù)的方法”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實(shí)用性強(qiáng)。下面就讓小編來帶大家學(xué)習(xí)“go根據(jù)配置文件操作influxdb并插入數(shù)據(jù)的方法”吧!
//配置文件
ip=http://10.102.110.93:8086 name=lbw pwd=123 portOrPid=8080,8086 pidName=java,influx telNetIp=10.102.110.100:3306,www.baidu.com:80
//讀取配置文件
func InitConfig(path string) map[string]string { config := make(map[string]string) f, err := os.Open(path) defer f.Close() if err != nil { panic(err) } r := bufio.NewReader(f) for { b, _, err := r.ReadLine() if err != nil { if err == io.EOF {break } panic(err) } s := strings.TrimSpace(string(b)) index := strings.Index(s, "=") if index < 0 { continue } key := strings.TrimSpace(s[:index]) if len(key) == 0 { continue } value := strings.TrimSpace(s[index+1:]) if len(value) == 0 { continue } config[key] = value } return config }
//influxdb初始化
func InitFluxdb() client.Client { config := InitConfig("./config/test.txt") ip := config["ip"] name := config["name"] pwd := config["pwd"] conn := ConnInflux(ip,name,pwd) return conn }func ConnInflux(ip string,name string,pwd string) client.Client { cli, err := client.NewHTTPClient(client.HTTPConfig{ Addr: ip, Username: name, Password: pwd, }) if err != nil { log.Fatal(err) } return cli }func InsertDB(cli client.Client,dbName string,tableName string,tags map[string]string,fields map[string]interface{},accuracy string) { bp, err := client.NewBatchPoints(client.BatchPointsConfig{ Database: dbName, Precision: accuracy, }) if err != nil { log.Fatal(err) } pt, err := client.NewPoint(tableName, 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") }
//調(diào)用
func main() { hostname:=common.GetHostName() mem:=common.GetMem() info, _ := disk.Partitions(true) cpu:=common.GetCpuPercent() memup:=mem.UsedPercent tags := map[string]string{"name": hostname} fields := map[string]interface{}{ "cpu_used_percent": cpu, "mem_used_percent": memup, } for _,info2:=range info{ disks:=info2 info3, _ := disk.Usage(disks.Device) fields[strings.ToLower( strings.Replace(disks.Device,":","_used_percent",-1))]=info3.UsedPercent } common.InsertDB(common.InitFluxdb(),"testdb","host_info",tags,fields,"s") }
到此,相信大家對(duì)“go根據(jù)配置文件操作influxdb并插入數(shù)據(jù)的方法”有了更深的了解,不妨來實(shí)際操作一番吧!這里是億速云網(wǎng)站,更多相關(guān)內(nèi)容可以進(jìn)入相關(guān)頻道進(jìn)行查詢,關(guān)注我們,繼續(xù)學(xué)習(xí)!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。