Ad Widget

Collapse

Zabbix 1.8.X Proxy API Feature Extension

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • rday
    Junior Member
    • Aug 2011
    • 3

    #1

    Zabbix 1.8.X Proxy API Feature Extension

    I've added the create() and update() methods to the Proxy API. It's tested and working, but I'm not sure if it fits in with the API best practices. I would love to learn though, thanks for any feedback!

    Code:
    /** 
     * Set Proxy data
     * {@source}
     * @access public
     * @static
     * @since 1.8.7
     * @version 1
     *
     * @param _array $options
     * @param string $options['host']
     * @param int $options['status']
     * @param boolean $options['useip']
     * @param string $options['ip']
     * @param string $options['dns']
     * @param int $options['port']
     * @param array $options['hosts']
     */
    
        public static function create($options=array()){
            global $USER_DETAILS;
    
            try{
                self::BeginTransaction(__METHOD__);
                $result = array();
                $user_type = $USER_DETAILS['type'];
    
                if(!isset($options['host']) || !isset($options['status']) || !isset($options['useip']) || !isset($options['ip']) || !isset($options['port'])){
                    self::exception(ZBX_API_ERROR_PARAMETERS, 'Missing input parameters');
                }
    
                $result = DBselect('SELECT * FROM hosts WHERE status IN ('.HOST_STATUS_PROXY_ACTIVE.','.HOST_STATUS_PROXY_PASSIVE.')'.
                        ' and '.DBin_node('hostid').' AND host='.zbx_dbstr($options['host']));
    
                if(DBfetch($result)){
                    self::exception(ZBX_API_ERROR_PARAMETERS, S_HOST.' [ '.$options['host'].' ] '.S_ALREADY_EXISTS_SMALL);
                }
    
                DBstart();
                if(!count(get_accessible_nodes_by_user($USER_DETAILS,PERM_READ_WRITE,PERM_RES_IDS_ARRAY))) {
                    self::exception(ZBX_API_ERROR_PERMISSIONS, 'No permission to create proxies');
                    access_deny();
                }
    
                $result = $hostid = add_proxy($options['host'], $options['status'], $options['useip'], $options['dns'], $options['ip'], $options['port'], $options['hosts']);
                $result = DBend($result);
    
                self::EndTransaction(true, __METHOD__);
                return array('proxyid' => $hostid);
            } catch(APIException $e){
                self::EndTransaction(false, __METHOD__);
                $error = $e->getErrors();
                $error = reset($error);
                self::setError(__METHOD__, $e->getCode(), $error);
                return false;
                }
        }
    
    /** 
     * Update Proxy data
     * {@source}
     * @access public
     * @static
     * @since 1.8.7
     * @version 1
     *
     * @param _array $options
     * @param string $options['proxyid']
     * @param string $options['host']
     * @param int $options['status']
     * @param boolean $options['useip']
     * @param string $options['ip']
     * @param string $options['dns']
     * @param int $options['port']
     * @param array $options['hosts']
     * @return boolean
     */
    
        public static function update($options=array()){
            global $USER_DETAILS;
    
            try{
                self::BeginTransaction(__METHOD__);
                $result = array();
                $user_type = $USER_DETAILS['type'];
    
                if(!isset($options['proxyid']) || !isset($options['host']) || !isset($options['status']) || !isset($options['useip']) || !isset($options['ip']) || !isset($options['port'])){
                    self::exception(ZBX_API_ERROR_PARAMETERS, 'Missing input parameters');
                }
    
                $result = DBselect('SELECT * FROM hosts WHERE status IN ('.HOST_STATUS_PROXY_ACTIVE.','.HOST_STATUS_PROXY_PASSIVE.')'.
                        ' and hostid='.zbx_dbstr($options['proxyid']));
    
                if(!DBfetch($result)){
                    self::exception(ZBX_API_ERROR_PARAMETERS, S_HOST.' [ '.$options['proxyid'].' ] '.S_DOES_NOT_EXIST_SMALL);
                }
    
                DBstart();
                if(!count(get_accessible_nodes_by_user($USER_DETAILS,PERM_READ_WRITE,PERM_RES_IDS_ARRAY))) {
                    self::exception(ZBX_API_ERROR_PERMISSIONS, 'No permission to update proxies');
                    access_deny();
                }
    
                $result     = update_proxy($options['proxyid'], $options['host'], $options['status'], $options['useip'], $options['dns'], $options['ip'], $options['port'], $hosts);
                $result = DBend($result);
    
                self::EndTransaction(true, __METHOD__);
                return true;
            } catch(APIException $e){
                self::EndTransaction(false, __METHOD__);
                $error = $e->getErrors();
                $error = reset($error);
                self::setError(__METHOD__, $e->getCode(), $error);
                return false;
                }
        }
Working...