#!/bin/bash
function cpu() {
NUM=1
while [ $NUM -le 3 ]; do
util=`vmstat |awk '{if(NR==3)print 100-$15"%"}'`
user=`vmstat |awk '{if(NR==3)print $13"%"}'`
sys=`vmstat |awk '{if(NR==3)print $14"%"}'`
iowait=`vmstat |awk '{if(NR==3)print $16"%"}'`
echo -e "CPU - 使用率:"$util",等待磁盘IO响应使用率:"$iowait
let NUM++
sleep 1
done
}
function memory() {
total=`free -m |awk '{if(NR==2)printf "%.1f",$2/1024}'`
used=`free -m |awk '{if(NR==2) printf "%.1f",($2-$NF)/1024}'`
available=`free -m |awk '{if(NR==2) printf "%.1f",$NF/1024}'`
echo "内存 - 总大小: ${total}G , 使用: ${used}G , 剩余: ${available}G"
}
function disk() {
fs=$(df -h |awk '/^\/dev/{print $1}')
for p in $fs; do
mounted=$(df -h |awk '$1=="'$p'"{print $NF}')
size=$(df -h |awk '$1=="'$p'"{print $2}')
used=$(df -h |awk '$1=="'$p'"{print $3}')
used_percent=$(df -h |awk '$1=="'$p'"{print $5}')
echo -e "硬盘 - 挂载点:"$mounted",总大小:"$size",使用:"$used",使用率:"$used_percent
done
}
function tcp_status() {
summary=$(ss -antp |awk '{status[$1]++}END{for(i in status) printf i":"status[i]" "}')
echo "TCP连接状态 - $summary"
}
function 80_port(){
apa=`netstat -tanpl | grep 80 | awk '{print $4}' | sed -n '1p'`
if [ $apa != "0.0.0.0:80" ];then
echo "80端口已经停止运行"
else
echo "80端口运行正常"
fi
}
echo -e $(cpu)"\n"$(memory)"\n"$(disk)"\n"$(tcp_status)"\n"$(80_port) | mail -s "huacai服务器状态" 1324928751@qq.com
分类存档: shell
监测阿里云CentOS服务器的shell脚本
监测服务器的shell脚本
#! /bin/bash
#jiance.sh
#硬盘使用率
ds=`df|awk '{if(NR==4){print int($5)}}'`
#内存使用率
used=`free -m|awk '{if(NR==2){print int($3)}}'`
tot=`free -m|awk '{if(NR==2){print int($2)}}'`
per=$(($used*100/$tot))
#apache2是否正常运行
apa=`netstat -tanpl | grep 80 | awk '{print $4}' | sed -n '1p'`
#mysql是否正常运行
mysql=`netstat -tanpl | grep 3306 | awk '{print $4}' | sed -n '1p'`
#统计
res1="硬盘使用率:"$ds"%\n内存使用率:"$per"%\n"
#echo -e $res1
if [ $apa != "0.0.0.0:80" ];then
str1="apache2已经停止运行"
else
str1="apache2正常运行"
fi
if [ $mysql != "127.0.0.1:3306" ];then
str2="myqsl已经停止运行"
else
str2="mysql正常运行"
fi
#统计输出
text=$res1$str1"\n"$str2;
#监控发邮件
if [ $ds -gt 45 ];then
echo -e $text|mail -s "hotsun.link的运行状态" 1324928751@qq.com
fi
if [ $per -gt 55 ];then
echo -e $text|mail -s "hotsun.link的运行状态" 1324928751@qq.com
fi
如何查看日志
日 志 文 件 说 明
/var/log/message 系统启动后的信息和错误日志,是Red Hat Linux中最常用的日志之一
/var/log/secure 与安全相关的日志信息
/var/log/maillog 与邮件相关的日志信息
/var/log/cron 与定时任务相关的日志信息
/var/log/spooler 与UUCP和news设备相关的日志信息
/var/log/boot.log 守护进程启动和停止相关的日志消息
系统:
# uname -a # 查看内核/操作系统/CPU信息
# cat /etc/issue
# cat /etc/redhat-release # 查看操作系统版本
# cat /proc/cpuinfo # 查看CPU信息
# hostname # 查看计算机名
# lspci -tv # 列出所有PCI设备
# lsusb -tv # 列出所有USB设备
# lsmod # 列出加载的内核模块
# env # 查看环境变量
资源:
# free -m # 查看内存使用量和交换区使用量
# df -h # 查看各分区使用情况
# du -sh <目录名> # 查看指定目录的大小
# grep MemTotal /proc/meminfo # 查看内存总量
# grep MemFree /proc/meminfo # 查看空闲内存量
# uptime # 查看系统运行时间、用户数、负载
# cat /proc/loadavg # 查看系统负载
磁盘和分区:
# mount | column -t # 查看挂接的分区状态
# fdisk -l # 查看所有分区
# swapon -s # 查看所有交换分区
# hdparm -i /dev/hda # 查看磁盘参数(仅适用于IDE设备)
# dmesg | grep IDE # 查看启动时IDE设备检测状况
网络:
# ifconfig # 查看所有网络接口的属性
# iptables -L # 查看防火墙设置
# route -n # 查看路由表
# netstat -lntp # 查看所有监听端口
# netstat -antp # 查看所有已经建立的连接
# netstat -s # 查看网络统计信息
进程:
# ps -ef # 查看所有进程
# top # 实时显示进程状态(另一篇文章里面有详细的介绍)
用户:
# w # 查看活动用户
# id <用户名> # 查看指定用户信息
# last # 查看用户登录日志
# cut -d: -f1 /etc/passwd # 查看系统所有用户
# cut -d: -f1 /etc/group # 查看系统所有组
# crontab -l # 查看当前用户的计划任务
服务:
# chkconfig –list # 列出所有系统服务
# chkconfig –list | grep on # 列出所有启动的系统服务
程序:
# rpm -qa # 查看所有安装的软件包
zip unzip tar通常用法
将/home/Blinux/html/这个目录下所有文件和文件夹打包为当前目录下的html.zip:zip -q -r html.zip /home/Blinux/html
上面的命令操作是将绝对地址的文件及文件夹进行压缩,以下给出压缩相对路径目录,比如目前在Bliux这个目录下,执行以下操作可以达到以上同样的效果:
zip -q -r html.zip html
比如现在我的html目录下,我操作的zip压缩命令是:
zip -q -r html.zip *
将压缩文件text.zip在当前目录下解压缩。
Google earth安装与删除
sudo dpkg -i google-earth.deb
sudo apt-get -f install
sudo dpkg –list ‘google-earth*’ | cat
sudo dpkg -P google-earth-stable
rm -rf ~/.googleearth
近期评论