1. Go to this page and download the library: Download posixpascal/spark-alarm library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
posixpascal / spark-alarm example snippets
use Spark\SparkAlarm;
use Spark\Spark;
class AlertIfCPULoadIsHigh extends SparkAlarm {
public function test(){
return !($this->getCPULoadAverage() > 80); // not higher than 80
}
}
class AlertIfCPULoadIsSuperHigh extends SparkAlarm {
public function test(){
return !$this->getCPULoadAverage() > 95; // not higher than 95
}
public function error(){ // CPU is above 95%
mail("[email protected]", "CPU for server is above 95%", "Be aware, your CPU resources are low...");
}
}
$spark = new Spark();
$spark
->addAlarm(new AlertIfCPULoadIsHigh())
->addAlarm(new AlertIfCPULoadIsSuperHigh())
->run();
use Spark\SparkAlarm;
use Spark\Spark;
class AlertIfWebsiteNotReachable extends SparkAlarm {
public function test(){
$ch = curl_init("https://google.de");
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_NOBODY, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
curl_close($ch);
return !$response;
}
public function success(){
// website is reachable, lets log it or do sth else.
}
public function error(){
// website if off, mail here or let notifier handle the rest.
}
}
$spark = new Spark();
$spark
->addAlarm(new AlertIfWebsiteNotReachable())
->run();
use Spark\Notifier;
use Spark\AlarmStatus;
class MyCustomNotifier implements Notifier {
/**
* in this example I build a string summary and publish it to a logfile.
* @param $alarms array holds every failed alarm class, you can attach custom functions to your alarmclass as well.
*/
public function send(array $alarms){
$str = "Test results:\n";
foreach ($alarms as $alarm){
if ($alarm->status == AlarmStatus::SUCCESS){
$str .= get_class($alarm).": executed successfully @".date("Y-m-d H:i:s");
} else {
$str .= get_class($alarm).": failed @".date("Y-m-d H:i:s");
}
$str .= "\n";
}
file_put_contents("/var/log/spark.log", $str);
}
}
$spark
->notifier(new MyCustomNotifier())
->run();
$spark->sendNotificationOnSuccess(true)
use Spark\SparkAlarm;
class AlertIfCPULoadIsHigh extends SparkAlarm {
public $sendNotificationOnSuccess = true;
}
$spark
->throttle(60 * 60) // send summaries of failed alerts every hour
->interval(60) // execute alarm tests every minute
interface SparkAlarmHelper {
// cpu
public function getCPULoadAverage();
// disk space
public function getFreeDiskSpace();
public function getTotalDiskSpace();
public function getFreeDiskSpaceInPercentage();
// memory
public function getTotalMemory();
public function getFreeMemory();
public function getFreeMemoryInPercentage();
}
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.