Announcement

Collapse
No announcement yet.

Zabbix Agent/Host Auto Install/Create Script using API

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

    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
    }

    #2
    Thanks

    This is great, thanks!


    JBB

    Comment


      #3
      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

      Comment


        #4
        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

        Comment


          #5
          Very nice scripts, do you have examples on how to delete hosts ?

          Comment


            #6
            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, 02:42.

            Comment


              #7
              glad you enjoy it I do not work with zabbix anymore, but I am trying to bring it into my new job.. look for more stuff if i do

              Comment


                #8
                Gentlemen!

                I've been working on updating this bash script to be able to function on the newer zabbix 2.0 and centos 6.x

                So far i've come into problems with the host creation. I get the to point where I can select the HOSTGROUP and TEMPLATE however after that point it errors out

                Once I've gotten this going on centos 6.x I'll convert the script to do OS detection to make it cross platform (among linux distros)

                Anyone here think they can shine some light on this?

                Code:
                #!/bin/sh
                #==============================================================
                # Zabbix-Create - Automated Agent/Host Install - (c) Travis Mathis - Millicorp 2011
                # Version 2.1.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-6/" >> /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-6/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?(Montreal,Eindh) " RESP
                  if [ "$RESP" != "Montreal" ] && [ "$RESP" != "Eindh" ] && [ "$RESP" != "FMY" ]; then
                    echo "That is not a valid option, please try again!"
                    echo ""
                  else
                    ERROR='1'
                  fi
                done
                
                if [ "$RESP" = "Montreal" ]; then
                  SERVER='xxx.xxx.xxx.xxx'
                  API='http://xxx.xxx.xxx.xxx/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=`hostname --ip-address`
                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 FOR ZABBIX ON CENTOS 5.X
                #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
                
                #CREATE CONFIG FILE FOR ZABBIX ON CENTOS 6.X
                rm -rf /usr/local/etc/zabbix_agentd.conf
                echo "LogFile=/tmp/zabbix_agentd.log" > /usr/local/etc/zabbix_agentd.conf
                echo "Server=$SERVER" >> /usr/local/etc/zabbix_agentd.conf
                echo "Hostname=$HOSTNAME" >> /usr/local/etc/zabbix_agentd.conf
                
                # start zabbix agent
                chkconfig --level 2345 zabbix-agentd on
                service zabbix-agentd start
                
                # Authenticate with Zabbix API
                authenticate()
                {
                    wget -O- -o /dev/null $API --header 'Content-Type: application/json-rpc' --post-data "{\"jsonrpc\": \"2.0\",\"method\": \"user.login\",\"params\": {\"user\": \"$ZABBIX_USER\",\"password\": \"$ZABBIX_PASS\"},\"id\": 0}" | cut -d'"' -f8
                }
                AUTH_TOKEN=$(authenticate)
                
                # Give user HostGroup option list to choose from
                
                get_host_groups() {
                  wget -O- -o /dev/null $API --header 'Content-Type: application/json-rpc' --post-data "{\"jsonrpc\": \"2.0\", \"method\": \"hostgroup.get\",\"params\": {\"output\": \"extend\", \"sortfield\": \"name\"},\"auth\": \"$AUTH_TOKEN\", \"id\": 0}" | 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() {
                  wget -O- -o /dev/null $API --header 'Content-Type: application/json-rpc' --post-data "{\"jsonrpc\": \"2.0\", \"method\": \"template.get\",\"params\": {\"output\": \"extend\", \"sortfield\": \"host\"},\"auth\": \"$AUTH_TOKEN\", \"id\": 0}" | 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() {
                 wget -O- -o /dev/null $API --header 'Content-Type: application/json-rpc' --post-data "{\"jsonrpc\":\"2.0\",\"method\":\"hostgroup.get\",\"params\":{\"output\": \"extend\",\"filter\":{\"name\":[\"$HOST\"]}},\"auth\":\"$AUTH_TOKEN\",\"id\":0}" | sed -e 's/[{}]/''/g' | sed -e 's/[""]/''/g' | grep -Eo groupid:[0-9]* | cut -d":" -f2;
                }
                
                get_template_id() {
                  wget -O- -o /dev/null $API --header 'Content-Type: application/json-rpc' --post-data "{\"jsonrpc\":\"2.0\",\"method\":\"template.get\",\"params\":{\"output\": \"extend\",\"filter\":{\"host\":[\"$TEMP\"]}},\"auth\":\"$AUTH_TOKEN\",\"id\":0}" | 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() {
                  wget -O- -o /dev/null $API --header 'Content-Type: application/json-rpc' --post-data "{\"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}"
                }
                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

                Comment


                  #9
                  You need to provide the output, as well as the specific line which is giving the error.


                  JBB

                  Originally posted by Nicarlo View Post
                  Gentlemen!

                  I've been working on updating this bash script to be able to function on the newer zabbix 2.0 and centos 6.x

                  So far i've come into problems with the host creation. I get the to point where I can select the HOSTGROUP and TEMPLATE however after that point it errors out

                  Once I've gotten this going on centos 6.x I'll convert the script to do OS detection to make it cross platform (among linux distros)

                  Anyone here think they can shine some light on this?

                  Code:
                  #!/bin/sh
                  #==============================================================
                  # Zabbix-Create - Automated Agent/Host Install - (c) Travis Mathis - Millicorp 2011
                  # Version 2.1.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-6/" >> /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-6/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?(Montreal,Eindh) " RESP
                    if [ "$RESP" != "Montreal" ] && [ "$RESP" != "Eindh" ] && [ "$RESP" != "FMY" ]; then
                      echo "That is not a valid option, please try again!"
                      echo ""
                    else
                      ERROR='1'
                    fi
                  done
                  
                  if [ "$RESP" = "Montreal" ]; then
                    SERVER='xxx.xxx.xxx.xxx'
                    API='http://xxx.xxx.xxx.xxx/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=`hostname --ip-address`
                  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 FOR ZABBIX ON CENTOS 5.X
                  #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
                  
                  #CREATE CONFIG FILE FOR ZABBIX ON CENTOS 6.X
                  rm -rf /usr/local/etc/zabbix_agentd.conf
                  echo "LogFile=/tmp/zabbix_agentd.log" > /usr/local/etc/zabbix_agentd.conf
                  echo "Server=$SERVER" >> /usr/local/etc/zabbix_agentd.conf
                  echo "Hostname=$HOSTNAME" >> /usr/local/etc/zabbix_agentd.conf
                  
                  # start zabbix agent
                  chkconfig --level 2345 zabbix-agentd on
                  service zabbix-agentd start
                  
                  # Authenticate with Zabbix API
                  authenticate()
                  {
                      wget -O- -o /dev/null $API --header 'Content-Type: application/json-rpc' --post-data "{\"jsonrpc\": \"2.0\",\"method\": \"user.login\",\"params\": {\"user\": \"$ZABBIX_USER\",\"password\": \"$ZABBIX_PASS\"},\"id\": 0}" | cut -d'"' -f8
                  }
                  AUTH_TOKEN=$(authenticate)
                  
                  # Give user HostGroup option list to choose from
                  
                  get_host_groups() {
                    wget -O- -o /dev/null $API --header 'Content-Type: application/json-rpc' --post-data "{\"jsonrpc\": \"2.0\", \"method\": \"hostgroup.get\",\"params\": {\"output\": \"extend\", \"sortfield\": \"name\"},\"auth\": \"$AUTH_TOKEN\", \"id\": 0}" | 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() {
                    wget -O- -o /dev/null $API --header 'Content-Type: application/json-rpc' --post-data "{\"jsonrpc\": \"2.0\", \"method\": \"template.get\",\"params\": {\"output\": \"extend\", \"sortfield\": \"host\"},\"auth\": \"$AUTH_TOKEN\", \"id\": 0}" | 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() {
                   wget -O- -o /dev/null $API --header 'Content-Type: application/json-rpc' --post-data "{\"jsonrpc\":\"2.0\",\"method\":\"hostgroup.get\",\"params\":{\"output\": \"extend\",\"filter\":{\"name\":[\"$HOST\"]}},\"auth\":\"$AUTH_TOKEN\",\"id\":0}" | sed -e 's/[{}]/''/g' | sed -e 's/[""]/''/g' | grep -Eo groupid:[0-9]* | cut -d":" -f2;
                  }
                  
                  get_template_id() {
                    wget -O- -o /dev/null $API --header 'Content-Type: application/json-rpc' --post-data "{\"jsonrpc\":\"2.0\",\"method\":\"template.get\",\"params\":{\"output\": \"extend\",\"filter\":{\"host\":[\"$TEMP\"]}},\"auth\":\"$AUTH_TOKEN\",\"id\":0}" | 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() {
                    wget -O- -o /dev/null $API --header 'Content-Type: application/json-rpc' --post-data "{\"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}"
                  }
                  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

                  Comment


                    #10
                    I've updated the script for Zabbix 2.0. I wasn't able to post it here, so I've posted a complete description and the file on my blog:

                    linuxnotes.us


                    JBB

                    Comment

                    Ask questions to Zabbix Dev Team in person at the Zabbix Summit 2018!
                    Working...
                    X