我开发了一个Web监控工具。它应该检查网站的状态代码,并决定它的启动或关闭。如果响应代码是200,它会很好,它会保存在数据库中,否则会保存。如果状态码发生变化,应该发送通知,即从上到下或反之亦然(它使用保存在数据库中的最后一个状态码检查新的resCode(响应代码)))。但是如果网站总是上线,请不要发送通知。但是如果网站关闭,请检查警报频率并发送通知。 sendNotification函数对此负责,但出于某种原因并且收到通知的垃圾邮件。它只是继续发送通知而不检查警报频率和状态更改。任何人都可以查看我的代码。提前tnx。Web监控工具:不断发送通知,而不应该发送通知
<?php
namespace App\Http\Controllers;
use \GuzzleHttp\Client;
use App\Utilities\Reporter;
use GuzzleHttp\Exception\ClientException;
use App\Notification;
use App\Status;
use App\Setting;
use Carbon;
class GuzzleController extends Controller
{
private $default_check_frequency;
protected $client;
protected $reporter;
public function __construct()
{
$this->client = new Client();
$this->reporter = new Reporter;
$this->default_check_frequency = Setting::defaultCheckFrequency();
}
private function addStatusToNotification(Notification $notification, Status $status, $resCode)
{
$notification->statuses()->attach($status, [
'values' => $resCode === 200
? 'up'
: 'down'
]);
}
private function report(Notification $notification, $resCode)
{
if(empty($resCode)){
$resCode = "no response found";
}
if($resCode <> 200){
$this->reporter->slack($notification->website_url . ':' . ' is down' . ' this is the status code!' . ' @- ' .$resCode,
$notification->slack_channel);
$this->reporter->mail($notification->email,$notification->website_url.' is down '. ' this is the status Code: '. $resCode);
}else{
$this->reporter->slack($notification->website_url . ':' . ' is up' . ' this is the status code!' . ' @- ' .$resCode,
$notification->slack_channel);
}
}
private function sendNotification(Notification $notification, Status $status, $status_health, $frequency, $resCode)
{
$elapsed_time = \Carbon\Carbon::parse($status_health['timestamp'])->diffInMinutes();
if ($elapsed_time >= $frequency) {
$this->addStatusToNotification($notification, $status, $resCode);
}
if ($resCode === 200){
if($resCode <> $status_health['value']){
var_dump($status_health['value']);
die();
$this->report($notification, $resCode);
}
}else{
if($resCode === $status_health['value'] && $elapsed_time >= $notification->alert_frequency){
$this->report($notification, $resCode);
}
}
}
public function status()
{
$notifications = Notification::where('active', 1)->get();
$status = Status::where('name', 'health')->first();
foreach ($notifications as $notification) {
$this->updateStatus($notification, $status);
}
}
private function updateStatus(Notification $notification, Status $status)
{
$resCode = $this->getStatusCode($notification->website_url);
$this->sendNotification(
$notification,
$status,
$notification->status('health'),
$this->getFrequency($notification,$resCode),
$resCode
);
}
private function getFrequency(Notification $notification)
{
return isset($notification->check_frequency)
? intval($notification->check_frequency)
: $this->default_check_frequency;
}
private function getStatusCode($url)
{
try {
$response = $this->client->get($url, [
'http_errors' => false
]);
return $response->getStatusCode();
} catch (\GuzzleHttp\Exception\ConnectException $e) {
}
}
}
这实际上是IAM获得status_health,模型,无论是向上还是黎明从数据库
<?php
namespace App;
use App\Status;
use App\Notification;
use Illuminate\Database\Eloquent\Model;
class Notification extends Model
{
protected $fillable = ['id','website_url','email','slack_channel','check_frequency','alert_frequency','speed_frequency','active'];
public function statuses(){
return $this->belongsToMany('App\Status')->withPivot('values')->withTimestamps();
}
public function status($s_type)
{
if (!in_array($s_type, ['speed', 'health'])){
return false;
}
$o_status = Status::where('name', strval($s_type))->first();
$o_response = $this->statuses()->where('status_id', $o_status->id)
->select('values', 'created_at')->orderBy('created_at','DESC')->first();
if($o_response === null){
return false;
}
return [
'value' => $o_response->values,
'timestamp' => $o_response->created_at
];
}
public function history($s_type)
{
if (!in_array($s_type, ['speed', 'health'])){
return false;
}
$o_status = Status::where('name', strval($s_type))->first();
$o_response = $this->statuses()->where('status_id', $o_status->id)
->select('values', 'created_at')->orderBy('created_at','DESC')->get();
if($o_response === null || $o_response->count() === 0){
return false;
}
$a_ret = [];
$last = null;
foreach ($o_response as $o_status) {
if($last != $o_status->values) {
$a_ret[] = [
'value' => $o_status->values,
'timestamp' => $o_status->created_at
];
$last = $o_status->values;
}
}
return $a_ret;
}
}
显然,代码检查RESP代码改变不能按预期的那样工作确保'$ status_health'包含你期望得到的结果 –