Ruby [RUBY 新手] 有关面向对象的疑问

ciscolive · 2021年05月11日 · 最后由 dawei 回复于 2021年05月11日 · 348 次阅读

各位前辈,最近在调试 ZABBIX 批量添加主机的脚本,有幸在 RUBYGEMS 发现一个模块部分功能可以直接调用。但没有集成批量添加的方法,于是想自行添加。追加的函数如下:

方法定义在 zabbixapi/classes/hosts.rb下面,需要调用到其他模块的hostgroups.get_id(属于zabbixapi/classes/hostgroups.rb)、templates.get_id_by_name(zabbixapi/classes/templates.rb)方法。直接调用下面方法直接报错,刚学RUBY不久,麻烦指教下,谢谢!

### zabbixapi/classes/hosts.rb class ZabbixApi class Hosts < Basic # The method name used for interacting with Hosts via Zabbix API # # @return [String] def method_name 'host' end

# The id field name used for identifying specific Host objects via Zabbix API # # @return [String] def identify 'host' end

def create_or_update_by_csv(path) CSV.foreach(path) do |host| create_or_update( host: host[0], interfaces: [ { type: 2, main: 1, useip: 1, ip: host[1], dns: "", port: 161, details: { version: 2, community: "{$SNMP_COMMUNITY}" } } ], groups: [ groupid: hostgroups.get_id(name: host[2]) ], templates: [ { templateid: templates.get_id_by_name(name: host[3]) } ], inventory_mode: 1, macros: [ { macro: "{$SNMP_COMMUNITY}", value: host[:community_string] } ] ) end end end

zabbixapi/classes/hostgroups.rb

class ZabbixApi class HostGroups < Basic # The method name used for interacting with HostGroups via Zabbix API # # @return [String] def method_name 'hostgroup' end

# The id field name used for identifying specific HostGroup objects via Zabbix API # # @return [String] def identify 'name' end

# The key field name used for HostGroup objects via Zabbix API # # @return [String] def key 'groupid' end end end

项目地址

https://github.com/express42/zabbixapi

需要 登录 后方可回复, 如果你还没有账号请 注册新账号