PHP Console Monitor

De Wiki levelKro
Révision datée du 25 janvier 2019 à 05:32 par LevelKro (discussion | contributions)
(diff) ← Version précédente | Voir la version actuelle (diff) | Version suivante → (diff)

Permet d'afficher le états des serveurs défini et de leur services (réponse). Simple affichage en console des résultats aux Ping avec personnalisation de l'affichage.

Pré-requis

  • PHP-CLI 5.4+
  • Shell (Telnet/SSH ou Prompt de commande)
  • PHP Minecraft Query API si vous désirez faire un ping d'un serveur Minecraft

Fichiers

colors.php

Pour rendre le résultat en couleur.

<?php
	class Colors {
		private $foreground_colors = array();
		private $background_colors = array();

		public function __construct() {
			// Set up shell colors
			$this->foreground_colors['black'] = '0;30';
			$this->foreground_colors['dark_gray'] = '1;30';
			$this->foreground_colors['blue'] = '0;34';
			$this->foreground_colors['light_blue'] = '1;34';
			$this->foreground_colors['green'] = '0;32';
			$this->foreground_colors['light_green'] = '1;32';
			$this->foreground_colors['cyan'] = '0;36';
			$this->foreground_colors['light_cyan'] = '1;36';
			$this->foreground_colors['red'] = '0;31';
			$this->foreground_colors['light_red'] = '1;31';
			$this->foreground_colors['purple'] = '0;35';
			$this->foreground_colors['light_purple'] = '1;35';
			$this->foreground_colors['brown'] = '0;33';
			$this->foreground_colors['yellow'] = '1;33';
			$this->foreground_colors['light_gray'] = '0;37';
			$this->foreground_colors['white'] = '1;37';

			$this->background_colors['black'] = '40';
			$this->background_colors['red'] = '41';
			$this->background_colors['green'] = '42';
			$this->background_colors['yellow'] = '43';
			$this->background_colors['blue'] = '44';
			$this->background_colors['magenta'] = '45';
			$this->background_colors['cyan'] = '46';
			$this->background_colors['light_gray'] = '47';
		}

		// Returns colored string
		public function getColoredString($string, $foreground_color = null, $background_color = null) {
			$colored_string = "";

			// Check if given foreground color found
			if (isset($this->foreground_colors[$foreground_color])) {
				$colored_string .= "\033[" . $this->foreground_colors[$foreground_color] . "m";
			}
			// Check if given background color found
			if (isset($this->background_colors[$background_color])) {
				$colored_string .= "\033[" . $this->background_colors[$background_color] . "m";
			}

			// Add string and end coloring
			$colored_string .=  $string . "\033[0m";

			return $colored_string;
		}

		// Returns all foreground color names
		public function getForegroundColors() {
			return array_keys($this->foreground_colors);
		}

		// Returns all background color names
		public function getBackgroundColors() {
			return array_keys($this->background_colors);
		}
	}

libs.php

Inclus les fonctions PHP du script.

<?php
	$services=array(
		"ftp"=>"21",
		"ssh"=>"22",
		"dns"=>"53",
		"http"=>"80",
		"https"=>"443",
		"sc"=>"8000",
		"tnet"=>"31457",
		"webmin"=>"10000",
		"sql"=>"3306",
		"source"=>"27015",
		"smtp"=>"25",
		"pop"=>"995",
		"rtmp"=>"1935",
		"mc"=>"25565",
		"mcpe"=>"19132",
		"smb"=>"445",
		"netb"=>"139",
		"proxy"=>"3128"
	);
	$names=array(
		"ftp"=>"FTP - Serveur de fichier",
		"ssh"=>"SSH - Shell de commande",
		"dns"=>"DNS - Serveur de nom de domaine (DNS)",
		"http"=>"HTTP - Serveur Web",
		"https"=>"HTTPS - Serveur Web sécurisé avec SSL ",
		"sc"=>"SHOUTCast - Serveur de streaming Audio",
		"tnet"=>"TetriNet - Serveur de jeu Tetrinet",
		"webmin"=>"Webmin/Virtualmin - Serveur de gestion",
		"sql"=>"MySQL - Base de données",
		"source"=>"Source - Serveur de jeu Source",
		"smtp"=>"SMTP - Serveur d'envoie des courriels",
		"pop"=>"POP - Service de boîte des courriels",
		"rtmp"=>"RTMP - Serveur de diffusion Flash",
		"mc"=>"Minecraft - Serveur Minecraft (Java)",
		"mcpe"=>"Minecraft PE - Serveur Minecraft Pocket Edition/Windows 10",
		"smb"=>"Samba - Réseau de partage de type Samba/Windows",
		"netbios"=>"Samba NetBIOS - Réseau de partage de type Samba/Windows avec NetBIOS",
		"proxy"=>"Proxy - Service de camouglage de l'adresse réel du client",
	);	
	
	function remoteState($host,$port){
	  	if($fp=@fsockopen($host, $port, $errno, $errstr, 20)){ $rep=true; } 
		else{ $rep=false; }
		return($rep); 
	  	@fclose($fp);
		$fp=null; $name=null; $rep=null; $port=null; $host=null;

	}	
	function remoteStateMC($host,$port){
		$json=exec("php ./mcstatus/json.php ".$host." ".$port,$ret);
		$array = json_decode($json,true);
		return (($array['status']=="Online")?true:false);
	}
	function remotePing($ip){
		$ping = exec("ping -c 1 $ip"); 
		$ping=explode("=",$ping); 
		if(!$ping[1]) return false;
		else return true;	
	}	

status.php

Le fichier à exécuter.

<?php
	
	/*
		Script de vérification réseau
		En ligne de commande PHP (PHP-CLI)
		Pour faire un écran de veille avec un serveur Linux
		Permet de monitorer rapidement son réseau
		
		Par Mathieu Légaré (levelKro) levelkro@yahoo.ca https://levelkro.com https://levelkro.net
		Version 5 des sources utilisé, version 2 de l'affichage
		Version du 5 octobre 2018
	*/
	
	echo date("H:i:s",time())." > Démarrage du script ...\r\n";
	system("setterm -blank 0"); // Éviter la fermeture de l'écran
	$s_cols=exec("tput cols");
	$s_rows=exec("tput rows");
	require("colors.php");
	$color=New Colors();
	echo date("H:i:s",time())." > ... module des couleurs chargé\r\n";
	require("libs.php");
	echo date("H:i:s",time())." > ... module des librairies chargé\r\n";
	
	start:
	include("config.php");
	echo date("H:i:s",time())." > ... configurations chargé\r\n";
	echo "\r\n";
	echo date("H:i:s",time())." > Démarrage de l'analyse\r\n";	
	system('clear');
	
	// HEADER
	echo "\r\n\r\n";
	$name="Script d'analyse de serveurs, services et réseau, v 2.4";
	
	echo str_repeat(" ",round(($s_cols-strlen($name))/2,0)).$color->getColoredString($name."\r\n","white","black");
	echo "\r\n";
	// LEGENDS
	if($config['legends']){
		echo "  ";
		echo $color->getColoredString("*** ","purple","black");
		echo $color->getColoredString("Légende des types de services","cyan","black");
		echo "\r\n";
		foreach($names as $tag=>$name){
			echo "      ";
			echo $color->getColoredString(strtoupper($tag),"light_purple","black");
			echo $color->getColoredString(str_repeat(" ",(6-strlen($tag)))."  :  ".$name."\r\n","light_cyan","black");	
		}
		echo "\r\n";
	}
	
	// SERVERS
	echo "  ";
	echo $color->getColoredString("*** ","purple","black");
	echo $color->getColoredString("Analyse des serveurs","cyan","black");
	echo "\r\n";
	foreach($servers as $server) {
		$str="[".$server['type']."] ".$server['name'];
		echo "    "; //4
		echo $color->getColoredString("[","light_gray","black");
		echo $color->getColoredString(strtoupper($server['type']),"white","black"); 
		echo $color->getColoredString("] ","light_gray","black");
		echo $color->getColoredString($server['name'],"light_cyan","black");
		echo " ".$color->getColoredString(str_repeat(".",($s_cols-(20+strlen($str)+strlen($server['ip'])))),"grey","black")." "; //2
		echo $color->getColoredString($server['ip']." ","light_cyan","black");
		$ping=remotePing($server['ip']);
		echo $color->getColoredString("[","light_gray","black");
		echo (($ping==true)?$color->getColoredString("ONLINE ","light_green","black"):$color->getColoredString("OFFLINE","light_red","black"));
		echo $color->getColoredString("]\r\n","light_gray","black");	//9
		
		// DOMAINS
		if($config['domains'] && count($server['domains'])>=1){
			if(is_array($server['domains'])){
				$x=0;
				$a=0;				
				if($config['ports'] && count($server['ports'])>=1) $follow=true;
				else $follow=false;
				foreach($server['domains'] as $domain){
					$a++;
					$x++;
					if($x==1) {
						echo "      ";
						echo $color->getColoredString((($follow || $a<=(count($server['domains'])-3))?"├":"└")." ","light_gray","black");
					}
					echo str_repeat(".",(32-strlen($domain)))." ".$color->getColoredString($domain,"light_cyan","black")." ";
					if((42+($x*32))>=$s_cols && $a<count($server['domains'])) { 
						$x=0; 
						echo "\r\n"; 
					}
				}
				echo "\r\n";
			}
		}
		
		// SERVICES
		if($config['ports'] && count($server['ports'])>=1){
			echo "      ";
			echo $color->getColoredString("└ ","light_gray","black");
			$z=12;
			foreach($server['ports'] as $v=>$port){
				$z=(3+$z)+strlen($port);
				if(($z)>=$s_cols) {
					echo "\r\n        ";
					$z=12;
				}				
				if($ping==true){
					// Add support for Minecraft custom server port
					if(in_array($port,array("mc","mcpe"))) {
						$answer=remoteStateMC($server['ip'],$services[$port]);
					}
					elseif(count(explode(":",$port))==2){ 
						$t=explode(":",$port); 
						$answer=remoteState($server['ip'],$t[1]); 
						$port=$t[0];   
					}
					else {
						$answer=remoteState($server['ip'],$services[$port]);
					}
					echo $color->getColoredString("[","light_gray","black");
					if($answer==true) echo $color->getColoredString(strtoupper($port),"light_green","black");
					else echo $color->getColoredString(strtoupper($port),"light_red","black");
					echo $color->getColoredString("] ","light_gray","black");				
				}
				else {
					if(count(explode(":",$port))==2){ 
						$t=explode(":",$port);
						$port=$t[0];						
					}
					echo $color->getColoredString("[","light_gray","black");
					echo $color->getColoredString(strtoupper($port),"brown","black");
					echo $color->getColoredString("] ","light_gray","black");				
				}
			}		
			echo "\r\n";
		}
		
	}
	
	// COOLDOWN
	echo "\r\n";
	echo "  ";
	echo $color->getColoredString("*** ","purple","black");
	echo $color->getColoredString("Cooldown de ".$config['cooldown']." secondes","cyan","black");
	echo "\r\n    ";
	$wc=$s_cols-8;
	$w=round($wc/$config['cooldown'],0);
	if(($w*$config['cooldown'])>$wc) {
		$dif=($w*$config['cooldown'])-$wc;
		//$jump=round($config['cooldown']/$dif,0);
		$jump=round($wc/$dif,0);
	}
	else {
		$jump=false;	
	}
	$jumpx=0;
	$v=0;
	for($x=0;$x<$config['cooldown'];$x++){
		if($jump>0) $jumpx++;
		if($jump==false || $jumpx<$jump) {
			echo $color->getColoredString(str_repeat("=",$w),"purple","magenta");
			$v=($v+$w);
		}
		elseif($jump>=1 && $jumpx>=$jump) $jumpx=0;
		sleep("1");

	}
	if($v<$wc) echo $color->getColoredString(str_repeat("=",$wc-$v),"purple","magenta");
	sleep("1");
	echo "\r\n";
	echo "\r\n";
	// Restart script
	echo date("H:i:s",time())." > Redémarrage...\r\n";	
	sleep(2);

	goto start;

config.php

Le fichier à éditer pour les informations, il est relu à chaque début de scan, alors inutile de redémarrer le script pour voir les changements.

<?php
	error_reporting(E_ALL & ~E_NOTICE & ~E_WARNING & ~E_DEPRECATED);
	ini_set("display_errors", 0); 
	@unlink("./.error");
	$config=array(
		"ports"=>true,	// false for disable or true for scan and display services ports
		"domains"=>false,	// false for disable or true for  display domains names
		"legends"=>false,	// false for disable or true for display services legends
		"cooldown"=>"120",	// Limiting ports and ping flood, wait before re-run script.
	);
	$servers=array(
		array(
			"ip"=>"192.168.0.101",
			"type"=>"lan",
			"domains"=>array(),
			"name"=>"Imprimante MFC-J625DW",
			"ports"=>array()
		),
		array(
			"ip"=>"192.168.0.104",
			"type"=>"lan",
			"domains"=>array(),
			"name"=>"Console Wii",
			"ports"=>array()
		),
		array(
			"ip"=>"192.168.0.111",
			"type"=>"lan",
			"domains"=>array(),
			"name"=>"LVKPC",
			"ports"=>array("IPCAM:8100")
		),
		array(
			"ip"=>"198.46.211.119",
			"type"=>"vps",
			"domains"=>array("exemple.com"),
			"name"=>"Exemple Server 01",
			"ports"=>array("ssh","webmin","ftp","dns","http","https","smtp:465","pop"),
		),			
	);

Exécution

Tous simplement faire la commande dans le terminal.

php status.php