Ad Widget

Collapse

Add host via SQL = Template problem

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • tazzu
    Junior Member
    • Nov 2006
    • 26

    #1

    Add host via SQL = Template problem

    Hi Guys,

    I've addet a host via mysql insert to zabbix:

    1. I've addet a host to the table hosts
    2. I've addet the link between hosts and template to the table hosts_templates

    In the Zabbix Web frontend I can see that the host was addet and the template linked.

    My Problem is that the host don't get the items from the template...

    I can add this items manually via sql but then they don't change when i change the template.

    Anyone who can help me?
  • moshen
    Junior Member
    • Aug 2008
    • 13

    #2
    Which version of Zabbix?

    I've only added groups, but in 1.4 you had to increment the id in the id table. I believe that you had to do this for hosts as well ( Although I used the XML import to add the hosts ).

    Code:
    (perl example)
    
    $dsn = "DBI:mysql:database=zabbix;host=127.0.0.1;port=3306";
    
    $dbh = DBI->connect($dsn, "zabbixdbuser", 'zabbixdbpassword');
            $sth = $dbh->prepare("SELECT nextid FROM ids WHERE field_name = 'groupid'");
            $sth->execute;
            $groupid = $sth->fetchrow();
    
    for ( $j = 0; $j < $nameCount; $j++ ) {
        $groupid++;
        $sth = $dbh->prepare("INSERT INTO groups VALUES ('$groupid', '$groupNames[$j]')");
        $sth->execute;
        print $groupNames[$j] . "\n";
    }
            $sth = $dbh->prepare("UPDATE ids SET nextid = '$groupid' WHERE field_name = 'groupid'");
            $sth->execute;

    Comment

    • tazzu
      Junior Member
      • Nov 2006
      • 26

      #3
      I use Zabbix 1.6

      I need an automatically host (with linked template) installation over command line.

      I think there is so way to upload xml via command line?

      Comment

      • tazzu
        Junior Member
        • Nov 2006
        • 26

        #4
        Here is my Script what I use (Shell Script)

        Code:
        new_row=1
        for new_id in `echo "SELECT hostid FROM hosts" | mysql -u zabbix --password=xxxx zabbix`
        do
                if [ "$new_id" != "hostid" ]
                then
                        if [ "$new_id" -gt "$new_row" ]
                        then
                                new_row=$new_id
                        fi
                fi
        done
        new_row=`expr $new_row + 1`
        
        tmp_id=1
        for new_tid in `echo "SELECT hosttemplateid FROM hosts_templates" | mysql -u zabbix --password=xxxx zabbix`
        do
                if [ "$new_tid" != "hosttemplateid" ]
                then
                        if [ "$new_tid" -gt "$tmp_id" ]
                        then
                                tmp_id=$new_tid
                        fi
                fi
        done
        tmp_id=`expr $tmp_id + 1`
        
        check1=$(echo "SELECT host FROM hosts where host='$r_name'" | mysql -u zabbix --password=xxxx zabbix | tail -1)
        check2=$(echo "SELECT ip FROM hosts where ip='$r_ip'" | mysql -u zabbix --password=xxxx zabbix | tail -1)
        check3=$(echo "SELECT host FROM hosts" | mysql -u zabbix --password=xxxx zabbix | grep -i $r_type | grep -i template)
        
        if [ "$check1" != "" ]
        then
                echo "Host already exists"
                err=1
        fi
        
        if [ "$check2" != "" ]
        then
                echo "IP already exists"
                err=1
        fi
        
        if [ "$check3" = "" ]
        then
                echo "No such Template Type known"
                err=1
        fi
        
        if [ "$err" = "0" ]
        then
                echo "INSERT INTO hosts (hostid,host,dns,useip,ip,port,status,error) VALUES ('$new_row','$r_name','$r_name','1','$r_ip','161','0','')" | mysql -u za
        bbix --password=xxxx zabbix
                template_id=$(echo "SELECT hostid FROM hosts where host='$check3'" | mysql -u zabbix --password=xxxx zabbix | tail -1)
                host_id=$(echo "SELECT hostid FROM hosts where host='$r_name'" | mysql -u zabbix --password=xxxx zabbix | tail -1)
                if [ "$host_id" != "" ]
                then
                        echo "INSERT INTO hosts_templates (hosttemplateid,hostid,templateid) VALUES ('$tmp_id','$host_id','$template_id')" | mysql -u zabbix --passw
        ord=xxxx zabbix
                fi
        fi
        In the Web frontend I can see the following:

        Name DNS IP Port Templates Status Availability Error Actions
        Testhost 192.168.0.69 161 Template_Cisco_837 Monitored Available - Select

        So the host is correct addet ( Also I've looked in the DB and it looks fine).

        In hosts.php and *.inc.php I can see that there are several actions perfomed when I add a host manually over web interface. But thats not what I need

        Someone who have an idea?

        Comment

        • moshen
          Junior Member
          • Aug 2008
          • 13

          #5
          If they have added one, they're keeping it a secret. What I have done is used a perl script with the LWP::UserAgent or WWW::Mechanize modules to login to the front end and upload the files through the import page. I can post an example if you like.

          The problem with that method is processing errors encountered during the import process.

          What I have taken to doing is adding all needed hosts to a single XML file and importing that. I have added hundreds at a time this way.

          Comment

          • tazzu
            Junior Member
            • Nov 2006
            • 26

            #6
            Originally posted by moshen
            If they have added one, they're keeping it a secret. What I have done is used a perl script with the LWP::UserAgent or WWW::Mechanize modules to login to the front end and upload the files through the import page. I can post an example if you like.

            The problem with that method is processing errors encountered during the import process.

            What I have taken to doing is adding all needed hosts to a single XML file and importing that. I have added hundreds at a time this way.
            Sounds good. Please Post example

            Edit:

            I've found another good solution with curl (save cookie and send post data to hosts.php)

            I will post it when I'm done
            Last edited by tazzu; 28-10-2008, 19:09.

            Comment

            • moshen
              Junior Member
              • Aug 2008
              • 13

              #7
              Curl would be another way to do it. Here would be a (partial) example on how to do it in perl for Zabbix 1.4 (modified code from PaulKennedy on the forum).

              Code:
              #!/usr/bin/perl -w
              use strict;
              use LWP::UserAgent;
              
              my $zsrv = "zabbix.example.com";
              
              sub do_post() {
              
              my $ua = LWP::UserAgent->new;
              $ua->cookie_jar( {} );
              $ua->agent("Mozilla 8.0 blah...");
              my $response = $ua->request ("POST http://$zsrv/zabbix/index.php",
                  Content_Type => "form-data",
                  Content      =>   ["form"         => "1",
                                     "form_refresh" => "1",
                                     "name"         => "zabbix",
                                     "password"     => "zabbix",
                                     "enter"        => "Enter"
                                    ]
                     );
              
              $ua->credentials("http://$zsrv:80","Zabbix","zscript","zscript");
              
              $response = $ua->request ("POST http://$zsrv/zabbix/exp_imp.php",
                  Content_Type => "form-data",
                  Content      =>   ["form"                => "1",
                                     "form_refresh"        => "1",
                                     "config"              => "1",
                                     "rules[host][exist]"  => "0",
                                     "rules[host][missed]" => "0",
                                     "import"              => "Import",
                                     "import_file"         => ["/some/file.xml"]
                                    ]);        
              }
              Last edited by moshen; 30-10-2008, 19:39.

              Comment

              Working...