Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
rundeck_zbx_node_update.rb
#!/usr/bin/env ruby
require 'yaml'
require 'zabbixapi'
USERNAME = '${job.username}'
INTERFACE_SSH_IDX = 0
def usage()
puts 'usage: zabbix_node_update.rb <rdeck_zbx_id_file_path>'
print <<"EOS"
<rdeck_zbx_id_file_path>:
zabbix_url: required <zabbix api end-point url>
zabbix_user: required <zabbix api user>
zabbix_password: required <zabbix api user password>
resource_path: optional <rundeck resource file path> default: /tmp/resources.yml
ssh_port: optional <ssh port for ssh execute> default: 22
execute_user: optional <user for ssh execute> default: ${job.username}
private_key_path: optional <private-key path for ssh execute> default: /keys/users/${job.username}/default.pem
pathword_path: optional <passphrase for ssh execute> default: /keys/users/${job.username}/default.password
EOS
exit 0
end
rdeck_zbx_id_file_path = ARGV.shift
usage if rdeck_zbx_id_file_path.nil?
rdeck_zbx_id = YAML.load_file(rdeck_zbx_id_file_path)
url = rdeck_zbx_id['zabbix_url'] || usage
user = rdeck_zbx_id['zabbix_user'] || usage
password = rdeck_zbx_id['zabbix_password'] || usage
ssh_port = rdeck_zbx_id['rundeck_ssh_port'] || '22'
execute_user = rdeck_zbx_id['execute_user'] || '${job.username}'
resource_path = rdeck_zbx_id['rundeck_resource_path'] || '/tmp/resources.yml'
private_key_path = rdeck_zbx_id['rundeck_private_key_path'] || '/keys/users/${job.username}/default.pem'
pathword_path = rdeck_zbx_id['rundeck_password_path'] || '/keys/users/${job.username}/default.password'
zabbix = ZabbixApi.connect(
:url => url,
:user => user,
:password => password
)
zbx_nodes = zabbix.query(
:method => 'host.get',
:params => {
:output => 'extend',
:selectInterfaces => ['ip']
}
)
# get zabbix inventory sample
#zbx_nodes = zabbix.query(
# :method => 'host.get',
# :params => {
# :output => 'extend',
# :selectInterfaces => ['ip'],
# :selectInventory => ['type', 'type_full', 'name', 'alias', 'os', 'os_full', 'os_short']
# }
#)
rdeck_resource = {}
zbx_nodes.each do |node|
ip = node['interfaces'][INTERFACE_SSH_IDX]['ip']
rdeck_resource[node['host']] = {
'hostname' => "#{ip}:#{ssh_port}",
'username' => execute_user,
'ssh-key-storage-path' => private_key_path,
'ssh-key-passphrase-storage-path' => pathword_path
}
end
File.open(resource_path, 'w') { |f| f.write rdeck_resource.to_yaml }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment