[racktables-users] Re: Report

  • From: Bruce Hoenig <ibmsorcerer@xxxxxxxxxx>
  • To: racktables-users@xxxxxxxxxxxxx
  • Date: Tue, 24 Jan 2012 19:07:13 -0800

Uncertain if this would even help. I did this as a quick and dirty to be 
able to copy/paste into Excel.
I only have a limited number of servers, switches, pdus and disk arrays.
I know I probably don't use anywhere near the capabilities of RackTables, 
but it sure makes a good way of tracking equipment.
This was my first attempt at php (test-report), so it was convenient that 
all the functions I needed were already available to do a lot of what I 
wanted.
One of the things I use this new tab for is to quickly browse for missing 
information.

My second attempt was to connect the system to LDAP and I didn't really 
understand all the associated problems with making the connection and 
having it authenticate properly. So I wrote this to help me understand 
what was going on while it authenticated.
 If you use the second program (test-ldap) remember to keep the 
db_password lines commented out as it will print your associated binding 
password out in clear text.

Thanks for the quite useful program!!

Bruce Hoenig
QA/Dev System Administrator
IBM Tivoli

+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
test-report.php

<?php

$tabhandler['reports']['table'] = 'renderTestReport'; // register a report 
rendering function
$tab['reports']['table'] = 'Table'; // title of the report tab

function renderTestReport()
{
        // fill the HW type stat array
        $stat = array();
        $total = 0;
        $a = array('{$typeid_4}', '{$typeid_5}' , '{$typeid_8}' , 
'{$typeid_2}' );
        foreach ($a as $b)
        {
        foreach (scanRealmByText ('object', $b) as $switch)
                $attributes = getAttrValues ($switch['id']);
        $hdr = $switch['id'];
        myrenderTable ($b,$hdr);
        }
}

function myrenderTable ($my_id,$hdr)
{
        $filter = "$my_id";
        echo "<table border=5 cellspacing=0 cellpadding=3 
width='100%'>\n";
        echo "<tr><td>Common name</td><td>Object type</td><td>Asset 
tag</td><td>Visible label</td>";
        myrenderHeaderObject ($hdr);
        echo "</tr>";
        foreach (scanRealmByText ('object', $filter) as $switch)
        {
                $attributes = getAttrValues ($switch['id']);
                if (isset ($attributes[2]))
                {
                        $attr = $attributes[2];
                        if (! isset ($stat[$attr['key']]))
                                $stat[$attr['key']] = array
                                (
                                        'value' => $attr['a_value'],
                                        'count' => 0,
                                );
                        ++$stat[$attr['key']]['count'];
                        ++$total;
                }
                myrenderRackObject ($switch['id']);
        }
        echo "</table>";
}

function myrenderHeaderObject ($object_id)
{
        foreach (getAttrValues ($object_id) as $record)
                echo "<td> ${record['name']} </td>";
}

function myrenderRackObject ($object_id)
{
        global $nextorder, $aac, $virtual_obj_types;
        $info = spotEntity ('object', $object_id);
        // Main layout starts.
        echo "<tr><td>${info['name']}</td>";
        echo "<td>" . decodeObjectType ($info['objtype_id'], 'o') . 
'</td>';
        echo "<td>${info['asset_no']}</td>";
        echo "<td>${info['label']}</td>";

        foreach (getAttrValues ($object_id) as $record)
           echo "" . myformatAttributeValue ($record) . "";
        myprintTagTRs
        (
                $info,
                makeHref
                (
                        array
                        (
                                'cfe' => '{$typeid_' . $info['objtype_id'] 
. '}',
                        )
                )."&"
        );
}

function myformatAttributeValue ($record)
{
                $href = makeHref
                (
                        array
                        (
                                'cfe' => '{$attr_' . $record['id'] . '_' . 
$record['key'] . '}',
                        )
                );
                $result = "<td>" . $record['a_value'] . "</td>";
                return $result;
}

function myprintTagTRs ($cell, $baseurl = '')
{
        if (getConfigVar ('SHOW_EXPLICIT_TAGS') == 'yes' and count 
($cell['etags']))
        {
                echo "<th class=tagchain>Explicit tags:</th><td 
class=tagchain>";
                echo serializeTags ($cell['etags'], $baseurl) . 
"</td></tr>";
        }
        if (getConfigVar ('SHOW_IMPLICIT_TAGS') == 'yes' and count 
($cell['itags']))
        {
                echo "<th class=tagchain>Implicit tags:</th><td 
class=tagchain>";
                echo serializeTags ($cell['itags'], $baseurl) . 
"</td></tr>";
        }
        if (getConfigVar ('SHOW_AUTOMATIC_TAGS') == 'yes' and count 
($cell['atags']))
        {
                echo "<th class=tagchain>Automatic tags:</th><td 
class=tagchain>";
                echo serializeTags ($cell['atags']) . "</td></tr>";
        }
}


?>

+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
test-ldap.php
<?php

$tabhandler['reports']['ldap'] = 'renderLDAPReport'; // register a report 
rendering function
$tab['reports']['ldap'] = 'LDAP'; // title of the report tab

function renderLDAPReport()
{
        myrenderLDAPTable ($LDAP_options,$hdr);
}

function myrenderLDAPTable ($myLDAP_options,$hdr)
{
        global  $LDAP_options, $pdo_dsn, $db_username, $db_password, 
$auth_user_name, $username,
                $remote_username, $user_auth_src, $ldap_dispname, 
$ldaprdn, $LDAP_defaults,
                $auto_tags;

        echo "<table border=5 cellspacing=0 cellpadding=3 width='40%'>\n";
        echo "<tr><td>Name</td><td>Value</td>";
        echo "</tr><tr>";
    echo "<td>pdo_dsn</td><td>".$pdo_dsn."</td>";
        echo "</tr><tr>";
    echo "<td>db_username</td><td>".$db_username."</td>";
        echo "</tr><tr>";
//    echo "<td>db_password</td><td>".$db_password."</td>";
//        echo "</tr><tr>";
    echo "<td>server</td><td>".$LDAP_options['server']."</td>";
        echo "</tr><tr>";
    echo "<td>domain</td><td>".$LDAP_options['domain']."</td>";
        echo "</tr><tr>";
    echo "<td>search_attr</td><td>".$LDAP_options['search_attr']."</td>";
        echo "</tr><tr>";
    echo "<td>search_dn</td><td>".$LDAP_options['search_dn']."</td>";
        echo "</tr><tr>";
    echo 
"<td>displayname_attrs</td><td>".$LDAP_options['displayname_attrs']."</td>";
        echo "</tr><tr>";
    echo "<td>remote_username</td><td>".$remote_username."</td>";
        echo "</tr><tr>";
    echo "<td>user_auth_src</td><td>$user_auth_src</td>";
        echo "</tr><tr>";
    echo 
"<td>group_filter</td><td>".$LDAP_defaults['group_filter']."</td>";
        echo "</tr>";
        echo "</table>";

// using ldap bind
$ldaprdn  = 'UserID';                                      // ldap rdn or 
dn
$ldappass = 'Password';                              // associated 
password
$ldapport = 389;                                        // ldap server's 
port number
$ldaphost  = $LDAP_options['server'];                    // ldap servers

    echo "<p>ldaprdn: {$ldaprdn}";
    echo "<br>ldaphost: {$ldaphost}";
    echo "<br>ldapport: {$ldapport}</p>";

// connect to ldap server
$ldapconn = ldap_connect ($LDAP_options['server'])
    or die("Could not connect to LDAP server: {$LDAP_options['server']}");

if(!$ldapconn){
      echo "<p>Connected to LDAP server: {$LDAP_options['server']}</p>";
      die("<p>Could not connect to {$LDAP_options['server']}</p>");
 }
else {
      echo "<p>Connected to LDAP server: {$LDAP_options['server']}</p>";
 }

if ($ldapconn) {
    // binding to ldap server
    $ldapbind = ldap_bind($ldapconn, $ldaprdn, $ldappass);
    // verify binding
    echo "<p>ldaphost: {$ldaphost}";
    echo "<br>ldapport: {$ldapport}";
    echo "<br>ldaprdn: {$ldaprdn}";
    echo "<br>ldapconn: {$ldapconn}";
    echo "<br>ldapbind: {$ldapbind}</p>";
    if ($ldapbind) {
        echo "<p>LDAP bind successful...</p>";
    } else {
        echo "<p>LDAP bind failed...</p>";
    }
}

//      phpinfo();
}

function myrenderLDAPHeaderObject ($object_id)
{
        foreach (getAttrValues ($object_id) as $record)
                echo "<td> ${record['name']} </td>";
}

function myrenderLDAPObject ($object_id)
{
        global $nextorder, $aac, $virtual_obj_types;
        $info = spotEntity ('object', $object_id);
        // Main layout starts.
        echo "<tr><td>${info['name']}</td>";
        echo "<td>" . decodeObjectType ($info['objtype_id'], 'o') . 
'</td>';
        echo "<td>${info['asset_no']}</td>";
        echo "<td>${info['label']}</td>";

        foreach (getAttrValues ($object_id) as $record)
           echo "" . myformatAttributeValue ($record) . "";
        myprintTagTRs
        (
                $info,
                makeHref
                (
                        array
                        (
                                'cfe' => '{$typeid_' . $info['objtype_id'] 
. '}',
                        )
                )."&"
        );
}

function myformatLDAPAttributeValue ($record)
{
                $href = makeHref
                (
                        array
                        (
                                'cfe' => '{$attr_' . $record['id'] . '_' . 
$record['key'] . '}',
                        )
                );
                $result = "<td>" . $record['a_value'] . "</td>";
                return $result;
}

function myprintLDAPTagTRs ($cell, $baseurl = '')
{
        if (getConfigVar ('SHOW_EXPLICIT_TAGS') == 'yes' and count 
($cell['etags']))
        {
                echo "<th class=tagchain>Explicit tags:</th><td 
class=tagchain>";
                echo serializeTags ($cell['etags'], $baseurl) . 
"</td></tr>";
        }
        if (getConfigVar ('SHOW_IMPLICIT_TAGS') == 'yes' and count 
($cell['itags']))
        {
                echo "<th class=tagchain>Implicit tags:</th><td 
class=tagchain>";
                echo serializeTags ($cell['itags'], $baseurl) . 
"</td></tr>";
        }
        if (getConfigVar ('SHOW_AUTOMATIC_TAGS') == 'yes' and count 
($cell['atags']))
        {
                echo "<th class=tagchain>Automatic tags:</th><td 
class=tagchain>";
                echo serializeTags ($cell['atags']) . "</td></tr>";
        }
}


?>

Other related posts: