此脚本为上次发布版本的更新版,添加了些内容,增加了用户体验!
使用方法:1.根据自己的实际环境新建自己的目录 2.将脚本’run.sh‘、'MonitorOS.py'、'alter_diskpath.sh'放到新建的目录下,并添加可执行权限 3.执行'alter_diskpath.sh'脚本来修改要监控的磁盘挂载目录!(注意:建议一次性的正确输入,此脚本未添加异常处理,执行后输错需要手动进入py脚本中手动修改!参考命令:“df -hT”) 4.执行'run.sh'脚本来启动监控程序,参考命令:./run.sh start | stop | status | restart 5.查看当前目录下面的monitoros.log日志来获取您的Linux系统性能信息 6.日志输出路径可自定义修改,在Crontab.sh脚本中修改即可 (默认路径为当前目录下,默认为每5分钟输出一次) 7.关于日志变大的清理方法,使用Linux自带的crontab中加入定时清空日志即可 此脚本作者还在持续优化中...... (更新时间2021.11.23)
alter_diskpath.sh代码如下:
#!/bin/bash
read -p '请输入本地环境要监控的磁盘路径:' input
path=$input
eval sed -i 's#DiskPath#${path}#g' MonitorOS.py
echo $?
echo '输入成功!'
Crontab.sh代码如下:
#!/bin/bash
#执行py脚本启动监控
while true
do
python3 MonitorOS.py >> ./monitoros.log
sleep 300
done
run.sh代码如下:
#!/usr/bin/bash
APP_NAME=Crontab.sh
#使用说明,用来提示输入参数
usage() {
echo "Usage: sh 执行脚本.sh [start|stop|restart|status]"
exit 1
}
#检查程序是否在运行
is_exist() {
pid=$(ps -aux | grep ${APP_NAME} | grep -v grep | awk '{print $2}')
#如果不存在返回1,存在返回0
if [[ -z "${pid}" ]]; then
return 1
else
return 0
fi
}
#启动方法
start() {
is_exist
if [[ $? -eq "0" ]]; then
echo "${APP_NAME} is already running. pid is ${pid} ."
else
nohup sh Crontab.sh &
# nohup /home/paas/jdk1.8.0_202/bin/java -jar ${PARAMS} ${APP_NAME} > /dev/null 2>&1 &
status
fi
}
#停止方法
stop() {
is_exist
if [[ $? -eq "0" ]]; then
kill -9 ${pid}
echo "${APP_NAME} is shutdown."
else
echo "${APP_NAME} is not running"
fi
}
#输出运行状态
status() {
is_exist
if [[ $? -eq "0" ]]; then
echo "${APP_NAME} is running. pid is ${pid}"
else
echo "${APP_NAME} is NOT running."
fi
}
#重启
restart() {
stop
start
}
#根据输入参数,选择执行对应方法,不输入则执行使用说明
case "$1" in
"start")
start
;;
"stop")
stop
;;
"status")
status
;;
"restart")
restart
;;
*)
usage
;;
esac
MonitorOS.py代码如下:
#!/usr/bin/python3
#!_*_ coding:utf-8 _*_
#导入使用的模块
import os,re
import time
def get_usage():
disk = os.popen("df -hT DiskPath | awk 'NR==2{print $6}'")
disk = disk.read().strip()
#print(time.strftime('%Y-%m-%d %H:%M:%S', time.localtime()),'[INFO]:当前磁盘使用:',disk)
disk_use = '当前磁盘使用:' + disk
disk_int = "df -hT DiskPath | awk 'NR==2{print $6}' | awk -F% '{print $1}'"
disk_shell = os.popen(disk_int)
disk_shell = disk_shell.read().strip()
disk_Surplus = 100 - int(disk_shell)
disk_surplus = '当前磁盘剩余:' + str(disk_Surplus) + '%'
disk_space = "df -hT | grep DiskPath | awk '{print $3}'"
disk_shell_2 = os.popen(disk_space)
disk_shell_2 = disk_shell_2.read().strip()
disk_Space = '总容量:' + disk_shell_2
use_percent = int(re.search("[0-9]+",disk).group()) #分组截取取判断值
disk_spaces = ' ['
if use_percent > 80:
print(time.strftime('%Y-%m-%d %H:%M:%S', time.localtime()),"[WARING]:磁盘容量剩余不足20%,请及时清理空间!" + '\n' + disk_spaces + disk_Space + ' ' + disk_surplus + ']')
else:
print(time.strftime('%Y-%m-%d %H:%M:%S', time.localtime()),"[INFO]:磁盘容量足够,请放心运行!" + '\n' + disk_spaces + disk_use + ']')
def get_uscpu():
#CPU使用率
cpu = os.popen("iostat | awk 'NR==4{print $6}'")
cpu = cpu.read().strip()
cpu_Use = 100 - float(cpu)
cpu_use = '当前CPU使用率为:' + str(cpu_Use)[:5] + '%'
use_cpu = int(re.search("[0-9]+",cpu_use).group())
cpu_spaces = ' ['
if use_cpu > 80:
print(time.strftime('%Y-%m-%d %H:%M:%S', time.localtime()),"[WARING]:CPU使用率占用过高,请注意!" + '\n' + cpu_spaces + cpu_use + ']' + '\n' + cpu_spaces + get_cpuus() + ']')
else:
print(time.strftime('%Y-%m-%d %H:%M:%S', time.localtime()),"[INFO]:CPU使用率占用正常,请放心运行!" + '\n' + cpu_spaces + cpu_use + ']' + '\n' + cpu_spaces + get_cpuus() + ']')
def get_cpuus():
#CPU每分钟使用值
cpu_1 = os.popen("uptime | awk '{print $10}'")
cpu_2 = os.popen("uptime | awk '{print $11}'")
cpu_3 = os.popen("uptime | awk '{print $12}'")
cpu_1 = cpu_1.read().strip()
cpu_2 = cpu_2.read().strip()
cpu_3 = cpu_3.read().strip()
cpu_onemin = '每分钟:'
cpu_fivemin = '5分钟/次:'
cpu_fifteen = '15分钟/次:'
cpu_uptime = 'CPU使用值:' + ' ' + cpu_onemin + ' ' + cpu_1 + ' ' + cpu_fivemin + ' ' + cpu_2 + ' ' + cpu_fifteen + ' ' +cpu_3
return cpu_uptime
def get_usmem():
#内存总量
mem_all = os.popen("free -mg | awk 'NR==2{print $2}'")
mem_all = mem_all.read().strip()
mem_all_use = '内存总大小:' + mem_all
#物理内存
mem = os.popen("free -mg | awk 'NR==2{print $3}'")
mem = mem.read().strip()
mem_use = '当前物理内存已使用:' + mem + 'G'
#交换内存
mem_swap = os.popen("free -mg | awk 'NR==3{print $3}'")
mem_swap = mem_swap.read().strip()
mem_swap_use = '当前交换内存已使用:' + mem_swap + 'G'
#剩余内存
mem_surplus = os.popen("free -mg | awk 'NR==2{print $7}'")
mem_surplus = mem_surplus.read().strip()
mem_surplus_use = '当前物理内存剩余:' + mem_surplus + 'G'
#内存剩余率
mem_surplus_int = mem_surplus
mem_int = mem
mem_mem = float(mem_surplus_int) / float(mem_int)
mem_mem_float = float(mem_mem) * 100
mem_mem_mem = '剩余百分率为:' + str(mem_mem_float)[:2] + '%'
#内存使用率
mem_all_int = mem_all
mem_int_int = mem
mem_all_use_mem = float(mem_int_int) / float(mem_all_int)
mem_all_use_mem_float = float(mem_all_use_mem) * 100
mem_all_use_mem_mem = '内存使用率:' + str(mem_all_use_mem_float)[:2] + '%'
mem_spaces = ' ['
if mem_mem_float < 10:
print(time.strftime('%Y-%m-%d %H:%M:%S', time.localtime()),'[WARING]:内存占用已超标,请检查!' + '\n' + mem_spaces + mem_all_use + ' ' + mem_surplus_use + ' ' + mem_mem_mem + ']')
else:
print(time.strftime('%Y-%m-%d %H:%M:%S', time.localtime()),'[INFO]:内存使用正常,请放心运行!' + '\n' + mem_spaces + mem_all_use_mem_mem + ' ' + mem_use + ' ' + mem_swap_use + ']')
if __name__ == '__main__':
try:
get_usage()
except Exception as a:
print('[ERROR]:main方法调用 “get_usage()” 函数失败,磁盘指标监控不可用!')
try:
get_uscpu()
except Exception as b:
print('[ERROR]:main方法调用 “get_uscpu()” 函数失败,cpu指标监控不可用!')
try:
get_usmem()
except Exception as c:
print('[ERROR]:main方法调用 “get_usmem()” 函数失败,内存指标监控不可用!')
感谢使用!