Search Host by Image
-
I tried to modify the fog 0.32 webgui to enable searching for hosts associated with a specified image.
I managed to display the image in the host search but i still cannot search for the image name.
I added the changes i made in the files in this post.Can anyone point me to what i am missing?
Maybe this can be useful for other people too.fog.main.js
[CODE]Host Search
$(‘#host-search’).fogAjaxSearch({
‘URL’: ‘ajax/host.search.php’,
‘Template’: function(data, i)
{
return ‘<tr id="host-’ + data[‘id’] + ‘"><td><input type=“checkbox” name="HID’ + data[‘id’] + ‘" checked=“checked” /></td><td><span class=“icon ping”></span></td><td><a href="?node=host&sub=edit&id=’ + data[‘id’] + ‘" title=“Edit”>’ + data[‘hostname’] + ‘</a></td><td>’ + data[‘mac’] + ‘</td><td>’ + (data[‘ip’] ? data[‘ip’] : ’ ') + ‘</td><td>’ + data[‘image’] + ‘</td><td class=“c”><a href="?node=host&sub=edit&id=’ + data[‘id’] + ‘"><span class=“icon icon-edit” title=“Edit: ’ + data[‘hostname’] + '”></span></a></td></tr>’;
}
});[/CODE]host.search.include.php
[PHP] <input id=“host-search” type=“text” value=“<?php echo(_(‘Search’)); ?>” class=“search-input” /><form method="POST" name="hosts" action="?node=host"> <table width="100%" cellpadding="0" cellspacing="0" id="search-content" border="0"> <thead> <tr class="header"> <td width="22"><input type="checkbox" name="no" checked="checked" /></td> <td width="20"></td> <td><?php print _('Host Name'); ?></td> <td width="120"><?php print _('MAC'); ?></td> <td width="120"><?php print _('IP Address'); ?></td> <td width="120"><?php print _('Host Image'); ?></td> <td class="c" width="40"><?php print _('Edit'); ?></td> </tr> </thead>[/PHP]
host.search.php
[PHP]$host = $arHosts[$i];
$mac = $host->getMAC();
$strMac = “”;
$Image = $host->getImage();
if ( $Image != null ) $strImage = $Image->getName();
else ($strImage = “”);
if ( $mac != null ) $strMac = $mac->getMACWithColon();// Minimum fields $x = array( 'id' => $host->getID(), 'hostname' => $host->getHostname(), 'image' => $strImage, 'mac' => $strMac ); [/PHP]
-
This is really very simple to do. Rather than editing multiple files you can do the lookup in one spot.
The SQL Code should look something along the lines of:
[php]‘SELECT hosts.* FROM hosts
LEFT OUTER JOIN
(SELECT * FROM hostMAC WHERE hmMAC LIKE “%${keyword}%”) hostMAC
ON (hmHostID=hostID)
LEFT OUTER JOIN
inventory
ON (iHostID=hostID)
LEFT OUTER JOIN
(SELECT * FROM groups INNER JOIN groupMembers ON (gmGroupID=groupID) WHERE groupName LIKE “%${keyword}%” OR groupDesc LIKE “%${keyword}%”) groupMembers
ON (gmHostID=hostID)
LEFT OUTER JOIN
images
ON (hostImage=imageID)
WHERE
hostID LIKE “%${keyword}%” OR
hostName LIKE “%${keyword}%” OR
hostDesc LIKE “%${keyword}%” OR
hostIP LIKE “%${keyword}%” OR
hostMAC LIKE “%${keyword}%” OR
groupID LIKE “%${keyword}%” OR
groupName LIKE “%${keyword}%” OR
groupDesc LIKE “%${keyword}%” OR
imageName LIKE “%${keyword}%” OR
imageDesc LIKE “%${keyword}%” OR
iSysserial LIKE “%${keyword}%” OR
iCaseserial LIKE “%${keyword}%” OR
iMbserial LIKE “%${keyword}%” OR
iPrimaryUser LIKE “%${keyword}%” OR
iOtherTag LIKE “%${keyword}%” OR
iOtherTag1 LIKE “%${keyword}%” OR
iSysman LIKE “%${keyword}%” OR
iSysproduct LIKE “%${keyword}%”
GROUP BY
hostID DESC’;[/php]
I’m currently looking up the code portion needed for 0.32.Lines 427 through 450 in /var/www/{FOGWEB}/lib/fog/HostManager.class.php appear to be where the sql is getting the data.
You’ll have to adjust my code above to match that in the file so things actually work for you.
-
It worked. Thanks.
/var/www/{FOGWEB}/lib/fog/HostManager.class.php:
[PHP]
…
left outer join
inventory
on ( iHostId = hostID )
left outer join
images
on ( hostImage = imageID )WHERE
hostName like ‘%" . $this->db->escape($crit) . "%’ or
hostDesc like ‘%" . $this->db->escape($crit) . "%’ or
hostIP like ‘%" . $this->db->escape($crit) . "%’ or
hostMAC like ‘%" . $this->db->escape($crit) . "%’ or
hmMAC like ‘%" . $this->db->escape($crit) . "%’ or
iSysSerial like ‘%" . $this->db->escape($crit) . "%’ or
iPrimaryUser like ‘%" . $this->db->escape($crit) . "%’ or
iOtherTag like ‘%" . $this->db->escape($crit) . "%’ or
iOtherTag1 like ‘%" . $this->db->escape($crit) . "%’ or
iSysman like ‘%" . $this->db->escape($crit) . "%’ or
imageName like ‘%" . $this->db->escape($crit) . "%’ or
iSysproduct like ‘%" . $this->db->escape($crit) . "%’
GROUP BY
hostID " . $this->getSortingOptions( $sortingOpts );
…
[/PHP] -
Thank you for posting the code. I’d have done it, but I’m more focused on 0.33 so thank you for taking the time and posting the code you used.