Advertisement
Guest User

Untitled

a guest
Jun 20th, 2016
220
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.     <#
  2.    
  3.     .SYNOPSIS
  4.     Writes a datapoint to an influxdb server v0.9+ using the line protocol over http api.
  5.    
  6.     .DESCRIPTION
  7.     A cmdlet to allow output of data to influxdb for graphing.
  8.    
  9.     .PARAMETER influxserver
  10.     The server name or IP of the influxdb server.
  11.    
  12.     .PARAMETER db
  13.     The influxdb database to write the data to.
  14.    
  15.     .PARAMETER port
  16.     Port of the influxdb server, normally 8086
  17.    
  18.     .PARAMETER user
  19.     Username for authentication
  20.    
  21.     .PARAMETER password
  22.     Password of the user
  23.    
  24.     .PARAMETER tags
  25.     Extra tags to be applied. Should be in the format region=eu-west1.
  26.     Multiple tags can be used but must be seperated by commas eg. region=eu-west1,datacenter=london
  27.    
  28.     .PARAMETER hostname
  29.     Hostname the datapoint relates to.
  30.    
  31.     .PARAMETER metricname
  32.     Name of the metric you are writing.
  33.    
  34.     .PARAMETER datapoint
  35.     The Value of the data.
  36.    
  37.     .EXAMPLE
  38.     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
  39.    
  40.    
  41.     .NOTES
  42.     Daryl Bizsley 2016
  43.    
  44.     #>
  45.    
  46.    
  47.    
  48.     Function Write-Influxdb {
  49.         param(
  50.             $influxserver,
  51.             $db,
  52.             $port,
  53.             $user,
  54.             $password,
  55.             $tags,
  56.             $hostname = $env:computername,
  57.             $metricname,
  58.             $datapoint
  59.         )
  60.         $error.Clear()
  61.         $passwordAsSecureString = ConvertTo-SecureString "$password" -AsPlainText -Force
  62.         $cred = new-object System.Management.Automation.PSCredential ("$user", $passwordAsSecureString)
  63.         if ($tags){
  64.             try{
  65.                 $request = Invoke-WebRequest ('http://{0}:{1}/write?db={2}' -f $influxserver,$port,$db ) -Method post -body ('{0},host={1},{2} value={3}' -f $metricname,$hostname,$tags,$datapoint) -Credential $cred
  66.                 }
  67.    
  68.             catch{
  69.                    $error[0].Exception
  70.                 }
  71.         }
  72.         ELSE{
  73.             try{
  74.                 $request = Invoke-WebRequest ('http://{0}:{1}/write?db={2}' -f $influxserver,$port,$db ) -Method post -body ('{0},host={1} value={2}' -f $metricname,$hostname,$datapoint) -Credential $cred
  75.                 }
  76.    
  77.             catch{
  78.                    $error[0].Exception
  79.                 }
  80.         }
  81.    
  82.         if ($request.StatusCode -eq 204){
  83.            
  84.         }
  85.         else {
  86.             write-output $request.body
  87.         }
  88.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement