fecbed4577f41092fbab49d7964bee9531394651
[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("ping", "$hip");
118                                         $this->config->delData("lastseen", "$hip");
119                                         $this->config->delData("hosts", "$hname", "$hip", "$hg");
120                                 }
121                                 if($operation == "ping") {
122                                         $ping_host[$nping++] = $hip;
123                                 }
124                         }
125                 }
126                 
127                 if($operation == "ping") {
128                         foreach($ping_host as $lip) {
129                                 $ips .= " $lip";
130                         }
131                         
132                         error_log("ping helper is going with $ips");
133
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 &";
137                                 system($scall);
138                         } else {
139                                 error_log("cant find download helper... dieing");
140                         }
141                         
142                 }
143                 /*echo "total to do:<br><pre>";
144                 print_r($hosts_to_do);
145                 print_r($_REQUEST);
146                 echo "</pre>";*/
147                 
148                 global $WEB_ROOT_FS, $URL_HANDLERS, $BASE_URL;
149                 header("Location: $BASE_URL/hosts");
150                 
151                 
152         }
153         
154         function deleteGroup($url)
155         {
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"];
161                         $hip = $val["name"];
162                         $hg = $val["val"];
163                         
164                         if($hg == $grpname) {
165                                 $this->config->delData("hosts", "$hname", "$hip", "$hg");
166                                 $this->config->addData("hosts", "$hname", "$hip", "");
167                         }
168                 }
169                 
170                 global $WEB_ROOT_FS, $URL_HANDLERS, $BASE_URL;
171                 header("Location: $BASE_URL/hosts");
172                 
173         }
174         
175         function scanIPRange($url)
176         {
177                 $iprange = $_REQUEST["scanip"];
178                 $hostgroup = "";
179                 if(isset($_REQUEST["hostgroup"])) {
180                         $hostgroup = $_REQUEST["hostgroup"];
181                 }
182                 
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 &";
188                         system($scall);
189                 } else {
190                         error_log("cant find download helper... dieing");
191                 }
192                 
193                 global $WEB_ROOT_FS, $URL_HANDLERS, $BASE_URL;
194                 header("Location: $BASE_URL/hosts");
195                 
196                 
197         }
198         
199         function deleteHost($url)
200         {
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");
205         }
206         
207         function addHost($url)
208         {
209                 $hg = $_REQUEST["hostgroup"];
210                 $hostname = $_REQUEST["hostname"];
211                 $ip = $_REQUEST["ipaddr"];
212                 
213                 if($hostname == "" && $ip == "") {
214                         echo "Error: must have either ip or hostname\n";
215                         return 0;
216                 }
217                 
218                 if($hostname == "") {
219                         // try to lookup hostname from ip
220                         $hostname = gethostbyaddr($ip);
221                 }
222                 
223                 if($ip == "") {
224                         // try to lookup ip from hostname
225                         $ip = gethostbyname($hostname);
226                 }
227                 
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>";
232                                 return 0;
233                         }
234                 }
235                 
236                 $this->config->addData("hosts", "$hostname", "$ip", "$hg");
237                 
238                 
239                 global $WEB_ROOT_FS, $URL_HANDLERS, $BASE_URL;
240                 header("Location: $BASE_URL/hosts");
241                 
242         }
243         
244         function addGroup($url)
245         {
246                 $grpname = $_REQUEST["groupname"];
247                 $groups = $this->config->getData("hostgroups");
248                 
249                 foreach($groups as $key => $val) {
250                         if($val["category"] == $grpname) {
251                                 echo "Error: gorup already exists";
252                                 return 0;
253                         }
254                 }
255                 
256                 $this->config->adddata("hostgroups", "$grpname", "", "");
257                 global $WEB_ROOT_FS, $URL_HANDLERS, $BASE_URL;
258                 header("Location: $BASE_URL/hosts");
259                 
260         }
261         
262         function mainBody($url)
263         {
264                 $hosts = $this->config->getData("hosts");
265                 $groups = $this->config->getData("hostgroups");
266                 
267                 $gs = 0;
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>";
274                 echo "</select>";
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>";
280                 }
281                 echo "</select>";
282                 echo "<input type=\"submit\" name=\"Go\" value=\"Go\">";
283                 
284                 
285                 echo "<table border=\"1\">";
286                 
287                 $gs++;
288                 // fist print ungrouped
289                 echo "<tr valign=\"top\">";
290                 echo "<td>";
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"];
295                         $hip = $val["name"];
296                         $hg = $val["val"];
297                         if($hg == "") $hg = "-";
298                         if($hg=="-") {
299                                 $lastping_v = $this->config->getData("ping", $hip);
300                                 $lastping_o = $lastping_v[0];
301                                 if(!$lastping_v) $lastping = "None";
302                                 else {
303                                         if($lastping_o["val"] == "failed") {
304                                                 $lastp_time = glcas_tdiffToAgo(time()-$lastping_o["name"]);
305                                                 $lastping = "$lastp_time - <font color=\"red\">Failed</font>";
306                                         } else {
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>";
310                                         }
311                                 }
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>";
314                         }
315                 }
316                 echo "</table>";
317                 echo "</td>";
318
319                 if($groups) {
320                         foreach($groups as $key=>$val) {
321                                 $gs++;
322                                 echo "<td>";
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"];
328                                         $hip = $val["name"];
329                                         $hg = $val["val"];
330                                         if($hg == "") $hg = "-";
331                                         if($hg==$grpname) {
332                                                 $lastping_v = $this->config->getData("ping", $hip);
333                                                 $lastping_o = $lastping_v[0];
334                                                 if(!$lastping_v) $lastping = "None";
335                                                 else {
336                                                         if($lastping_o["val"] == "failed") {
337                                                                 $lastp_time = glcas_tdiffToAgo(time()-$lastping_o["name"]);
338                                                                 $lastping = "$lastp_time - <font color=\"red\">Failed</font>";
339                                                         } else {
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>";
343                                                         }
344                                                 }
345                                                                                                 
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>";
348                                         }
349                                 }
350                                 echo "</table>";
351                                 echo "</td>";
352                                 if(($gs%4)==0) echo "</tr><tr>";
353                         }
354                 }
355                 
356                 
357                 echo "</tr></table>";
358                 echo "</form>";
359                 
360                 echo "<hr>";
361                 
362                 
363                 
364                 echo "<table><tr valign=\"top\"><td>";
365                 
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>";
371                 if($groups) {
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>";
377                         }
378                         echo "</select><br>";
379                 }
380                 echo "<input type=\"submit\" name=\"add\" value=\"Add\">";
381                 echo "</form>";
382                 
383                 echo "</td><td>";
384                 
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>";
390                 echo "</form>";
391                 
392                 echo "</td><td>";
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>";
397                 if($groups) {
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>";
403                         }
404                         echo "</select><br>";
405                 }
406                 echo "<input type=\"submit\" name=\"Add\" value=\"Add\"><br>";
407                 echo "</form>";
408                                 
409                 echo "</table>";
410                 
411         }
412         
413         private $config;
414 }
415
416 ?>