Last active
          October 25, 2023 09:19 
        
      - 
      
- 
        Save lechuhuuha/784b06fc50894327cc366bf1abe5c6ec to your computer and use it in GitHub Desktop. 
    php monitor
  
        
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | <?php | |
| $monitors = Monitor::doesntHave('uptimeRecords')->select('id', 'url')->limit(10)->get(); | |
| $countMonitorsWithoutUTR = $monitors->count(); | |
| if ($countMonitorsWithoutUTR < 10) { | |
| $orderByDescUptimeRecord = UptimeRecord::select('id_monitor') | |
| ->selectRaw('MAX(checked_at) as max_checked_at') | |
| ->groupBy('id_monitor') | |
| ->orderBy('max_checked_at', 'asc') | |
| ->take(10 - $countMonitorsWithoutUTR) | |
| ->get(); | |
| foreach ($orderByDescUptimeRecord as $utr) { | |
| $monitor = Monitor::findOrFail($utr->id_monitor); | |
| $monitors->push($monitor); | |
| } | |
| } | |
| foreach ($monitors as $monitor) { | |
| $domains[] = $monitor->url; | |
| } | |
| $requestData = ["domains" => $domains]; | |
| $checkDomainsRequest = json_encode($requestData); | |
| $curl = curl_init(); | |
| curl_setopt_array($curl, array( | |
| CURLOPT_URL => '103.56.163.62:28091/api/private/check-domains', | |
| CURLOPT_RETURNTRANSFER => true, | |
| CURLOPT_ENCODING => '', | |
| CURLOPT_MAXREDIRS => 10, | |
| CURLOPT_TIMEOUT => 0, | |
| CURLOPT_FOLLOWLOCATION => true, | |
| CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1, | |
| CURLOPT_CUSTOMREQUEST => 'POST', | |
| CURLOPT_POSTFIELDS => $checkDomainsRequest, | |
| CURLOPT_HTTPHEADER => array( | |
| 'Content-Type: application/json' | |
| ), | |
| )); | |
| $response = curl_exec($curl); | |
| curl_close($curl); | |
| $data = json_decode($response)->data ?: []; | |
| if (count($data) > 0) { | |
| foreach ($data as $domainInfo) { | |
| foreach ($monitors as $monitor) { | |
| if ($domainInfo->Name === $monitor->url) { | |
| $idMonitor = $monitor->id; | |
| $urlMonitor = $monitor->url; | |
| break; | |
| } | |
| } | |
| $incidentPre = Incident::where('id_monitor', '=', $idMonitor) | |
| ->whereNull('resolved_at')->get()->first(); | |
| $uptimeRecord = new UptimeRecord(); | |
| if ($domainInfo->StatusCode !== 200) { | |
| if (!$incidentPre) { | |
| $incident = new Incident(); | |
| $incident->status = 'Ongoing'; | |
| $incident->started_at = now(); | |
| $incident->name = $urlMonitor; | |
| $incident->id_monitor = $idMonitor; | |
| $incident->count = 1; | |
| $incident->save(); | |
| } else { | |
| $incidentPre->count = $incidentPre->count + 1; | |
| $incidentPre->save(); | |
| } | |
| $uptimeRecord->is_up = 0; | |
| } else { | |
| if ($incidentPre) { | |
| $incidentPre->status = 'Resolved'; | |
| $incidentPre->resolved_at = now(); | |
| $incidentPre->save(); | |
| } | |
| $uptimeRecord->is_up = 1; | |
| } | |
| $createdUnix = Carbon::now()->timestamp; | |
| $uptimeRecord->checked_at = $createdUnix; | |
| $uptimeRecord->status_code = $domainInfo->StatusCode; | |
| $uptimeRecord->response_time = $domainInfo->Time; | |
| $header = json_encode($domainInfo->Header); | |
| $uptimeRecord->raw_header = serialize($header); | |
| $uptimeRecord->id_monitor = $idMonitor; | |
| $uptimeRecord->save(); | |
| } | |
| return "Uptime record created!"; | |
| } else { | |
| return "Error!"; | |
| } | 
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment