-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathWrite-InfluxDBlineprotocol.ps1
95 lines (73 loc) · 2.3 KB
/
Write-InfluxDBlineprotocol.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
<#
.SYNOPSIS
Writes a datapoint to an influxdb server v0.9+ using the line protocol over http api.
.DESCRIPTION
A cmdlet to allow output of data to influxdb for graphing.
.PARAMETER influxserver
The server name or IP of the influxdb server.
.PARAMETER db
The influxdb database to write the data to.
.PARAMETER port
Port of the influxdb server, normally 8086
.PARAMETER user
Username for authentication
.PARAMETER password
Password of the user
.PARAMETER tags
Extra tags to be applied. Should be in the format region=eu-west1.
Multiple tags can be used but must be seperated by commas eg. region=eu-west1,datacenter=london
.PARAMETER hostname
Hostname the datapoint relates to.
.PARAMETER metricname
Name of the metric you are writing.
.PARAMETER datapoint
The Value of the data.
.EXAMPLE
Write-influxdb -influxserver influxdb.local -db default -port 8086 -user admin -password admin -tags "region=eu-west1,datacenter=london" -hostname server01 -metricname load_5 -value 3
.NOTES
Daryl Bizsley 2016
#>
Function Write-Influxdb {
param(
$influxserver,
$db,
$port,
$user,
$password,
$tags,
$hostname = $env:computername,
$metricname,
$datapoint,
[switch]$https
)
$error.Clear()
$passwordAsSecureString = ConvertTo-SecureString "$password" -AsPlainText -Force
$cred = new-object System.Management.Automation.PSCredential ("$user", $passwordAsSecureString)
if ($https) {
$https = "https"
}
Else {
$https= "http"
}
if ($tags){
try{
$request = Invoke-WebRequest -usebasicparsing ('{0}://{1}:{2}/write?db={3}' -f $https,$influxserver,$port,$db ) -Method post -body ('{0},host={1},{2} value={3}' -f $metricname,$hostname,$tags,$datapoint) -Credential $cred
}
catch{
$error[0].Exception
}
}
ELSE{
try{
$request = Invoke-WebRequest -usebasicparsing ('{0}://{1}:{2}/write?db={3}' -f $https,$influxserver,$port,$db ) -Method post -body ('{0},host={1} value={2}' -f $metricname,$hostname,$datapoint) -Credential $cred
}
catch{
$error[0].Exception
}
}
if ($request.StatusCode -eq 204){
}
else {
write-output $request.body
}
}