Using Curl to monitor Web shell scripts under Linux

Source: Internet
Author: User

#!/bin/bash

Smail () {
Mail-s "$" [email protected] <<eof
$
$
====
Report time: ' Date + '%F%T '
Current User: ' WhoAmI '
Shell script: ' Echo $ '
====
Eof
}

SSMs () {
/usr/local/feixin/fetion--mobile=150000000--pwd=********--to=13810000000--msg-gb= "FX $"
}

cd/home/maintain/gaojianwei/script/
File=monitor_ip.txt
Sed-i/.*/d Curl_out.txt
Sed-i/.*/d Curl_out_1.txt

Sed-e '/^#/d;/^$/d ' ${file} | While read Ip Port URL
Do
/usr/bin/curl--connect-timeout 8--max-time 12-o/dev/null-s-W%{time_total}:%{size_download}:%{http_code} http://${ URL}-X ${ip}:${port} >> curl_out.txt
echo ": ${ip}:${url}" >> Curl_out.txt
Done

Awk-f ":" ' {if ($1*1000<8000 && ($2>0) && ($3== "200" | | $3== "301" | | $30== "302" | | $3== "401")) {} else {print $ >> "Curl_out_1.txt"}} ' Curl_out.txt

If [-S Curl_out_1.txt];then
warning= "' awk ' {printf ("%s# ", $ A)} ' Curl_out_1.txt '"
SSMs ${warning}
Smail Curl_monitor ${warning}
Fi notes:
Curl is a command-line HTTP download tool, similar to wget. Similar to wget, it can also determine the status of a service by sending a specified HTTP header to the server. Here's a way to monitor page usability using curl. You can use the following command to capture the status code of the page. If this command returns a result of 200, the service is OK. If a different page is returned, the exception is indicated. Curl-o/dev/null-s-W%{http_code} http://zys.8800.org/The- o parameter, which redirects all downloaded content to the/dev/null,-s command, masks the output of curl itself, while the-w parameter customizes the output format of curl according to our own needs. using this command, together with messages and text messages, enables you to monitor the usability of the page. By deploying this program across the country's machines, you can monitor the availability of your CDN network.

Curl only returns the server response status, does not return content, return 200 is normal, other abnormal, simple commands are as follows:

[Email protected] ~]$ echo ' Curl-o/dev/null-s-M--connect-timeout 10-w%{http_code} "Http://www.coomix.net/index. JSP "'
200
[Email protected] ~]$ echo ' Curl-o/dev/null-s-M--connect-timeout 10-w%{http_code} "http://www.coomix.net/index5 . JSP "'
404

====================================================

Monitor Machine list file:

Server.list

Server1

Server2

Server3

To establish a monitoring script: webstatus.sh

#!/bin/sh
monitor_dir=/home/admin/monitor/
if [!-D $monitor _dir]; Then
mkdir $monitor _dir
Fi
CD $monitor _dir
Web_stat_log=web.status
if [!-f $web _stat_log]; Then
Touch $web _stat_log
Fi
Server_list_file=server.list
if [!-f $server _list_file]; Then
echo "' Date ' +%y-%m-%d%h:%m:%s ' ERROR: $server _list_file not exists!" >> $web _stat_log
Exit 1
Fi
#total = ' wc-l $server _list_file|awk ' {print $} '
For website in ' Cat $server _list_file '
Do
Url= "/http $website/app.htm"
Server_status_code= ' Curl-o/dev/null-s-M--connect-timeout 10-w%{http_code} "$url" '
If ["$server _status_code" = "200"]; Then
echo "' Date ' +%y-%m-%d%h:%m:%s ' visit $website status code OK" >> $web _stat_log
Else
echo "' Date ' +%y-%m-%d%h:%m:%s ' visit $website error!!! Server can ' t connect at 10s or stop response at ten s, send alerm sms ... ">> $web _stat_log
echo "!app alarm @136xxxxxxxx server: $website can ' t connect at 10s or stop response at 10s ..." | NC smsserver Port &
Fi
Done
Exit 0

The main is to use Curl-o/dev/null-s-M--connect-timeout 10-w%{http_code} "$url" Return status code whether 200, if 10s did not return 200 status code, the alarm

Finally, let Linux execute the script at timed intervals:

Crontab-e

*/10 * * * */home/admin/app/bin/webstatus.sh

This will be done every 10 minutes.

This is another script notation:

#!/bin/bash

While Read URL
Do
Echo ' Date '
result= ' Curl-o/dev/null-s-M--connect-timeout 10-w%{http_code} $URL '
Test= ' echo $result '
if [["$test" = "200"]]
Then
echo "$URL is OK"
Else
echo "Test Err"
/usr/sbin/sendmail-t << EOF
From:sd-detect
To:[email Protected],[email protected]
Subject:detected $URL
------------------------------
${url} is err!!
------------------------------
Eof
Fi
Done </root/jiankong/httplist.txt

Using Curl to monitor Web shell scripts under Linux

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.