<commit_before># Author:: <NAME> (<<EMAIL>>)
# Cookbook Name:: zabbix
# Recipe:: agent_registration
#
# Apache 2.0
#

chef_gem "zabbixapi" do
  action :install
  version "~> 0.5.8"
end

require 'zabbixapi'

unless Chef::Config[:solo]
  zabbix_server = search(:node, "recipes:zabbix\\:\\:server").first
elsif node['zabbix']['server']['ipaddress']
  zabbix_server = node['zabbix']['server']['ipaddress']
else
  Chef::Log.warn("This recipe uses search. Chef Solo does not support search.")
  Chef::Log.warn("You don't set node['zabbix']['server']['ipaddress']. Recipe fail")
  return
end

if port_open?(zabbix_server['zabbix']['web']['fqdn'], 80)
  zbx = ZabbixApi.connect(
    :url => "http://#{zabbix_server['zabbix']['web']['fqdn']}/api_jsonrpc.php",
    :user => zabbix_server['zabbix']['web']['login'],
    :password => <PASSWORD>_server['zabbix']['web']['password']
  )

  groups_id = []
  node['zabbix']['agent']['groups'].each do |group|
    ruby_block "Create group #{group} on Zabbix server" do
      block do
        zbx.hostgroups.create(
          :host => group
        )
      end
      not_if { zbx.hostgroups.get_id(:name => group) }
    end
    groups_id += [ :groupid => zbx.hostgroups.get_id(:name => group) ]
  end

  ruby_block "Create or update host on Zabbix server" do
    block do
      zbx.hosts.create_or_update(
        :host => node['zabbix']['agent']['hostname'],
        :interfaces => [
          {
            :dns => node['zabbix']['agent']['hostname'],
            :useip => 0
          }
        ],
        :groups => groups_id
      )
    end
  end
end<commit_msg>Fix registration with last library version<commit_after># Author:: <NAME> (<<EMAIL>>)
# Cookbook Name:: zabbix
# Recipe:: agent_registration
#
# Apache 2.0
#

chef_gem "zabbixapi" do
  action :install
  version "~> 0.5.9"
end

require 'zabbixapi'

unless Chef::Config[:solo]
  zabbix_server = search(:node, "recipes:zabbix\\:\\:server").first
elsif node['zabbix']['server']['ipaddress']
  zabbix_server = node['zabbix']['server']['ipaddress']
else
  Chef::Log.warn("This recipe uses search. Chef Solo does not support search.")
  Chef::Log.warn("You don't set node['zabbix']['server']['ipaddress']. Recipe fail")
  return
end

if port_open?(zabbix_server['zabbix']['web']['fqdn'], 80)
  zbx = ZabbixApi.connect(
    :url => "http://#{zabbix_server['zabbix']['web']['fqdn']}/api_jsonrpc.php",
    :user => zabbix_server['zabbix']['web']['login'],
    :password => zabbix_server['zabbix']['web']['password']
  )

  groups_id = []
  node['zabbix']['agent']['groups'].each do |group|
    ruby_block "Create group #{group} on Zabbix server" do
      block do
        zbx.hostgroups.create(
          :host => group
        )
      end
      not_if { zbx.hostgroups.get_id(:name => group) }
    end
    groups_id += [ :groupid => zbx.hostgroups.get_id(:name => group) ]
  end

  ruby_block "Create or update host on Zabbix server" do
    block do
      zbx.hosts.create_or_update(
        :host => node['zabbix']['agent']['hostname'],
        :interfaces => [
          {
            :type => 1,
            :main => 1,
            :ip => '',
            :port => 10050,
            :dns => node['zabbix']['agent']['hostname'],
            :useip => 0
          }
        ],
        :groups => groups_id
      )
    end
  end
end
{'fraction_non_alphanumeric': 0.18181818181818182, 'fraction_numerical': 0.006923540036122818, 'mean_word_length': 3.1228287841191067, 'pattern_counts': {'":': 0, '<': 10, '<?xml version=': 0, '>': 40, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1183193', 'n_tokens_mistral': 1187, 'n_tokens_neox': 1144, 'n_words': 233}