3 error_log("hosts loaded");
6 $URL_HANDLERS["hosts.*"] = "GLCASHosts";
7 global $BASE_URL, $MENU_ITEMS;
8 $MENU_ITEMS["10hosts"]["name"] = "Known Machines";
9 $MENU_ITEMS["10hosts"]["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 GLCASpageBuilder($this, "scanIPRange");
35 GLCASpageBuilder($this, "deleteGroup");
38 error_log("in do operation");
39 GLCASpageBuilder($this, "doOperation");
44 GLCASpageBuilder($this,"mainBody");
50 // TODO: this whole function wont work correctly
51 // once we move to hosts being in multiple groups..
52 function doOperation($url)
54 $operation = $_REQUEST["operation"];
55 $groupop = $_REQUEST["groupop"];
56 $hosts = $this->config->getData("hosts");
57 $groups = $this->config->getData("hostgroups");
66 // first check if nogroup is selected
67 if(isset($_REQUEST["nonegroup-selected"])) {
68 //echo "nonegroup operation<br>";
72 if($groups !== false) foreach($groups as $key=>$val) {
73 $grpname = $val["category"];
74 if(isset($_REQUEST["$grpname-selectgroup"])) {
75 $group_to_do[$ngroups++] = $grpname;
80 foreach($hosts as $key=>$val) {
82 $hname = $val["category"];
83 // this bits annoying TODO: find out why this happens
84 $thname = preg_replace("/\./", "_", $hname);
88 //echo "<br>Checking $hname, $hip, $hg<br>";
90 // check if no group is on
91 if($nogroup && $hg == "") {
92 //echo "Set doing true on nogroup<br>";
96 // check if host was in a group selected
97 if($group_to_do !== null) foreach($group_to_do as $dothese) {
98 if($hg == $dothese) $dohost = true;
101 // directly selected hosts
102 if(isset($_REQUEST["$thname-selecthost"])) {
103 //echo "add host $hname<br>";
109 if($operation == "move") {
110 $this->config->delData("hosts", "$hname", "$hip", "$hg");
111 $this->config->addData("hosts", "$hname", "$hip", "$groupop");
113 if($operation == "delete") {
114 $this->config->delData("ping", "$hip");
115 $this->config->delData("lastseen", "$hip");
116 $this->config->delData("hosts", "$hname", "$hip", "$hg");
118 if($operation == "ping") {
119 $ping_host[$nping++] = $hip;
124 if($operation == "ping") {
125 foreach($ping_host as $lip) {
129 error_log("ping helper is going with $ips");
131 global $WEB_ROOT_FS, $URL_HANDLERS, $BASE_URL;
132 if(file_exists("$WEB_ROOT_FS/../bin/scanhelper.php")) {
133 $scall = "/usr/bin/php $WEB_ROOT_FS/../bin/pinghelper.php $ips > /tmp/ping 2>&1 &";
136 error_log("cant find download helper... dieing");
140 /*echo "total to do:<br><pre>";
141 print_r($hosts_to_do);
145 global $WEB_ROOT_FS, $URL_HANDLERS, $BASE_URL;
146 header("Location: $BASE_URL/hosts");
151 function deleteGroup($url)
153 $grpname = $_REQUEST["grpname"];
154 $this->config->delData("hostgroups", "$grpname");
155 $hosts = $this->config->getData("hosts");
156 foreach($hosts as $key=>$val) {
157 $hname = $val["category"];
161 if($hg == $grpname) {
162 $this->config->delData("hosts", "$hname", "$hip", "$hg");
163 $this->config->addData("hosts", "$hname", "$hip", "");
167 global $WEB_ROOT_FS, $URL_HANDLERS, $BASE_URL;
168 header("Location: $BASE_URL/hosts");
172 function scanIPRange($url)
174 $iprange = $_REQUEST["scanip"];
176 if(isset($_REQUEST["hostgroup"])) {
177 $hostgroup = $_REQUEST["hostgroup"];
180 // we just assume class c atm
181 error_log("background scanner, start");
182 global $WEB_ROOT_FS, $URL_HANDLERS, $BASE_URL;
183 if(file_exists("$WEB_ROOT_FS/../bin/scanhelper.php")) {
184 $scall = "/usr/bin/php $WEB_ROOT_FS/../bin/scanhelper.php '$iprange' '$hostgroup' > /tmp/scanlog 2>&1 &";
187 error_log("cant find download helper... dieing");
190 global $WEB_ROOT_FS, $URL_HANDLERS, $BASE_URL;
191 header("Location: $BASE_URL/hosts");
196 function addHost($url)
198 $hg = $_REQUEST["hostgroup"];
199 $hostname = $_REQUEST["hostname"];
200 $ip = $_REQUEST["ipaddr"];
202 if($hostname == "" && $ip == "") {
203 echo "Error: must have either ip or hostname\n";
207 if($hostname == "") {
208 // try to lookup hostname from ip
209 $hostname = gethostbyaddr($ip);
213 // try to lookup ip from hostname
214 $ip = gethostbyname($hostname);
217 $hosts = $this->config->getData("hosts");
218 foreach($hosts as $key => $val) {
219 if($val["category"] == $hostname && $val["name"] == $ip) {
220 echo "Error: host already exists<br>";
225 $this->config->addData("hosts", "$hostname", "$ip", "$hg");
228 global $WEB_ROOT_FS, $URL_HANDLERS, $BASE_URL;
229 header("Location: $BASE_URL/hosts");
233 function addGroup($url)
235 $grpname = $_REQUEST["groupname"];
236 $groups = $this->config->getData("hostgroups");
238 foreach($groups as $key => $val) {
239 if($val["category"] == $grpname) {
240 echo "Error: gorup already exists";
245 $this->config->adddata("hostgroups", "$grpname", "", "");
246 global $WEB_ROOT_FS, $URL_HANDLERS, $BASE_URL;
247 header("Location: $BASE_URL/hosts");
251 function mainBody($url)
253 $hosts = $this->config->getData("hosts");
254 $groups = $this->config->getData("hostgroups");
257 echo "<h2>Hosts and Groups</h2>";
258 echo "<form method=\"post\"action=\"?action=dooperation\">";
259 echo "Selected hosts operation: <select name=\"operation\">";
260 echo "<option value=\"move\">Move To</option>";
261 echo "<option value=\"delete\">Delete</option>";
262 echo "<option value=\"ping\">Ping</option>";
264 echo "Group <select name=\"groupop\">";
265 echo "<option value=\"\">None</option>";
266 foreach($groups as $key => $val) {
267 $grpname = $val["category"];
268 echo "<option value=\"$grpname\">$grpname</option>";
271 echo "<input type=\"submit\" name=\"Go\" value=\"Go\">";
274 echo "<table border=\"1\">";
277 // fist print ungrouped
278 echo "<tr valign=\"top\">";
280 echo "<h3>Ungrouped Hosts</h3><br>";
281 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>";
282 foreach($hosts as $key => $val) {
283 $hname = $val["category"];
286 if($hg == "") $hg = "-";
288 $lastping_v = $this->config->getData("ping", $hip);
289 $lastping_o = $lastping_v[0];
290 if(!$lastping_v) $lastping = "None";
292 if($lastping_o["val"] == "failed") {
293 $lastp_time = glcas_tdiffToAgo(time()-$lastping_o["name"]);
294 $lastping = "$lastp_time - <font color=\"red\">Failed</font>";
296 $lastp_time = glcas_tdiffToAgo(time()-$lastping_o["name"]);
297 $lping_delay = $lastping_o["val"];
298 $lastping = "$lastp_time - <font color=\"green\">$lping_delay ms</font>";
301 $lastseen = glcas_getLastSeen($hip, $this->config->getData("lastseen", "$hip"));
302 echo "<tr><td><input type=\"checkbox\" name=\"$hname-selecthost\"></td><td>$hname</td><td>$hip</td><td>$lastseen</td><td>$lastping</td></tr>";
309 foreach($groups as $key=>$val) {
312 $grpname = $val["category"];
313 echo "<h3>Host Group: $grpname <a href=\"?action=delgroup&grpname=$grpname\">Delete</a></h3><br>";
314 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>";
315 if($hosts != false) foreach($hosts as $key => $val) {
316 $hname = $val["category"];
319 if($hg == "") $hg = "-";
321 $lastping_v = $this->config->getData("ping", $hip);
322 $lastping_o = $lastping_v[0];
323 if(!$lastping_v) $lastping = "None";
325 if($lastping_o["val"] == "failed") {
326 $lastp_time = glcas_tdiffToAgo(time()-$lastping_o["name"]);
327 $lastping = "$lastp_time - <font color=\"red\">Failed</font>";
329 $lastp_time = glcas_tdiffToAgo(time()-$lastping_o["name"]);
330 $lping_delay = $lastping_o["val"];
331 $lastping = "$lastp_time - <font color=\"green\">$lping_delay ms</font>";
335 $lastseen = glcas_getLastSeen($hip, $this->config->getData("lastseen", "$hip"));
336 echo "<tr><td><input type=\"checkbox\" name=\"$hname-selecthost\"></td><td>$hname</td><td>$hip</td><td>$lastseen</td><td>$lastping</td></tr>";
341 if(($gs%4)==0) echo "</tr><tr>";
346 echo "</tr></table>";
353 echo "<table><tr valign=\"top\"><td>";
355 // the add hosts dialog
356 echo "<h2>Add Host</h2>";
357 echo "<form method=\"post\" action=\"?action=addhost\">";
358 echo "Hostname: <input type=\"text\" name=\"hostname\"><br>";
359 echo "IP Address: <input type=\"text\" name=\"ipaddr\"><br>";
361 echo "Host Group: <select name=\"hostgroup\">";
362 echo "<option value=\"\">None</option>";
363 foreach($groups as $key => $val) {
364 $hgname = $val["category"];
365 echo "<option value=\"$hgname\">$hgname</option>";
367 echo "</select><br>";
369 echo "<input type=\"submit\" name=\"add\" value=\"Add\">";
374 // the add groups dialog
375 echo "<h2>Add Group</h2>";
376 echo "<form method=\"post\" action=\"?action=addgroup\">";
377 echo "Groupname: <input type=\"text\" name=\"groupname\"><br>";
378 echo "<input type=\"submit\" name=\"Add\" value=\"Add\"><br>";
382 //scan ip range via dns
383 echo "<h2>Scan Range</h2>";
384 echo "<form method=\"post\" action=\"?action=scanrange\">";
385 echo "Range (i.e. 10.1.2.0): <input type=\"text\" name=\"scanip\"><br>";
387 echo "Host Group: <select name=\"hostgroup\">";
388 echo "<option value=\"\">None</option>";
389 foreach($groups as $key => $val) {
390 $hgname = $val["category"];
391 echo "<option value=\"$hgname\">$hgname</option>";
393 echo "</select><br>";
395 echo "<input type=\"submit\" name=\"Add\" value=\"Add\"><br>";