moved admin to repo admin
[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["10hosts"]["name"] = "Known Machines";
9 $MENU_ITEMS["10hosts"]["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 "scanrange":
32                                         GLCASpageBuilder($this, "scanIPRange");
33                                         return;
34                                 case "delgroup":
35                                         GLCASpageBuilder($this, "deleteGroup");
36                                         return;
37                                 case "dooperation":
38                                         error_log("in do operation");
39                                         GLCASpageBuilder($this, "doOperation");
40                                         return;
41                         }
42                 }
43                 
44                 GLCASpageBuilder($this,"mainBody");
45                 return;
46                 
47         }
48         
49         
50         // TODO: this whole function wont work correctly
51         // once we move to hosts being in multiple groups..
52         function doOperation($url)
53         {
54                 $operation = $_REQUEST["operation"];
55                 $groupop = $_REQUEST["groupop"];
56                 $hosts = $this->config->getData("hosts");
57                 $groups = $this->config->getData("hostgroups");
58                 $nogroup = false;
59                 $hosts_to_do = null;
60                 $nhosts = 0;
61                 $group_to_do = null;
62                 $ngroups = 0;
63                 $ping_host = null;
64                 $nping = 0;
65                 
66                 // first check if nogroup is selected
67                 if(isset($_REQUEST["nonegroup-selected"])) {
68                         //echo "nonegroup operation<br>";
69                         $nogroup = true;
70                 }
71                 
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;
76                                 
77                         }
78                 }
79                 
80                 foreach($hosts as $key=>$val) {
81                         $dohost = false;
82                         $hname = $val["category"];
83                         // this bits annoying TODO: find out why this happens
84                         $thname = preg_replace("/\./", "_", $hname);
85                         $hip = $val["name"];
86                         $hg = $val["val"];
87                         
88                         //echo "<br>Checking $hname, $hip, $hg<br>";
89                         
90                         // check if no group is on
91                         if($nogroup && $hg == "") {
92                                 //echo "Set doing true on nogroup<br>";
93                                 $dohost = true;
94                         }
95                         
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;
99                         }
100                         
101                         // directly selected hosts
102                         if(isset($_REQUEST["$thname-selecthost"])) {
103                                 //echo "add host $hname<br>";
104                                 $dohost = true;
105                         }
106                         
107                         // now do the op
108                         if($dohost) {
109                                 if($operation == "move") {
110                                         $this->config->delData("hosts", "$hname", "$hip", "$hg");
111                                         $this->config->addData("hosts", "$hname", "$hip", "$groupop");
112                                 }
113                                 if($operation == "delete") {
114                                         $this->config->delData("ping", "$hip");
115                                         $this->config->delData("lastseen", "$hip");
116                                         $this->config->delData("hosts", "$hname", "$hip", "$hg");
117                                 }
118                                 if($operation == "ping") {
119                                         $ping_host[$nping++] = $hip;
120                                 }
121                         }
122                 }
123                 
124                 if($operation == "ping") {
125                         foreach($ping_host as $lip) {
126                                 $ips .= " $lip";
127                         }
128                         
129                         error_log("ping helper is going with $ips");
130
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 &";
134                                 system($scall);
135                         } else {
136                                 error_log("cant find download helper... dieing");
137                         }
138                         
139                 }
140                 /*echo "total to do:<br><pre>";
141                 print_r($hosts_to_do);
142                 print_r($_REQUEST);
143                 echo "</pre>";*/
144                 
145                 global $WEB_ROOT_FS, $URL_HANDLERS, $BASE_URL;
146                 header("Location: $BASE_URL/hosts");
147                 
148                 
149         }
150         
151         function deleteGroup($url)
152         {
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"];
158                         $hip = $val["name"];
159                         $hg = $val["val"];
160                         
161                         if($hg == $grpname) {
162                                 $this->config->delData("hosts", "$hname", "$hip", "$hg");
163                                 $this->config->addData("hosts", "$hname", "$hip", "");
164                         }
165                 }
166                 
167                 global $WEB_ROOT_FS, $URL_HANDLERS, $BASE_URL;
168                 header("Location: $BASE_URL/hosts");
169                 
170         }
171         
172         function scanIPRange($url)
173         {
174                 $iprange = $_REQUEST["scanip"];
175                 $hostgroup = "";
176                 if(isset($_REQUEST["hostgroup"])) {
177                         $hostgroup = $_REQUEST["hostgroup"];
178                 }
179                 
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 &";
185                         system($scall);
186                 } else {
187                         error_log("cant find download helper... dieing");
188                 }
189                 
190                 global $WEB_ROOT_FS, $URL_HANDLERS, $BASE_URL;
191                 header("Location: $BASE_URL/hosts");
192                 
193                 
194         }
195         
196         function addHost($url)
197         {
198                 $hg = $_REQUEST["hostgroup"];
199                 $hostname = $_REQUEST["hostname"];
200                 $ip = $_REQUEST["ipaddr"];
201                 
202                 if($hostname == "" && $ip == "") {
203                         echo "Error: must have either ip or hostname\n";
204                         return 0;
205                 }
206                 
207                 if($hostname == "") {
208                         // try to lookup hostname from ip
209                         $hostname = gethostbyaddr($ip);
210                 }
211                 
212                 if($ip == "") {
213                         // try to lookup ip from hostname
214                         $ip = gethostbyname($hostname);
215                 }
216                 
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>";
221                                 return 0;
222                         }
223                 }
224                 
225                 $this->config->addData("hosts", "$hostname", "$ip", "$hg");
226                 
227                 
228                 global $WEB_ROOT_FS, $URL_HANDLERS, $BASE_URL;
229                 header("Location: $BASE_URL/hosts");
230                 
231         }
232         
233         function addGroup($url)
234         {
235                 $grpname = $_REQUEST["groupname"];
236                 $groups = $this->config->getData("hostgroups");
237                 
238                 foreach($groups as $key => $val) {
239                         if($val["category"] == $grpname) {
240                                 echo "Error: gorup already exists";
241                                 return 0;
242                         }
243                 }
244                 
245                 $this->config->adddata("hostgroups", "$grpname", "", "");
246                 global $WEB_ROOT_FS, $URL_HANDLERS, $BASE_URL;
247                 header("Location: $BASE_URL/hosts");
248                 
249         }
250         
251         function mainBody($url)
252         {
253                 $hosts = $this->config->getData("hosts");
254                 $groups = $this->config->getData("hostgroups");
255                 
256                 $gs = 0;
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>";
263                 echo "</select>";
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>";
269                 }
270                 echo "</select>";
271                 echo "<input type=\"submit\" name=\"Go\" value=\"Go\">";
272                 
273                 
274                 echo "<table border=\"1\">";
275                 
276                 $gs++;
277                 // fist print ungrouped
278                 echo "<tr valign=\"top\">";
279                 echo "<td>";
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"];
284                         $hip = $val["name"];
285                         $hg = $val["val"];
286                         if($hg == "") $hg = "-";
287                         if($hg=="-") {
288                                 $lastping_v = $this->config->getData("ping", $hip);
289                                 $lastping_o = $lastping_v[0];
290                                 if(!$lastping_v) $lastping = "None";
291                                 else {
292                                         if($lastping_o["val"] == "failed") {
293                                                 $lastp_time = glcas_tdiffToAgo(time()-$lastping_o["name"]);
294                                                 $lastping = "$lastp_time - <font color=\"red\">Failed</font>";
295                                         } else {
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>";
299                                         }
300                                 }
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>";
303                         }
304                 }
305                 echo "</table>";
306                 echo "</td>";
307
308                 if($groups) {
309                         foreach($groups as $key=>$val) {
310                                 $gs++;
311                                 echo "<td>";
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"];
317                                         $hip = $val["name"];
318                                         $hg = $val["val"];
319                                         if($hg == "") $hg = "-";
320                                         if($hg==$grpname) {
321                                                 $lastping_v = $this->config->getData("ping", $hip);
322                                                 $lastping_o = $lastping_v[0];
323                                                 if(!$lastping_v) $lastping = "None";
324                                                 else {
325                                                         if($lastping_o["val"] == "failed") {
326                                                                 $lastp_time = glcas_tdiffToAgo(time()-$lastping_o["name"]);
327                                                                 $lastping = "$lastp_time - <font color=\"red\">Failed</font>";
328                                                         } else {
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>";
332                                                         }
333                                                 }
334                                                                                                 
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>";
337                                         }
338                                 }
339                                 echo "</table>";
340                                 echo "</td>";
341                                 if(($gs%4)==0) echo "</tr><tr>";
342                         }
343                 }
344                 
345                 
346                 echo "</tr></table>";
347                 echo "</form>";
348                 
349                 echo "<hr>";
350                 
351                 
352                 
353                 echo "<table><tr valign=\"top\"><td>";
354                 
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>";
360                 if($groups) {
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>";
366                         }
367                         echo "</select><br>";
368                 }
369                 echo "<input type=\"submit\" name=\"add\" value=\"Add\">";
370                 echo "</form>";
371                 
372                 echo "</td><td>";
373                 
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>";
379                 echo "</form>";
380                 
381                 echo "</td><td>";
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>";
386                 if($groups) {
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>";
392                         }
393                         echo "</select><br>";
394                 }
395                 echo "<input type=\"submit\" name=\"Add\" value=\"Add\"><br>";
396                 echo "</form>";
397                                 
398                 echo "</table>";
399                 
400         }
401         
402         private $config;
403 }
404
405 ?>