Reputation: 172
I'm new to ruby and I'm trying to re-structure my script which adds some servers to zabbix monitor etc.The issue that I'm facing is below:
zbx = ZabbixApi.connect(
:url => 'http://zabbixserver.net/zabbix/api_jsonrpc.php',
:user => 'admin',
:password => 'admin'
)
def createtemplate
zbx.templates.create(
:host => "RealDoc MS Template",
:groups => [:groupid => zbx.hostgroups.get_id(:name => "RealDoc")]
) ..../will create Items, graphs etc...
end
if templateid.empty?
createtemplate
else
puts "Template Exists"
end
When is accessing the createtemplate method it's throwing the following error: undefined local variable or method `zbx' for main:Object (NameError)
Upvotes: 0
Views: 65
Reputation: 172
It's working with adding the variable to our method def createtemplate(zbx) , and the same thing when you are calling the methood , you will call it with zbx variable.
Upvotes: 0
Reputation: 20232
well zbx
isn't in scope, as it isn't a global. you have a couple options.
either pass it into the method
def createtemplate(zbx)
zbx.templates.create(
:host => "RealDoc MS Template",
:groups => [:groupid => zbx.hostgroups.get_id(:name => "RealDoc")]
) ..../will create Items, graphs etc...
end
if templateid.empty?
createtemplate zbx
else
puts "Template Exists"
en
or you can make it global with a $
.
$zbx = ZabbixApi.connect(
:url => 'http://zabbixserver.net/zabbix/api_jsonrpc.php',
:user => 'admin',
:password => 'admin'
)
def createtemplate
$zbx.templates.create(
:host => "RealDoc MS Template",
:groups => [:groupid => zbx.hostgroups.get_id(:name => "RealDoc")]
) ..../will create Items, graphs etc...
end
if templateid.empty?
createtemplate
else
puts "Template Exists"
end
I would do the first option, as global variables should be used sparingly, but in such a short script it probably doesn't matter that much..
Upvotes: 3