influxdb 实时监查日志系统

16 篇文章 0 订阅

linux安装influxdb

 wget https://dl.influxdata.com/influxdb/releases/influxdb-1.7.0.x86_64.rpm 


sudo yum localinstall influxdb-1.7.0.x86_64.rpm 

安装完成

#service influxdb start/stop 开启/关闭

#influx -precision rfc3339   进入influxdb  

接下来的操作和mysql基本就一样了
#显示所有的数据库
show databases
     
#删除数据库
drop database "db_name"
     
#使用数据库
use db_name
     
#显示该数据库中所有的表
show measurements

HTTP方式 操作influxdb数据库

curl -G 'http://localhost:8086/query?pretty=true' --data-urlencode "db=metrics" --data-urlencode "q=select * from test order by time desc"

 以上就是一些基本的命令操作。

下面是总结一下这次操作  总结的知识点

golang的包链接 https://github.com/influxdata/influxdb1-client/blob/master/v2/   需要go get到本地 暂时是这个以后估计还会变吧

然后这个目录下有个文件是example_test.go  打开如下:下面是一些增删改查的一些方法  直接调用即可

package client_test

import (
	"fmt"
	"math/rand"
	"os"
	"time"

	"github.com/influxdata/influxdb1-client/v2"
)

// Create a new client
func ExampleClient() {
	// NOTE: this assumes you've setup a user and have setup shell env variables,
	// namely INFLUX_USER/INFLUX_PWD. If not just omit Username/Password below.
	_, err := client.NewHTTPClient(client.HTTPConfig{
		Addr:     "http://localhost:8086",
		Username: os.Getenv("INFLUX_USER"),
		Password: os.Getenv("INFLUX_PWD"),
	})
	if err != nil {
		fmt.Println("Error creating InfluxDB Client: ", err.Error())
	}
}

// Write a point using the UDP client
func ExampleClient_uDP() {
	// Make client
	config := client.UDPConfig{Addr: "localhost:8089"}
	c, err := client.NewUDPClient(config)
	if err != nil {
		fmt.Println("Error: ", err.Error())
	}
	defer c.Close()

	// Create a new point batch
	bp, _ := client.NewBatchPoints(client.BatchPointsConfig{
		Precision: "s",
	})

	// Create a point and add to batch
	tags := map[string]string{"cpu": "cpu-total"}
	fields := map[string]interface{}{
		"idle":   10.1,
		"system": 53.3,
		"user":   46.6,
	}
	pt, err := client.NewPoint("cpu_usage", tags, fields, time.Now())
	if err != nil {
		fmt.Println("Error: ", err.Error())
	}
	bp.AddPoint(pt)

	// Write the batch
	c.Write(bp)
}

// Ping the cluster using the HTTP client
func ExampleClient_Ping() {
	// Make client
	c, err := client.NewHTTPClient(client.HTTPConfig{
		Addr: "http://localhost:8086",
	})
	if err != nil {
		fmt.Println("Error creating InfluxDB Client: ", err.Error())
	}
	defer c.Close()

	_, _, err = c.Ping(0)
	if err != nil {
		fmt.Println("Error pinging InfluxDB Cluster: ", err.Error())
	}
}

// Write a point using the HTTP client
func ExampleClient_write() {
	// Make client
	c, err := client.NewHTTPClient(client.HTTPConfig{
		Addr: "http://localhost:8086",
	})
	if err != nil {
		fmt.Println("Error creating InfluxDB Client: ", err.Error())
	}
	defer c.Close()

	// Create a new point batch
	bp, _ := client.NewBatchPoints(client.BatchPointsConfig{
		Database:  "BumbleBeeTuna",
		Precision: "s",
	})

	// Create a point and add to batch
	tags := map[string]string{"cpu": "cpu-total"}
	fields := map[string]interface{}{
		"idle":   10.1,
		"system": 53.3,
		"user":   46.6,
	}
	pt, err := client.NewPoint("cpu_usage", tags, fields, time.Now())
	if err != nil {
		fmt.Println("Error: ", err.Error())
	}
	bp.AddPoint(pt)

	// Write the batch
	c.Write(bp)
}

// Create a batch and add a point
func ExampleBatchPoints() {
	// Create a new point batch
	bp, _ := client.NewBatchPoints(client.BatchPointsConfig{
		Database:  "BumbleBeeTuna",
		Precision: "s",
	})

	// Create a point and add to batch
	tags := map[string]string{"cpu": "cpu-total"}
	fields := map[string]interface{}{
		"idle":   10.1,
		"system": 53.3,
		"user":   46.6,
	}
	pt, err := client.NewPoint("cpu_usage", tags, fields, time.Now())
	if err != nil {
		fmt.Println("Error: ", err.Error())
	}
	bp.AddPoint(pt)
}

// Using the BatchPoints setter functions
func ExampleBatchPoints_setters() {
	// Create a new point batch
	bp, _ := client.NewBatchPoints(client.BatchPointsConfig{})
	bp.SetDatabase("BumbleBeeTuna")
	bp.SetPrecision("ms")

	// Create a point and add to batch
	tags := map[string]string{"cpu": "cpu-total"}
	fields := map[string]interface{}{
		"idle":   10.1,
		"system": 53.3,
		"user":   46.6,
	}
	pt, err := client.NewPoint("cpu_usage", tags, fields, time.Now())
	if err != nil {
		fmt.Println("Error: ", err.Error())
	}
	bp.AddPoint(pt)
}

// Create a new point with a timestamp
func ExamplePoint() {
	tags := map[string]string{"cpu": "cpu-total"}
	fields := map[string]interface{}{
		"idle":   10.1,
		"system": 53.3,
		"user":   46.6,
	}
	pt, err := client.NewPoint("cpu_usage", tags, fields, time.Now())
	if err == nil {
		fmt.Println("We created a point: ", pt.String())
	}
}

// Create a new point without a timestamp
func ExamplePoint_withoutTime() {
	tags := map[string]string{"cpu": "cpu-total"}
	fields := map[string]interface{}{
		"idle":   10.1,
		"system": 53.3,
		"user":   46.6,
	}
	pt, err := client.NewPoint("cpu_usage", tags, fields)
	if err == nil {
		fmt.Println("We created a point w/o time: ", pt.String())
	}
}

// Write 1000 points
func ExampleClient_write1000() {
	sampleSize := 1000

	// Make client
	c, err := client.NewHTTPClient(client.HTTPConfig{
		Addr: "http://localhost:8086",
	})
	if err != nil {
		fmt.Println("Error creating InfluxDB Client: ", err.Error())
	}
	defer c.Close()

	rand.Seed(42)

	bp, _ := client.NewBatchPoints(client.BatchPointsConfig{
		Database:  "systemstats",
		Precision: "us",
	})

	for i := 0; i < sampleSize; i++ {
		regions := []string{"us-west1", "us-west2", "us-west3", "us-east1"}
		tags := map[string]string{
			"cpu":    "cpu-total",
			"host":   fmt.Sprintf("host%d", rand.Intn(1000)),
			"region": regions[rand.Intn(len(regions))],
		}

		idle := rand.Float64() * 100.0
		fields := map[string]interface{}{
			"idle": idle,
			"busy": 100.0 - idle,
		}

		pt, err := client.NewPoint(
			"cpu_usage",
			tags,
			fields,
			time.Now(),
		)
		if err != nil {
			println("Error:", err.Error())
			continue
		}
		bp.AddPoint(pt)
	}

	err = c.Write(bp)
	if err != nil {
		fmt.Println("Error: ", err.Error())
	}
}

// Make a Query
func ExampleClient_query() {
	// Make client
	c, err := client.NewHTTPClient(client.HTTPConfig{
		Addr: "http://localhost:8086",
	})
	if err != nil {
		fmt.Println("Error creating InfluxDB Client: ", err.Error())
	}
	defer c.Close()

	q := client.NewQuery("SELECT count(value) FROM shapes", "square_holes", "ns")
    // 第二种	
    //q := client.Query{
	//	Command:  cmd,
	//	Database: MyDB,
	//}
	if response, err := c.Query(q); err == nil && response.Error() == nil {
		fmt.Println(response.Results)
	}
}

// Create a Database
func ExampleClient_createDatabase() {
	// Make client
	c, err := client.NewHTTPClient(client.HTTPConfig{
		Addr: "http://localhost:8086",
	})
	if err != nil {
		fmt.Println("Error creating InfluxDB Client: ", err.Error())
	}
	defer c.Close()

	q := client.NewQuery("CREATE DATABASE telegraf", "", "")
	if response, err := c.Query(q); err == nil && response.Error() == nil {
		fmt.Println(response.Results)
	}
}

 报错  如果没有找到包要怎么做

influxdb1-clientv2
influxdb1-clientv2 is the current Go client API for InfluxDB 1.x. A Go client for the 2.0 API will be coming soon.

InfluxDB is an open-source distributed time series database, find more about InfluxDB at https://docs.influxdata.com/influxdb/latest

Usage
To import into your Go project, run the following command in your terminal: go get github.com/influxdata/influxdb1-client/v2 Then, in your import declaration section of your Go file, paste the following: import "github.com/influxdata/influxdb1-client/v2"

If you get the error build github.com/user/influx: cannot find module for path github.com/influxdata/influxdb1-client/v2 when trying to build: change your import to:

import(
	_ "github.com/influxdata/influxdb1-client" // this is important because of the bug in go mod
	client "github.com/influxdata/influxdb1-client/v2"
)

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在 Windows 系统中使用 InfluxDB 进行实时监控,需要遵循以下步骤: 1. 安装 Telegraf:Telegraf 是一个开源的数据采集引擎,它可以采集各种系统和应用程序的指标数据,并将其发送给 InfluxDB。您可以从 https://portal.influxdata.com/downloads/ 下载最新版本的 Telegraf,或者使用以下命令在 Windows 上安装它: ``` Invoke-WebRequest -Uri "https://dl.influxdata.com/telegraf/releases/telegraf-1.19.3_windows_amd64.zip" -OutFile "C:\temp\telegraf.zip" Expand-Archive -Path "C:\temp\telegraf.zip" -DestinationPath "C:\Program Files\Telegraf" ``` 2. 配置 Telegraf:在安装完成后,需要配置 Telegraf 来收集您感兴趣的指标。打开 C:\Program Files\Telegraf\telegraf.conf 文件,找到 `[[inputs.win_perf_counters.object]]` 部分。在其中添加以下内容: ``` [[inputs.win_perf_counters.object]] ObjectName = "Processor" Instances = ["*"] Counters = [ "% Processor Time", "% Privileged Time", "% User Time", "% Interrupt Time", "% DPC Time" ] Measurement = "win_cpu" IncludeTotal=true ``` 这将告诉 Telegraf 监控处理器的使用情况,并将其写入名为 `win_cpu` 的测量值。 3. 启动 Telegraf:保存配置文件后,启动 Telegraf 服务。您可以使用以下命令启动: ``` cd "C:\Program Files\Telegraf" .\telegraf.exe -service install .\telegraf.exe -service start ``` 4. 查看数据:现在,Telegraf 将开始收集处理器使用情况的数据,并将其写入 InfluxDB。您可以使用 InfluxDB 的查询语言(InfluxQL)或 Grafana 等可视化工具来查看这些数据。 例如,要检索最近 5 分钟内处理器使用率的平均值,可以使用以下命令: ``` SELECT mean("% Processor Time") FROM "win_cpu" WHERE time > now() - 5m GROUP BY time(10s) ``` 希望这可以帮助您实现 InfluxDB 实时监控 CPU 在 Windows 上的使用情况。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值