Amélioration du fichier ping.php

Contribuer au projet de part vos connaissances: PHP, HTML, CSS, SQL, JS ....
Répondre
wlf35
Gsup LEVEL 0
Messages : 9
Enregistré le : lun. 7 janv. 2019 14:06

Bonjour,
Je ne sais pas si on est beaucoup à utiliser la fonction "Ping Global" présent dans la documentation sur le chemin suivant : "Documentations>Foire aux questions>Comment remonter l'état de connectivité des équipements IP ?" qui utilise le fichier ping.php.
Après plusieurs tentatives pour le mettre en place, je pensais que cette fonction ne fonctionnait pas alors que si :

Cette dernière met à jour la table tassets et la colonne date_last_ping mais aucun visuel sur la fiche de l'équipement.

Quand nous cliquons sur "Ping de cet l'équipement sur l'adresse ip : XXX.XXX.XXX.XXX", un petit drapeau nous informe de l'état de l'interface : rouge si échoué, vert si cela répond.

Je propose donc une petite évolution du code qui permet de mettre à jour la table tassets_iface, soit date_ping_ok si cela répond, soit date_ping_ko si cela échoue.

Code : Tout sélectionner

<?php
###############################################################################################
# @Name : ./core/ping.php
# @Description : test if asset is connected
# @Call : ./core/asset.php or via windows or linux command line
# @Parameters : GET_[ip] or globalping and server key for command line execution
# @Author : Flox
# @Create : 19/12/2015
# @Update : 23/03/2018
# @Version : 3.1.32
# Modification : updating iface date_ping_ok et date_ping_ko ligne 83-89 pour Windows et 113-119 pour Linux
###############################################################################################

//initialize variables 
if(!isset($_GET['server_key'])) $_GET['server_key'] = '';
if(!isset($rparameters['server_private_key'])) $rparameters['server_private_key'] = '';
if(!isset($_GET['iptoping'])) $_GET['iptoping'] = '';
if(!isset($_SERVER['HTTP_ACCEPT_LANGUAGE'])) $_SERVER['HTTP_ACCEPT_LANGUAGE']=0;

if(!isset($argv[1])) $argv[1] = '';
if(!isset($argv[2])) $argv[2] = '';

//call via external script for cron 
if(!$rparameters['server_private_key'])
{
	//database connection
	require_once(__DIR__."/../connect.php");
	
	//load parameters table
	$qry=$db->prepare("SELECT * FROM `tparameters`");
	$qry->execute();
	$rparameters=$qry->fetch();
	$qry->closeCursor();
	
	//locales
	$lang = substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2);
	if ($lang=='fr') {$_GET['lang'] = 'fr_FR';}
	else {$_GET['lang'] = 'en_US';}
	define('PROJECT_DIR', realpath('../'));
	define('LOCALE_DIR', PROJECT_DIR .'/locale');
	define('DEFAULT_LOCALE', '($_GET[lang]');
	require_once(__DIR__.'/../components/php-gettext/gettext.inc');
	$encoding = 'UTF-8';
	$locale = (isset($_GET['lang']))? $_GET['lang'] : DEFAULT_LOCALE;
	T_setlocale(LC_MESSAGES, $locale);
	T_bindtextdomain($_GET['lang'], LOCALE_DIR);
	T_bind_textdomain_codeset($_GET['lang'], $encoding);
	T_textdomain($_GET['lang']);
}

$today=date("Y-m-d");

//multi ping for update all assets last ping value
if ($argv[1]=='globalping')
{
	if($argv[2]==$rparameters['server_private_key'] && $rparameters['server_private_key'])
	{
		//load all enabled iface of all enabled assets with ip adresse
		$qry=$db->prepare("SELECT tassets.id, tassets.netbios, tassets_iface.ip FROM tassets_iface,tassets WHERE tassets.id=tassets_iface.asset_id AND tassets_iface.ip!=:ip AND tassets.state=:state AND tassets.disable=:tassets_disable AND tassets_iface.disable=:tassets_iface_disable ORDER BY tassets.id");
		$qry->execute(array(
			'ip' => '',
			'state' => 2,
			'tassets_disable' => 0,
			'tassets_iface_disable' => 0
		));
		if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') //windows server case
		{
			while ($row=$qry->fetch())
			{
				//check if ipv4 is well formed
				$error='';
				$cnt=0;
				if(!preg_match('#\.#', $row['ip'])) {$error='error no point detected';}
				foreach (explode('.',$row['ip']) as $val) {$cnt++;if (!is_numeric($val)) { $error='not numeric value'; break;} if($val>254) { $error='error bloc more than 255'; break;}}
				if(!$error) {if ($cnt!=4) {$error='error not 4 blocs';}}
				
				if (!$error)
				{
					echo '[WIN] ping '.$row['netbios'].' (id:'.$row['id'].') on IP '.$row['ip'].':';
					$result=exec("ping -n 1 -w 1 $row[ip]");
					if((preg_match('#ms#', $result)))
					{
						echo ' OK (updating asset last ping flag)'.PHP_EOL;
						$db->exec('UPDATE tassets SET date_last_ping=\''.$today.'\' WHERE id=\''.$row['id'].'\'');
						echo ' OK (updating iface date_ping_ok)'.PHP_EOL;
						$db->exec('UPDATE tassets_iface SET date_ping_ok=\''.date('Y-m-d H-i-s').'\' WHERE asset_id=\''.$row['id'].'\' AND ip=\''.$row['ip'].'\'');
					} else {
						echo ' KO '.PHP_EOL;
						$db->exec('UPDATE tassets_iface SET date_ping_ko=\''.date('Y-m-d H-i-s').'\' WHERE asset_id=\''.$row['id'].'\' AND ip=\''.$row['ip'].'\'');
					}
					sleep(1); //timeout 1 seconds to limit network trafic
				} else {
					echo '[WIN] ping '.$row['netbios'].' (id:'.$row['id'].') on IP '.$row['ip'].': no check, invalid ip address ('.$error.')'.PHP_EOL;
				}
			}
			$qry->closeCursor();
		} else { //linux server case
			while ($row=$qry->fetch())
			{
				//check if ipv4 is well formed
				$error='';
				$cnt=0;
				if(!preg_match('#\.#', $row['ip'])) {$error='error no point detected';}
				foreach (explode('.',$row['ip']) as $val) {$cnt++;if (!is_numeric($val)) { $error='not numeric value'; break;} if($val>254) { $error='error bloc more than 255'; break;}}
				if(!$error) {if ($cnt!=4) {$error='error not 4 blocs';}}
				
				if (!$error)
				{
					echo '[LINUX] ping '.$row['netbios'].' (id:'.$row['id'].') on IP '.$row['ip'].':';
					$result=exec("ping -W 1 -c 1  $row[ip]");
					if((preg_match('#min#', $result)))
					{
						echo ' OK (updating asset last ping flag)'.PHP_EOL;
						$db->exec('UPDATE tassets SET date_last_ping=\''.$today.'\' WHERE id=\''.$row['id'].'\'');
						echo ' OK (updating iface date_ping_ok)'.PHP_EOL;
						$db->exec('UPDATE tassets_iface SET date_ping_ok=\''.date('Y-m-d H-i-s').'\' WHERE asset_id=\''.$row['id'].'\' AND ip=\''.$row['ip'].'\'');
					} else {
						echo ' KO (updating iface date_ping_ko)'.PHP_EOL;
						$db->exec('UPDATE tassets_iface SET date_ping_ko=\''.date('Y-m-d H-i-s').'\' WHERE asset_id=\''.$row['id'].'\' AND ip=\''.$row['ip'].'\'');
					}
					sleep(1); //timeout 1 seconds to limit network trafic
				} else {
					echo '[LINUX] ping '.$row['netbios'].' (id:'.$row['id'].') on IP '.$row['ip'].': no check, invalid ip address ('.$error.')'.PHP_EOL;;
				}
			}
			$qry->closeCursor();
		}
	} else {echo "ERROR: Wrong server key go to application system page to get your key";}
} elseif($_GET['iptoping']) { //single ping from ticket with OS detection
	//test each iface with ip
	$qry=$db->prepare("SELECT `id`,`ip`,`date_ping_ok`,`date_ping_ko` FROM `tassets_iface` WHERE asset_id=:asset_id AND disable=:disable");
	$qry->execute(array(
		'asset_id' => $globalrow['id'],
		'disable' => 0
		));
	while($row=$qry->fetch()) 
	{
		if($row['ip'])
		{
			$test_ip=$row['ip'];
			//check if ipv4 is well formed
			$error='';
			$cnt=0;
			if(!preg_match('#\.#', $test_ip)) {$error='error no point detected';}
			foreach (explode('.',$test_ip) as $val) {$cnt++;if (!is_numeric($val)) { $error='not numeric value'; break;} if($val>254) { $error='error bloc more than 255'; break;}}
			if(!$error) {if ($cnt!=4) {$error='error not 4 blocs';}}
			
			if (!$error)
			{
				if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {
					$result=exec("ping -n 1 -w 1 $test_ip");	
					//test result
					if((preg_match('#Minimum#', $result)))
					{
						//display message
						echo '<div class="alert alert-block alert-success"><i class="icon-exchange bigger-130 green"></i>&nbsp; <strong>'.T_('PING').'</strong> de <b>'.$test_ip.'</b> : OK <span style="font-size: x-small;">('.$result.')</span></div>';
						//update asset flag
						$db->exec('UPDATE tassets SET date_last_ping=\''.$today.'\' WHERE id=\''.$globalrow['id'].'\'');
						$db->exec('UPDATE tassets_iface SET date_ping_ok=\''.date('Y-m-d H-i-s').'\' WHERE id=\''.$row['id'].'\'');
					} else {
						$result=mb_convert_encoding($result, "UTF-8");
						//display message
						echo '<div class="alert alert-danger"><i class="icon-remove"></i> <strong>'.T_('PING').'</strong> de <b>'.$test_ip.'</b> : KO <span style="font-size: x-small;">('.$result.')</span></div>';
						$db->exec('UPDATE tassets_iface SET date_ping_ko=\''.date('Y-m-d H-i-s').'\' WHERE id=\''.$row['id'].'\'');
				}
				} else {
					$result=exec("ping -W 1 -c 1 $test_ip");
					//test result
					if((preg_match('#min#', $result)))
					{
						//display message
						echo '<div class="alert alert-block alert-success"><i class="icon-exchange bigger-130 green"></i>&nbsp; <strong>'.T_('PING').'</strong> de <b>'.$test_ip.'</b> : OK <span style="font-size: x-small;">('.$result.')</span></div>';
						//update asset flag
						$db->exec('UPDATE tassets SET date_last_ping=\''.$today.'\' WHERE id=\''.$globalrow['id'].'\'');
						$db->exec('UPDATE tassets_iface SET date_ping_ok=\''.date('Y-m-d H-i-s').'\' WHERE id=\''.$row['id'].'\'');
					} else {
						$result=mb_convert_encoding($result, "UTF-8");
						//display message
						echo '<div class="alert alert-danger"><i class="icon-remove"></i><strong>'.T_('PING').'</strong> de <b>'.$test_ip.'</b> : KO <span style="font-size: x-small;">('.$result.')</span> </div>';
						$db->exec('UPDATE tassets_iface SET date_ping_ko=\''.date('Y-m-d H-i-s').'\' WHERE id=\''.$row['id'].'\'');
					}
				}
			} else {
				echo '<div class="alert alert-danger"><i class="icon-remove"></i> <strong>'.T_('PING').'</strong> de <b>'.$test_ip.'</b> : KO <span style="font-size: x-small;">(Invalid IPv4 address: '.$error.')</span> </div>';
			}
		}
	}
	$qry->closeCursor();
}
?>
Pour que cette dernière soit réellement efficace, il faut également modifier la crontab pour que cela s'applique lors de la production (midi dans mon exemple):

Code : Tout sélectionner

echo "0 12 * * * root php /var/www/html/core/ping.php globalping YOURPRIVATEKEY > /dev/null" >> /etc/crontab && service cron reload
J'espère que ce partage servira à certains d'entre vous.

Bonne journée.
EME
GestSup: 3.1.41 | Apache: 2.4.39 | MariaDB : 10.1.38-MariaDB-0+deb9u1 | PHP : 7.0.33-0+deb9u3
Avatar du membre
Flox
Administrateur du site
Messages : 8972
Enregistré le : jeu. 21 juin 2012 19:00

Bonjour Mr EME ;) ,

la fonction à été implémentée pour la prochaine version 3.1.42.

Tu trouvera ma version en pièce jointe.

+++
Fichiers joints
patch_3.1.41.zip
(2.38 Kio) Téléchargé 368 fois
GestSup: 3.2.47 | Debian: 12 | Apache: 2.4.58 | MariaDB: 11.3.2 | PHP: 8.3.4 | https://doc.gestsup.fr/
Répondre