[php:1:266eef350c]<?php
session_start();
$polaczenie=mysql_connect("localhost","X","X") or die("Brak połączenia z baza.<br>".mysql_error());
mysql_select_db('baza') or die("Brak bazy.<br>".mysql_error());

function scan($ipek)
{
$cmd = shell_exec('/sbin/ping -c 1 -t 1 '.$ipek);
if((!stristr($cmd, '100% packet loss'))&&(strlen($cmd)>0))
{
$poczatek = strpos($cmd, 'time=',1);
$koniec = strpos($cmd, ' ms',1);
return round(substr($cmd, $poczatek+5, $koniec-$poczatek));
}else{
return 0;
}
}

function insert2db($ip)
{
$ip_bez_kropek=str_replace('.','',$ip);

$q=mysql_query("SELECT SUM(ping) AS suma_ping, SUM(noping) AS suma_noping FROM ip".$ip_bez_kropek) or die('01'.mysql_error());
$r=mysql_fetch_array($q);

mysql_query('UPDATE ipsummary SET ping = '.($r["suma_ping"]/10).', sprawnosc = '.(($r["suma_noping"]/10)*100).' WHERE ip = "'.$ip.'"') or die('02'.mysql_error());

if($r["suma_noping"]==0)
{
$result = mysql_query('SELECT ip,active FROM ipdamage WHERE (ip = "'.$ip.'" and active=1)') or die('03'.mysql_error());
$num_rows = mysql_num_rows($result);

if($num_rows==1)
{
mysql_query('UPDATE ipdamage SET end_date = "'.mktime().'" WHERE (ip = "'.$ip.'" and active=1)') or die('04'.mysql_error());
}else{
mysql_query('INSERT INTO ipdamage (ip,begin_date,end_date,active) VALUES("'.$ip.'","'.mktime().'","'.mktime().'",1)') or die('05'.mysql_error());
}
}else{
$result = mysql_query('SELECT ip,active FROM ipdamage WHERE (ip = "'.$ip.'" and active=1)') or die('06'.mysql_error());
$num_rows = mysql_num_rows($result);

if($num_rows==1)
{
mysql_query('UPDATE ipdamage SET active = 0 WHERE (ip = "'.$ip.'" and active=1)') or die('07'.mysql_error());
}
}
$ping=scan($ip);


if($ping>0)
{
mysql_query('UPDATE ip'.$ip_bez_kropek.' SET ping = '.$ping.' WHERE id = '.$_SESSION['liczba']) or die('08'.mysql_error());
mysql_query('UPDATE ip'.$ip_bez_kropek.' SET noping = 1 WHERE id = '.$_SESSION['liczba']) or die('09'.mysql_error());
}else{

mysql_query('UPDATE ip'.$ip_bez_kropek.' SET noping = 0 WHERE id = '.$_SESSION['liczba']) or die('10'.mysql_error());
}
}

$tablicaIP[]="212.77.100.101"; //tylko jako przykład; tablica ma 68 elementów

$child=0;

while(1){
while ( $child < count($tablicaIP) )
{
$_SESSION['liczba']++;
if($_SESSION['liczba']>10)
{
$_SESSION['liczba']=1;
}

$pid = pcntl_fork();

if ( $pid == -1 ) {
die( "errorn" );
} elseif ( $pid == 0 ) {
$child++;
} else {
insert2db($tablicaIP[$child]);
die();
}
}
$child=0;
}
?>[/php:1:266eef350c]

[sql:1:266eef350c]
#
# Struktura tabeli dla `ip21277100101` //ip przykladowe tongue.gif
#

CREATE TABLE `ip21277100101` (
`id` int(3) NOT NULL auto_increment,
`ping` int(5) NOT NULL default '0',
`noping` int(1) NOT NULL default '0',
UNIQUE KEY `id` (`id`),
KEY `ping` (`ping`),
KEY `noping` (`noping`)
) TYPE=MyISAM;

#
# Struktura tabeli dla `ipdamage`
#

CREATE TABLE `ipdamage` (
`id` int(11) NOT NULL auto_increment,
`ip` varchar(15) NOT NULL default '',
`begin_date` int(10) NOT NULL default '0',
`end_date` int(10) NOT NULL default '0',
`active` tinyint(1) NOT NULL default '0',
PRIMARY KEY (`id`)
) TYPE=MyISAM;

#
# Struktura tabeli dla `ipsummary`
#

CREATE TABLE `ipsummary` (
`id` int(11) NOT NULL auto_increment,
`ip` varchar(15) NOT NULL default '',
`dns` varchar(255) NOT NULL default '',
`ping` int(11) NOT NULL default '0',
`sprawnosc` int(3) NOT NULL default '0',
PRIMARY KEY (`id`),
UNIQUE KEY `ip` (`ip`)
) TYPE=MyISAM;[/sql:1:266eef350c]

Skrypt ma służyć do monitoringu sieci lokalnej. Chodzi o to, aby pingował wszystkie 68 lokalizacji w jak najkrótszym czasie.

pytanie 1

Jeśli pinguje lokalizacje po kolei jest OK, ale gdy chce pingować jednocześnie wszystkie lokalizacje to dostaje błąd Lost connection to server during query

Chyba nie kapuje działania funkcji pctnl , bo mi się kod wykrzacza smile.gif

pytanie 2

Kod napewno nie jest optymalny. Co można by w nim jeszcze usprawnić?