All pastes #275749 Raw Edit

Troy

public text v1 · immutable
·published 2006-12-11 19:49 UTC
rendered paste body
#!name = HTTPCONTENT_PROBE
########################################################################################
#
# Description :
#    Script sends a http 1.0 GET request to a webserver to test if the server return 
#    page is  certain content-type. Probe success only if server returns the Content-type 
#    header with a configured value.
#    
# CSM version :
#   3.1+
#
# Parameters : 
#   <requestHearder> <expectFileType> [debugFlag]
#     regquestHeader - HTTP request to send e.g "GET /yahoo.html HTTP/1.0"
#     expectFileType - file type expected e.g text
#     debugFlag     - default 0. Do NOT turn on while multiple probes are configured
#
# Example config :
#   probe httpProbe script
#         script HTTPCONTENT_PROBE "GET /index.html HTTP/1.0" text [0]
#
########################################################################################

#-------------------------------------------
# debug procedure
# set the EXIT_MSG environment varaible to help debug
# also print the debug message when debug flag is on
#-------------------------------------------

proc csm_debug { msg } { 
    global debug ip port EXIT_MSG

    set EXIT_MSG $msg
    if { [ info exists ip ] && [ info exists port ] } { 
	set EXIT_MSG "[ info script ]:$ip:$port: $EXIT_MSG "
    }  
    if { [ info exists debug ] && $debug } { 
	puts $EXIT_MSG
    } 
} 



#-------------------------------------------
# main 
#-------------------------------------------

# parse cmd line args and initialize variables
csm_debug "initializing varaible"
if { $argc < 2 } {
    set EXIT_MSG "ERR config:  script HTTPCONTENT_PROBE <requet_header> <expectFileTyep> <debug(0|1)\texample : script HTTPCONTNET_PROBE_SCRIPT \"GET / HTTP/1.0\" html 0"
    puts $EXIT_MSG 
    exit 5001 
} 

set ip $csm_env(realIP) 
set port $csm_env(realPort) 
# if port is zero the use well known http port 80 
if { $port == 0} {
    set port 80
}

set requestHeader [ lindex $argv 0 ]
set expectFileType [ lindex $argv 1 ]
set debug [ lindex $argv 2 ]
if { $debug == "" } { 
    set debug 0
} 
# open connection 
csm_debug "opening socket"
set sock [ socket $ip $port ]


#  send http requeset to server
csm_debug "sending request : $requestHeader"
puts -nonewline $sock "$requestHeader\n\n"
flush $sock
 
#  read string back from server
csm_debug "receiving response"
set lines [ read $sock ]
 
#  close connection
csm_debug "closing socket"
close $sock


#  parsing http  response to decide if probe success or failed
#  all the following condition casing probe faile. should return exit 5001
if { ![ regexp -nocase "^HTTP/1\.\[0-9\] (\[0-9\]\[0-9\]\[0-9\])" $lines match statuscode ] } {
    csm_debug "probe fail : can't find status code"
    exit 5001
}  

if { $statuscode != "200" } { 
    csm_debug "probe fail : status code is $statuscode"
    exit 5001
} 

if { ![ regexp  -nocase  "Content-Type *:(.*)\n" $lines match foundContentType] } {
    csm_debug "probe fail : does not found \'Content-Type\' header"
    exit 5001
} 

if { ![ regexp "$expectFileType" $foundContentType]  } {
    csm_debug "probe fail : expect content-type \'$expectFileType\', but got \'$foundContentType\'"
    exit 5001

}   

#  Everything went fine. probe exit with success exit_code 5000
csm_debug "probe success"
exit 5000





************ OR *****************

#!name = HTTP_PROBE_SCRIPT
##################################################################################################
#
# Description :
#    This script send a http request to a webserver and probe if
#    server returns the Content-type header with expected File type 
#    as user configured
#    
# Pamapaters : 
#	<regquestHeader> HTTP request to send e.g "GET /yahoo.html HTTP/1.0"
#	<expectFileType> file type expected e.g text
#	[debug]			optional debug flag
#
# Example config :
#   probe httpProbe script
#         script HTTP_PROBE_SCRIPT "GET /index.html HTTP/1.0" html 0
#
#####################################################################

#-------------------------------------------
# debug procedure
# set the EXIT_MSG environment varaible to help debug
# also print the debug message when debug flag is on
#-------------------------------------------

proc set_exit_msg { msg } { 
    global debug ip port EXIT_MSG

    set EXIT_MSG $msg
    if { [ info exists ip ] && [ info exists port ] } { 
	set EXIT_MSG "[ info script ]:$ip:$port: $EXIT_MSG "
    }  
    if { [ info exists debug ] && $debug } { 
	puts $EXIT_MSG
    } 
} 



#-------------------------------------------
# main 
#-------------------------------------------

# parse cmd line args and initialize variables
set_exit_msg "initializing varaible"
if { $argc != 3 } { 
    set_exit_msg "[ info script ] paramaters : <requet_header> <expectFileType> <debug>"
    exit 5001 
} 
set ip $csm_env(realIP) 
set port $csm_env(realPort) 
# if port is zero the use well known http port 80 
if { $port == 0} {
    set port 80
}

set requestHeader [ lindex $argv 0 ]
set expectFileType [ lindex $argv 1 ]
set debug [ lindex $argv 2 ]

# open connection 
set_exit_msg "opening socket"
set sock [ socket $ip $port ]


#  send http requeset to server
set_exit_msg "sending request : $requestHeader"
puts -nonewline $sock "$requestHeader\n\n"
flush $sock
 
#  read string back from server
set_exit_msg "receiving response"
set lines [ read $sock ]
 
#  close connection
set_exit_msg "closing socket"
close $sock


#  parsing http  response to decide if probe success or failed
#  all the following condition casing probe faile. should return exit 5001
if { ![ regexp -nocase "^HTTP/1\.\[0-9\] (\[0-9\]\[0-9\]\[0-9\])" $lines match statuscode ] } {
    set_exit_msg "probe fail : can't find status code"
    exit 5001
}  

if { $statuscode != "200" } { 
    set_exit_msg "probe fail : status code is $statuscode"
    exit 5001
} 

if { ![ regexp  -nocase  "Content-Type *:(.*)\n" $lines match foundContentType] } {
    set_exit_msg "probe fail : does not found \'Content-Type\' header"
    exit 5001
} 

if { ![ regexp "$expectFileType" $foundContentType]  } {
    set_exit_msg "probe fail : expect content-type \'$expectFileType\', but got \'$foundContentType\'"
    exit 5001

}   

#  Everything went fine. probe exit with success exit_code 5000
set_exit_msg "probe success"
exit 5000