ZABBIX Forums  

Go Back   ZABBIX Forums > Zabbix Discussions and Feedback > Zabbix Cookbook

Reply
 
Thread Tools Display Modes
  #1  
Old 24-06-2011, 00:12
Axilla Axilla is offline
Senior Member
 
Join Date: Aug 2010
Posts: 128
Default Zabbix Agent/Host Auto Install/Create Script using API

Hello Zabbix Community. Here is a handy bash script I create for CentOS 5.6 and Zabbix 1.8.5. Automatically installs and configures the zabbix agent from repo. Prompts user for a few responses and creates the host in zabbix frontend. This was written for a 3 node distributed monitoring setup so you will need to adjust it to fit your environment.

Code:
#==============================================================
# Zabbix-Create - Automated Agent/Host Install - (c) Travis Mathis - Millicorp 2011
# Version 2
# Change Log: 6/20/11 Finished Host Creation
#==============================================================

# VARIABLES
HOSTNAME=''
SERVER=''
IP=''
API=''

# CONSTANT VARIABLES
ERROR='0'
ZABBIX_USER='user'
ZABBIX_PASS='pass'

# Create zabbix.repo file in /etc/yum.repos.d/zabbix.repo
echo "[zabbix]" > /etc/yum.repos.d/zabbix.repo
echo "name=Zabbix (CentOS_5)" >> /etc/yum.repos.d/zabbix.repo
echo "type=rpm-md" >> /etc/yum.repos.d/zabbix.repo
echo "baseurl=http://download.opensuse.org/repositories/home:/ericgearhart:/zabbix/CentOS_CentOS-5/" >> /etc/yum.repos.d/zabbix.repo
echo "gpgcheck=1" >> /etc/yum.repos.d/zabbix.repo
echo "gpgkey=http://download.opensuse.org/repositories/home:/ericgearhart:/zabbix/CentOS_CentOS-5/repodata/repomd.xml.key" >> /etc/yum.repos.d/zabbix.repo
echo "enabled=1" >> /etc/yum.repos.d/zabbix.repo

# Find out where the server is located and store it as 2 variables.
# Edit Your information below

while [ $ERROR = 0 ]
do
  read -p "What DATACENTER is this server located at?(MIA, FMY, LAX) " RESP
  if [ "$RESP" != "MIA" ] && [ "$RESP" != "LAX" ] && [ "$RESP" != "FMY" ]; then
    echo "That is not a valid option, please try again!"
    echo ""
  else
    ERROR='1'
  fi
done

if [ "$RESP" = "MIA" ]; then
  SERVER='192.168.1.99'
  API='http://192.168.1.99/zabbix/api_jsonrpc.php'
fi

if [ "$RESP" = "FMY" ]; then
  SERVER='192.168.5.99'
  API='http://192.168.5.99/zabbix/api_jsonrpc.php'
fi

if [ "$RESP" = "LAX" ]; then
  SERVER='192.168.9.99'
  API='http://192.168.9.99/zabbix/api_jsonrpc.php'
fi

# Request the Hostname of the server
echo ""
read -p "What is the HOSTNAME of the server? " RESP2
  HOSTNAME="$RESP2"

# Request IPADDRESS of the server
echo ""
read -p "What is the IP ADDRESS of the server? " RESP3
  IP="$RESP3"

# Install zabbix-agent
yum -y install zabbix-agent

create config file in /etc/zabbix/zabbix_agentd.conf
rm -rf /etc/zabbix/zabbix_agentd.conf
echo "LogFile=/tmp/zabbix_agentd.log" > /etc/zabbix/zabbix_agentd.conf
echo "Server=$SERVER" >> /etc/zabbix/zabbix_agentd.conf
echo "Hostname=$HOSTNAME" >> /etc/zabbix/zabbix_agentd.conf

# start zabbix agent
chkconfig --level 2345 zabbix-agentd on
service zabbix-agentd start

# Authenticate with Zabbix API
authenticate() {
curl -i -X POST -H 'Content-Type: application/json-rpc' -d "{\"params\": {\"password\": \"$ZABBIX_PASS\", \"user\": \"$ZABBIX_USER\"}, \"jsonrpc\": \"2.0\", \"method\": \"user.authenticate\",\"auth\": \"\", \"id\": 0}" $API | grep -Eo 'Set-Cookie: zbx_sessionid=.+' | head -n 1 | cut -d '=' -f 2 | tr -d '\r'
}
AUTH_TOKEN=$(authenticate)

# Give user HostGroup option list to choose from

get_host_groups() {
  curl -i -X POST -H 'Content-Type: application/json-rpc' -d "{\"params\": {\"output\": \"extend\", \"sortfield\": \"name\"}, \"jsonrpc\": \"2.0\", \"method\": \"hostgroup.get\",\"auth\": \"$AUTH_TOKEN\", \"id\": 0}" $API | sed -e 's/[{}]/''/g' | awk -v RS=',"' -F: '/^name/ {print $2}' | sed 's/\(^"\|"$\)//g'
}

HOST_GROUPS=$(get_host_groups)

echo ""
echo "Please select a HOSTGROUP"
echo ""
select HOST in $HOST_GROUPS
do
echo ""
break;
done


# Give user Template option list to choose from
get_templates() {
  curl -i -X POST -H 'Content-Type: application/json-rpc' -d "{\"params\": {\"output\": \"extend\", \"sortfield\": \"template\"}, \"jsonrpc\": \"2.0\", \"method\": \"template.get\",\"auth\": \"$AUTH_TOKEN\", \"id\": 0}" $API | sed -e 's/[{}]/''/g' | awk -v RS=',"' -F: '/^host"/ {print $2}' | sed 's/\(^"\|"$\)//g'
}


TEMPLATES=$(get_templates)

echo ""
echo "Please select a TEMPLATE"
echo ""
select TEMP in $TEMPLATES
do
echo ""
break;
done

# Get Host_Group and Template ID's for host creation
get_host_group_id() {
 curl -i -X POST -H 'Content-Type: application/json-rpc' -d "{\"jsonrpc\":\"2.0\",\"method\":\"hostgroup.get\",\"params\":{\"output\": \"extend\",\"filter\":{\"name\":[\"$HOST\"]}},\"auth\":\"$AUTH_TOKEN\",\"id\":0}" $API | sed -e 's/[{}]/''/g' | sed -e 's/[""]/''/g' | grep -Eo groupid:[0-9]* | cut -d":" -f2;
}

get_template_id() {
  curl -i -X POST -H 'Content-Type: application/json-rpc' -d "{\"jsonrpc\":\"2.0\",\"method\":\"template.get\",\"params\":{\"output\": \"extend\",\"filter\":{\"host\":[\"$TEMP\"]}},\"auth\":\"$AUTH_TOKEN\",\"id\":0}" $API | sed -e 's/[{}]/''/g' | awk -v RS=',"' -F: '/^templateid/ {print $2}' | sed 's/\(^"\|"$\)//g' | sed -e 's/["]]/''/g'
}

HOSTGROUPID=$(get_host_group_id)
TEMPLATEID=$(get_template_id)

# Create Host
create_host() {
  curl -i -X POST -H 'Content-Type: application/json-rpc' -d "{\"jsonrpc\":\"2.0\",\"method\":\"host.create\",\"params\":{\"host\":\"$HOSTNAME\",\"ip\":\"$IP\",\"port\":10050,\"useip\":1,\"groups\":[{\"groupid\":$HOSTGROUPID}],\"templates\":[{\"templateid\":$TEMPLATEID}]},\"auth\":\"$AUTH_TOKEN\",\"id\":0}" $API
}
Reply With Quote
  #2  
Old 29-07-2011, 23:48
linuxgurugamer linuxgurugamer is offline
Member
 
Join Date: Oct 2010
Posts: 57
Send a message via AIM to linuxgurugamer
Default Thanks

This is great, thanks!


JBB
Reply With Quote
  #3  
Old 09-08-2011, 22:22
linuxgurugamer linuxgurugamer is offline
Member
 
Join Date: Oct 2010
Posts: 57
Send a message via AIM to linuxgurugamer
Default Bugs

your script has a few bugs. One of which is that it doesn't actually create the host, and another where you are missing the comment character at the beginning of the line.

First, find the line which is:

create config file in /etc/zabbix/zabbix_agentd.conf

and add a pound sign to the beginning; it is a comment and generates an error if you don't do this.

Second, add the following at the end of the script to actually create the host, and check for errors:

output=$(create_host)

echo $output | grep -q "hostids"
rc=$?
if [ $rc -ne 0 ]; then
echo -e "Error in adding host:\n"
echo $output | grep -Po '"message":.*?[^\\]",'
echo $output | grep -Po '"data":.*?[^\\]"'
exit
else
echo -e "\nHost added successfully\n"
exit
fi



JBB
Reply With Quote
  #4  
Old 26-08-2011, 10:05
Flaxe Flaxe is offline
Junior Member
 
Join Date: Aug 2011
Posts: 1
Default

Very nice script..

Here is a small updated version of me.
It fills the host in automatic and the ip.

Also adds hostname to the guest with the api

And it can add the iptables rules.

Code:
#!/bin/sh
#==============================================================
# Zabbix-Create - Automated Agent/Host Install - (c) Travis Mathis - Millicorp 2011
# Version 2.1
#
# Change Log
#
# 8/26/11 Added automatic hostname detection (flaxe)
# 8/26/11 Added automatic ip detection (flaxe)
# 8/26/11 Added Iptables firewall rules (flaxe)
# 8/26/11 Added hotname in api (flaxe)
#
# 6/20/11 Finished Host Creation
#==============================================================

# VARIABLES
#HOSTNAME=''
SERVER=''
IP=''
API=''

# CONSTANT VARIABLES
ERROR='0'
ZABBIX_USER=' '
ZABBIX_PASS=' '

# Create zabbix.repo file in /etc/yum.repos.d/zabbix.repo
echo "[zabbix]" > /etc/yum.repos.d/zabbix.repo
echo "name=Zabbix (CentOS_5)" >> /etc/yum.repos.d/zabbix.repo
echo "type=rpm-md" >> /etc/yum.repos.d/zabbix.repo
echo "baseurl=http://download.opensuse.org/repositories/home:/ericgearhart:/zabbix/CentOS_CentOS-5/" >> /etc/yum.repos.d/zabbix.repo
echo "gpgcheck=1" >> /etc/yum.repos.d/zabbix.repo
echo "gpgkey=http://download.opensuse.org/repositories/home:/ericgearhart:/zabbix/CentOS_CentOS-5/repodata/repomd.xml.key" >> /etc/yum.repos.d/zabbix.repo
echo "enabled=1" >> /etc/yum.repos.d/zabbix.repo

# Find out where the server is located and store it as 2 variables.
# Edit Your information below

while [ $ERROR = 0 ]
do
  read -p "What DATACENTER is this server located at?(Mesh,Eindh) " RESP
  if [ "$RESP" != "Mesh" ] && [ "$RESP" != "Eindh" ] && [ "$RESP" != "FMY" ]; then
    echo "That is not a valid option, please try again!"
    echo ""
  else
    ERROR='1'
  fi
done

if [ "$RESP" = "Mesh" ]; then
  SERVER='xxx.xxx.xxx.xxx'
  API='http://xxx.xxx.xxx.xx/zabbix/api_jsonrpc.php'
fi

if [ "$RESP" = "Eind" ]; then
  SERVER='xxx.xxx.xxx.xxx'
  API='http://xxx.xxx.xxx.xxx/zabbix/api_jsonrpc.php'
fi

if [ "$RESP" = "LAX" ]; then
  SERVER='192.168.9.99'
  API='http://192.168.9.99/zabbix/api_jsonrpc.php'
fi

# Request the Hostname of the server
echo ""

read -p "What is the HOSTNAME of the server? ($HOSTNAME)" RESP2

  NAMEHOST=${RESP2:-$HOSTNAME}



# Request IPADDRESS of the server
echo ""
IP=`gethostip -d $NAMEHOST`
read -p "What is the IP ADDRESS of the server? ($IP)" RESP3
 IP=${RESP3:-$IP}





ERROR='0'
while [ $ERROR = 0 ]
do
  read -p "Add port to iptables (Only add if no csf or apf is loaded)  (Yes/No)" RESP
  if [ "$RESP" != "Yes" ] && [ "$RESP" != "No" ]; then
    echo "That is not a valid option, please try again!"
    echo ""
      else
    ERROR='1'
  fi
done

if [ "$RESP" = "Yes" ]; then
  iptables -I INPUT -p tcp --dport 10050 -j ACCEPT
  iptables -I INPUT -p tcp --dport 10051 -j ACCEPT
  service iptables save
  service iptables restart
fi

# Install zabbix-agent
yum -y install zabbix-agent

#create config file in /etc/zabbix/zabbix_agentd.conf
rm -rf /etc/zabbix/zabbix_agentd.conf
echo "LogFile=/tmp/zabbix_agentd.log" > /etc/zabbix/zabbix_agentd.conf
echo "Server=$SERVER" >> /etc/zabbix/zabbix_agentd.conf
echo "Hostname=$HOSTNAME" >> /etc/zabbix/zabbix_agentd.conf

# start zabbix agent
chkconfig --level 2345 zabbix-agentd on
service zabbix-agentd start

# Authenticate with Zabbix API
authenticate() {
curl -i -X POST -H 'Content-Type: application/json-rpc' -d "{\"params\": {\"password\": \"$ZABBIX_PASS\", \"user\": \"$ZABBIX_USER\"}, \"jsonrpc\":\"2.0\", \"method\": \"user.authenticate\",\"auth\": \"\", \"id\": 0}" $API | grep -Eo 'Set-Cookie: zbx_sessionid=.+' | head -n 1 | cut -d '=' -f 2 | tr -d '\r'
}
AUTH_TOKEN=$(authenticate)

# Give user HostGroup option list to choose from

get_host_groups() {
  curl -i -X POST -H 'Content-Type: application/json-rpc' -d "{\"params\": {\"output\": \"extend\", \"sortfield\": \"name\"}, \"jsonrpc\": \"2.0\", \"method\": \"hostgroup.get\",\"auth\": \"$AUTH_TOKEN\", \"id\": 0}" $API | sed -e 's/[{}]/''/g' | awk -v RS=',"' -F: '/^name/ {print $2}' | sed 's/\(^"\|"$\)//g'
}

HOST_GROUPS=$(get_host_groups)

echo ""
echo "Please select a HOSTGROUP"
echo ""
select HOST in $HOST_GROUPS
do
echo ""
break;
done


# Give user Template option list to choose from
get_templates() {
  curl -i -X POST -H 'Content-Type: application/json-rpc' -d "{\"params\": {\"output\": \"extend\", \"sortfield\": \"template\"}, \"jsonrpc\": \"2.0\", \"method\": \"template.get\",\"auth\": \"$AUTH_TOKEN\", \"id\": 0}" $API | sed -e 's/[{}]/''/g' | awk -v RS=',"' -F: '/^host"/ {print $2}' | sed 's/\(^"\|"$\)//g'
}


TEMPLATES=$(get_templates)

echo ""
echo "Please select a TEMPLATE"
echo ""
select TEMP in $TEMPLATES
do
echo ""
break;
done

# Get Host_Group and Template ID's for host creation
get_host_group_id() {
 curl -i -X POST -H 'Content-Type: application/json-rpc' -d "{\"jsonrpc\":\"2.0\",\"method\":\"hostgroup.get\",\"params\":{\"output\": \"extend\",\"filter\":{\"name\":[\"$HOST\"]}},\"auth\":\"$AUTH_TOKEN\",\"id\":0}" $API | sed -e 's/[{}]/''/g' | sed -e 's/[""]/''/g' | grep -Eo groupid:[0-9]* | cut -d":" -f2;
}

get_template_id() {
  curl -i -X POST -H 'Content-Type: application/json-rpc' -d "{\"jsonrpc\":\"2.0\",\"method\":\"template.get\",\"params\":{\"output\": \"extend\",\"filter\":{\"host\":[\"$TEMP\"]}},\"auth\":\"$AUTH_TOKEN\",\"id\":0}" $API | sed -e 's/[{}]/''/g' | awk -v RS=',"' -F: '/^templateid/ {print $2}' | sed 's/\(^"\|"$\)//g' | sed -e 's/["]]/''/g'
}

HOSTGROUPID=$(get_host_group_id)
TEMPLATEID=$(get_template_id)

# Create Host
create_host() {
  curl -i -X POST -H 'Content-Type: application/json-rpc' -d "{\"jsonrpc\":\"2.0\",\"method\":\"host.create\",\"params\":{\"host\":\"$HOSTNAME\",\"ip\":\"$IP\",\"dns\":\"$HOSTNAME\",\"port\":10050,\"useip\":1,\"groups\":[{\"groupid\":$HOSTGROUPID}],\"templates\":[{\"templateid\":$TEMPLATEID}]},\"auth\":\"$AUTH_TOKEN\",\"id\":0}" $API
}
output=$(create_host)

echo $output | grep -q "hostids"
rc=$?
if [ $rc -ne 0 ]; then
echo -e "Error in adding host:\n"
echo $output | grep -Po '"message":.*?[^\\]",'
echo $output | grep -Po '"data":.*?[^\\]"'
exit
else
echo -e "\nHost added successfully\n"
exit
fi
Reply With Quote
  #5  
Old 28-09-2011, 20:02
nicolasg nicolasg is offline
Member
 
Join Date: Apr 2011
Posts: 49
Default

Very nice scripts, do you have examples on how to delete hosts ?
Reply With Quote
  #6  
Old 04-10-2011, 01:38
reward reward is offline
Junior Member
 
Join Date: Sep 2011
Posts: 11
Question Zabbix Agent/Host Auto Install/Create Script using API

Hello,

Is this script for Linux 32bit or 64bit? I am newbie to both Linux and zabbix. I have installed Centos 5.7 32bit and want to install zabbix but I encounter problem after problem during installation. How do I use your script. I am trying to use zabbix locally at home to test. The IP address of the zabbix server will be 192.168.0.100. Thank you

Last edited by reward; 04-10-2011 at 03:42.
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +2. The time now is 17:04.