使用shell脚本监控Linux主机

编写如下脚本(根据实际情况来修改邮件发送目的地址):

[[email protected] ~]# cat sysmon.sh
#!/bin/bash
dug=$(df -h | grep "/$" | awk ‘{print $5}‘ | awk -F% ‘{print $1}‘)
cug=$(expr 100 - $(mpstat | tail -1 | awk ‘{print $12}‘ | awk -F. ‘{print $1}‘))
mug=$(expr $(free | grep "Mem:" | awk ‘{print $3}‘) \* 100 / $(free | grep "Mem:" | awk ‘{print $2}‘))
alog="/tmp/alert.txt"
email="root"                   #邮件发送目的地址变量
if [ $dug -gt 90 ]
then
        echo " 磁盘占用率:$dug % " >> $alog
fi
if [ $cug -gt 80 ]
then
         echo " cpu使用率: $cug % " >> $alog
fi
if [ $mug -gt 90 ]
then
        echo " 内存使用率:$mug % " >> $alog
fi
if [ -f $alog ]
then
 cat $alog | mail -s "Host Alert" $email      #有关发送邮件的配置
        rm -rf $alog
rm -rf $alog
fi

[[email protected] ~]# systemctl status crond            #查询crond服务是否为启动状态
● crond.service - Command Scheduler
   Loaded: loaded (/usr/lib/systemd/system/crond.service; enabled; vendor preset: enabled)
   Active: active (running) since 四 2019-05-23 05:03:09 CST; 1h 24min ago
 Main PID: 1339 (crond)
    Tasks: 1
   CGroup: /system.slice/crond.service
           └─1339 /usr/sbin/crond -n
         ...................

[[email protected] ~]# crontab -e                    #创建新的任务计划
*/30          *      *      *           *       /root/sysmon.sh
[[email protected] ~]# crontab -l              #查看任务计划
*/30          *      *      *           *       /root/sysmon.sh

原文地址:https://blog.51cto.com/14154700/2398780

时间: 2024-08-05 10:40:07

使用shell脚本监控Linux主机的相关文章

使用 shell 脚本监控 Linux 主机

编写以下脚本命令: [[email protected] sh]# vim sysmon.sh #!/bin/bash #提取性能监控指标(磁盘占用.CPU使用.内存使用) DUG=$(df -h | grep "/$" | awk '{print $5}' | awk -F% '{print $1}') CUG=$(expr 100 - $(mpstat | tail -1 | awk '{print $12}' | awk -F. '{print $1}')) MUG=$(expr

用shell脚本监控linux系统 自动发送邮件

此脚本可以做一个定时的检测,超出设定的值,即往邮箱发送警告 脚本用到bc,sendmail,163邮箱, yum install bc #!/bin/bash #System Monitoring Script while [ 1 ] do #本机需开启postfix或sendmail服务. #报警邮件地址设置 [email protected] [email protected] #设置脚本运行间隔时间.单位(秒). RUNTIME=900 #内存使用率监控设置,单位 (%) MEMTHRE=

shell脚本监控-linux操作系统

https://www.jb51.net/article/58799.htm 在没有nagios监控软件的情况下,只要服务器能上互联网,就可通过发邮件的方式来提醒管理员系统负载与CPU占用的使用情况. 一.安装linux下面的一个邮件客户端msmtp软件(类似于一个foxmail的工具) 1.下载安装: http://downloads.sourceforge.net/msmtp/msmtp-1.4.16.tar.bz2?modtime=1217206451&big_mirror=0 复制代码代

Shell脚本监控Linux某个后台进程,当进程死掉后重新启动服务,以httpd为例

Shell脚本如下: vim monitor.sh #!/bin/bash while true # 无限循环 flag=`ps -aux |grep "httpd" |grep -v "grep" |wc -l` do if [[ $flag -eq 0 ]] # 判断进程数如果等于0,则启动httpd then `systemctl start httpd` # 启动httpd echo `date` - "Apache restart" &

shell脚本批量操作linux主机:

一.环境介绍: 开启192.168.100.150-152(ctos1-3)和192.168.100.100(vsftpd). 1.ssh登录:在192.168.100.100上操作 1)密码登录192.168.100.150: ssh [email protected]  ##输入root密码123123 2)配置ssh密钥对登192.168.100.150: ssh-keygen ##创建密钥对,提示直接回车 ssh-copy-id -i /root/.ssh/id_rsa.pub [ema

shell脚本监控Linux系统的登录情况

一.登录日志记录 vi /etc/profile 在最后面添加一行: echo "`who`" > /var/log/login.log 二.监控日志文件 #!/bin/bash while true do sleep 2 a=`stat -c %Y /var/log/login.log` b=`date +%s` if [ $(($b-$a)) -le 10 ]; then echo "somebody login in!!!do something!!!"

shell脚本监控(ping)主机是否存活

#!/bin/bash #http://wangjiatong.blog.51cto.com/ #create2016-11-22 ping_success(){ if ping -c 1 $IP >/dev/null; then echo -e "\033[032m$IP ping issuccessful!!\033[0m" continue fi } IP_LIST="192.168.217.130192.168.217.142" for IP in$I

使用Shell脚本对Linux系统和进程资源进行监控

ShellLinux脚本 摘要:Shell语言对于接触Linux的人来说都比较熟悉,它是系统的用户界面,提供了用户与内核进行交互操作的一种接口.本文我们以Bash做为实例总结了使用Shell对系统和进程资源进行监控的一些内容,希望对您能有帮助. Shell语言对于接触Linux的人来说都比较熟悉,它是系统的用户界面,提供了用户与内核进行交互操作的一种接口.它接收用户输入的命 令并把它送入内核去执行.实际上Shell是一个命令解释器,它解释由用户输入的命令并且把它们送到内核.它没有一般编程语言的“

使用 shell 脚本对 Linux 系统和进程资源进行监控

Shell 简介 Shell 语言对于接触 LINUX 的人来说都比较熟悉,它是系统的用户界面,提供了用户与内核进行交互操作的一种接口.它接收用户输入的命令并把它送入内核去执行.实际上 Shell 是一个命令解释器,它解释由用户输入的命令并且把它们送到内核.它没有一般编程语言的“编译 - 链接 - 运行”过程.不仅如此,Shell 有自己的编程语言用于对命令的编辑,它允许用户编写由 shell 命令组成的程序.Shell 编程语言具有普通编程语言的很多特点,比如它也有循环结构和分支控制结构等,用