詳談java聚集框架。本站提示廣大學習愛好者:(詳談java聚集框架)文章只能為提供參考,不一定能成為您想要的結果。以下是詳談java聚集框架正文
監控硬件運轉狀態
shell 監控cpu,memory,load average,記載到log,當負載壓力時,發電郵告訴治理員。
道理:
1.獲得cpu,memory,load average的數值
2.斷定數值能否跨越自界說的規模,例如(CPU>90%,Memory<10%,load average>2)
3.如數值跨越規模,發送電郵告訴治理員。發送有時光距離,每小時只會發送一次。
4.將數值寫入log。
5.設置crontab 每30秒運轉一次。
ServerMonitor.sh
#!/bin/bash # 體系監控,記載cpu、memory、load average,當跨越劃定數值時發電郵告訴治理員 # *** config start *** # 以後目次途徑 ROOT=$(cd "$(dirname "$0")"; pwd) # 以後辦事器名 HOST=$(hostname) # log 文件途徑 CPU_LOG="${ROOT}/logs/cpu.log" MEM_LOG="${ROOT}/logs/mem.log" LOAD_LOG="${ROOT}/logs/load.log" # 告訴電郵列表 NOTICE_EMAIL='[email protected]' # cpu,memory,load average 記載上一次發送告訴電郵時光 CPU_REMARK='/tmp/servermonitor_cpu.remark' MEM_REMARK='/tmp/servermonitor_mem.remark' LOAD_REMARK='/tmp/servermonitor_loadaverage.remark' # 發告訴電郵距離時光 REMARK_EXPIRE=3600 NOW=$(date +%s) # *** config end *** # *** function start *** # 獲得CPU占用 function GetCpu() { cpufree=$(vmstat 1 5 |sed -n '3,$p' |awk '{x = x + $15} END {print x/5}' |awk -F. '{print $1}') cpuused=$((100 - $cpufree)) echo $cpuused local remark remark=$(GetRemark ${CPU_REMARK}) # 檢討CPU占用能否跨越90% if [ "$remark" = "" ] && [ "$cpuused" -gt 90 ]; then echo "Subject: ${HOST} CPU uses more than 90% $(date +%Y-%m-%d' '%H:%M:%S)" | sendmail ${NOTICE_EMAIL} echo "$(date +%s)" > "$CPU_REMARK" fi } # 獲得內存應用情形 function GetMem() { mem=$(free -m | sed -n '3,3p') used=$(echo $mem | awk -F ' ' '{print $3}') free=$(echo $mem | awk -F ' ' '{print $4}') total=$(($used + $free)) limit=$(($total/10)) echo "${total} ${used} ${free}" local remark remark=$(GetRemark ${MEM_REMARK}) # 檢討內存占用能否跨越90% if [ "$remark" = "" ] && [ "$limit" -gt "$free" ]; then echo "Subject: ${HOST} Memory uses more than 90% $(date +%Y-%m-%d' '%H:%M:%S)" | sendmail ${NOTICE_EMAIL} echo "$(date +%s)" > "$MEM_REMARK" fi } # 獲得load average function GetLoad() { load=$(uptime | awk -F 'load average: ' '{print $2}') m1=$(echo $load | awk -F ', ' '{print $1}') m5=$(echo $load | awk -F ', ' '{print $2}') m15=$(echo $load | awk -F ', ' '{print $3}') echo "${m1} ${m5} ${m15}" m1u=$(echo $m1 | awk -F '.' '{print $1}') local remark remark=$(GetRemark ${LOAD_REMARK}) # 檢討能否負載能否有壓力 if [ "$remark" = "" ] && [ "$m1u" -gt "2" ]; then echo "Subject: ${HOST} Load Average more than 2 $(date +%Y-%m-%d' '%H:%M:%S)" | sendmail ${NOTICE_EMAIL} echo "$(date +%s)" > "$LOAD_REMARK" fi } # 獲得上一次發送電郵時光 function GetRemark() { local remark if [ -f "$1" ] && [ -s "$1" ]; then remark=$(cat $1) if [ $(( $NOW - $remark )) -gt "$REMARK_EXPIRE" ]; then rm -f $1 remark="" fi else remark="" fi echo $remark } # *** function end *** cpuinfo=$(GetCpu) meminfo=$(GetMem) loadinfo=$(GetLoad) echo "cpu: ${cpuinfo}" >> "${CPU_LOG}" echo "mem: ${meminfo}" >> "${MEM_LOG}" echo "load: ${loadinfo}" >> "${LOAD_LOG}" exit 0
監控網站能否異常
shell 監控網站能否異常的劇本,若有異常主動發電郵告訴治理員。
流程:
1.檢討網站前往的http_code能否等於200,如不是200視為異常。
2.檢討網站的拜訪時光,跨越MAXLOADTIME(10秒)視為異常。
3.發送告訴電郵後,在/tmp/monitor_load.remark記載發送時光,在一小時內不反復發送,如一小時後則清空/tmp/monitor_load.remark。
#!/bin/bash SITES=("http://web01.example.com" "http://web02.example.com") # 要監控的網站 NOTICE_EMAIL='[email protected]' # 治理員電郵 MAXLOADTIME=10 # 拜訪超不時間設置 REMARKFILE='/tmp/monitor_load.remark' # 記載時否發送過告訴電郵,如發送過則一小時內不再發送 ISSEND=0 # 能否有發送電郵 EXPIRE=3600 # 每次發送電郵的距離秒數 NOW=$(date +%s) if [ -f "$REMARKFILE" ] && [ -s "$REMARKFILE" ]; then REMARK=$(cat $REMARKFILE) # 刪除過時的電郵發送時光記載文件 if [ $(( $NOW - $REMARK )) -gt "$EXPIRE" ]; then rm -f ${REMARKFILE} REMARK="" fi else REMARK="" fi # 輪回斷定每一個site for site in ${SITES[*]}; do printf "start to load ${site}\n" site_load_time=$(curl -o /dev/null -s -w "time_connect: %{time_connect}\ntime_starttransfer: %{time_starttransfer}\ntime_total: %{time_total}" "${site}") site_access=$(curl -o /dev/null -s -w %{http_code} "${site}") time_total=${site_load_time##*:} printf "$(date '+%Y-%m-%d %H:%M:%S')\n" printf "site load time\n${site_load_time}\n" printf "site access:${site_access}\n\n" # not send if [ "$REMARK" = "" ]; then # check access if [ "$time_total" = "0.000" ] || [ "$site_access" != "200" ]; then echo "Subject: ${site} can access $(date +%Y-%m-%d' '%H:%M:%S)" | sendmail ${NOTICE_EMAIL} ISSEND=1 else # check load time if [ "${time_total%%.*}" -ge ${MAXLOADTIME} ]; then echo "Subject: ${site} load time total:${time_total} $(date +%Y-%m-%d' '%H:%M:%S)" | sendmail ${NOTICE_EMAIL} ISSEND=1 fi fi fi done # 發送電郵跋文錄發送時光 if [ "$ISSEND" = "1" ]; then echo "$(date +%s)" > $REMARKFILE fi exit 0