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