Hello all,
I have written a utility in Perl that I use to deploy the Zabbix Agent to servers in the environment I work in. This utility may or may not be useful to you, and is customized quite a lot to fit my specific environment, but maybe you can customize it for use with yours.
This utility depends on Perl and two Perl modules which can be acquired with CPAN. The modules are JSON::RPC::Client and Data:
umper. I have also replaced key variables with placeholders, so please read through the script and add your own variable values in.
This utility will ssh into a list of servers the user provides and install and configure Zabbix, and then send said data to the Zabbix API so that the hosts appear in the frontend. I hope this is helpful to you all. Please let me know if you have any questions. Script is pasted below.
I have written a utility in Perl that I use to deploy the Zabbix Agent to servers in the environment I work in. This utility may or may not be useful to you, and is customized quite a lot to fit my specific environment, but maybe you can customize it for use with yours.
This utility depends on Perl and two Perl modules which can be acquired with CPAN. The modules are JSON::RPC::Client and Data:
umper. I have also replaced key variables with placeholders, so please read through the script and add your own variable values in.This utility will ssh into a list of servers the user provides and install and configure Zabbix, and then send said data to the Zabbix API so that the hosts appear in the frontend. I hope this is helpful to you all. Please let me know if you have any questions. Script is pasted below.
Code:
#!/usr/bin/perl
# Simon Watson 2014-05-08
# This script will read from a file a list of IPs seperated by newlines. It will then reach
# out to those IPs and:
# 1. Install a Zabbix Agent
# 2. Configure that agent to send data to a Zabbix Server or Proxy
# 3. Hit the Zabbix API to add the host to the front end
use 5.010;
use strict;
use warnings;
use JSON::RPC::Client;
use Data::Dumper;
# Check for root
#print "Checking for root...\n";
#my $uID = `id -u`;
#if ($uID != 0) {
# print "You are not root, exiting...\n";
# exit 1;
#}
# Put list of IPs in serverList.txt into an array
# Make sure entries are seperated by newlines
my $fileName = "serverList.txt";
open(my $fh, '<', $fileName);
my @nodeList = <$fh>;
#chomp @nodeList;
close ($fh);
print "The script will now perform Zabbix Installation and Setup on these nodes:\n";
print @nodeList;
foreach (@nodeList) {
# Authenticate yourself
my $client = new JSON::RPC::Client;
my $url = 'YOUR_API_URL'; # Example: http://0.0.0.0/zabbix/api_jsonrpc.php;
my $authID;
my $response;
my $server='YOUR_ZABX_SERVER_IP'; # Example: 0.0.0.0
my $json = {
jsonrpc => "2.0",
method => "user.login",
params => {
user => "YOUR_ZABBIX_API_USER",
password => "YOUR_PASSWORD"
},
id => 1
};
print "Trying to authenticate with the Zabbix API...\n";
$response = $client->call($url, $json);
# Check if response was successful
print Dumper($response) and die "Authentication failed\n" unless $response->content->{'result'};
$authID = $response->content->{'result'};
print "Authentication successful. Auth ID: " . $authID . "\n";
print "\n";
chomp $_;
print "Now installing Zabbix agent on $_...\n";
system("ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no root\@$_ 'apt-get -y install zabbix-agent; sed -i \'s/Server=localhost/Server=$server/g\' /etc/zabbix/zabbix_agentd.conf; service zabbix-agent restart'");
# We are going to pass this varible to the API
print "Setting nodeIP variable...\n";
my $nodeIP = `ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no root\@$_ 'ifconfig | grep inet | awk \"{print \\\$2}\" | sed s/addr://g | head -n1'`;
chomp $nodeIP;
print "nodeIP variable set to: $nodeIP\n";
# We are going to pass this variable to the API
print "Setting up hostname variable...\n";
my $hostname = `ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no root\@$_ 'hostname'`;
chomp $hostname;
print "Hostname variable set to: $hostname\n";
print "Configuring hostname in Zabbix config file...\n";
system("ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no root\@$_ 'sed -i \'s/Hostname=localhost/Hostname=$hostname/g\' /etc/zabbix/zabbix_agentd.conf'");
print "Restarting Zabbix agent to apply config...\n";
system("ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no root\@$_ 'service zabbix-agent restart'");
print "Building JSON to send to Zabbix API...\n";
my $hostJson = {
jsonrpc => "2.0",
method => "host.create",
params => {
host => "$hostname",
interfaces => [
{
type => "1",
main => "1",
useip => "1",
ip => "$nodeIP",
dns => "",
port => "10050"
}
],
groups => [
{
groupid => "ZABX_GRP_YOU_WANT_HOST_ADDED_TO"
# Example: groudid => "42"
}
],
templates => [
{
emplateid => "TEMPLATE_YOU_WANT_ADDED_TO_HOST"
# Example: templateid => "10523"
}
],
},
auth => "$authID",
id => "1"
};
# Hit the API
print "Sending JSON to Zabbix API...\n";
my $response2;
$response2 = $client->call($url, $hostJson);
# Check if response was successful
print "Checking if response was successful...\n";
print Dumper($response2);
print "Script success!\n";
print "Zabbix agent has been installed and configured on $_\n";
print "Host has been added to Zabbix frontend...\n";
}
exit 0;