epcim
12/1/2015 - 7:58 PM

zabbix api connection

zabbix api connection



#curl -i -X POST -H 'Content-Type: application/json' -d '{"params": {"password": "mypassword", "user": "myuser"}, "jsonrpc": "2.0", "method": "user.authenticate"}' http://0.0.0.0/zabbix/api_jsonrpc.php

require 'net/http'
require 'json'

def post
  host = '0.0.0.0'
  port = '80'
  path = '/zabbix/api_jsonrpc.php'

  data = {
  "jsonrpc" => "2.0",
  "method"  => "user.authenticate",
  "params" => {
    "user" => "myuser",
    "password" => "mypassword"
  }
  }.to_json

  req = Net::HTTP::Post.new(path, initheader = {'Content-Type' =>'application/json'})
  req.body = data
  response = Net::HTTP.new(host, port).start {|http| http.request(req) }
  #puts "Response #{response.code} #{response.message}: #{response.body}"
  if response.code == '200'
    return response['set-cookie']
  end
end

thepost = post
puts thepost.split(", ")[0].split("=")[1]