Example #1
0
function format_array($array = array())
{
    $text = '';
    foreach (array_keys($array) as $key) {
        // Make some data look pretty
        if ($key == 'ip_addr') {
            $array[$key] = ip_mangle($array[$key], 'dotted');
        } else {
            if ($key == 'ip_addr_start') {
                $array[$key] = ip_mangle($array[$key], 'dotted');
            } else {
                if ($key == 'ip_addr_end') {
                    $array[$key] = ip_mangle($array[$key], 'dotted');
                } else {
                    if ($key == 'ip_mask') {
                        $array[$key] = ip_mangle($array[$key]) . '  (/' . ip_mangle($array[$key], 'cidr') . ')';
                    } else {
                        if ($key == 'mac_addr') {
                            $array[$key] = mac_mangle($array[$key]);
                            if ($array[$key] == -1) {
                                $array[$key] = '';
                            }
                        } else {
                            if ($key == 'host_id') {
                                list($status, $rows, $host) = ona_find_host($array[$key]);
                                if ($host['id']) {
                                    $array[$key] = str_pad($array[$key], 20) . strtolower("({$host['fqdn']})");
                                }
                            } else {
                                if ($key == 'server_id') {
                                    list($status, $rows, $server) = ona_get_server_record(array('id' => $array[$key]));
                                    list($status, $rows, $host) = ona_find_host($server['host_id']);
                                    if ($host['id']) {
                                        $array[$key] = str_pad($array[$key], 20) . strtolower("({$host['fqdn']})");
                                    }
                                } else {
                                    if ($key == 'subnet_id') {
                                        list($status, $rows, $subnet) = ona_get_subnet_record(array('id' => $array[$key]));
                                        if ($subnet['id']) {
                                            $array[$key] = str_pad($array[$key], 20) . strtoupper("({$subnet['name']})");
                                        }
                                    } else {
                                        if ($key == 'domain_id' or $key == 'primary_dns_domain_id') {
                                            list($status, $rows, $domain) = ona_get_domain_record(array('id' => $array[$key]));
                                            $array[$key] = str_pad($array[$key], 20) . strtolower("({$domain['fqdn']})");
                                        } else {
                                            if ($key == 'interface_id') {
                                                list($status, $rows, $interface) = ona_get_interface_record(array('id' => $array[$key]));
                                                $array[$key] = str_pad($array[$key], 20) . '(' . ip_mangle($interface['ip_addr'], 'dotted') . ')';
                                            } else {
                                                if ($key == 'device_type_id') {
                                                    list($status, $rows, $devtype) = ona_get_device_type_record(array('id' => $array[$key]));
                                                    if ($devtype['id']) {
                                                        list($status, $rows, $model) = ona_get_model_record(array('id' => $devtype['model_id']));
                                                        list($status, $rows, $role) = ona_get_role_record(array('id' => $devtype['role_id']));
                                                        list($status, $rows, $manu) = ona_get_manufacturer_record(array('id' => $model['manufacturer_id']));
                                                        $array[$key] = str_pad($array[$key], 20) . "({$manu['name']}, {$model['name']} ({$role['name']}))";
                                                    }
                                                } else {
                                                    if ($key == 'custom_attribute_type_id') {
                                                        list($status, $rows, $ca) = ona_get_custom_attribute_type_record(array('id' => $array[$key]));
                                                        if ($ca['id']) {
                                                            $array[$key] = str_pad($array[$key], 20) . "({$ca['name']})";
                                                        }
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
        // Align columns
        if ($array[$key]) {
            $text .= str_pad("  {$key}", 30) . $array[$key] . "\n";
        }
    }
    // Return a nice string :)
    return $text;
}
Example #2
0
function dhcp_entry_display($options = "")
{
    // The important globals
    global $conf, $self, $onadb;
    // Version - UPDATE on every edit!
    $version = '1.00';
    printmsg("DEBUG => dhcp_entry_display({$options}) called", 3);
    // Parse incoming options string to an array
    $options = parse_options($options);
    // Return the usage summary if we need to
    if ($options['help'] or !$options['host'] and !$options['server'] and !$options['subnet']) {
        // NOTE: Help message lines should not exceed 80 characters for proper display on a console
        $self['error'] = 'ERROR => Insufficient parameters';
        return array(1, <<<EOM

dhcp_entry_display-v{$version}
Displays an dhcp_entry record from the database

  Synopsis: dhcp_entry_display [KEY=VALUE] ...

  Required:
    host=NAME[.DOMAIN] or id      hostname or id of the host to display
    OR
    subnet=NAME or id            description or id of the subnet to display
    OR
    server=NAME[.DOMAIN] or id    hostname or id of the server to display

  Notes:
    * DOMAIN will default to {$conf['dns_defaultdomain']} if not specified


EOM
);
    }
    if ($options['host']) {
        // Determine the host is valid
        list($status, $rows, $host) = ona_find_host($options['host']);
        if (!$host['id']) {
            printmsg("DEBUG => The host specified, {$options['host']}, does not exist!", 3);
            $self['error'] = "ERROR => The host specified, {$options['host']}, does not exist!";
            return array(2, $self['error'] . "\n");
        }
        $anchor = 'host';
        $desc = $host['FQDN'];
        $where = array('HOST_id' => $host['id']);
    } elseif ($options['subnet']) {
        // Determine the subnet is valid
        list($status, $rows, $subnet) = ona_find_subnet($options['subnet']);
        if (!$subnet['id']) {
            printmsg("DEBUG => The subnet specified, {$options['subnet']}, does not exist!", 3);
            $self['error'] = "ERROR => The subnet specified, {$options['subnet']}, does not exist!";
            return array(3, $self['error'] . "\n");
        }
        $anchor = 'subnet';
        $desc = "{$subnet['DESCRIPTION']} (" . ip_mangle($subnet['IP_ADDRESS']) . ")";
        $where = array('NETWORK_id' => $subnet['id']);
    } elseif ($options['server']) {
        // Determine the server is valid
        list($status, $rows, $host) = ona_find_host($options['server']);
        if (!$host['id']) {
            printmsg("DEBUG => The server specified, {$options['server']}, does not exist!", 3);
            $self['error'] = "ERROR => The server specified, {$options['server']}, does not exist!";
            return array(4, $self['error'] . "\n");
        }
        // Determine the host that was found is actually a server
        list($status, $rows, $server) = ona_get_server_record(array('HOST_id' => $host['id']));
        if (!$server['id']) {
            printmsg("DEBUG => The host specified, {$host['FQDN']}, is not a server!", 3);
            $self['error'] = "ERROR => The host specified, {$host['FQDN']}, is not a server!";
            return array(5, $self['error'] . "\n");
        }
        $anchor = 'server';
        $desc = $host['FQDN'];
        $where = array('SERVER_id' => $server['id']);
    }
    // Debugging
    printmsg("DEBUG => dhcp_entry_display(): Found {$anchor}: {$desc}", 3);
    // Build text to return
    $text = strtoupper($anchor) . " RECORD ({$desc})\n";
    // Display the record(s)
    $i = 0;
    do {
        list($status, $rows, $entry) = ona_get_dhcp_entry_record($where);
        if ($rows == 0) {
            $text .= "\nNO ASSOCIATED DHCP ENTRY RECORDS\n";
            break;
        }
        $i++;
        $text .= "\nASSOCIATED DHCP ENTRY RECORD ({$i} of {$rows})\n";
        $text .= format_array($entry);
    } while ($i < $rows);
    // Return the success notice
    return array(0, $text);
}