Example #1
0
function get_assets($conn, $id, $type, $host_types)
{
    $filters = array('where' => 'sensor_properties.has_nagios = 1', 'order_by' => 'priority desc');
    list($nagios_list, $nagios_total) = Av_sensor::get_list($conn, $filters);
    $sensor = NULL;
    $sensors = array();
    $type = strtolower($type);
    $id = strtoupper($id);
    // in_assets first
    $in_assets = is_in_assets($conn, $id, $type);
    //Host, sensor or server
    if (in_array($type, $host_types)) {
        $table = $type;
        if ($type == 'host') {
            $what = 'host_id';
            $table = 'host_ip';
        } else {
            $what = 'id';
        }
        $query = "SELECT INET6_NTOA(ip) AS ip FROM {$table} WHERE {$what} = UNHEX(?) LIMIT 1";
        $params = array($id);
        $rs = $conn->Execute($query, $params);
        if ($rs) {
            $ips = $rs->fields['ip'];
        }
        // Related sensors
        if ($type == 'host') {
            $sensors = Asset_host_sensors::get_sensors_by_id($conn, $id);
        } elseif ($type == 'sensor') {
            $sensors[$id] = $ips;
        } else {
            $s_id = Av_sensor::get_id_by_ip($conn, Util::get_default_admin_ip());
            $sensors[$s_id] = Util::get_default_admin_ip();
        }
    } elseif ($type == 'net') {
        $query = "SELECT ips FROM net WHERE id = UNHEX(?)";
        $params = array($id);
        $rs = $conn->Execute($query, $params);
        if ($rs) {
            $ips = $rs->fields['ips'];
        }
        // Related sensors
        $sensors = Asset_net_sensors::get_sensors_by_id($conn, $id);
    } elseif ($type == 'net_group' || $type == 'netgroup') {
        $query = "SELECT hex(ngr.net_id) as net_id, n.ips FROM net_group_reference AS ngr, net AS n\n            WHERE ngr.net_group_id = UNHEX(?) AND ngr.net_id = n.id";
        $net_ids = array($id);
        $params = $net_ids;
        $rs = $conn->Execute($query, $params);
        if ($rs) {
            $ipng = array();
            if (!$rs->EOF) {
                $net_ids = array();
            }
            while (!$rs->EOF) {
                $ipng[] = $rs->fields['ips'];
                $net_ids[] = $rs->fields['net_id'];
                $rs->MoveNext();
            }
            $ips = count($ipng) > 0 ? implode(",", $ipng) : "'0.0.0.0/0'";
            if (count($ipng) == 0) {
                $in_assets = 0;
            }
        }
        // Related sensors
        foreach ($net_ids as $net_id) {
            $_sensors_aux = Asset_net_sensors::get_sensors_by_id($conn, $net_id);
            foreach ($_sensors_aux as $sensor_id => $sensor_data) {
                $sensors[$sensor_id] = $sensor_data['ip'];
            }
        }
    } elseif ($type == 'host_group' || $type == 'hostgroup') {
        $query = "SELECT hex(hg.host_id) as host_id, INET6_NTOA(hi.ip) AS ip FROM host_group_reference hg, host_ip hi\n            WHERE hi.host_id=hg.host_id AND hg.host_group_id = UNHEX(?)";
        $host_ids = array($id);
        $params = $host_ids;
        $rs = $conn->Execute($query, $params);
        if ($rs) {
            $iphg = array();
            if (!$rs->EOF) {
                $host_ids = array();
            }
            while (!$rs->EOF) {
                $iphg[] = "'" . $rs->fields['ip'] . "'";
                $host_ids[] = $rs->fields['host_id'];
                $rs->MoveNext();
            }
            $ips = count($iphg) > 0 ? implode(',', $iphg) : "'0.0.0.0'";
            if (count($iphg) == 0) {
                $in_assets = 0;
            }
        }
        // Related sensors
        foreach ($host_ids as $host_id) {
            $_sensors_aux = Asset_host_sensors::get_sensors_by_id($conn, $host_id);
            foreach ($_sensors_aux as $sensor_id => $sensor_data) {
                $sensors[$sensor_id] = $sensor_data['ip'];
            }
        }
    }
    //Getting first Nagios sensor (By priority)
    if ($nagios_total > 0) {
        foreach ($nagios_list as $n_sensor_id => $n_sensor_data) {
            if (array_key_exists($n_sensor_id, $sensors)) {
                $sensor = $n_sensor_data['ip'];
                break;
            }
        }
    }
    return array($id, $sensor, $type, $ips, $in_assets);
}
Example #2
0
function get_assets($conn, $name, $type, $host_types)
{
    // in_assets first
    $in_assets = is_in_assets($conn, $name, $type);
    // Asset values
    $ips = $name;
    $what = "name";
    if (in_array($type, $host_types)) {
        if ($type == "host") {
            $what = "hostname";
        }
        $query = "select ip from {$type} where {$what} = ?";
        $params = array($name);
        if ($rs3 =& $conn->Execute($query, $params)) {
            $name = $rs3->fields["ip"];
            if ($rs3->EOF) {
                $in_assets = 0;
            }
        }
        // Related sensor
        $sensor = $name;
        if ($type == "host") {
            require_once 'classes/Host.inc';
            $sensors = Host::get_related_sensors($conn, $name, false);
            $sensor = $sensors[0] != "" ? $sensors[0] : $name;
        }
    } elseif ($type == "net") {
        $query = "select ips from net where name = ?";
        $params = array($name);
        if ($rs3 =& $conn->Execute($query, $params)) {
            $ips = $rs3->fields["ips"];
            if ($rs3->EOF) {
                $in_assets = 0;
            }
        }
        // related sensor
        require_once 'classes/Net.inc';
        $sensors = Net::get_related_sensors($conn, $name);
        $sensor = $sensors[0] != "" ? $sensors[0] : "";
    } elseif ($type == "host_group") {
        $query = "select host_ip from host_group_reference where host_group_name = ?";
        $params = array($name);
        if ($rs3 =& $conn->Execute($query, $params)) {
            $iphg = array();
            while (!$rs3->EOF) {
                $iphg[] = "'" . $rs3->fields["host_ip"] . "'";
                $rs3->MoveNext();
            }
            $ips = count($iphg) > 0 ? implode(",", $iphg) : "'0.0.0.0'";
            if (count($iphg) == 0) {
                $in_assets = 0;
            }
        }
        // related sensor{
        require_once 'classes/Host_group.inc';
        $sensors = Host_group::get_related_sensors($conn, $name);
        $sensor = $sensors[0] != "" ? $sensors[0] : $name;
    }
    return array($name, $sensor, $type, $ips, $what, $in_assets);
}