hostgroup scanning works.
[glcas.git] / plugins / hosts.php
index 76e3e59..3634682 100644 (file)
@@ -22,11 +22,18 @@ class GLCASHosts {
                        switch($_REQUEST["action"]) {
                                case "addhost":
                                        error_log("in updaterepo");
-                                       GLCASpageBuilder($this,"addPackage");
+                                       GLCASpageBuilder($this,"addHost");
                                        return;
-                               case "delhost":
-                                       error_log("in delete pkg");
-                                       GLCASpageBuilder($this, "delPackage");
+                               case "addgroup":
+                                       error_log("in add group");
+                                       GLCASpageBuilder($this, "addGroup");
+                                       return;
+                               case "deletehost":
+                                       error_log("in add group");
+                                       GLCASpageBuilder($this, "deleteHost");
+                                       return;
+                               case "scanrange":
+                                       GLCASpageBuilder($this, "scanIPRange");
                                        return;
                        }
                }
@@ -36,9 +43,194 @@ class GLCASHosts {
                
        }
        
+       function scanIPRange($url)
+       {
+               $iprange = $_REQUEST["scanip"];
+               $hostgroup = "";
+               if(isset($_REQUEST["hostgroup"])) {
+                       $hostgroup = $_REQUEST["hostgroup"];
+               }
+               
+               // we just assume class c atm
+               $ips_v = explode(".", $iprange);
+               
+               $ips = $ips_v[0].".".$ips_v[1].".".$ips_v[2];
+               
+               for($i = 1; $i < 32; $i++) {
+                       $hostname = "";
+                       $ips_me = "$ips.$i";
+                       echo "Scanning $ips_me<br>";
+                       error_log("Scanning $ips_me<br>");
+                       flush();
+                       $hostname = gethostbyaddr($ips_me);
+                       if($hostname != $ips_me) {
+                               echo "Found host on $ips_me as $hostname<br>";
+                               error_log("Found host on $ips_me as $hostname");
+                               flush();
+                               $hosts = $this->config->getData("hosts");
+                               $exists = false;
+                               foreach($hosts as $key => $val) {
+                                       if($val["category"] == $hostname && $val["name"] == $ips_me) {
+                                               echo "Host in db already<br>";
+                                               $exists = true;
+                                               flush();
+                                       }
+                                       
+                               }
+                               if(!$exists) $this->config->addData("hosts", "$hostname", "$ips_me", "$hostgroup");
+                               
+                               
+                       }
+               }
+               echo "Finished";
+       }
+       
+       function deleteHost($url)
+       {
+               $hostname = $_REQUEST["hostname"];
+               $this->config->delData("hosts", "$hostname");
+               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");
+               
+               echo "<h2>Hosts</h2>";
+               if($hosts) {
+                       echo "<table border=\"1\"><tr><th>Host</th><th>IP</th><th>Host Group</th><th>Last Seen</th><th>Control</th></tr>";
+                       foreach($hosts as $key => $val) {
+                               $hname = $val["category"];
+                               $hip = $val["name"];
+                               $hg = $val["val"];
+                               if($hg == "") $hg = "-";
+                               echo "<tr><td>$hname</td><td>$hip</td><td>$hg</td><td>...</td><td><a href=\"?action=deletehost&hostname=$hname\">Delete</a></tr>";
+                       }
+                       echo "</table>";
+               } else {
+                       echo "No hosts defined yet<br>"; 
+               }
+               echo "<hr>";
+               
+               // groups
+               echo "<h2>Host Groups</h2>";
+               if($groups) {
+                       echo "<table border=\"1\"><tr><th>Group Name</th></tr>";
+                       foreach($groups as $key=>$val) {
+                               $grpname = $val["category"];
+                               echo "<tr><td>$grpname</td></tr>";
+                       }
+                       echo "</table>";
+               } else {
+                       echo "No host groups defined yet<br>";
+               }
+               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;