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("ping", "$hip");
118 $this->config->delData("lastseen", "$hip");
119 $this->config->delData("hosts", "$hname", "$hip", "$hg");
121 if($operation == "ping") {
122 $ping_host[$nping++] = $hip;
127 if($operation == "ping") {
128 foreach($ping_host as $lip) {
132 error_log("ping helper is going with $ips");
134 global $WEB_ROOT_FS, $URL_HANDLERS, $BASE_URL;
135 if(file_exists("$WEB_ROOT_FS/../bin/scanhelper.php")) {
136 $scall = "/usr/bin/php $WEB_ROOT_FS/../bin/pinghelper.php $ips > /tmp/ping 2>&1 &";
139 error_log("cant find download helper... dieing");
143 /*echo "total to do:<br><pre>";
144 print_r($hosts_to_do);
148 global $WEB_ROOT_FS, $URL_HANDLERS, $BASE_URL;
149 header("Location: $BASE_URL/hosts");
154 function deleteGroup($url)
156 $grpname = $_REQUEST["grpname"];
157 $this->config->delData("hostgroups", "$grpname");
158 $hosts = $this->config->getData("hosts");
159 foreach($hosts as $key=>$val) {
160 $hname = $val["category"];
164 if($hg == $grpname) {
165 $this->config->delData("hosts", "$hname", "$hip", "$hg");
166 $this->config->addData("hosts", "$hname", "$hip", "");
170 global $WEB_ROOT_FS, $URL_HANDLERS, $BASE_URL;
171 header("Location: $BASE_URL/hosts");
175 function scanIPRange($url)
177 $iprange = $_REQUEST["scanip"];
179 if(isset($_REQUEST["hostgroup"])) {
180 $hostgroup = $_REQUEST["hostgroup"];
183 // we just assume class c atm
184 error_log("background scanner, start");
185 global $WEB_ROOT_FS, $URL_HANDLERS, $BASE_URL;
186 if(file_exists("$WEB_ROOT_FS/../bin/scanhelper.php")) {
187 $scall = "/usr/bin/php $WEB_ROOT_FS/../bin/scanhelper.php '$iprange' '$hostgroup' > /tmp/scanlog 2>&1 &";
190 error_log("cant find download helper... dieing");
193 global $WEB_ROOT_FS, $URL_HANDLERS, $BASE_URL;
194 header("Location: $BASE_URL/hosts");
199 function deleteHost($url)
201 $hostname = $_REQUEST["hostname"];
202 $this->config->delData("hosts", "$hostname");
203 global $WEB_ROOT_FS, $URL_HANDLERS, $BASE_URL;
204 header("Location: $BASE_URL/hosts");
207 function addHost($url)
209 $hg = $_REQUEST["hostgroup"];
210 $hostname = $_REQUEST["hostname"];
211 $ip = $_REQUEST["ipaddr"];
213 if($hostname == "" && $ip == "") {
214 echo "Error: must have either ip or hostname\n";
218 if($hostname == "") {
219 // try to lookup hostname from ip
220 $hostname = gethostbyaddr($ip);
224 // try to lookup ip from hostname
225 $ip = gethostbyname($hostname);
228 $hosts = $this->config->getData("hosts");
229 foreach($hosts as $key => $val) {
230 if($val["category"] == $hostname && $val["name"] == $ip) {
231 echo "Error: host already exists<br>";
236 $this->config->addData("hosts", "$hostname", "$ip", "$hg");
239 global $WEB_ROOT_FS, $URL_HANDLERS, $BASE_URL;
240 header("Location: $BASE_URL/hosts");
244 function addGroup($url)
246 $grpname = $_REQUEST["groupname"];
247 $groups = $this->config->getData("hostgroups");
249 foreach($groups as $key => $val) {
250 if($val["category"] == $grpname) {
251 echo "Error: gorup already exists";
256 $this->config->adddata("hostgroups", "$grpname", "", "");
257 global $WEB_ROOT_FS, $URL_HANDLERS, $BASE_URL;
258 header("Location: $BASE_URL/hosts");
262 function mainBody($url)
264 $hosts = $this->config->getData("hosts");
265 $groups = $this->config->getData("hostgroups");
268 echo "<h2>Hosts and Groups</h2>";
269 echo "<form method=\"post\"action=\"?action=dooperation\">";
270 echo "Selected hosts operation: <select name=\"operation\">";
271 echo "<option value=\"move\">Move To</option>";
272 echo "<option value=\"delete\">Delete</option>";
273 echo "<option value=\"ping\">Ping</option>";
275 echo "Group <select name=\"groupop\">";
276 echo "<option value=\"\">None</option>";
277 foreach($groups as $key => $val) {
278 $grpname = $val["category"];
279 echo "<option value=\"$grpname\">$grpname</option>";
282 echo "<input type=\"submit\" name=\"Go\" value=\"Go\">";
285 echo "<table border=\"1\">";
288 // fist print ungrouped
289 echo "<tr valign=\"top\">";
291 echo "<h3>Ungrouped Hosts</h3><br>";
292 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>";
293 foreach($hosts as $key => $val) {
294 $hname = $val["category"];
297 if($hg == "") $hg = "-";
299 $lastping_v = $this->config->getData("ping", $hip);
300 $lastping_o = $lastping_v[0];
301 if(!$lastping_v) $lastping = "None";
303 if($lastping_o["val"] == "failed") {
304 $lastp_time = glcas_tdiffToAgo(time()-$lastping_o["name"]);
305 $lastping = "$lastp_time - <font color=\"red\">Failed</font>";
307 $lastp_time = glcas_tdiffToAgo(time()-$lastping_o["name"]);
308 $lping_delay = $lastping_o["val"];
309 $lastping = "$lastp_time - <font color=\"green\">$lping_delay ms</font>";
312 $lastseen = glcas_getLastSeen($hip, $this->config->getData("lastseen", "$hip"));
313 echo "<tr><td><input type=\"checkbox\" name=\"$hname-selecthost\"></td><td>$hname</td><td>$hip</td><td>$lastseen</td><td>$lastping</td></tr>";
320 foreach($groups as $key=>$val) {
323 $grpname = $val["category"];
324 echo "<h3>Host Group: $grpname <a href=\"?action=delgroup&grpname=$grpname\">Delete</a></h3><br>";
325 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>";
326 if($hosts != false) foreach($hosts as $key => $val) {
327 $hname = $val["category"];
330 if($hg == "") $hg = "-";
332 $lastping_v = $this->config->getData("ping", $hip);
333 $lastping_o = $lastping_v[0];
334 if(!$lastping_v) $lastping = "None";
336 if($lastping_o["val"] == "failed") {
337 $lastp_time = glcas_tdiffToAgo(time()-$lastping_o["name"]);
338 $lastping = "$lastp_time - <font color=\"red\">Failed</font>";
340 $lastp_time = glcas_tdiffToAgo(time()-$lastping_o["name"]);
341 $lping_delay = $lastping_o["val"];
342 $lastping = "$lastp_time - <font color=\"green\">$lping_delay ms</font>";
346 $lastseen = glcas_getLastSeen($hip, $this->config->getData("lastseen", "$hip"));
347 echo "<tr><td><input type=\"checkbox\" name=\"$hname-selecthost\"></td><td>$hname</td><td>$hip</td><td>$lastseen</td><td>$lastping</td></tr>";
352 if(($gs%4)==0) echo "</tr><tr>";
357 echo "</tr></table>";
364 echo "<table><tr valign=\"top\"><td>";
366 // the add hosts dialog
367 echo "<h2>Add Host</h2>";
368 echo "<form method=\"post\" action=\"?action=addhost\">";
369 echo "Hostname: <input type=\"text\" name=\"hostname\"><br>";
370 echo "IP Address: <input type=\"text\" name=\"ipaddr\"><br>";
372 echo "Host Group: <select name=\"hostgroup\">";
373 echo "<option value=\"\">None</option>";
374 foreach($groups as $key => $val) {
375 $hgname = $val["category"];
376 echo "<option value=\"$hgname\">$hgname</option>";
378 echo "</select><br>";
380 echo "<input type=\"submit\" name=\"add\" value=\"Add\">";
385 // the add groups dialog
386 echo "<h2>Add Group</h2>";
387 echo "<form method=\"post\" action=\"?action=addgroup\">";
388 echo "Groupname: <input type=\"text\" name=\"groupname\"><br>";
389 echo "<input type=\"submit\" name=\"Add\" value=\"Add\"><br>";
393 //scan ip range via dns
394 echo "<h2>Scan Range</h2>";
395 echo "<form method=\"post\" action=\"?action=scanrange\">";
396 echo "Range (i.e. 10.1.2.0): <input type=\"text\" name=\"scanip\"><br>";
398 echo "Host Group: <select name=\"hostgroup\">";
399 echo "<option value=\"\">None</option>";
400 foreach($groups as $key => $val) {
401 $hgname = $val["category"];
402 echo "<option value=\"$hgname\">$hgname</option>";
404 echo "</select><br>";
406 echo "<input type=\"submit\" name=\"Add\" value=\"Add\"><br>";