工作中常用的shell操作總結(jié)2
1. url地址監(jiān)控
#!/bin/bash
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH
# need you set this URL and Listening CONTEXT
URL='www.baidu.com'
CONTEXT='百度一下'
# define url
WEB_URL=('http://'+$URL)
# check network
NET_ALIVE=$(ping -c 5 ${URL} |grep 'received'|awk 'BEGIN {FS=","} {print $2}'|awk '{print $1}')
if [[ ${NET_ALIVE} == 0 ]]; then
echo "Network is not active,please check your network configuration!"
exit 0
fi
# check url
for((i=0; i!=${#WEB_URL[@]}; ++i))
{
ALIVE=$(curl -o /dev/null -s -m 30 -connect-timeout 30 -w %http_code ${WEB_URL[i]} |grep ${CONTEXT})
echo ${ALIVE}
if [[ ${ALIVE} =~ $CONTEXT ]]; then
echo "'${WEB_URL[i]}' is OK!"
else
echo "failed!"
fi
}
2. linux系統(tǒng)監(jiān)控相關整理
#!/bin/bash
# func:sys info check
[[ $(id -u) -gt 0 ]] && echo "請用root用戶執(zhí)行此腳本!" && exit 1
sys_version=$(rpm -q centos-release|cut -d- -f3)
line="-------------------------------------------------"
[[ -d logs ]] || mkdir logs
sys_check_file="logs/$(ip a show dev eth0|grep -w inet|awk '{print $2}'|awk -F '/' '{print $1}')-`date +%Y%m%d`.txt"
# 獲取系統(tǒng)cpu信息
function get_cpu_info() {
Physical_CPUs=$(grep "physical id" /proc/cpuinfo| sort | uniq | wc -l)
Virtual_CPUs=$(grep "processor" /proc/cpuinfo | wc -l)
CPU_Kernels=$(grep "cores" /proc/cpuinfo|uniq| awk -F ': ' '{print $2}')
CPU_Type=$(grep "model name" /proc/cpuinfo | awk -F ': ' '{print $2}' | sort | uniq)
CPU_Arch=$(uname -m)
cat <<EOF
CPU信息:
物理CPU個數(shù):${Physical_CPUs}
邏輯CPU個數(shù):${Virtual_CPUs}
每CPU核心數(shù):${CPU_Kernels}
CPU型號:${CPU_Type}
CPU架構:${CPU_Arch}
EOF
}
# 獲取系統(tǒng)內(nèi)存信息
function get_mem_info() {
check_mem=$(free -m)
MemTotal=$(grep MemTotal /proc/meminfo| awk '{print $2}') #KB
MemFree=$(grep MemFree /proc/meminfo| awk '{print $2}') #KB
let MemUsed=MemTotal-MemFree
MemPercent=$(awk "BEGIN {if($MemTotal==0){printf 100}else{printf \"%.2f\",$MemUsed*100/$MemTotal}}")
report_MemTotal="$((MemTotal/1024))""MB" #內(nèi)存總?cè)萘?MB)
report_MemFree="$((MemFree/1024))""MB" #內(nèi)存剩余(MB)
report_MemUsedPercent="$(awk "BEGIN {if($MemTotal==0){printf 100}else{printf \"%.2f\",$MemUsed*100/$MemTotal}}")""%" #內(nèi)存使用率%
cat <<EOF
內(nèi)存信息:${check_mem}
EOF
}
# 獲取系統(tǒng)網(wǎng)絡信息
function get_net_info() {
pri_ip_add=$(ip a show dev eth0|grep -w inet|awk '{print $2}'|awk -F '/' '{print $1}')
pub_ip_add=$(curl ifconfig.me -s)
gateway=$(ip route | grep default | awk '{print $3}')
mac_info=$(ip link| egrep -v "lo"|grep link|awk '{print $2}')
dns_config=$(egrep -v "^$|^#" /etc/resolv.conf)
route_info=$(route -n)
cat <<EOF
IP信息:
系統(tǒng)公網(wǎng)地址:${pub_ip_add}
系統(tǒng)私網(wǎng)地址:${pri_ip_add}
網(wǎng)關地址:${gateway}
MAC地址:${mac_info}
路由信息:${route_info}
DNS 信息:${dns_config}
EOF
}
# 獲取系統(tǒng)磁盤信息
function get_disk_info() {
disk_info=$(fdisk -l|grep "Disk /dev"|cut -d, -f1)
disk_use=$(df -hTP|awk '$2!="tmpfs"{print}')
disk_inode=$(df -hiP|awk '$1!="tmpfs"{print}')
cat <<EOF
磁盤信息:${disk_info}
磁盤使用:${disk_use}
inode信息:${disk_inode}
EOF
}
# 獲取系統(tǒng)信息
function get_sys_status_info() {
sys_os=$(uname -o)
sys_release=$(cat /etc/redhat-release)
sys_kernel=$(uname -r)
sys_hostname=$(hostname)
sys_selinux=$(getenforce)
sys_lang=$(echo $LANG)
sys_lastreboot=$(who -b | awk '{print $3,$4}')
sys_runtime=$(uptime |awk '{print $3,$4}'|cut -d, -f1)
sys_time=$(date)
sys_load=$(uptime |cut -d: -f5)
cat <<EOF
系統(tǒng)信息:
系統(tǒng): ${sys_os}
發(fā)行版本: ${sys_release}
系統(tǒng)內(nèi)核: ${sys_kernel}
主機名: ${sys_hostname}
selinux狀態(tài): ${sys_selinux}
系統(tǒng)語言: ${sys_lang}
系統(tǒng)當前時間: ${sys_time}
系統(tǒng)最后重啟時間: ${sys_lastreboot}
系統(tǒng)運行時間: ${sys_runtime}
系統(tǒng)負載: ${sys_load}
EOF
}
# 獲取服務信息
function get_service_info() {
port_listen=$(netstat -lntup|grep -v "Active Internet")
kernel_config=$(sysctl -p 2>/dev/null)
if [[ ${sys_version} -gt 6 ]];then
service_config=$(systemctl list-unit-files --type=service --state=enabled|grep "enabled")
run_service=$(systemctl list-units --type=service --state=running |grep ".service")
else
service_config=$(/sbin/chkconfig | grep -E ":on|:啟用" |column -t)
run_service=$(/sbin/service --status-all|grep -E "running")
fi
cat <<EOF
服務啟動配置:${service_config}
${line}
運行的服務:${run_service}
${line}
監(jiān)聽端口:${port_listen}
${line}
內(nèi)核參考配置:${kernel_config}
EOF
}
function get_sys_user() {
login_user=$(awk -F: '{if ($NF=="/bin/bash") print $0}' /etc/passwd)
ssh_config=$(egrep -v "^#|^$" /etc/ssh/sshd_config)
sudo_config=$(egrep -v "^#|^$" /etc/sudoers |grep -v "^Defaults")
host_config=$(egrep -v "^#|^$" /etc/hosts)
crond_config=$(for cronuser in /var/spool/cron/* ;do ls ${cronuser} 2>/dev/null|cut -d/ -f5;egrep -v "^$|^#" ${cronuser} 2>/dev/null;echo "";done)
cat <<EOF
系統(tǒng)登錄用戶:${login_user}
${line}
ssh 配置信息:${ssh_config}
${line}
sudo 配置用戶:${sudo_config}
${line}
定時任務配置:${crond_config}
${line}
hosts 信息:${host_config}
EOF
}
function process_top_info() {
top_title=$(top -b n1|head -7|tail -1)
cpu_top10=$(top b -n1 | head -17 | tail -11)
mem_top10=$(top -b n1|head -17|tail -10|sort -k10 -r)
cat <<EOF
CPU占用top10:
${top_title}
${cpu_top10}
內(nèi)存占用top10:
${top_title}
${mem_top10}
EOF
}
function capture_stats() {
DATE=`date +%m/%d/%Y`
TIME=`date +%l:%M%p`
# Logged in Users
users=`who | wc -l`
# Load Average
LOAD=`uptime | cut -d: -f5 | cut -d, -f3`
# Memory Free
FREE=`free -m | head -n 2 | tail -n 1 | awk {'print $4'}`
# Swap Free
SWAP=`free -m | tail -n 1 | awk {'print $3'}`
# CPU Idle
CPU=`vmstat 1 2 | sed -n '/[0-9]/p' | sed -n '2p' | gawk '{print $15}'`
# Disk Space Available
ROOT=`df -h / | awk '{ a = $4 } END { print a }'`
VAR=`df -h /var | awk '{ a = $4 } END { print a }'`
USR=`df -h /usr | awk '{ a = $4 } END { print a }'`
cat <<EOF
${DATE},${TIME},${users},${LOAD},${FREE} MB,${SWAP} MB,${CPU} %,${ROOT},${VAR},${USR}
EOF
}
function sys_check() {
get_cpu_info
echo ${line}
get_mem_info
echo ${line}
get_net_info
echo ${line}
get_disk_info
echo ${line}
get_sys_status_info
echo ${line}
get_service_info
echo ${line}
get_sys_user
echo ${line}
process_top_info
}
sys_check > ${sys_check_file}