Summary
require 'phppinger.class.php';
$ping = new PHP_Pinger();
The easy way:
$ping->addHost('nicolabricot.com');
You can also specified a port (default port is 80
):
$ping->addHost('nicolabricot.com', 80);
You can optionnaly add a sweety name (default label is the hostname):
$ping->addHost('nicolabricot.com', 80, 'My blog');
PHP Pinger use a socket to check if the host is online. It opens a socket at the given host and port, and if no error occures the conclusion is that the host or service is up.
$ping->run();
Nothing display? Don't be worry: by default if no callback function is given, PHP Pinger will ping hosts but display nothing.
To display the result, see next paragraph.
The run()
method must be called at the end, otherwise other options will not be set.
The callback function is called when a host has been pinged, that means the function is called for each host you added.
The callback function takes four parameters:
$online
[boolean]
$host
[string]
$port
[int]
$label
[string]
function myCallbackFunction($online, $host, $port, $label) {
// the callback you want, for example:
echo '<li>', $label, ' is ', ($online ? 'online' : 'offline'),
' (', $host, ':', $port, ')', '</li>';
}
Once definied, you have to add it to the created instance by:
$ping->registerCallback('myCallbackFunction');
Such as you registred the previous callback function, you can register a callback function executed before the run, and/or a callback function executed after the run:
// register an action done before run:
$ping->registerPreRun('myPreRunFunction');
// register an action done after run:
$ping->registerPostRun('myPostRunFunction');
You can combine all callbacks:
$ping->registerPreRun('myPreRun')
->registerCallback('myCallback')
->registerPostRun('myPostRun');
If your web server can send e-mail, you can be notify by e-mail. Notifications are by default disable.
Three levels of notifications are available:
NotificationLevel::NONE
NotificationLevel::PROBLEMS
NotificationLevel::REPORT
To enable notifications, you have to add at least one destinator for e-mail:
$ping->setNotificationTo(array('your-email@domain.tld'));
Notifications could be sent to several e-mails:
$ping->setNotificationTo(array(
'your-email@domain.tld',
'other-email@domain.tld',
'boss@domain.tld'
));
Just add the following line:
$ping->enableNotification();
By default, enabling notifications will set the level to NotificationLevel::PROBLEMS
.
You can specify another level, just add it as a parameter:
// enable PROBLEMS level (similar to previous line)
$ping->enableNotification(NotificationLevel::PROBLEMS);
// enable REPORT level
$ping->enableNotification(NotificationLevel::REPORT);
You have two ways to do that:
// dedicated method:
$ping->disableNotification();
// or use the level NONE:
$ping->enableNotification(NotificationLevel::NONE);
When an e-mail is sent, the default preffix is Status. You can change it with:
$ping->setNotificationPreffix('Network Monitoring');
To add a text at the end of the e-mail sent, just add:
$ping->setNotificationSignature('--'.PHP_EOL.'By PHP Pinger');
// load PHP Pinger
require 'phppinger.class.php';
/* callbacks for example */
function callback($is_online, $hostname, $port, $sweety_name) {
echo '<li>', $hostname, ':', $port, ' is ',
($is_online ? 'online' : 'offline'), '</li>';
}
function preRun() { echo '<ul>', PHP_EOL; }
function postRun() { echo '</ul>', PHP_EOL; }
/* instanciate a new Pinger and run it with callbacks and notifications */
$ping = new PHP_Pinger();
$ping->registerCallback('callback')
->registerPreRun('preRun')
->registerPostRun('postRun')
->addHost('nicolabricot.com')
->addHost('blog.nicolabricot.com', 80)
->addHost('ftp.github.com', 21, 'GitHub FTP')
->setNotificationTo(array('email@domain.tld', 'other-dude@domain.tld'))
->setNotificationPreffix('Network Monitoring')
->setNotificationSignature('--'.PHP_EOL.'By PHP Pinger')
->enableNotification(NotificationLevel::REPORT)
->run();
You can combine methods as you want, except the run()
method which must be at the end.