3 error_log("hosts loaded");
6 $URL_HANDLERS["hosts.*"] = "GLCASHosts";
7 global $BASE_URL, $MENU_ITEMS;
8 $MENU_ITEMS["30hosts"]["name"] = "Known Machines";
9 $MENU_ITEMS["30hosts"]["link"] = "$BASE_URL/hosts";
12 function __construct($config)
14 $this->config = $config;
15 error_log("constructor for GLCASHosts");
21 if(isset($_REQUEST["action"])) {
22 switch($_REQUEST["action"]) {
24 error_log("in updaterepo");
25 GLCASpageBuilder($this,"addHost");
28 error_log("in add group");
29 GLCASpageBuilder($this, "addGroup");
32 error_log("in add group");
33 GLCASpageBuilder($this, "deleteHost");
36 GLCASpageBuilder($this, "scanIPRange");
39 GLCASpageBuilder($this, "deleteGroup");
42 GLCASpageBuilder($this, "doOperation");
47 GLCASpageBuilder($this,"mainBody");
53 // TODO: this whole function wont work correctly
54 // once we move to hosts being in multiple groups..
55 function doOperation($url)
57 $operation = $_REQUEST["operation"];
58 $groupop = $_REQUEST["groupop"];
59 $hosts = $this->config->getData("hosts");
60 $groups = $this->config->getData("hostgroups");
69 // first check if nogroup is selected
70 if(isset($_REQUEST["nonegroup-selected"])) {
71 //echo "nonegroup operation<br>";
75 if($groups !== false) foreach($groups as $key=>$val) {
76 $grpname = $val["category"];
77 if(isset($_REQUEST["$grpname-selectgroup"])) {
78 $group_to_do[$ngroups++] = $grpname;
83 foreach($hosts as $key=>$val) {
85 $hname = $val["category"];
86 // this bits annoying TODO: find out why this happens
87 $thname = preg_replace("/\./", "_", $hname);
91 echo "<br>Checking $hname, $hip, $hg<br>";
93 // check if no group is on
94 if($nogroup && $hg == "") {
95 //echo "Set doing true on nogroup<br>";
99 // check if host was in a group selected
100 if($group_to_do !== null) foreach($group_to_do as $dothese) {
101 if($hg == $dothese) $dohost = true;
104 // directly selected hosts
105 if(isset($_REQUEST["$thname-selecthost"])) {
106 //echo "add host $hname<br>";
112 if($operation == "move") {
113 $this->config->delData("hosts", "$hname", "$hip", "$hg");
114 $this->config->addData("hosts", "$hname", "$hip", "$groupop");
116 if($operation == "delete") {
117 $this->config->delData("hosts", "$hname", "$hip", "$hg");
119 if($operation == "ping") {
120 $ping_host[$nping++] = $hip;
125 if($operation == "ping") {
126 foreach($ping_host as $lip) {
130 error_log("ping helper is going with $ips");
132 global $WEB_ROOT_FS, $URL_HANDLERS, $BASE_URL;
133 if(file_exists("$WEB_ROOT_FS/../bin/scanhelper.php")) {
134 $scall = "/usr/bin/php $WEB_ROOT_FS/../bin/pinghelper.php $ips > /tmp/ping 2>&1 &";
137 error_log("cant find download helper... dieing");
141 /*echo "total to do:<br><pre>";
142 print_r($hosts_to_do);
146 global $WEB_ROOT_FS, $URL_HANDLERS, $BASE_URL;
147 header("Location: $BASE_URL/hosts");
152 function deleteGroup($url)
154 $grpname = $_REQUEST["grpname"];
155 $this->config->delData("hostgroups", "$grpname");
156 $hosts = $this->config->getData("hosts");
157 foreach($hosts as $key=>$val) {
158 $hname = $val["category"];
162 if($hg == $grpname) {
163 $this->config->delData("hosts", "$hname", "$hip", "$hg");
164 $this->config->addData("hosts", "$hname", "$hip", "");
168 global $WEB_ROOT_FS, $URL_HANDLERS, $BASE_URL;
169 header("Location: $BASE_URL/hosts");
173 function scanIPRange($url)
175 $iprange = $_REQUEST["scanip"];
177 if(isset($_REQUEST["hostgroup"])) {
178 $hostgroup = $_REQUEST["hostgroup"];
181 // we just assume class c atm
182 error_log("background scanner, start");
183 global $WEB_ROOT_FS, $URL_HANDLERS, $BASE_URL;
184 if(file_exists("$WEB_ROOT_FS/../bin/scanhelper.php")) {
185 $scall = "/usr/bin/php $WEB_ROOT_FS/../bin/scanhelper.php '$iprange' '$hostgroup' > /tmp/scanlog 2>&1 &";
188 error_log("cant find download helper... dieing");
191 global $WEB_ROOT_FS, $URL_HANDLERS, $BASE_URL;
192 header("Location: $BASE_URL/hosts");
197 function deleteHost($url)
199 $hostname = $_REQUEST["hostname"];
200 $this->config->delData("hosts", "$hostname");
201 global $WEB_ROOT_FS, $URL_HANDLERS, $BASE_URL;
202 header("Location: $BASE_URL/hosts");
205 function addHost($url)
207 $hg = $_REQUEST["hostgroup"];
208 $hostname = $_REQUEST["hostname"];
209 $ip = $_REQUEST["ipaddr"];
211 if($hostname == "" && $ip == "") {
212 echo "Error: must have either ip or hostname\n";
216 if($hostname == "") {
217 // try to lookup hostname from ip
218 $hostname = gethostbyaddr($ip);
222 // try to lookup ip from hostname
223 $ip = gethostbyname($hostname);
226 $hosts = $this->config->getData("hosts");
227 foreach($hosts as $key => $val) {
228 if($val["category"] == $hostname && $val["name"] == $ip) {
229 echo "Error: host already exists<br>";
234 $this->config->addData("hosts", "$hostname", "$ip", "$hg");
237 global $WEB_ROOT_FS, $URL_HANDLERS, $BASE_URL;
238 header("Location: $BASE_URL/hosts");
242 function addGroup($url)
244 $grpname = $_REQUEST["groupname"];
245 $groups = $this->config->getData("hostgroups");
247 foreach($groups as $key => $val) {
248 if($val["category"] == $grpname) {
249 echo "Error: gorup already exists";
254 $this->config->adddata("hostgroups", "$grpname", "", "");
255 global $WEB_ROOT_FS, $URL_HANDLERS, $BASE_URL;
256 header("Location: $BASE_URL/hosts");
260 function mainBody($url)
262 $hosts = $this->config->getData("hosts");
263 $groups = $this->config->getData("hostgroups");
266 echo "<h2>Hosts and Groups</h2>";
267 echo "<form method=\"post\"action=\"?action=dooperation\">";
268 echo "Selected hosts operation: <select name=\"operation\">";
269 echo "<option value=\"move\">Move To</option>";
270 echo "<option value=\"delete\">Delete</option>";
271 echo "<option value=\"ping\">Ping</option>";
273 echo "Group <select name=\"groupop\">";
274 echo "<option value=\"\">None</option>";
275 foreach($groups as $key => $val) {
276 $grpname = $val["category"];
277 echo "<option value=\"$grpname\">$grpname</option>";
280 echo "<input type=\"submit\" name=\"Go\" value=\"Go\">";
283 echo "<table border=\"1\">";
286 // fist print ungrouped
287 echo "<tr valign=\"top\">";
289 echo "<h3>Ungrouped Hosts</h3><br>";
290 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><th>Control</th></tr>";
291 foreach($hosts as $key => $val) {
292 $hname = $val["category"];
295 if($hg == "") $hg = "-";
297 $lastping_v = $this->config->getData("ping", $hip);
298 $lastping_o = $lastping_v[0];
299 if(!$lastping_v) $lastping = "None";
301 if($lastping_o["val"] == "failed") {
302 $lastp_time = glcas_tdiffToAgo(time()-$lastping_o["name"]);
303 $lastping = "$lastp_time - <font color=\"red\">Failed</font>";
305 $lastp_time = glcas_tdiffToAgo(time()-$lastping_o["name"]);
306 $lping_delay = $lastping_o["val"];
307 $lastping = "$lastp_time - <font color=\"green\">$lping_delay ms</font>";
310 $lastseen = glcas_getLastSeen($hip, $this->config->getData("lastseen", "$hip"));
311 echo "<tr><td><input type=\"checkbox\" name=\"$hname-selecthost\"></td><td>$hname</td><td>$hip</td><td>$lastseen</td><td>$lastping</td><td><a href=\"?action=deletehost&hostname=$hname\">Delete</a></tr>";
318 foreach($groups as $key=>$val) {
321 $grpname = $val["category"];
322 echo "<h3>Host Group: $grpname <a href=\"?action=delgroup&grpname=$grpname\">Delete</a></h3><br>";
323 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><th>Control</th></tr>";
324 if($hosts != false) foreach($hosts as $key => $val) {
325 $hname = $val["category"];
328 if($hg == "") $hg = "-";
330 $lastping_v = $this->config->getData("ping", $hip);
331 $lastping_o = $lastping_v[0];
332 if(!$lastping_v) $lastping = "None";
334 if($lastping_o["val"] == "failed") {
335 $lastp_time = glcas_tdiffToAgo(time()-$lastping_o["name"]);
336 $lastping = "$lastp_time - <font color=\"red\">Failed</font>";
338 $lastp_time = glcas_tdiffToAgo(time()-$lastping_o["name"]);
339 $lping_delay = $lastping_o["val"];
340 $lastping = "$lastp_time - <font color=\"green\">$lping_delay ms</font>";
344 $lastseen = glcas_getLastSeen($hip, $this->config->getData("lastseen", "$hip"));
345 echo "<tr><td><input type=\"checkbox\" name=\"$hname-selecthost\"></td><td>$hname</td><td>$hip</td><td>$lastseen</td><td>$lastping</td><td><a href=\"?action=deletehost&hostname=$hname\">Delete</a></tr>";
350 if(($gs%4)==0) echo "</tr><tr>";
355 echo "</tr></table>";
362 echo "<table><tr valign=\"top\"><td>";
364 // the add hosts dialog
365 echo "<h2>Add Host</h2>";
366 echo "<form method=\"post\" action=\"?action=addhost\">";
367 echo "Hostname: <input type=\"text\" name=\"hostname\"><br>";
368 echo "IP Address: <input type=\"text\" name=\"ipaddr\"><br>";
370 echo "Host Group: <select name=\"hostgroup\">";
371 echo "<option value=\"\">None</option>";
372 foreach($groups as $key => $val) {
373 $hgname = $val["category"];
374 echo "<option value=\"$hgname\">$hgname</option>";
376 echo "</select><br>";
378 echo "<input type=\"submit\" name=\"add\" value=\"Add\">";
383 // the add groups dialog
384 echo "<h2>Add Group</h2>";
385 echo "<form method=\"post\" action=\"?action=addgroup\">";
386 echo "Groupname: <input type=\"text\" name=\"groupname\"><br>";
387 echo "<input type=\"submit\" name=\"Add\" value=\"Add\"><br>";
391 //scan ip range via dns
392 echo "<h2>Scan Range</h2>";
393 echo "<form method=\"post\" action=\"?action=scanrange\">";
394 echo "Range (i.e. 10.1.2.0): <input type=\"text\" name=\"scanip\"><br>";
396 echo "Host Group: <select name=\"hostgroup\">";
397 echo "<option value=\"\">None</option>";
398 foreach($groups as $key => $val) {
399 $hgname = $val["category"];
400 echo "<option value=\"$hgname\">$hgname</option>";
402 echo "</select><br>";
404 echo "<input type=\"submit\" name=\"Add\" value=\"Add\"><br>";