[PHP] Script to Time Remote Server Response and Email


 
Thread Tools Search this Thread
Top Forums Web Development [PHP] Script to Time Remote Server Response and Email
# 1  
Old 12-10-2009
[PHP] Script to Time Remote Server Response and Email

Here is a simple PHP script I wrote that times getting a link using mtime with curl and emails if it does not meet my objective. I use it in production for checking the performance of a Content Delivery Network (CDN), but you can use it for any web server.

PHP Code:
<?php


        $heartbeat 
"your.remote.server.com/file.html";
        
$delay 2500;

        
// create curl resource
        
$ch curl_init();

        
// set url
        
curl_setopt($chCURLOPT_URL$heartbeat);

        
// header
        
curl_setopt($chCURLOPT_HEADER1);

        
// connection timeout
        
curl_setopt($chCURLOPT_CONNECTTIMEOUT_MS$delay);

        
// store results as the return of curl_exec
        
curl_setopt($chCURLOPT_RETURNTRANSFERTRUE);

        
// curl timeout
        
curl_setopt($chCURLOPT_TIMEOUT_MS$delay);

        
// if HTML error 400 over over, fail
        
curl_setopt($chCURLOPT_FAILONERRORTRUE);

        
// set start time
        
$mtime microtime(); 
        
$mtime explode(' '$mtime); 
        
$mtime $mtime[1] + $mtime[0]; 
        
$starttime $mtime
      
        
$output curl_exec($ch);

        
//set end time
        
$mtime microtime(); 
        
$mtime explode(" "$mtime); 
        
$mtime $mtime[1] + $mtime[0]; 
        
$endtime $mtime
        
$totaltime = ($endtime $starttime);

        
curl_close($ch);

        
$pattern '/Your_Pattern/';
        
$errno preg_match($pattern$output$matchesPREG_OFFSET_CAPTURE);

        if(
$errno == 0){

            
$to "your.email@yourdomain.com";
            
$subject "Problem with Server";
            
$body "There is a problem with the Server\n\n";
            
$body .= "This script executed in " .$totaltime" seconds.";
            
$header "From: root@yourdomain.com\r\n";
            
$header .= "Reply-To: root@yourdomain.com\r\n";
            
$header .= "Return-Path: root@yourdomain.com\r\n";
            
$success mail($to$subject$body$header);
 }
?>
# 2  
Old 12-12-2009
For some reason, I could not get millisecond (ms) granularity out of the CURLOPT_CONNECTTIMEOUT_MS and CURLOPT_TIMEOUT_MS. If I set timeout (as in my first post) to 2500 ms, I get return values of any value over 2 seconds. I am not sure why the _MS options in curl are not working properly (with ms granularity).

So, I changed the script to seconds instead of ms.... and will revisit this at a later date. If anyone has time to troubleshoot this, please do so and let me know. Thanks.

PHP Code:
<?php

        $connecttimeout 
3;
        
$curltimeout 4;

        
$heartbeat "your.remote.server.com/file.html";

        
// create curl resource
        
$ch curl_init();

        
// set url
        
curl_setopt($chCURLOPT_URL$heartbeat);

        
// header
        
curl_setopt($chCURLOPT_HEADER1);

        
// connection timeout
        
curl_setopt($chCURLOPT_CONNECTTIMEOUT$connecttimeout);

        
// store results as the return of curl_exec
        
curl_setopt($chCURLOPT_RETURNTRANSFERTRUE);

        
// curl timeout
        
curl_setopt($chCURLOPT_TIMEOUT$curltimeout);

        
// if HTML error 400 over over, fail
        
curl_setopt($chCURLOPT_FAILONERRORTRUE);

        
// set start time
        
$mtime microtime(); 
        
$mtime explode(' '$mtime); 
        
$mtime $mtime[1] + $mtime[0]; 
        
$starttime $mtime
      
        
$output curl_exec($ch);

        
//set end time
        
$mtime microtime(); 
        
$mtime explode(" "$mtime); 
        
$mtime $mtime[1] + $mtime[0]; 
        
$endtime $mtime
        
$totaltime = ($endtime $starttime);

        
curl_close($ch);

        
$pattern '/Your_Pattern/';
        
$errno preg_match($pattern$output$matchesPREG_OFFSET_CAPTURE);

        if(
$errno == 0){

            
$to "your.email@yourdomain.com";
            
$subject "Problem with Server";
            
$body "There is a problem with the Server\n\n";
            
$body .= "This script executed in " .$totaltime" seconds.";
            
$header "From: root@yourdomain.com\r\n";
            
$header .= "Reply-To: root@yourdomain.com\r\n";
            
$header .= "Return-Path: root@yourdomain.com\r\n";
            
$success mail($to$subject$body$header);
 }
?>
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Choosing VPN server based on server response times

Hello all, I am using the VPN provider Private Internet Access. I am using the Raspberry Pi 4 with 4GB of RAM, performance on this upgraded board is great. Anyways I am connecting to its service using systemd's openvpn-client @ US_New_York_City.service I wonder if I can create a... (5 Replies)
Discussion started by: haloslayer255
5 Replies

2. Shell Programming and Scripting

Script connect to remote server, not find files and exit only from remote server, but not from scrip

I have a script, which connecting to remote server and first checks, if the files are there by timestamp. If not I want the script exit without error. Below is a code TARFILE=${NAME}.tar TARGZFILE=${NAME}.tar.gz ssh ${DESTSERVNAME} 'cd /export/home/iciprod/download/let/monthly;... (3 Replies)
Discussion started by: digioleg54
3 Replies

3. Shell Programming and Scripting

Script to check response time from nginx logs

Hi, My goal is to monitor the response time from the access logs of nginx server. I am using gawk to print the needed fields - 'response time' and 'name of the service' from nginx logs. Command: gawk '($6 ~ /cloudservice/) {print $10, $6}' access.log Output: 0.645 /nc/cloudservice... (6 Replies)
Discussion started by: nshah11
6 Replies

4. Shell Programming and Scripting

How to Append the output of a script running in remote server to a file in local server?

Hi guys, So i am in server1 and i have to login to server 2, 3,4 and run some script there(logging script) and output its result. What i am doing is running the script in server2 and outputting it to a file in server 2 and then Scp'ing the file to server1. Similarly i am doing this for other... (5 Replies)
Discussion started by: srkmish
5 Replies

5. Solaris

Script to get files from remote server to local server through sftp without prompting for password

Hi, I am trying to automate the process of fetching files from remote server to local server through sftp. I have the username and password for the remote solaris server. But I need to give password manually everytime i run the script. Can anyone help me in automating the script such that it... (3 Replies)
Discussion started by: ssk250
3 Replies

6. IP Networking

Tshark/pcap and web-server response time

Hi everyone! How can I get response time difference between GET and HTTP/1.0 200 OK (i mean time latency of web-server) with using of tshark&shell or something else for each hostname from pcap file? What can you recommend me to do that? (1 Reply)
Discussion started by: lepetal
1 Replies

7. Shell Programming and Scripting

Need help on how to exit a script run on a server from a remote server

hi, I am using the below line to run a script from remote server(say server A) to another server(say server B). ssh username@servername ksh script name. The issue is the script logs into server B, executes the script on server B, transfers the file to server A but does not exit from... (4 Replies)
Discussion started by: yohasini
4 Replies

8. UNIX for Dummies Questions & Answers

to get remote server date & time

Hi, i dont have remote m/c user credential. i only know remote m/c ip address. and i am able to ping that remote m/c. In windows we use: "net time \\computername" to get the remote m/c time. so how can i get remote m/c time in unix m/c? (means a unix command) Thanks for the help. ... (9 Replies)
Discussion started by: partha_ori
9 Replies

9. Shell Programming and Scripting

Can a script runned in local server access remote server?

Hi, Im creating a script that is supposed to run commands on remote server using sftp. My script is as below: #!/bin/ksh sftp remote_server mypassword cd /u08/mydir/allfiles mget * .. But this is what I got when I runned the script: Connecting to remote server...... (3 Replies)
Discussion started by: luna_soleil
3 Replies

10. Shell Programming and Scripting

FTP - Get the file date and time on the remote server

I would like to know if there is a way to get the date and timestamp of the file that is being FTP from the remote server using shell script. Currently the get command from FTP will have current date and timestamp. Tried the earlier suggestion 'HardFeed' but still getting the current date and time... (12 Replies)
Discussion started by: gthokala
12 Replies
Login or Register to Ask a Question