Ad Widget

Collapse

Host Copy Script

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • globlalways
    Junior Member
    • Jun 2006
    • 9

    #1

    Host Copy Script

    As we needed a 1:1-copy of a host template we quickly discovered that it is a bit tricky to copy the data, because there are some relations that must be also duplicated. As we didn't find script to do this, we wrote our own (very basic) script. So there is no guarantee that it will work for your case.
    Eventually a similar functionality will be implemented in future Zabbix versions.

    This script will do the following (nothing more):

    1. Copy selected host (append _1 to its new name)
    2. Copy related items,functions and triggers for host

    It was wrote to use no dependent scripts so dont be scared if the code is kinda ugly.
    PHP Code:
    <pre><?php
    error_reporting
    (E_ALL);

    $cleanExit false;

    function 
    rollback() {
        global 
    $cleanExit;

        if(
    $cleanExit === false) {
            
    $stack debug_backtrace();
            
    var_export($stack);
        
        
            
    $result mysql_query("ROLLBACK");
            if(!
    $result) {
                echo 
    mysql_error();
                echo 
    "Error on shutdown";
            } else {
                echo 
    "Shutdown completed with rollback";
            }
        }
        else {
            
    mysql_query("COMMIT");
            echo 
    "Shutdown completed with commit";
        }
    }

    register_shutdown_function("rollback");

    $db mysql_connect("localhost","root","");

    if(!
    $db) exit(__LINE__ mysql_error());

    if(!
    mysql_select_db("zabbix")) exit(__LINE__ mysql_error());

    $hostId = @$_GET['hostId'];

    if(!
    $hostId) {

        
    $result mysql_query("SELECT * FROM hosts");

        echo 
    "<form action=host_copy.php method=get><table border=1>";
        while(
    $row mysql_fetch_assoc($result)) {
          echo 
    "<tr>";
          echo 
    "<td><input type=radio name=hostId value=".$row['hostid']."></td>";
          echo 
    "<td>".$row['host'] ."</td>";
          echo 
    "</tr>";
        }
        echo 
    "</table><input type=submit></form>";
        exit;

    }

    $result mysql_query("SET autocommit=0");

    if(!
    $result) exit(__LINE__ mysql_error());

    $result mysql_query("START TRANSACTION");

    if(!
    $result) exit(__LINE__ mysql_error());

    $query "INSERT INTO `hosts` (`host`, `useip`, `ip`, `port`, `status`, `disable_until`, `error`, `available`, `errors_from`, `templateid`)
    SELECT CONCAT(`host`,'_1'), `useip`, `ip`, `port`, `status`, `disable_until`, `error`, `available`, `errors_from`, `templateid` FROM `hosts` WHERE `hostid`="
    .$hostId;

    $result mysql_query($query);

    if(!
    $result) exit(__LINE__ mysql_error());

    $newHostId mysql_insert_id();

    if(!
    $newHostId) exit(__LINE__ mysql_error());

    echo 
    "Created Host: $newHostId\n";

    echo 
    "Searching for items related to the host:\n";

    $query "SELECT `itemid` FROM `items` WHERE `hostid`=$hostId";

    $result mysql_query($query);

    if(!
    $result) exit(__LINE__ mysql_error());

    echo 
    "Found ".mysql_num_rows($result) ." items\n";

    while(
    $items_row mysql_fetch_assoc($result)) {
        echo 
    "Duplicating item #".$items_row['itemid']."\n";
        
        
    $query "INSERT INTO `items` (`hostid`, `type`, `snmp_community`, `snmp_oid`, `snmp_port`, `description`, `key_`, `delay`, `history`, `trends`, `nextcheck`, `lastvalue`, `lastclock`, `prevvalue`, `status`, `value_type`, `trapper_hosts`, `units`, `multiplier`, `delta`, `prevorgvalue`, `snmpv3_securityname`, `snmpv3_securitylevel`, `snmpv3_authpassphrase`, `snmpv3_privpassphrase`, `formula`, `error`, `lastlogsize`, `logtimefmt`, `templateid`, `valuemapid`)
        SELECT "
    .$newHostId.", `type`, `snmp_community`, `snmp_oid`, `snmp_port`, `description`, `key_`, `delay`, `history`, `trends`, `nextcheck`, `lastvalue`, `lastclock`, `prevvalue`, `status`, `value_type`, `trapper_hosts`, `units`, `multiplier`, `delta`, `prevorgvalue`, `snmpv3_securityname`, `snmpv3_securitylevel`, `snmpv3_authpassphrase`, `snmpv3_privpassphrase`, `formula`, `error`, `lastlogsize`, `logtimefmt`, `templateid`, `valuemapid` FROM `items` WHERE `itemid`=".$items_row['itemid'];

        
    $result_dup_item mysql_query($query);

        if(!
    $result_dup_item) exit(__LINE__ mysql_error());

        
    $newItemId mysql_insert_id();

        if(!
    $newItemId) exit(__LINE__ mysql_error());

        echo 
    "Successfull - new itemid: #".$newItemId "\n";

        echo 
    "Searching for functions for item\n";

        
    $query "SELECT `functionid`,`itemid`,`triggerid` FROM `functions` WHERE `itemid`=".$items_row['itemid'];

        
    $result_functions mysql_query($query);

        if(!
    $result_functions) exit(__LINE__ mysql_error());

        echo 
    "Found ".mysql_num_rows($result_functions)." functions\n";

        while(
    $row_functions mysql_fetch_assoc($result_functions)) {
            echo 
    "Duplicating Trigger #".$row_functions['triggerid']." for function\n";

            
    $query "INSERT INTO `triggers` (`expression`, `description`, `url`, `status`, `value`, `priority`, `lastchange`, `dep_level`, `comments`, `error`, `templateid`)
            SELECT `expression`, `description`, `url`, `status`, `value`, `priority`, `lastchange`, `dep_level`, `comments`, `error`, `templateid` FROM `triggers` WHERE `triggerid`="
    .$row_functions['triggerid'];

            
    $result_dup_trigger mysql_query($query);

            if(!
    $result_dup_trigger) exit(__LINE__ mysql_error());

            
    $newTriggerId mysql_insert_id();

            if(!
    $newTriggerId) exit(__LINE__ mysql_error());

            echo 
    "Successful - new triggerid: #"$newTriggerId."\n";
            
            echo 
    "Duplicating function #"$row_functions['functionid']."\n";

            
    $query "INSERT INTO `functions` (`itemid`, `triggerid`, `lastvalue`, `function`, `parameter`)
            SELECT "
    .$newItemId.", ".$newTriggerId.", `lastvalue`, `function`, `parameter` FROM `functions` WHERE `functionid`=".$row_functions['functionid'];

            
    $result_dup_function mysql_query($query);

            if(!
    $result_dup_function) exit(__LINE__ mysql_error());

            
    $newFunctionId mysql_insert_id();

            if(!
    $newFunctionId) exit(__LINE__ mysql_error());

            echo 
    "Successful - new functionid: #".$newFunctionId."\n";

            echo 
    "Updating trigger #".$newTriggerId." with new expression\n";

            
    $query "SELECT `expression` FROM `triggers` WHERE `triggerid`=".$newTriggerId;

            
    $result_get_trigger mysql_query($query);
            
            if(!
    $result_get_trigger) exit(__LINE__ mysql_error());

            
    $trigger_data mysql_fetch_assoc($result_get_trigger);

            
    $expression $trigger_data['expression'];

    #        var_export($expression);

            
    $newExpression preg_replace('/^\{[0-9]+\}(.*)/','{'.$newFunctionId '}\1',$expression);

    #        var_export($expression);

            
    if($newExpression == $expression || empty($newExpression)) {
                exit(
    __LINE__ " Expression can not be changed: $expression --> $newExpression");
            }

            
    $query "UPDATE `triggers` SET `expression`='".mysql_real_escape_string($newExpression)."' WHERE `triggerid`=".$newTriggerId;

            
    $result_update_trigger mysql_query($query);

            if(!
    $result_update_trigger) exit(__LINE__ mysql_error());

            echo 
    "Successfully updated trigger expression: $expression --> $newExpression\n";

        }
    }

    echo 
    "Done.\n";
    $cleanExit true;
    How to use it: Simply copy it into your zabbix-docroot-folder and open it with your browser.
    Last edited by globlalways; 28-06-2006, 17:28. Reason: Bugfix in script
  • LEM
    Senior Member
    Zabbix Certified Specialist
    • Sep 2004
    • 112

    #2
    thx! very usefull!!!
    --
    LEM

    Comment

    Working...