can('host','read')) return; $menuItems[] = array( 'sort'=>3, 'url'=>'index.php?module=host', 'label'=>'Hébergement', 'icon'=> 'fab fa-ubuntu', 'color'=> '#d35400' ); } //Cette fonction va generer une page quand on clique sur Hébergement dans menu function host_page(){ global $_; if(!isset($_['module']) || $_['module'] !='host') return; $page = !isset($_['page']) ? 'list.machine' : $_['page']; $page = str_replace('..','',$page); $file = __DIR__.SLASH.'page.'.$page.'.php'; if(!file_exists($file)) throw new Exception("Page ".$page." inexistante"); require_once($file); } //Fonction executée lors de l'activation du plugin function host_install($id){ if($id != 'fr.core.host') return; Entity::install(__DIR__); $dictionary = new Dictionary(); $dictionary->slug = 'host_machine_os'; if(Dictionary::rowCount(array('slug'=>$dictionary->slug)) ==0){ $dictionary->label = 'Machine / Environnement : Système d\'exploitation'; $dictionary->parent = 0; $dictionary->state = Dictionary::ACTIVE; $dictionary->save(); foreach(array( 'other'=>'Autre', 'linux_ubuntu'=>'Linux - Ubuntu 12.4', 'linux_ubuntu'=>'Linux - Ubuntu 14.4', 'linux_ubuntu'=>'Linux - Ubuntu 16.4', 'linux_ubuntu'=>'Linux - Ubuntu 18.4', 'linux_ubuntu'=>'Linux - Ubuntu 20.4', 'linux_ubuntu'=>'Linux - Ubuntu 22.4', 'window'=>'Windows server', ) as $key=>$value){ $item = new Dictionary(); $item->slug = 'host_machine_os_'.$key; $item->label = $value; $item->parent = $dictionary->id; $item->state = Dictionary::ACTIVE; $item->save(); } } } //Fonction executée lors de la désactivation du plugin function host_uninstall($id){ if($id != 'fr.core.host') return; Entity::uninstall(__DIR__); $dictionary = Dictionary::bySlug('host_machine_os'); if($dictionary!= false && $dictionary->id!=0){ Dictionary::delete(array('parent'=>$dictionary->id)); Dictionary::delete(array('id'=>$dictionary->id)); } } //Déclaration des sections de droits du plugin Right::register("host",array('label'=>"Gestion des droits sur le plugin Hébergement")); //cette fonction comprends toutes les actions du plugin qui ne nécessitent pas de vue html function host_action(){ require_once(__DIR__.SLASH.'action.php'); } //Déclaration du menu de réglages function host_menu_setting(&$settingMenu){ global $myUser; if(!$myUser->can('host','configure')) return; $settingMenu[]= array( 'sort' =>1, 'url' => 'setting.php?section=global.host', 'icon' => 'fas fa-angle-right', 'label' => 'Hébergement' ); } //Déclaration des pages de réglages function host_content_setting(){ global $_; if(file_exists(__DIR__.SLASH.'setting.'.$_['section'].'.php')) require_once(__DIR__.SLASH.'setting.'.$_['section'].'.php'); } require_once(__DIR__.SLASH.'action.php'); function host_ping_url($url){ $response = array(); try{ if(!preg_match('/^https?:\/\//im', $url)) $url = 'http://'.$url; $request = @get_headers($url, true); $response['code'] = 500; $response['message'] = 'Erreur inconnue'; if(!$request){ $response['code'] = 500; $response['message'] = 'Impossible de requeter l\'url'; return $response; } if(is_array($request) && isset($request[0])){ $headers = array(); // Make more sane response foreach($request as $h => $v) { if(is_int($h)){ $headers[$h]['Status'] = $v; } else { if(is_string($v)) $headers[0][$h] = $v; } } $lastHeader = end($headers); $response['code'] = $lastHeader['Status']; preg_match('/HTTP[^\s]* ([0-9]*) (.*)/i',$lastHeader['Status'],$match); $response['code'] = $match[1]; $response['message'] = $match[2]; }else{ $response['message'] = 'Pas de détails'; } }catch(Exception $e){ $response['code'] = 500; $response['message'] = $e->getMessage(); } return $response; } function host_cron($time){ global $conf,$_; if(!in_array(date('H:i', $time), array(5,10,15,20,25,30,35,40,45,50,55,0)) && !isset($_['force-host']) ) return; $errors = array(); $resolved = array(); require_once(__DIR__.SLASH.'Machine.class.php'); require_once(__DIR__.SLASH.'MachineApplication.class.php'); Plugin::need('client/Client'); foreach (MachineApplication::loadAll(array('monitored'=>1), null, null, array('*'), 2) as $key => $application) { if(empty($application->url)) continue; $machine = $application->join('machine'); $pendingDown = History::load(array('uid'=>$application->id,'scope'=>'host_application','meta'=>null,'type'=>'down')); foreach(explode(',',$application->url) as $url){ $response = host_ping_url($url); echo $response['code'].' : '. $url.PHP_EOL; if($response['code']!=200){ $item = new History(); $item->uid = $application->id; $item->scope = 'host_application'; $item->type = 'down'; $item->comment = 'L\'application est en erreur avec le code '. $response['code'].' : '.$response['message']; if(!$pendingDown){ $errors[] = array('machine'=>$machine,'application'=>$application,'ping'=>$response); $item->save(); } }else{ if(!is_object($pendingDown)) continue; $pendingDown->meta = json_encode(array('resolved' => time())); $pendingDown->save(); $item = new History(); $item->uid = $application->id; $item->scope = 'host_application'; $item->type = 'up'; $item->comment = 'L\'application est de nouveau opérationnelle après '.relative_time($item->created,time()).' d\'inactivité'; $item->save(); $resolved[]= array('machine'=>$machine,'application'=>$application,'ping'=>$response,'down'=>$pendingDown); } } } if(count($errors) > 0){ if(count($errors)>1){ $title = '🔥 '.count($errors).' noms de domaines ne répondent plus'; $message = '

Les noms de domaines suivants sont down :