VMware Cloud Community
AmoghMaheshvip
Enthusiast
Enthusiast
Jump to solution

Write Points from file to InfluxDB using Invoke-RestMethod

With respect to Writing data with the HTTP API | InfluxData Documentation we can use curl to post multiple points from file to insert to influxdb

[ curl -i -XPOST 'http://localhost:8086/write?db=mydb' --data-binary @cpu_data.txt ]

As I am using PowerCLI script to post points to influxdb using

[ Invoke-RestMethod -Uri 'http://localhost:8086/write?&db=DBName' -Method Post -Body "measurement-name,tag-name=tag-value value-name=value" ]

I want know the similar syntax in Invoke-RestMethod to post from file.

Tags (1)
0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

Afaik that is not possible.

I loop through the file like this

Get-Content -Path .\cpu_data.txt | %{

    Invoke-WebRequest 'http://localhost:8086/write?db=DBName' -Method POST -Body $_

}


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

View solution in original post

0 Kudos
6 Replies
LucD
Leadership
Leadership
Jump to solution

Afaik that is not possible.

I loop through the file like this

Get-Content -Path .\cpu_data.txt | %{

    Invoke-WebRequest 'http://localhost:8086/write?db=DBName' -Method POST -Body $_

}


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
AmoghMaheshvip
Enthusiast
Enthusiast
Jump to solution

But, This will take lot of time to insert if the file has 3000+ lines.

Is there any alternative way to post all lines at one go?

0 Kudos
LucD
Leadership
Leadership
Jump to solution

I know, I suffer from the same :smileygrin:

As an alternative, when using PowerShell Core/PowerCLI Core, you can run your script on a Linux platform.

And then call curl from your script.

Or us a curl clone for Windows (but I have no experience with any of those).


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
AmoghMaheshvip
Enthusiast
Enthusiast
Jump to solution

I am trying to do work around process

$postdata = Get-Content -Path ./file.text

>$postdata

[ testtime value=2.2 1457628961\ntesttime value=2.3 1457629321 ]

Invoke-RestMethod -Uri "http://localhost:8086/write?&db=vipl" -Method Post -Body "$postdata"

or

Invoke-RestMethod -Uri "http://localhost:8086/write?&db=vipl" -Method Post -Body "testtime value=2.2 1457628961\ntesttime value=2.3 1457629321"

Either of way it is inserting only a single point into influxdb not two

0 Kudos
AmoghMaheshvip
Enthusiast
Enthusiast
Jump to solution

I have tried to find some alternative way, but not fully successful.

1. I am able to use Invoke-RestMethod to insert all the points from powercli window using:

     Invoke-RestMethod -Uri "http://localhost:8086/write?&db=vipl" -Method Post -Body "testtime value=2.2 1457628961`ntesttime value=2.3 1457629321"

    

2. But, if the same text [ testtime value=2.2 1457628961`ntesttime value=2.3 1457629321 ], I write to a file and later read it using Get-Content like,

     $post = Get-Content -Path post.txt

     Invoke-RestMethod -Uri "http://localhost:8086/write?&db=vipl" -Method Post -Body "$post"

it shows me an error.

3. Can I know how can I replace EOL with `n

     the post.txt contains

[ testtime value=2.2 1457628961

  testtime value=2.3 1457629321 ]

how can I make it in this format [ testtime value=2.2 1457628961`ntesttime value=2.3 1457629321 ]

I have tried this {$post = Get-Content -Path post.txt -replace "`n`r ","``n" }

but was unsuccessful

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Can you try like this?

(Get-Content -Path post.txt) | %{

   $_ -replace "`r`n",'`n'

  } | Set-Content -Path new-post.txt


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos