Class: DatalayerLight::InfluxDB
- Inherits:
-
Object
- Object
- DatalayerLight::InfluxDB
- Defined in:
- lib/thm/datalayerlight.rb
Overview
Metrics / Measurements Engine InfluxDB RestAPI
Instance Attribute Summary collapse
-
#dbhost ⇒ Object
Returns the value of attribute dbhost.
-
#dbpass ⇒ Object
Returns the value of attribute dbpass.
-
#dbport ⇒ Object
Returns the value of attribute dbport.
-
#dburl ⇒ Object
Returns the value of attribute dburl.
-
#dbuser ⇒ Object
Returns the value of attribute dbuser.
Instance Method Summary collapse
- #apiget(sql) ⇒ Object
- #apipost(data) ⇒ Object
-
#initialize ⇒ InfluxDB
constructor
A new instance of InfluxDB.
- #query(sql, mode = "r") ⇒ Object
Constructor Details
#initialize ⇒ InfluxDB
Returns a new instance of InfluxDB.
154 155 156 157 158 159 160 |
# File 'lib/thm/datalayerlight.rb', line 154 def initialize @dbhost = "127.0.0.1" @dbuser = "threatmonitor" @dbpass = "dk3rbi9l" @dbport = 8086 @dbname = "threatmonitor" end |
Instance Attribute Details
#dbhost ⇒ Object
Returns the value of attribute dbhost.
152 153 154 |
# File 'lib/thm/datalayerlight.rb', line 152 def dbhost @dbhost end |
#dbpass ⇒ Object
Returns the value of attribute dbpass.
152 153 154 |
# File 'lib/thm/datalayerlight.rb', line 152 def dbpass @dbpass end |
#dbport ⇒ Object
Returns the value of attribute dbport.
152 153 154 |
# File 'lib/thm/datalayerlight.rb', line 152 def dbport @dbport end |
#dburl ⇒ Object
Returns the value of attribute dburl.
152 153 154 |
# File 'lib/thm/datalayerlight.rb', line 152 def dburl @dburl end |
#dbuser ⇒ Object
Returns the value of attribute dbuser.
152 153 154 |
# File 'lib/thm/datalayerlight.rb', line 152 def dbuser @dbuser end |
Instance Method Details
#apiget(sql) ⇒ Object
162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 |
# File 'lib/thm/datalayerlight.rb', line 162 def apiget(sql) @dburl = "http://#{@dbhost}:#{@dbport}" sqlunicode = URI.encode(sql) puts "InfluxDB SQL URL: #{@dburl}/query?db=#{@dbname}&q=#{sqlunicode}" uri = URI.parse("#{@dburl}/query?db=#{@dbname}&q=#{sqlunicode}") puts "Request URI: #{uri}" http = Net::HTTP.new(uri.host, uri.port) begin response = http.request(Net::HTTP::Get.new(uri.request_uri)) begin j = JSON.parse(response.body) rescue JSON::ParserError puts "Could not read JSON data" end rescue puts "Error retrieving data" end end |
#apipost(data) ⇒ Object
181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 |
# File 'lib/thm/datalayerlight.rb', line 181 def apipost(data) @dburl = "http://#{@dbhost}:#{@dbport}" #puts "InfluxDB SQL URL: #{@dburl}/query?db=#{@dbname}" uri = URI.parse("#{@dburl}/write?db=#{@dbname}") #puts "Request URI: #{uri}" http = Net::HTTP.new(uri.host, uri.port) request = Net::HTTP::Post.new(uri.request_uri) request.set_content_type("application/x-www-form-urlencoded") begin request.body = data unless data.empty? response = http.request(request) if response.code == 204 # Good response puts "OK" elsif response.code == 200 or response.code == 400 # 200 can be an error in some cases !! puts "Error code #{response.code}" end rescue puts "Error posting data" end end |
#query(sql, mode = "r") ⇒ Object
202 203 204 205 206 207 208 |
# File 'lib/thm/datalayerlight.rb', line 202 def query(sql, mode="r") if mode == "r" apiget("#{sql}") elsif mode == "w" apipost(sql) end end |