3v4l.org

run code in 150+ php & hhvm versions
Bugs & Features
<?php class pstate { //wantedstate: 1: ipmi on, 2: icmp ping an ip ok, 3: sockping an port ok private $groupMembers=array(); private $mgmtGroupsToCheck = array(); private $mgmtGroupNames=array(); private $mgmtGroups=array(); private $todo = array(); private $pdf20; private $portsToCheck=array('ig-p01'=>22); //usage: 'hostname'=>port-number function __construct(&$config) { $this->config = $config; $this->base = new base($config); require_once('lib/wpa_class_pdf20.php'); $this->pdf20=new pdf20($this->base); }//construct public function getGroups() { if(!isset($_REQUEST['groupID'])||empty($_REQUEST['groupID'])) { echo "KeineGroup-ID abgegeben!"; exit; } $this->mgmtGroupsToCheck=explode(';',$_REQUEST['groupID']); $this->mgmtGroupsToCheck=array_unique($this->mgmtGroupsToCheck); for($i=0;$i<count($this->mgmtGroupsToCheck);$i++){ if(strlen($this->mgmtGroupsToCheck[$i])==0) unset($this->mgmtGroupsToCheck[$i]); } } private function getStatusProj(&$projstatus, &$projector) { $projstatus = substr($projstatus, 15, 1); switch ($projstatus){ case 1: $col = 'yellow'; break; case 2: $col = 'green'; break; case 3: $col = 'red'; break; case 4: $col = 'blue'; break; } echo '<tr><td>'.$projector.'</td><td bgcolor=' . $col . '>'; switch ($projstatus){ case 1: echo 'warming up'; //$col = 'yellow'; break; case 2: echo 'online'; //$col = 'green'; break; case 3: echo 'offline'; //$col = 'red'; break; case 4: echo 'cooling down'; //$col = 'blue'; break; } echo "</td><td></td></tr>"; }//getStatusProj private function getStatus(&$host) { $col = 'red'; if ($host['wantedstate'] == $host['status']) $col = 'green'; echo '<tr><td>'.$host['hostname'].'</td><td bgcolor=' . $col . '>'; switch ($host['status']){ case -1: echo 'Status konnte nicht ermittelt werden'; break; case 0: $array['poweron'][0]=$host['mgmtip']; echo 'Netzteil aus </td><td style="width:67px; background-color:grey; border:1px solid white; font-weight:bold; text-align:center;"><a style=" background-color:grey; color:black; text-decoration:none;" href="'.$_SERVER['PHP_SELF'].'?correct='.base64_encode(serialize($array)).'&groupID='.$_REQUEST['groupID'].'">Start</a>'; if ('red' == $col) $this->todo['poweron'][] = $host['mgmtip']; break; case 1: $array['reboot'][0]=$host['ip']; echo 'Netzteil ein </td><td style="width:67px; background-color:grey; border:1px solid white; font-weight:bold; text-align:center;"><a style=" background-color:grey; color:black; text-decoration:none;" href="'.$_SERVER['PHP_SELF'].'?correct='.base64_encode(serialize($array)).'&groupID='.$_REQUEST['groupID'].'">Neustart</a>'; if ('red' == $col) $this->todo['reboot'][] = $host['ip']; break; case 2: $array['reboot'][0]=$host['ip']; echo 'Ping ok </td><td style="width:67px; background-color:grey; border:1px solid white; font-weight:bold; text-align:center;"> <a style="background-color:grey; color:black; text-decoration:none;" href="'.$_SERVER['PHP_SELF'].'?correct='.base64_encode(serialize($array)).'&groupID='.$_REQUEST['groupID'].'">Neustart</a>'; if ('red' == $col) $this->todo['reboot'][] = $host['ip']; break; case 3: $array['reboot'][0]=$host['ip']; echo 'online <td style="width:67px; background-color:grey; border:1px solid white; font-weight:bold; text-align:center;"></td> <a style=" background-color:grey; color:black; text-decoration:none;" href="'.$_SERVER['PHP_SELF'].'?correct='.base64_encode(serialize($array)).'&groupID='.$_REQUEST['groupID'].'">Neustart</a>'; if ('red' == $col) $this->todo['reboot'][] = $host['ip']; break; } echo "</td></tr>"; }//getStatus public function getUpHosts($todo) { foreach ($todo['reboot'] as $reboot){ shell_exec("net rpc -Uesigma%esigma -I$reboot shutdown -r -t 1 -f"); } foreach ($todo['poweron'] as $pon){ shell_exec("ipmitool -UADMIN -PADMIN -H$pon power on"); } echo "Bitte eine Minute warten und den Status erneut <a href=".$_SERVER['PHP_SELF'].'?groupID='.$_REQUEST['groupID'].'>abfragen</a>.'; exit; }//getUpHosts public function getPortsFormDB($tmpHosts) //gets ports out of db { $sqlString='SELECT * from os_types'; $osTypes=$this->base->db->query($sqlString); $sqlstring='Select regex from hosts where os_type=1'; $linuxHostsTmp=$this->base->db->query($sqlstring); $linuxHosts=array(); foreach($linuxHostsTmp as $host) { $linuxHosts[]=$host['regex']; } unset($linuxHostsTmp); for($i=0;$i<count($tmpHosts);$i++) { //echo $host['hostname']; if(in_array($tmpHosts[$i]['hostname'],$linuxHosts)) { $tmpHosts[$i]['port']=22; } } return $tmpHosts; } public function simpleGetPorts($tmpHosts) //gehts ports from$this->portsToCheck { for($i=0;$i<count($tmpHosts);$i++) { //echo $host['hostname']; if(array_key_exists($tmpHosts[$i]['hostname'],$this->portsToCheck)) { $tmpHosts[$i]['port']=$this->portsToCheck[$tmpHosts[$i]['hostname']]; } } return $tmpHosts; } public function setPortToCheck($tmpHosts,$mode=1) { switch($mode){ case 1: $tmpHosts=$this->getPortsFromDB($tmpHosts); break; case 2: $tmpHosts=$this->simpleGetPorts($tmpHosts); break; case 3: $tmpHosts=$this->getPortsFromDB($tmpHosts); $tmpHosts=$this->simpleGetPorts($tmpHosts); break; default: return false; break; }//switch return $tmpHosts; } public function run() { $this->getGroups(); $this->base->connectDB(); $sqlPre = 'SELECT H.regex AS hostname, U.value AS IP, M.ip AS mgmtip, -1 AS status, 33445 AS port, 3 AS wantedstate ' . ' FROM unattended U LEFT JOIN (hosts H LEFT JOIN (mgmt M JOIN mgmt_group_members MGM ON (M.id = MGM.mid )) ON ' . " H.mgmt_id = M.id) ON U.lookup = H.lookup where U.property = 'IP' AND MGM.gid = "; $sqlPost = ' ORDER BY hostname'; $cmdPre = 'php ' . dirname($_SERVER['SCRIPT_FILENAME']) . '/tools/checkPower.php '; $hosts = array(); $allHosts = array(); $allHosts2 = array(); $sqlString = 'SELECT gid from mgmt_groups'; $this->mgmtGroups=$this->base->db->query($sqlString); for($i=0;$i<count($this->mgmtGroups);$i++) { $this->mgmtGroups[$i]=$this->mgmtGroups[$i]['gid']; } $isntGID=array(); foreach($this->mgmtGroupsToCheck as $gid) //existiert groupID? { if(!in_array($gid,$this->mgmtGroups)){ $isntGID[]= $gid; unset($this->mgmtGroupsToCheck[array_search($gid,$this->mgmtGroupsToCheck)]); } } $sqlString = 'SELECT mgmt_groups.gid AS gid, mgmt_groups.groupname AS name FROM mgmt_groups where gid in ('.implode(',',$this->mgmtGroupsToCheck).')'; $tmpGroupNames=$this->base->db->query($sqlString); foreach($tmpGroupNames as $group) { $this->mgmtGroupNames[$group['gid']]=$group['name']; } foreach ($this->mgmtGroupsToCheck as $gid) { $sqlString='Select mid AS mid from mgmt_group_members where gid='.$gid; $gmem=$this->base->db->query($sqlString); for($i=0;$i<count($gmem);$i++) { $gmem[$i]=$gmem[$i]['mid']; } $sqlString="select mgmt.ip AS ip , mgmt.port AS port, mgmt.description AS hostname, 3 AS wantedstate, mgmt.ip as mgmtip, -1 AS status FROM mgmt where id in (".implode(',',$gmem).")AND type='pdf20'"; $pdf20Proj=$this->base->db->query($sqlString); $tmpHosts = $this->base->db->query($sqlPre . $gid . $sqlPost); $tmpHosts=$this->setPortToCheck($tmpHosts,2); $tmpHosts=array_merge($tmpHosts,$pdf20Proj); $hosts[]=$tmpHosts; foreach($hosts as $memberArray) { foreach($memberArray as $member) $this->groupMembers[$member['hostname']]=$gid; } $allHosts = array_merge($allHosts, end($hosts)); } $exchangeFile = tempnam('/tmp', 'wpkgWeb'); file_put_contents($exchangeFile, serialize($allHosts)); $err = exec($cmdPre . $exchangeFile); $tStatus = unserialize(file_get_contents($exchangeFile)); unlink($exchangeFile); echo '<table border=0 bgcolor=lightgrey cellspacing=0 style="font-family:verdana;font-size:14;font-weight:bold">'; echo '<tr bgcolor=grey>'; foreach($this->mgmtGroupsToCheck as $gid) echo "<td>".$this->mgmtGroupNames[$gid]."</td>"; if(count($this->mgmtGroupsToCheck)<3) { $num=3-count($this->mgmtGroupsToCheck); for($i=0;$i<$num;$i++) echo '<td></td>'; } echo '</tr>'; echo '<tr valign=top>'; for ($i=0; $i<count($this->mgmtGroupsToCheck); $i++){ echo '<td><table border=0 cellspacing=2 style="font-family:verdana;font-size:14">'; $grpPrefix = substr($hosts[$i][0]['hostname'], 0, 2); foreach ($hosts[$i] as &$host){ if($this->groupMembers[$host['hostname']]==$this->mgmtGroupsToCheck[$i]); if($host['port']==1025) { $retVal=$this->pdf20->getPowerState($host['ip'], $host['port']); $this->getStatusProj($retVal, $host['hostname']); }else $this->getStatus($tStatus[$host['hostname']]['host']); // $this->getStatus($host); } echo '</table></td>'; } echo '</tr>'; echo '<tr bgcolor=grey><td><a href='.$_SERVER['PHP_SELF'].'?groupID='.$_REQUEST['groupID'].'>Aktualisieren</a>'; echo ' <a href=' . $_SERVER['PHP_SELF'].'?auto=5&groupID='.$_REQUEST['groupID'].'>(Auto)</a>'; echo '</td>'; echo '<td colspan='.(count($this->mgmtGroupsToCheck)-2).' align=center>' . date('d.m.y H:i:s') . '</td><td align=right>'; if (count($this->todo)>0) echo '<a href='.$_SERVER['PHP_SELF'].'?correct='.base64_encode(serialize($this->todo)).'&groupID='.$_REQUEST['groupID'].'>Fehler beheben</a>'; echo'</td></tr></table>'; if(count($isntGID)>0) { $i=0; echo "<font color=red>Zu folgenden Group-IDs existiert keine Gruppe : "; foreach($isntGID as $gid){ if($i>0) echo ', '.$gid; else echo $gid; $i++; } echo "</font>"; } }//run }//class pstate echo '<html><head><title>SSIMINFWA Powerstatus</title>'; if (array_key_exists('auto', $_REQUEST)) echo '<meta http-equiv="refresh" content=' . $_REQUEST['auto'] . '>'; echo '</head><body>'; require_once('config.php'); if (isset($_REQUEST['noDebug'])) $config->debugmode = false; if ($config->debugmode) ini_set('display_errors','on'); else ini_set('display_errors','off'); require_once('lib/wpa_class_base.php'); $power = new pstate($config); if (array_key_exists('correct', $_REQUEST)) $power->getUpHosts(unserialize(base64_decode($_REQUEST['correct']))); $power->run(); echo '</body></html>';
Output for 5.3.0 - 5.6.28, 7.0.0 - 7.1.0
<html><head><title>SSIMINFWA Powerstatus</title></head><body> Warning: require_once(config.php): failed to open stream: No such file or directory in /in/fINjl on line 329 Fatal error: require_once(): Failed opening required 'config.php' (include_path='.:') in /in/fINjl on line 329
Process exited with code 255.
Output for hhvm-3.10.0 - 3.12.0
<html><head><title>SSIMINFWA Powerstatus</title></head><body> Fatal error: File not found: config.php in /in/fINjl on line 329
Process exited with code 255.