not sure what i did, but you can bet it was AWESOME
[glcas.git] / plugins / hosts.php
1 <?php 
2
3 error_log("hosts loaded");
4
5 global $URL_HANDLERS;
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";
10
11 class GLCASHosts {
12         function __construct($config)
13         {
14                 $this->config = $config;
15                 error_log("constructor for GLCASHosts");
16                 
17         }
18         
19         function go($url)
20         {
21                 if(isset($_REQUEST["action"])) {
22                         switch($_REQUEST["action"]) {
23                                 case "addhost":
24                                         error_log("in updaterepo");
25                                         GLCASpageBuilder($this,"addHost");
26                                         return;
27                                 case "addgroup":
28                                         error_log("in add group");
29                                         GLCASpageBuilder($this, "addGroup");
30                                         return;
31                                 case "deletehost":
32                                         error_log("in add group");
33                                         GLCASpageBuilder($this, "deleteHost");
34                                         return;
35                                 case "scanrange":
36                                         GLCASpageBuilder($this, "scanIPRange");
37                                         return;
38                                 case "delgroup":
39                                         GLCASpageBuilder($this, "deleteGroup");
40                                         return;
41                                 case "dooperation":
42                                         GLCASpageBuilder($this, "doOperation");
43                                         return;
44                         }
45                 }
46                 
47                 GLCASpageBuilder($this,"mainBody");
48                 return;
49                 
50         }
51         
52         
53         // TODO: this whole function wont work correctly
54         // once we move to hosts being in multiple groups..
55         function doOperation($url)
56         {
57                 $operation = $_REQUEST["operation"];
58                 $groupop = $_REQUEST["groupop"];
59                 $hosts = $this->config->getData("hosts");
60                 $groups = $this->config->getData("hostgroups");
61                 $nogroup = false;
62                 $hosts_to_do = null;
63                 $nhosts = 0;
64                 $group_to_do = null;
65                 $ngroups = 0;
66                 $ping_host = null;
67                 $nping = 0;
68                 
69                 // first check if nogroup is selected
70                 if(isset($_REQUEST["nonegroup-selected"])) {
71                         //echo "nonegroup operation<br>";
72                         $nogroup = true;
73                 }
74                 
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;
79                                 
80                         }
81                 }
82                 
83                 foreach($hosts as $key=>$val) {
84                         $dohost = false;
85                         $hname = $val["category"];
86                         // this bits annoying TODO: find out why this happens
87                         $thname = preg_replace("/\./", "_", $hname);
88                         $hip = $val["name"];
89                         $hg = $val["val"];
90                         
91                         echo "<br>Checking $hname, $hip, $hg<br>";
92                         
93                         // check if no group is on
94                         if($nogroup && $hg == "") {
95                                 //echo "Set doing true on nogroup<br>";
96                                 $dohost = true;
97                         }
98                         
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;
102                         }
103                         
104                         // directly selected hosts
105                         if(isset($_REQUEST["$thname-selecthost"])) {
106                                 //echo "add host $hname<br>";
107                                 $dohost = true;
108                         }
109                         
110                         // now do the op
111                         if($dohost) {
112                                 if($operation == "move") {
113                                         $this->config->delData("hosts", "$hname", "$hip", "$hg");
114                                         $this->config->addData("hosts", "$hname", "$hip", "$groupop");
115                                 }
116                                 if($operation == "delete") {
117                                         $this->config->delData("hosts", "$hname", "$hip", "$hg");
118                                 }
119                                 if($operation == "ping") {
120                                         $ping_host[$nping++] = $hip;
121                                 }
122                         }
123                 }
124                 
125                 if($operation == "ping") {
126                         foreach($ping_host as $lip) {
127                                 $ips .= " $lip";
128                         }
129                         
130                         error_log("ping helper is going with $ips");
131
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 &";
135                                 system($scall);
136                         } else {
137                                 error_log("cant find download helper... dieing");
138                         }
139                         
140                 }
141                 /*echo "total to do:<br><pre>";
142                 print_r($hosts_to_do);
143                 print_r($_REQUEST);
144                 echo "</pre>";*/
145                 
146                 global $WEB_ROOT_FS, $URL_HANDLERS, $BASE_URL;
147                 header("Location: $BASE_URL/hosts");
148                 
149                 
150         }
151         
152         function deleteGroup($url)
153         {
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"];
159                         $hip = $val["name"];
160                         $hg = $val["val"];
161                         
162                         if($hg == $grpname) {
163                                 $this->config->delData("hosts", "$hname", "$hip", "$hg");
164                                 $this->config->addData("hosts", "$hname", "$hip", "");
165                         }
166                 }
167                 
168                 global $WEB_ROOT_FS, $URL_HANDLERS, $BASE_URL;
169                 header("Location: $BASE_URL/hosts");
170                 
171         }
172         
173         function scanIPRange($url)
174         {
175                 $iprange = $_REQUEST["scanip"];
176                 $hostgroup = "";
177                 if(isset($_REQUEST["hostgroup"])) {
178                         $hostgroup = $_REQUEST["hostgroup"];
179                 }
180                 
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 &";
186                         system($scall);
187                 } else {
188                         error_log("cant find download helper... dieing");
189                 }
190                 
191                 global $WEB_ROOT_FS, $URL_HANDLERS, $BASE_URL;
192                 header("Location: $BASE_URL/hosts");
193                 
194                 
195         }
196         
197         function deleteHost($url)
198         {
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");
203         }
204         
205         function addHost($url)
206         {
207                 $hg = $_REQUEST["hostgroup"];
208                 $hostname = $_REQUEST["hostname"];
209                 $ip = $_REQUEST["ipaddr"];
210                 
211                 if($hostname == "" && $ip == "") {
212                         echo "Error: must have either ip or hostname\n";
213                         return 0;
214                 }
215                 
216                 if($hostname == "") {
217                         // try to lookup hostname from ip
218                         $hostname = gethostbyaddr($ip);
219                 }
220                 
221                 if($ip == "") {
222                         // try to lookup ip from hostname
223                         $ip = gethostbyname($hostname);
224                 }
225                 
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>";
230                                 return 0;
231                         }
232                 }
233                 
234                 $this->config->addData("hosts", "$hostname", "$ip", "$hg");
235                 
236                 
237                 global $WEB_ROOT_FS, $URL_HANDLERS, $BASE_URL;
238                 header("Location: $BASE_URL/hosts");
239                 
240         }
241         
242         function addGroup($url)
243         {
244                 $grpname = $_REQUEST["groupname"];
245                 $groups = $this->config->getData("hostgroups");
246                 
247                 foreach($groups as $key => $val) {
248                         if($val["category"] == $grpname) {
249                                 echo "Error: gorup already exists";
250                                 return 0;
251                         }
252                 }
253                 
254                 $this->config->adddata("hostgroups", "$grpname", "", "");
255                 global $WEB_ROOT_FS, $URL_HANDLERS, $BASE_URL;
256                 header("Location: $BASE_URL/hosts");
257                 
258         }
259         
260         function mainBody($url)
261         {
262                 $hosts = $this->config->getData("hosts");
263                 $groups = $this->config->getData("hostgroups");
264                 
265                 $gs = 0;
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>";
272                 echo "</select>";
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>";
278                 }
279                 echo "</select>";
280                 echo "<input type=\"submit\" name=\"Go\" value=\"Go\">";
281                 
282                 
283                 echo "<table border=\"1\">";
284                 
285                 $gs++;
286                 // fist print ungrouped
287                 echo "<tr valign=\"top\">";
288                 echo "<td>";
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"];
293                         $hip = $val["name"];
294                         $hg = $val["val"];
295                         if($hg == "") $hg = "-";
296                         if($hg=="-") {
297                                 $lastping_v = $this->config->getData("ping", $hip);
298                                 $lastping_o = $lastping_v[0];
299                                 if(!$lastping_v) $lastping = "None";
300                                 else {
301                                         if($lastping_o["val"] == "failed") {
302                                                 $lastp_time = glcas_tdiffToAgo(time()-$lastping_o["name"]);
303                                                 $lastping = "$lastp_time - <font color=\"red\">Failed</font>";
304                                         } else {
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>";
308                                         }
309                                 }
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>";
312                         }
313                 }
314                 echo "</table>";
315                 echo "</td>";
316
317                 if($groups) {
318                         foreach($groups as $key=>$val) {
319                                 $gs++;
320                                 echo "<td>";
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"];
326                                         $hip = $val["name"];
327                                         $hg = $val["val"];
328                                         if($hg == "") $hg = "-";
329                                         if($hg==$grpname) {
330                                                 $lastping_v = $this->config->getData("ping", $hip);
331                                                 $lastping_o = $lastping_v[0];
332                                                 if(!$lastping_v) $lastping = "None";
333                                                 else {
334                                                         if($lastping_o["val"] == "failed") {
335                                                                 $lastp_time = glcas_tdiffToAgo(time()-$lastping_o["name"]);
336                                                                 $lastping = "$lastp_time - <font color=\"red\">Failed</font>";
337                                                         } else {
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>";
341                                                         }
342                                                 }
343                                                                                                 
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>";
346                                         }
347                                 }
348                                 echo "</table>";
349                                 echo "</td>";
350                                 if(($gs%4)==0) echo "</tr><tr>";
351                         }
352                 }
353                 
354                 
355                 echo "</tr></table>";
356                 echo "</form>";
357                 
358                 echo "<hr>";
359                 
360                 
361                 
362                 echo "<table><tr valign=\"top\"><td>";
363                 
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>";
369                 if($groups) {
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>";
375                         }
376                         echo "</select><br>";
377                 }
378                 echo "<input type=\"submit\" name=\"add\" value=\"Add\">";
379                 echo "</form>";
380                 
381                 echo "</td><td>";
382                 
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>";
388                 echo "</form>";
389                 
390                 echo "</td><td>";
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>";
395                 if($groups) {
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>";
401                         }
402                         echo "</select><br>";
403                 }
404                 echo "<input type=\"submit\" name=\"Add\" value=\"Add\"><br>";
405                 echo "</form>";
406                                 
407                 echo "</table>";
408                 
409         }
410         
411         private $config;
412 }
413
414 ?>