コード例 #1
0
ファイル: triggers.inc.php プロジェクト: rennhak/zabbix
function update_trigger($triggerid, $expression = NULL, $description = NULL, $type = NULL, $priority = NULL, $status = NULL, $comments = NULL, $url = NULL, $deps = array(), $templateid = 0)
{
    $trigger = get_trigger_by_triggerid($triggerid);
    $trig_hosts = get_hosts_by_triggerid($triggerid);
    $trig_host = DBfetch($trig_hosts);
    $event_to_unknown = false;
    if (is_null($expression)) {
        /* Restore expression */
        $expression = explode_exp($trigger["expression"], 0);
    } else {
        if ($expression != explode_exp($trigger["expression"], 0)) {
            $event_to_unknown = true;
        }
    }
    if (!validate_expression($expression)) {
        return false;
    }
    $exp_hosts = get_hosts_by_expression($expression);
    if ($exp_hosts) {
        $chd_hosts = get_hosts_by_templateid($trig_host["hostid"]);
        if (DBfetch($chd_hosts)) {
            $exp_host = DBfetch($exp_hosts);
            $db_chd_triggers = get_triggers_by_templateid($triggerid);
            while ($db_chd_trigger = DBfetch($db_chd_triggers)) {
                $chd_trig_hosts = get_hosts_by_triggerid($db_chd_trigger["triggerid"]);
                $chd_trig_host = DBfetch($chd_trig_hosts);
                $newexpression = str_replace("{" . $exp_host["host"] . ":", "{" . $chd_trig_host["host"] . ":", $expression);
                // recursion
                update_trigger($db_chd_trigger["triggerid"], $newexpression, $description, $type, $priority, NULL, $comments, $url, replace_template_dependencies($deps, $chd_trig_host['hostid']), $triggerid);
            }
        }
    }
    $result = delete_function_by_triggerid($triggerid);
    if (!$result) {
        return $result;
    }
    $expression = implode_exp($expression, $triggerid);
    /* errors can be ignored cose function must return NULL */
    if ($event_to_unknown) {
        add_event($triggerid, TRIGGER_VALUE_UNKNOWN);
    }
    reset_items_nextcheck($triggerid);
    $sql = "UPDATE triggers SET";
    if (!is_null($expression)) {
        $sql .= ' expression=' . zbx_dbstr($expression) . ',';
    }
    if (!is_null($description)) {
        $sql .= ' description=' . zbx_dbstr($description) . ',';
    }
    if (!is_null($type)) {
        $sql .= ' type=' . $type . ',';
    }
    if (!is_null($priority)) {
        $sql .= ' priority=' . $priority . ',';
    }
    if (!is_null($status)) {
        $sql .= ' status=' . $status . ',';
    }
    if (!is_null($comments)) {
        $sql .= ' comments=' . zbx_dbstr($comments) . ',';
    }
    if (!is_null($url)) {
        $sql .= ' url=' . zbx_dbstr($url) . ',';
    }
    if (!is_null($templateid)) {
        $sql .= ' templateid=' . $templateid . ',';
    }
    $sql .= ' value=2 WHERE triggerid=' . $triggerid;
    $result = DBexecute($sql);
    delete_dependencies_by_triggerid($triggerid);
    foreach ($deps as $id => $triggerid_up) {
        if (!($result2 = add_trigger_dependency($triggerid, $triggerid_up))) {
            error(S_INCORRECT_DEPENDENCY . ' [' . expand_trigger_description($triggerid_up) . ']');
        }
        $result &= $result2;
    }
    if ($result) {
        $trig_hosts = get_hosts_by_triggerid($triggerid);
        $msg = "Trigger '" . $trigger["description"] . "' updated";
        $trig_host = DBfetch($trig_hosts);
        if ($trig_host) {
            $msg .= " for host '" . $trig_host["host"] . "'";
        }
        info($msg);
    }
    if ($result) {
        $trigger_new = get_trigger_by_triggerid($triggerid);
        add_audit_ext(AUDIT_ACTION_UPDATE, AUDIT_RESOURCE_TRIGGER, $triggerid, $trigger["description"], 'triggers', $trigger, $trigger_new);
    }
    return $result;
}
コード例 #2
0
function db_save_application($name, $hostid, $applicationid = null, $templateid = 0)
{
    if (!is_string($name)) {
        error('Incorrect parameters for "db_save_application"');
        return false;
    }
    $host = get_host_by_hostid($hostid);
    $hostids = array();
    $db_hosts = get_hosts_by_templateid($host['hostid']);
    while ($db_host = DBfetch($db_hosts)) {
        $hostids[] = $db_host['hostid'];
    }
    $sql = 'SELECT applicationid
			FROM applications
			WHERE name=' . zbx_dbstr($name) . '
				AND ' . DBcondition('hostid', $hostids);
    $lower_app = DBfetch(DBselect($sql));
    if ($lower_app) {
        error(S_APPLICATION . SPACE . "'{$name}'" . SPACE . S_ALREADY_EXISTS_IN_LINKED_HOSTS_SMALL);
        return false;
    }
    $sql = 'SELECT applicationid
			FROM applications
			WHERE name=' . zbx_dbstr($name) . '
				AND hostid=' . $hostid;
    if (!is_null($applicationid)) {
        $sql .= ' AND applicationid<>' . $applicationid;
    }
    $db_app = DBfetch(DBselect($sql));
    if ($db_app && $templateid == 0) {
        error(S_APPLICATION . SPACE . "'{$name}'" . SPACE . S_ALREADY_EXISTS_SMALL);
        return false;
    }
    if ($db_app && !is_null($applicationid)) {
        // delete old application with same name
        delete_application($db_app['applicationid']);
    }
    if ($db_app && is_null($applicationid)) {
        // if found application with same name update them, adding not needed
        $applicationid = $db_app['applicationid'];
    }
    if (is_null($applicationid)) {
        $applicationid_new = get_dbid('applications', 'applicationid');
        $sql = 'INSERT INTO applications (applicationid, name, hostid, templateid) ' . " VALUES ({$applicationid_new}, " . zbx_dbstr($name) . ", {$hostid}, {$templateid})";
        if ($result = DBexecute($sql)) {
            info(S_ADDED_NEW_APPLICATION . SPACE . $host['host'] . ':' . $name);
        }
    } else {
        $old_app = get_application_by_applicationid($applicationid);
        $result = DBexecute('UPDATE applications SET name=' . zbx_dbstr($name) . ', hostid=' . $hostid . ', templateid=' . $templateid . ' WHERE applicationid=' . $applicationid);
        if ($result) {
            info(S_UPDATED_APPLICATION . SPACE . $host['host'] . ':' . $old_app['name']);
        }
    }
    if (!$result) {
        return $result;
    }
    if (is_null($applicationid)) {
        // create application for childs
        $applicationid = $applicationid_new;
        $db_childs = get_hosts_by_templateid($hostid);
        while ($db_child = DBfetch($db_childs)) {
            // recursion
            $result = add_application($name, $db_child['hostid'], $applicationid);
            if (!$result) {
                break;
            }
        }
    } else {
        $db_applications = get_applications_by_templateid($applicationid);
        while ($db_app = DBfetch($db_applications)) {
            // recursion
            $result = update_application($db_app['applicationid'], $name, $db_app['hostid'], $applicationid);
            if (!$result) {
                break;
            }
        }
    }
    if ($result) {
        return $applicationid;
    }
    if ($templateid == 0) {
        delete_application($applicationid);
    }
    return false;
}
コード例 #3
0
ファイル: graphs.inc.php プロジェクト: phedders/zabbix
function add_graph_with_items($name, $width, $height, $ymin_type, $ymax_type, $yaxismin, $yaxismax, $ymin_itemid, $ymax_itemid, $showworkperiod, $showtriggers, $graphtype, $legend, $graph3d, $percent_left, $percent_right, $gitems = array(), $templateid = 0)
{
    $result = false;
    if (!is_array($gitems) || count($gitems) < 1) {
        error('Missing items for graph "' . $name . '"');
        return $result;
    }
    /* check items for template graph */
    unset($new_host_is_template);
    $host_list = array();
    $itemid = array(0);
    foreach ($gitems as $gitem) {
        $itemid[] = $gitem['itemid'];
    }
    $db_item_hosts = DBselect('SELECT DISTINCT h.hostid,h.host,h.status ' . ' FROM items i, hosts h ' . ' WHERE h.hostid=i.hostid ' . ' AND ' . DBcondition('i.itemid', $itemid));
    while ($db_item_host = DBfetch($db_item_hosts)) {
        $host_list[] = '"' . $db_item_host['host'] . '"';
        if (HOST_STATUS_TEMPLATE == $db_item_host['status']) {
            $new_host_is_template = true;
        }
    }
    if (isset($new_host_is_template) && count($host_list) > 1) {
        error('Graph "' . $name . '" with template host can not contain items from other hosts.');
        return $result;
    }
    if ($graphid = add_graph($name, $width, $height, $ymin_type, $ymax_type, $yaxismin, $yaxismax, $ymin_itemid, $ymax_itemid, $showworkperiod, $showtriggers, $graphtype, $legend, $graph3d, $percent_left, $percent_right, $templateid)) {
        $result = true;
        foreach ($gitems as $gitem) {
            if (!($result = add_item_to_graph($graphid, $gitem['itemid'], $gitem['color'], $gitem['drawtype'], $gitem['sortorder'], $gitem['yaxisside'], $gitem['calc_fnc'], $gitem['type'], $gitem['periods_cnt']))) {
                break;
            }
        }
    }
    if ($result) {
        info('Graph "' . $name . '" added to hosts ' . implode(',', $host_list));
        /* add graphs for child hosts */
        $tmp_hosts = get_hosts_by_graphid($graphid);
        $host = DBfetch($tmp_hosts);
        $chd_hosts = get_hosts_by_templateid($host['hostid']);
        while ($chd_host = DBfetch($chd_hosts)) {
            copy_graph_to_host($graphid, $chd_host['hostid'], false);
        }
    }
    if (!$result && $graphid) {
        delete_graph($graphid);
        $graphid = false;
    }
    return $graphid;
}
コード例 #4
0
ファイル: hosts.php プロジェクト: rennhak/zabbix
check_fields($fields);
validate_sort_and_sortorder('h.host', ZBX_SORT_UP);
update_profile('web.hosts.config', $_REQUEST['config'], PROFILE_TYPE_INT);
/************ ACTIONS FOR HOSTS ****************/
/* this code menages operations to unlink 1 template from multiple hosts */
if ($_REQUEST['config'] == 2 && isset($_REQUEST['save'])) {
    $hosts = get_request('hosts', array());
    if (isset($_REQUEST['hostid'])) {
        $templateid = $_REQUEST['hostid'];
        $result = true;
        // Permission check
        $hosts = array_intersect($hosts, $available_hosts);
        //-- unlink --
        DBstart();
        $linked_hosts = array();
        $db_childs = get_hosts_by_templateid($templateid);
        while ($db_child = DBfetch($db_childs)) {
            $linked_hosts[$db_child['hostid']] = $db_child['hostid'];
        }
        $unlink_hosts = array_diff($linked_hosts, $hosts);
        foreach ($unlink_hosts as $id => $value) {
            $result &= unlink_template($value, $templateid, false);
        }
        //----------
        //-- link --
        $link_hosts = array_diff($hosts, $linked_hosts);
        $template_name = DBfetch(DBselect('SELECT host FROM hosts WHERE hostid=' . $templateid));
        foreach ($link_hosts as $id => $hostid) {
            $host_groups = array();
            $db_hosts_groups = DBselect('SELECT groupid FROM hosts_groups WHERE hostid=' . $hostid);
            while ($hg = DBfetch($db_hosts_groups)) {
コード例 #5
0
function add_graph_with_items($name, $width, $height, $ymin_type, $ymax_type, $yaxismin, $yaxismax, $ymin_itemid, $ymax_itemid, $showworkperiod, $showtriggers, $graphtype, $legend, $graph3d, $percent_left, $percent_right, $gitems = array(), $templateid = 0)
{
    $result = false;
    if (!is_array($gitems) || count($gitems) < 1) {
        error(S_MISSING_ITEMS_FOR_GRAPH . SPACE . '"' . $name . '"');
        return $result;
    }
    /* check items for template graph */
    unset($new_host_is_template);
    $host_list = array();
    $itemid = array(0);
    foreach ($gitems as $gitem) {
        $itemid[] = $gitem['itemid'];
    }
    $db_item_hosts = DBselect('SELECT DISTINCT h.hostid,h.host,h.status ' . ' FROM items i, hosts h ' . ' WHERE h.hostid=i.hostid ' . ' AND ' . DBcondition('i.itemid', $itemid));
    $graph_hostids = array();
    while ($db_item_host = DBfetch($db_item_hosts)) {
        $host_list[] = '"' . $db_item_host['host'] . '"';
        $graph_hostids[] = $db_item_host['hostid'];
        if (HOST_STATUS_TEMPLATE == $db_item_host['status']) {
            $new_host_is_template = true;
        }
    }
    if (isset($new_host_is_template) && count($host_list) > 1) {
        error(S_GRAPH . SPACE . '"' . $name . '"' . SPACE . S_GRAPH_TEMPLATE_HOST_CANNOT_OTHER_ITEMS_HOSTS_SMALL);
        return $result;
    }
    // $filter = array(
    // 'name' => $name,
    // 'hostids' => $graph_hostids
    // );
    // if(CGraph::exists($filter)){
    // error('Graph already exists [ '.$name.' ]');
    // return false;
    // }
    if ($graphid = add_graph($name, $width, $height, $ymin_type, $ymax_type, $yaxismin, $yaxismax, $ymin_itemid, $ymax_itemid, $showworkperiod, $showtriggers, $graphtype, $legend, $graph3d, $percent_left, $percent_right, $templateid)) {
        $result = true;
        foreach ($gitems as $gitem) {
            if (!($result = add_item_to_graph($graphid, $gitem['itemid'], $gitem['color'], $gitem['drawtype'], $gitem['sortorder'], $gitem['yaxisside'], $gitem['calc_fnc'], $gitem['type'], $gitem['periods_cnt']))) {
                break;
            }
        }
    }
    if ($result) {
        info('Graph "' . $name . '" added to hosts ' . implode(',', $host_list));
        /* add graphs for child hosts */
        $tmp_hosts = get_hosts_by_graphid($graphid);
        $host = DBfetch($tmp_hosts);
        $chd_hosts = get_hosts_by_templateid($host['hostid']);
        while ($chd_host = DBfetch($chd_hosts)) {
            copy_graph_to_host($graphid, $chd_host['hostid'], false);
        }
    }
    if (!$result && $graphid) {
        delete_graph($graphid);
        $graphid = false;
    }
    return $graphid;
}
コード例 #6
0
ファイル: hosts.inc.php プロジェクト: rennhak/zabbix
function db_save_application($name, $hostid, $applicationid = null, $templateid = 0)
{
    if (!is_string($name)) {
        error("Incorrect parameters for 'db_save_application'");
        return false;
    }
    if (is_null($applicationid)) {
        $result = DBselect('SELECT * FROM applications WHERE name=' . zbx_dbstr($name) . ' AND hostid=' . $hostid);
    } else {
        $result = DBselect('SELECT * ' . ' FROM applications ' . ' WHERE name=' . zbx_dbstr($name) . ' AND hostid=' . $hostid . ' AND applicationid<>' . $applicationid);
    }
    $db_app = DBfetch($result);
    if ($db_app && $templateid == 0) {
        error('Application "' . $name . '" already exists');
        return false;
    }
    if ($db_app && $applicationid != null) {
        // delete old application with same name
        delete_application($db_app["applicationid"]);
    }
    if ($db_app && $applicationid == null) {
        // if found application with same name update them, adding not needed
        $applicationid = $db_app["applicationid"];
    }
    $host = get_host_by_hostid($hostid);
    if (is_null($applicationid)) {
        $applicationid_new = get_dbid('applications', 'applicationid');
        $sql = 'INSERT INTO applications (applicationid,name,hostid,templateid) ' . " VALUES ({$applicationid_new}," . zbx_dbstr($name) . ",{$hostid},{$templateid})";
        if ($result = DBexecute($sql)) {
            info("Added new application " . $host["host"] . ":{$name}");
        }
    } else {
        $old_app = get_application_by_applicationid($applicationid);
        if ($result = DBexecute('UPDATE applications ' . ' SET name=' . zbx_dbstr($name) . ',hostid=' . $hostid . ',templateid=' . $templateid . ' WHERE applicationid=' . $applicationid)) {
            info("Updated application " . $host["host"] . ":" . $old_app["name"]);
        }
    }
    if (!$result) {
        return $result;
    }
    if (is_null($applicationid)) {
        // create application for childs
        $applicationid = $applicationid_new;
        $db_childs = get_hosts_by_templateid($hostid);
        while ($db_child = DBfetch($db_childs)) {
            // recursion
            $result = add_application($name, $db_child["hostid"], $applicationid);
            if (!$result) {
                break;
            }
        }
    } else {
        $db_applications = get_applications_by_templateid($applicationid);
        while ($db_app = DBfetch($db_applications)) {
            // recursion
            $result = update_application($db_app["applicationid"], $name, $db_app["hostid"], $applicationid);
            if (!$result) {
                break;
            }
        }
    }
    if ($result) {
        return $applicationid;
    }
    if ($templateid == 0) {
        delete_application($applicationid);
    }
    return false;
}
コード例 #7
0
ファイル: items.inc.php プロジェクト: rennhak/zabbix
function add_item($item)
{
    /*
    		$item = array('description','key','hostid','delay','history','status','type',
    		'snmp_community','snmp_oid','value_type','trapper_hosts','snmp_port','units','multiplier','delta',
    		'snmpv3_securityname','snmpv3_securitylevel','snmpv3_authpassphrase','snmpv3_privpassphrase',
    		'formula','trends','logtimefmt','valuemapid','delay_flex','params','ipmi_sensor','applications','templateid');
    //*/
    $item_db_fields = array('description' => null, 'key_' => null, 'hostid' => null, 'delay' => 60, 'history' => 7, 'status' => ITEM_STATUS_ACTIVE, 'type' => ITEM_TYPE_ZABBIX, 'snmp_community' => '', 'snmp_oid' => '', 'value_type' => ITEM_VALUE_TYPE_STR, 'data_type' => ITEM_DATA_TYPE_DECIMAL, 'trapper_hosts' => 'localhost', 'snmp_port' => 161, 'units' => '', 'multiplier' => 0, 'delta' => 0, 'snmpv3_securityname' => '', 'snmpv3_securitylevel' => 0, 'snmpv3_authpassphrase' => '', 'snmpv3_privpassphrase' => '', 'formula' => 0, 'trends' => 365, 'logtimefmt' => '', 'valuemapid' => 0, 'delay_flex' => '', 'params' => '', 'ipmi_sensor' => '', 'applications' => array(), 'templateid' => 0);
    if (!check_db_fields($item_db_fields, $item)) {
        error('Incorrect arguments pasted to function [add_item]');
        return false;
    }
    $host = get_host_by_hostid($item['hostid']);
    if (($i = array_search(0, $item['applications'])) !== FALSE) {
        unset($item['applications'][$i]);
    }
    if (!eregi('^' . ZBX_EREG_ITEM_KEY_FORMAT . '$', $item['key_'])) {
        error("Incorrect key format 'key_name[param1,param2,...]'");
        return false;
    }
    if ($item['delay'] < 1) {
        error("Delay cannot be less than 1 second");
        return FALSE;
    }
    if ($item['delay_flex'] != '') {
        $arr_of_delay = explode(';', $item['delay_flex']);
        foreach ($arr_of_delay as $one_delay_flex) {
            $arr = explode('/', $one_delay_flex);
            if ($arr[0] < 1) {
                error('Delay cannot be less than 1 second ');
                return FALSE;
            }
        }
    }
    if ($item['snmp_port'] < 1 || $item['snmp_port'] > 65535) {
        error('Invalid SNMP port');
        return FALSE;
    }
    if ($item['value_type'] == ITEM_VALUE_TYPE_STR) {
        $item['delta'] = 0;
    }
    if ($item['value_type'] != ITEM_VALUE_TYPE_UINT64) {
        $item['data_type'] = 0;
    }
    if ($item['type'] == ITEM_TYPE_AGGREGATE && $item['value_type'] != ITEM_VALUE_TYPE_FLOAT) {
        error('Value type must be Float for aggregate items');
        return FALSE;
    }
    if ($item['type'] == ITEM_TYPE_AGGREGATE) {
        /* grpfunc('group','key','itemfunc','numeric param') */
        //			if(eregi('^((.)*)(\(\'((.)*)\'\,\'((.)*)\'\,\'((.)*)\'\,\'([0-9]+)\'\))$', $key, $arr))
        if (eregi('^((.)*)(\\[\\"((.)*)\\"\\,\\"((.)*)\\"\\,\\"((.)*)\\"\\,\\"([0-9]+)\\"\\])$', $item['key_'], $arr)) {
            $g = $arr[1];
            if (!str_in_array($g, array("grpmax", "grpmin", "grpsum", "grpavg"))) {
                error("Group function [{$g}] is not one of [grpmax,grpmin,grpsum,grpavg]");
                return FALSE;
            }
            // Group
            $g = $arr[4];
            // Key
            $g = $arr[6];
            // Item function
            $g = $arr[8];
            if (!str_in_array($g, array('last', 'min', 'max', 'avg', 'sum', 'count'))) {
                error('Item function [' . $g . '] is not one of [last, min, max, avg, sum,count]');
                return FALSE;
            }
            // Parameter
            $g = $arr[10];
        } else {
            error('Key does not match grpfunc["group","key","itemfunc","numeric param")');
            return FALSE;
        }
    }
    $db_item = DBfetch(DBselect('SELECT itemid,hostid ' . ' FROM items ' . ' WHERE hostid=' . $item['hostid'] . ' AND key_=' . zbx_dbstr($item['key_'])));
    if ($db_item && $item['templateid'] == 0) {
        error('An item with the Key [' . $item['key_'] . '] already exists for host [' . $host['host'] . ']. The key must be unique.');
        return FALSE;
    } else {
        if ($db_item && $item['templateid'] != 0) {
            $item['hostid'] = $db_item['hostid'];
            $item['applications'] = get_same_applications_for_host($item['applications'], $db_item['hostid']);
            $result = update_item($db_item['itemid'], $item);
            return $result;
        }
    }
    // first add mother item
    $itemid = get_dbid('items', 'itemid');
    $result = DBexecute('INSERT INTO items ' . ' (itemid,description,key_,hostid,delay,history,nextcheck,status,type,' . 'snmp_community,snmp_oid,value_type,data_type,trapper_hosts,' . 'snmp_port,units,multiplier,' . 'delta,snmpv3_securityname,snmpv3_securitylevel,snmpv3_authpassphrase,' . 'snmpv3_privpassphrase,formula,trends,logtimefmt,valuemapid,' . 'delay_flex,params,ipmi_sensor,templateid)' . ' VALUES (' . $itemid . ',' . zbx_dbstr($item['description']) . ',' . zbx_dbstr($item['key_']) . ',' . $item['hostid'] . ',' . $item['delay'] . ',' . $item['history'] . ',0,' . $item['status'] . ',' . $item['type'] . ',' . zbx_dbstr($item['snmp_community']) . ',' . zbx_dbstr($item['snmp_oid']) . ',' . $item['value_type'] . ',' . $item['data_type'] . ',' . zbx_dbstr($item['trapper_hosts']) . ',' . $item['snmp_port'] . ',' . zbx_dbstr($item['units']) . ',' . $item['multiplier'] . ',' . $item['delta'] . ',' . zbx_dbstr($item['snmpv3_securityname']) . ',' . $item['snmpv3_securitylevel'] . ',' . zbx_dbstr($item['snmpv3_authpassphrase']) . ',' . zbx_dbstr($item['snmpv3_privpassphrase']) . ',' . zbx_dbstr($item['formula']) . ',' . $item['trends'] . ',' . zbx_dbstr($item['logtimefmt']) . ',' . $item['valuemapid'] . ',' . zbx_dbstr($item['delay_flex']) . ',' . zbx_dbstr($item['params']) . ',' . zbx_dbstr($item['ipmi_sensor']) . ',' . $item['templateid'] . ')');
    if ($result) {
        add_audit_ext(AUDIT_ACTION_ADD, AUDIT_RESOURCE_ITEM, $itemid, $item['description'], NULL, NULL, NULL);
    } else {
        return $result;
    }
    foreach ($item['applications'] as $key => $appid) {
        $itemappid = get_dbid('items_applications', 'itemappid');
        DBexecute('INSERT INTO items_applications (itemappid,itemid,applicationid) VALUES(' . $itemappid . ',' . $itemid . ',' . $appid . ')');
    }
    info('Added new item ' . $host['host'] . ':' . $item['key_']);
    // add items to child hosts
    $db_hosts = get_hosts_by_templateid($host['hostid']);
    while ($db_host = DBfetch($db_hosts)) {
        // recursion
        $item['hostid'] = $db_host['hostid'];
        $item['applications'] = get_same_applications_for_host($applications, $db_host['hostid']);
        $item['templateid'] = $itemid;
        $result = add_item($item);
        if (!$result) {
            break;
        }
    }
    if ($result) {
        return $itemid;
    }
    if ($item['templateid'] == 0) {
        delete_item($itemid);
    }
    return $result;
}
コード例 #8
0
function add_item($item)
{
    $item_db_fields = array('description' => null, 'key_' => null, 'hostid' => null, 'delay' => 60, 'history' => 7, 'status' => ITEM_STATUS_ACTIVE, 'type' => ITEM_TYPE_ZABBIX, 'snmp_community' => '', 'snmp_oid' => '', 'value_type' => ITEM_VALUE_TYPE_STR, 'data_type' => ITEM_DATA_TYPE_DECIMAL, 'trapper_hosts' => '', 'snmp_port' => 161, 'units' => '', 'multiplier' => 0, 'delta' => 0, 'snmpv3_securityname' => '', 'snmpv3_securitylevel' => 0, 'snmpv3_authpassphrase' => '', 'snmpv3_privpassphrase' => '', 'formula' => 0, 'trends' => 365, 'logtimefmt' => '', 'valuemapid' => 0, 'delay_flex' => '', 'authtype' => 0, 'username' => '', 'password' => '', 'publickey' => '', 'privatekey' => '', 'params' => '', 'ipmi_sensor' => '', 'applications' => array(), 'templateid' => 0);
    if (!check_db_fields($item_db_fields, $item)) {
        error(S_INCORRECT_ARGUMENTS_PASSED_TO_FUNCTION . SPACE . '[add_item]');
        return false;
    }
    $host = get_host_by_hostid($item['hostid']);
    if (!$host) {
        return false;
    }
    if (($i = array_search(0, $item['applications'])) !== FALSE) {
        unset($item['applications'][$i]);
    }
    if (!preg_match('/^' . ZBX_PREG_ITEM_KEY_FORMAT . '$/u', $item['key_'])) {
        error(S_INCORRECT_KEY_FORMAT . SPACE . "'key_name[param1,param2,...]'");
        return false;
    }
    if ($item['type'] == ITEM_TYPE_DB_MONITOR && $item['key_'] == 'db.odbc.select[<unique short description>]' || $item['type'] == ITEM_TYPE_SSH && $item['key_'] == 'ssh.run[<unique short description>,<ip>,<port>,<encoding>]' || $item['type'] == ITEM_TYPE_TELNET && $item['key_'] == 'telnet.run[<unique short description>,<ip>,<port>,<encoding>]') {
        error(S_ITEMS_CHECK_KEY_DEFAULT_EXAMPLE_PASSED);
        return false;
    }
    $res = calculate_item_nextcheck(0, $item['type'], $item['delay'], $item['delay_flex'], time());
    if ($res['delay'] == SEC_PER_YEAR && $item['type'] != ITEM_TYPE_ZABBIX_ACTIVE && $item['type'] != ITEM_TYPE_TRAPPER) {
        error(S_ITEM_WILL_NOT_BE_REFRESHED_PLEASE_ENTER_A_CORRECT_UPDATE_INTERVAL);
        return FALSE;
    }
    if (($item['snmp_port'] < 1 || $item['snmp_port'] > 65535) && in_array($item['type'], array(ITEM_TYPE_SNMPV1, ITEM_TYPE_SNMPV2C, ITEM_TYPE_SNMPV3))) {
        error(S_INVALID_SNMP_PORT);
        return FALSE;
    }
    if (preg_match('/^(log|logrt|eventlog)\\[/', $item['key_']) && $item['value_type'] != ITEM_VALUE_TYPE_LOG) {
        error(S_TYPE_INFORMATION_BUST_LOG_FOR_LOG_KEY);
        return FALSE;
    }
    if ($item['value_type'] == ITEM_VALUE_TYPE_STR) {
        $item['delta'] = 0;
    }
    if ($item['value_type'] != ITEM_VALUE_TYPE_UINT64) {
        $item['data_type'] = 0;
    }
    if ($item['type'] == ITEM_TYPE_AGGREGATE && $item['value_type'] != ITEM_VALUE_TYPE_FLOAT) {
        error(S_VALUE_TYPE_MUST_FLOAT_FOR_AGGREGATE_ITEMS);
        return FALSE;
    }
    if ($item['type'] == ITEM_TYPE_AGGREGATE) {
        /* grpfunc['group','key','itemfunc','numeric param'] */
        if (preg_match('/^((.)*)(\\[\\"((.)*)\\"\\,\\"((.)*)\\"\\,\\"((.)*)\\"\\,\\"([0-9]+)\\"\\])$/i', $item['key_'], $arr)) {
            $g = $arr[1];
            if (!str_in_array($g, array("grpmax", "grpmin", "grpsum", "grpavg"))) {
                error(S_GROUP_FUNCTION . SPACE . "[{$g}]" . SPACE . S_IS_NOT_ONE_OF . SPACE . "[grpmax, grpmin, grpsum, grpavg]");
                return FALSE;
            }
            // Group
            $g = $arr[4];
            // Key
            $g = $arr[6];
            // Item function
            $g = $arr[8];
            if (!str_in_array($g, array('last', 'min', 'max', 'avg', 'sum', 'count'))) {
                error(S_ITEM_FUNCTION . SPACE . '[' . $g . ']' . SPACE . S_IS_NOT_ONE_OF . SPACE . '[last, min, max, avg, sum, count]');
                return FALSE;
            }
            // Parameter
            $g = $arr[10];
        } else {
            error(S_KEY_DOES_NOT_MATCH . SPACE . 'grpfunc["group","key","itemfunc","numeric param"]');
            return FALSE;
        }
    }
    $sql = 'SELECT itemid, hostid, templateid ' . ' FROM items ' . ' WHERE hostid=' . $item['hostid'] . ' AND key_=' . zbx_dbstr($item['key_']);
    $db_item = DBfetch(DBselect($sql));
    if ($db_item && ($item['templateid'] == 0 || $db_item['templateid'] != 0 && $item['templateid'] != 0 && $item['templateid'] != $db_item['templateid'])) {
        error(S_AN_ITEM_WITH_THE_KEY . SPACE . '[' . $item['key_'] . ']' . SPACE . S_ALREADY_EXISTS_FOR_HOST_SMALL . SPACE . '[' . $host['host'] . '].' . SPACE . S_THE_KEY_MUST_BE_UNIQUE);
        return FALSE;
    } else {
        if ($db_item && $item['templateid'] != 0) {
            $item['hostid'] = $db_item['hostid'];
            $item['applications'] = get_same_applications_for_host($item['applications'], $db_item['hostid']);
            $result = update_item($db_item['itemid'], $item);
            return $result;
        }
    }
    //validating item key
    $checkResult = check_item_key($item['key_']);
    if (!$checkResult['valid']) {
        error(S_ERROR_IN_ITEM_KEY . SPACE . $checkResult['description']);
        return false;
    }
    // first add mother item
    $itemid = get_dbid('items', 'itemid');
    $result = DBexecute('INSERT INTO items ' . ' (itemid,description,key_,hostid,delay,history,status,type,' . 'snmp_community,snmp_oid,value_type,data_type,trapper_hosts,' . 'snmp_port,units,multiplier,' . 'delta,snmpv3_securityname,snmpv3_securitylevel,snmpv3_authpassphrase,' . 'snmpv3_privpassphrase,formula,trends,logtimefmt,valuemapid,' . 'delay_flex,params,ipmi_sensor,templateid,authtype,username,password,publickey,privatekey)' . ' VALUES (' . $itemid . ',' . zbx_dbstr($item['description']) . ',' . zbx_dbstr($item['key_']) . ',' . (!$item['hostid'] ? '0' : $item['hostid']) . ',' . (!$item['delay'] ? '0' : $item['delay']) . ',' . (!$item['history'] ? '0' : $item['history']) . ',' . (!$item['status'] ? '0' : $item['status']) . ',' . (!$item['type'] ? '0' : $item['type']) . ',' . zbx_dbstr($item['snmp_community']) . ',' . zbx_dbstr($item['snmp_oid']) . ',' . (!$item['value_type'] ? '0' : $item['value_type']) . ',' . (!$item['data_type'] ? '0' : $item['data_type']) . ',' . zbx_dbstr($item['trapper_hosts']) . ',' . $item['snmp_port'] . ',' . zbx_dbstr($item['units']) . ',' . (!$item['multiplier'] ? '0' : $item['multiplier']) . ',' . intval($item['delta']) . ',' . zbx_dbstr($item['snmpv3_securityname']) . ',' . intval($item['snmpv3_securitylevel']) . ',' . zbx_dbstr($item['snmpv3_authpassphrase']) . ',' . zbx_dbstr($item['snmpv3_privpassphrase']) . ',' . zbx_dbstr($item['formula']) . ',' . (!$item['trends'] ? '0' : $item['trends']) . ',' . zbx_dbstr($item['logtimefmt']) . ',' . (!$item['valuemapid'] ? '0' : $item['valuemapid']) . ',' . zbx_dbstr($item['delay_flex']) . ',' . zbx_dbstr($item['params']) . ',' . zbx_dbstr($item['ipmi_sensor']) . ',' . $item['templateid'] . ',' . intval($item['authtype']) . ',' . zbx_dbstr($item['username']) . ',' . zbx_dbstr($item['password']) . ',' . zbx_dbstr($item['publickey']) . ',' . zbx_dbstr($item['privatekey']) . ')');
    if ($result) {
        add_audit_ext(AUDIT_ACTION_ADD, AUDIT_RESOURCE_ITEM, $itemid, $host['host'] . ':' . $item['description'], NULL, NULL, NULL);
    } else {
        return $result;
    }
    foreach ($item['applications'] as $key => $appid) {
        $itemappid = get_dbid('items_applications', 'itemappid');
        DBexecute('INSERT INTO items_applications (itemappid,itemid,applicationid) VALUES(' . $itemappid . ',' . $itemid . ',' . $appid . ')');
    }
    info(S_ADDED_NEW_ITEM . SPACE . $host['host'] . ':' . $item['key_']);
    // add items to child hosts
    $db_hosts = get_hosts_by_templateid($host['hostid']);
    while ($db_host = DBfetch($db_hosts)) {
        // recursion
        $item['hostid'] = $db_host['hostid'];
        $item['applications'] = get_same_applications_for_host($item['applications'], $db_host['hostid']);
        $item['templateid'] = $itemid;
        $result = add_item($item);
        if (!$result) {
            break;
        }
    }
    if ($result) {
        return $itemid;
    }
    if ($item['templateid'] == 0) {
        delete_item($itemid);
    }
    return $result;
}
コード例 #9
0
function update_trigger($triggerid, $expression = NULL, $description = NULL, $type = NULL, $priority = NULL, $status = NULL, $comments = NULL, $url = NULL, $deps = array(), $templateid = 0)
{
    $trigger = get_trigger_by_triggerid($triggerid);
    $trig_hosts = get_hosts_by_triggerid($triggerid);
    $trig_host = DBfetch($trig_hosts);
    $event_to_unknown = false;
    // Restore expression
    if (is_null($expression)) {
        $expression = explode_exp($trigger['expression'], 0);
        $expr = new CTriggerExpression(array('expression' => $expression));
    } else {
        $expr = new CTriggerExpression(array('expression' => $expression));
        $event_to_unknown = empty($expr->errors) && $expression != explode_exp($trigger['expression'], 0);
    }
    if (!empty($expr->errors)) {
        foreach ($expr->errors as $error) {
            error($error);
        }
        return false;
    }
    if (!is_null($deps) && !validate_trigger_dependency($expression, $deps)) {
        error(S_WRONG_DEPENDENCY_ERROR);
        return false;
    }
    if (is_null($description)) {
        $description = $trigger['description'];
    }
    if (CTrigger::exists(array('description' => $description, 'expression' => $expression))) {
        $host = reset($expr->data['hosts']);
        $options = array('filter' => array('description' => $description, 'host' => $host), 'output' => API_OUTPUT_EXTEND, 'editable' => 1);
        $triggers_exist = CTrigger::get($options);
        $trigger_exist = false;
        foreach ($triggers_exist as $tnum => $tr) {
            $tmp_exp = explode_exp($tr['expression'], false);
            if (strcmp($tmp_exp, $expression) == 0) {
                $trigger_exist = $tr;
                break;
            }
        }
        if ($trigger_exist && $trigger_exist['triggerid'] != $trigger['triggerid']) {
            error('Trigger with name "' . $trigger['description'] . '" and expression "' . $expression . '" already exists.');
            return false;
        } else {
            if (!$trigger_exist) {
                error('No Permissions');
                return false;
            }
        }
    }
    $exp_hosts = $expr->data['hosts'];
    if (!empty($exp_hosts)) {
        $chd_hosts = get_hosts_by_templateid($trig_host['hostid']);
        if (DBfetch($chd_hosts)) {
            $expHostName = reset($exp_hosts);
            $db_chd_triggers = get_triggers_by_templateid($triggerid);
            while ($db_chd_trigger = DBfetch($db_chd_triggers)) {
                $chd_trig_hosts = get_hosts_by_triggerid($db_chd_trigger['triggerid']);
                $chd_trig_host = DBfetch($chd_trig_hosts);
                $newexpression = str_replace('{' . $expHostName . ':', '{' . $chd_trig_host['host'] . ':', $expression);
                // recursion
                update_trigger($db_chd_trigger['triggerid'], $newexpression, $description, $type, $priority, $status, $comments, $url, is_null($deps) ? null : replace_template_dependencies($deps, $chd_trig_host['hostid']), $triggerid);
            }
        }
    }
    $result = delete_function_by_triggerid($triggerid);
    if (!$result) {
        return $result;
    }
    $expression = implode_exp($expression, $triggerid);
    if (is_null($expression)) {
        return false;
    }
    $update_values = array();
    if (!is_null($expression)) {
        $update_values['expression'] = $expression;
    }
    if (!is_null($description)) {
        $update_values['description'] = $description;
    }
    if (!is_null($type)) {
        $update_values['type'] = $type;
    }
    if (!is_null($priority)) {
        $update_values['priority'] = $priority;
    }
    if (!is_null($status)) {
        $update_values['status'] = $status;
    }
    if (!is_null($comments)) {
        $update_values['comments'] = $comments;
    }
    if (!is_null($url)) {
        $update_values['url'] = $url;
    }
    if (!is_null($templateid)) {
        $update_values['templateid'] = $templateid;
    }
    if ($event_to_unknown || !is_null($status) && $status != TRIGGER_STATUS_ENABLED) {
        if ($trigger['value'] != TRIGGER_VALUE_UNKNOWN) {
            addEvent($triggerid, TRIGGER_VALUE_UNKNOWN);
            $update_values['value'] = TRIGGER_VALUE_UNKNOWN;
            $update_values['lastchange'] = time();
        }
    }
    DB::update('triggers', array('values' => $update_values, 'where' => array('triggerid=' . $triggerid)));
    if (!is_null($deps)) {
        delete_dependencies_by_triggerid($triggerid);
        foreach ($deps as $id => $triggerid_up) {
            if (!($result2 = add_trigger_dependency($triggerid, $triggerid_up))) {
                error(S_INCORRECT_DEPENDENCY . ' [' . expand_trigger_description($triggerid_up) . ']');
            }
            $result &= $result2;
        }
    }
    if ($result) {
        $trig_hosts = get_hosts_by_triggerid($triggerid);
        $msg = S_TRIGGER . SPACE . '"' . $trigger['description'] . '"' . SPACE . S_UPDATED_SMALL;
        $trig_host = DBfetch($trig_hosts);
        if ($trig_host) {
            $msg .= SPACE . S_FOR_HOST_SMALL . SPACE . '"' . $trig_host['host'] . '"';
        }
        info($msg);
    }
    if ($result) {
        $trigger_new = get_trigger_by_triggerid($triggerid);
        add_audit_ext(AUDIT_ACTION_UPDATE, AUDIT_RESOURCE_TRIGGER, $triggerid, $trig_host['host'] . ':' . $trigger['description'], 'triggers', $trigger, $trigger_new);
    }
    $result = $result ? $triggerid : $result;
    return $result;
}