global $URL_HANDLERS;
$URL_HANDLERS["hosts.*"] = "GLCASHosts";
global $BASE_URL, $MENU_ITEMS;
-$MENU_ITEMS["30hosts"]["name"] = "Known Machines";
-$MENU_ITEMS["30hosts"]["link"] = "$BASE_URL/hosts";
+$MENU_ITEMS["10hosts"]["name"] = "Known Machines";
+$MENU_ITEMS["10hosts"]["link"] = "$BASE_URL/hosts";
class GLCASHosts {
function __construct($config)
{
if(isset($_REQUEST["action"])) {
switch($_REQUEST["action"]) {
- case "addpackage":
+ case "addhost":
error_log("in updaterepo");
- GLCASpageBuilder($this,"addPackage");
+ GLCASpageBuilder($this,"addHost");
return;
- case "deletepkg":
- error_log("in delete pkg");
- GLCASpageBuilder($this, "delPackage");
+ case "addgroup":
+ error_log("in add group");
+ GLCASpageBuilder($this, "addGroup");
+ return;
+ case "scanrange":
+ GLCASpageBuilder($this, "scanIPRange");
+ return;
+ case "delgroup":
+ GLCASpageBuilder($this, "deleteGroup");
+ return;
+ case "dooperation":
+ error_log("in do operation");
+ GLCASpageBuilder($this, "doOperation");
return;
}
}
}
+
+ // TODO: this whole function wont work correctly
+ // once we move to hosts being in multiple groups..
+ function doOperation($url)
+ {
+ $operation = $_REQUEST["operation"];
+ $groupop = $_REQUEST["groupop"];
+ $hosts = $this->config->getData("hosts");
+ $groups = $this->config->getData("hostgroups");
+ $nogroup = false;
+ $hosts_to_do = null;
+ $nhosts = 0;
+ $group_to_do = null;
+ $ngroups = 0;
+ $ping_host = null;
+ $nping = 0;
+
+ // first check if nogroup is selected
+ if(isset($_REQUEST["nonegroup-selected"])) {
+ //echo "nonegroup operation<br>";
+ $nogroup = true;
+ }
+
+ if($groups !== false) foreach($groups as $key=>$val) {
+ $grpname = $val["category"];
+ if(isset($_REQUEST["$grpname-selectgroup"])) {
+ $group_to_do[$ngroups++] = $grpname;
+
+ }
+ }
+
+ foreach($hosts as $key=>$val) {
+ $dohost = false;
+ $hname = $val["category"];
+ // this bits annoying TODO: find out why this happens
+ $thname = preg_replace("/\./", "_", $hname);
+ $hip = $val["name"];
+ $hg = $val["val"];
+
+ //echo "<br>Checking $hname, $hip, $hg<br>";
+
+ // check if no group is on
+ if($nogroup && $hg == "") {
+ //echo "Set doing true on nogroup<br>";
+ $dohost = true;
+ }
+
+ // check if host was in a group selected
+ if($group_to_do !== null) foreach($group_to_do as $dothese) {
+ if($hg == $dothese) $dohost = true;
+ }
+
+ // directly selected hosts
+ if(isset($_REQUEST["$thname-selecthost"])) {
+ //echo "add host $hname<br>";
+ $dohost = true;
+ }
+
+ // now do the op
+ if($dohost) {
+ if($operation == "move") {
+ $this->config->delData("hosts", "$hname", "$hip", "$hg");
+ $this->config->addData("hosts", "$hname", "$hip", "$groupop");
+ }
+ if($operation == "delete") {
+ $this->config->delData("ping", "$hip");
+ $this->config->delData("lastseen", "$hip");
+ $this->config->delData("hosts", "$hname", "$hip", "$hg");
+ }
+ if($operation == "ping") {
+ $ping_host[$nping++] = $hip;
+ }
+ }
+ }
+
+ if($operation == "ping") {
+ foreach($ping_host as $lip) {
+ $ips .= " $lip";
+ }
+
+ error_log("ping helper is going with $ips");
+
+ global $WEB_ROOT_FS, $URL_HANDLERS, $BASE_URL;
+ if(file_exists("$WEB_ROOT_FS/../bin/scanhelper.php")) {
+ $scall = "/usr/bin/php $WEB_ROOT_FS/../bin/pinghelper.php $ips > /tmp/ping 2>&1 &";
+ system($scall);
+ } else {
+ error_log("cant find download helper... dieing");
+ }
+
+ }
+ /*echo "total to do:<br><pre>";
+ print_r($hosts_to_do);
+ print_r($_REQUEST);
+ echo "</pre>";*/
+
+ global $WEB_ROOT_FS, $URL_HANDLERS, $BASE_URL;
+ header("Location: $BASE_URL/hosts");
+
+
+ }
+
+ function deleteGroup($url)
+ {
+ $grpname = $_REQUEST["grpname"];
+ $this->config->delData("hostgroups", "$grpname");
+ $hosts = $this->config->getData("hosts");
+ foreach($hosts as $key=>$val) {
+ $hname = $val["category"];
+ $hip = $val["name"];
+ $hg = $val["val"];
+
+ if($hg == $grpname) {
+ $this->config->delData("hosts", "$hname", "$hip", "$hg");
+ $this->config->addData("hosts", "$hname", "$hip", "");
+ }
+ }
+
+ global $WEB_ROOT_FS, $URL_HANDLERS, $BASE_URL;
+ header("Location: $BASE_URL/hosts");
+
+ }
+
+ function scanIPRange($url)
+ {
+ $iprange = $_REQUEST["scanip"];
+ $hostgroup = "";
+ if(isset($_REQUEST["hostgroup"])) {
+ $hostgroup = $_REQUEST["hostgroup"];
+ }
+
+ // we just assume class c atm
+ error_log("background scanner, start");
+ global $WEB_ROOT_FS, $URL_HANDLERS, $BASE_URL;
+ if(file_exists("$WEB_ROOT_FS/../bin/scanhelper.php")) {
+ $scall = "/usr/bin/php $WEB_ROOT_FS/../bin/scanhelper.php '$iprange' '$hostgroup' > /tmp/scanlog 2>&1 &";
+ system($scall);
+ } else {
+ error_log("cant find download helper... dieing");
+ }
+
+ global $WEB_ROOT_FS, $URL_HANDLERS, $BASE_URL;
+ header("Location: $BASE_URL/hosts");
+
+
+ }
+
+ function addHost($url)
+ {
+ $hg = $_REQUEST["hostgroup"];
+ $hostname = $_REQUEST["hostname"];
+ $ip = $_REQUEST["ipaddr"];
+
+ if($hostname == "" && $ip == "") {
+ echo "Error: must have either ip or hostname\n";
+ return 0;
+ }
+
+ if($hostname == "") {
+ // try to lookup hostname from ip
+ $hostname = gethostbyaddr($ip);
+ }
+
+ if($ip == "") {
+ // try to lookup ip from hostname
+ $ip = gethostbyname($hostname);
+ }
+
+ $hosts = $this->config->getData("hosts");
+ foreach($hosts as $key => $val) {
+ if($val["category"] == $hostname && $val["name"] == $ip) {
+ echo "Error: host already exists<br>";
+ return 0;
+ }
+ }
+
+ $this->config->addData("hosts", "$hostname", "$ip", "$hg");
+
+
+ global $WEB_ROOT_FS, $URL_HANDLERS, $BASE_URL;
+ header("Location: $BASE_URL/hosts");
+
+ }
+
+ function addGroup($url)
+ {
+ $grpname = $_REQUEST["groupname"];
+ $groups = $this->config->getData("hostgroups");
+
+ foreach($groups as $key => $val) {
+ if($val["category"] == $grpname) {
+ echo "Error: gorup already exists";
+ return 0;
+ }
+ }
+
+ $this->config->adddata("hostgroups", "$grpname", "", "");
+ global $WEB_ROOT_FS, $URL_HANDLERS, $BASE_URL;
+ header("Location: $BASE_URL/hosts");
+
+ }
+
function mainBody($url)
{
- echo "I am a hosts thingy";
+ $hosts = $this->config->getData("hosts");
+ $groups = $this->config->getData("hostgroups");
+
+ $gs = 0;
+ echo "<h2>Hosts and Groups</h2>";
+ echo "<form method=\"post\"action=\"?action=dooperation\">";
+ echo "Selected hosts operation: <select name=\"operation\">";
+ echo "<option value=\"move\">Move To</option>";
+ echo "<option value=\"delete\">Delete</option>";
+ echo "<option value=\"ping\">Ping</option>";
+ echo "</select>";
+ echo "Group <select name=\"groupop\">";
+ echo "<option value=\"\">None</option>";
+ foreach($groups as $key => $val) {
+ $grpname = $val["category"];
+ echo "<option value=\"$grpname\">$grpname</option>";
+ }
+ echo "</select>";
+ echo "<input type=\"submit\" name=\"Go\" value=\"Go\">";
+
+
+ echo "<table border=\"1\">";
+
+ $gs++;
+ // fist print ungrouped
+ echo "<tr valign=\"top\">";
+ echo "<td>";
+ echo "<h3>Ungrouped Hosts</h3><br>";
+ echo "<table border=\"1\"><tr><th><input type=\"checkbox\" name=\"nonegroup-selected\"></th><th>Host</th><th>IP</th><th>Last Seen</th><th>Last Ping</th></tr>";
+ foreach($hosts as $key => $val) {
+ $hname = $val["category"];
+ $hip = $val["name"];
+ $hg = $val["val"];
+ if($hg == "") $hg = "-";
+ if($hg=="-") {
+ $lastping_v = $this->config->getData("ping", $hip);
+ $lastping_o = $lastping_v[0];
+ if(!$lastping_v) $lastping = "None";
+ else {
+ if($lastping_o["val"] == "failed") {
+ $lastp_time = glcas_tdiffToAgo(time()-$lastping_o["name"]);
+ $lastping = "$lastp_time - <font color=\"red\">Failed</font>";
+ } else {
+ $lastp_time = glcas_tdiffToAgo(time()-$lastping_o["name"]);
+ $lping_delay = $lastping_o["val"];
+ $lastping = "$lastp_time - <font color=\"green\">$lping_delay ms</font>";
+ }
+ }
+ $lastseen = glcas_getLastSeen($hip, $this->config->getData("lastseen", "$hip"));
+ echo "<tr><td><input type=\"checkbox\" name=\"$hname-selecthost\"></td><td>$hname</td><td>$hip</td><td>$lastseen</td><td>$lastping</td></tr>";
+ }
+ }
+ echo "</table>";
+ echo "</td>";
+
+ if($groups) {
+ foreach($groups as $key=>$val) {
+ $gs++;
+ echo "<td>";
+ $grpname = $val["category"];
+ echo "<h3>Host Group: $grpname <a href=\"?action=delgroup&grpname=$grpname\">Delete</a></h3><br>";
+ echo "<table border=\"1\"><tr><th><input type=\"checkbox\" name=\"$grpname-selectgroup\"></th><th>Host</th><th>IP</th><th>Last Seen</th><th>Last Ping</th></tr>";
+ if($hosts != false) foreach($hosts as $key => $val) {
+ $hname = $val["category"];
+ $hip = $val["name"];
+ $hg = $val["val"];
+ if($hg == "") $hg = "-";
+ if($hg==$grpname) {
+ $lastping_v = $this->config->getData("ping", $hip);
+ $lastping_o = $lastping_v[0];
+ if(!$lastping_v) $lastping = "None";
+ else {
+ if($lastping_o["val"] == "failed") {
+ $lastp_time = glcas_tdiffToAgo(time()-$lastping_o["name"]);
+ $lastping = "$lastp_time - <font color=\"red\">Failed</font>";
+ } else {
+ $lastp_time = glcas_tdiffToAgo(time()-$lastping_o["name"]);
+ $lping_delay = $lastping_o["val"];
+ $lastping = "$lastp_time - <font color=\"green\">$lping_delay ms</font>";
+ }
+ }
+
+ $lastseen = glcas_getLastSeen($hip, $this->config->getData("lastseen", "$hip"));
+ echo "<tr><td><input type=\"checkbox\" name=\"$hname-selecthost\"></td><td>$hname</td><td>$hip</td><td>$lastseen</td><td>$lastping</td></tr>";
+ }
+ }
+ echo "</table>";
+ echo "</td>";
+ if(($gs%4)==0) echo "</tr><tr>";
+ }
+ }
+
+
+ echo "</tr></table>";
+ echo "</form>";
+
+ echo "<hr>";
+
+
+
+ echo "<table><tr valign=\"top\"><td>";
+
+ // the add hosts dialog
+ echo "<h2>Add Host</h2>";
+ echo "<form method=\"post\" action=\"?action=addhost\">";
+ echo "Hostname: <input type=\"text\" name=\"hostname\"><br>";
+ echo "IP Address: <input type=\"text\" name=\"ipaddr\"><br>";
+ if($groups) {
+ echo "Host Group: <select name=\"hostgroup\">";
+ echo "<option value=\"\">None</option>";
+ foreach($groups as $key => $val) {
+ $hgname = $val["category"];
+ echo "<option value=\"$hgname\">$hgname</option>";
+ }
+ echo "</select><br>";
+ }
+ echo "<input type=\"submit\" name=\"add\" value=\"Add\">";
+ echo "</form>";
+
+ echo "</td><td>";
+
+ // the add groups dialog
+ echo "<h2>Add Group</h2>";
+ echo "<form method=\"post\" action=\"?action=addgroup\">";
+ echo "Groupname: <input type=\"text\" name=\"groupname\"><br>";
+ echo "<input type=\"submit\" name=\"Add\" value=\"Add\"><br>";
+ echo "</form>";
+
+ echo "</td><td>";
+ //scan ip range via dns
+ echo "<h2>Scan Range</h2>";
+ echo "<form method=\"post\" action=\"?action=scanrange\">";
+ echo "Range (i.e. 10.1.2.0): <input type=\"text\" name=\"scanip\"><br>";
+ if($groups) {
+ echo "Host Group: <select name=\"hostgroup\">";
+ echo "<option value=\"\">None</option>";
+ foreach($groups as $key => $val) {
+ $hgname = $val["category"];
+ echo "<option value=\"$hgname\">$hgname</option>";
+ }
+ echo "</select><br>";
+ }
+ echo "<input type=\"submit\" name=\"Add\" value=\"Add\"><br>";
+ echo "</form>";
+
+ echo "</table>";
+
}
private $config;