作者是小白菜鸡,嘴下留情!
===使用者必读===
此脚本仅供运维人员使用!
在平时的运维过程当中,可使得我们清晰、可视化、自动化来监控Linux系统的性能,进而提升效率和质量!
主要监控的内容:CPU、内存、磁盘、IO
使用方法:1.根据自己的实际环境新建自己的目录
2.将Shell脚本’run.sh‘和Python脚本'MonitorOS.py'放到新建的目录下,并添加可执行权限
3.打开MonitorOS.py脚本,修改disk、disk_int、disk_space后面的磁盘路径(根据自己的实际环境修改!否则运行失败)
4.执行Shell脚本'run.sh'并将起加入后台进程实现定时执行,参考命令“nohup sh run.sh &”
5.查看当前目录下面的monitoros.log日志来获取您的Linux系统性能信息
6.日志输出路径可自定义修改,在Shell脚本中修改即可
7.关于日志变大的清理方法,使用Linux自带的crontab中加入定时清空日志即可
MonitorOS.py代码如下:
#!/usr/bin/python3
#!_*_ coding:utf-8 _*_
#导入使用的模块
import os,re
import time
def get_usage():
disk = os.popen("df -hT /dev/mapper/rhel-home | 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 /dev/mapper/rhel-home | 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 /dev/mapper/rhel-home | 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()) #分组截取取判断值
if use_percent > 80:
print(time.strftime('%Y-%m-%d %H:%M:%S', time.localtime()),"[WARING]:磁盘容量剩余不足20%,请及时清理空间!" + '\n' + ' [' + disk_Space + ' ' + disk_surplus + ']')
else:
print(time.strftime('%Y-%m-%d %H:%M:%S', time.localtime()),"[INFO]:磁盘容量足够,请放心运行!" + '\n' + ' [' + 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())
if use_cpu > 80:
print(time.strftime('%Y-%m-%d %H:%M:%S', time.localtime()),"[WARING]:CPU使用率占用过高,请注意!" + '\n' + ' [' + cpu_use + ']' + '\n' + ' [' + get_cpuus() + ']')
else:
print(time.strftime('%Y-%m-%d %H:%M:%S', time.localtime()),"[INFO]:CPU使用率占用正常,请放心运行!" + '\n' + ' [' + cpu_use + ']' + '\n' + ' [' + 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 -mh | awk 'NR==2{print $2}'")
mem_all = mem_all.read().strip()
mem_all_use = '内存总大小:' + mem_all
#物理内存
mem = os.popen("free -mh | awk 'NR==2{print $3}'")
mem = mem.read().strip()
mem_use = '当前物理内存已使用:' + mem
#交换内存
mem_swap = os.popen("free -mh | awk 'NR==3{print $3}'")
mem_swap = mem_swap.read().strip()
mem_swap_use = '当前交换内存已使用:' + mem_swap
#剩余内存
mem_surplus = os.popen("free -mh | awk 'NR==2{print $7}'")
mem_surplus = mem_surplus.read().strip()
mem_surplus_use = '当前物理内存剩余:' + mem_surplus
#内存剩余率
mem_surplus_int = mem_surplus[:2]
mem_int = mem[:2]
mem_mem = int(mem_surplus_int) / int(mem_int)
mem_mem_float = float(mem_mem) * 100
mem_mem_mem = '剩余百分率为:' + str(mem_mem_float)[:2] + '%'
#内存使用率
mem_all_int = mem_all[:2]
mem_int_int = mem[:2]
mem_all_use_mem = int(mem_int_int) / int(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] + '%'
if mem_mem_float < 10:
print(time.strftime('%Y-%m-%d %H:%M:%S', time.localtime()),'[WARING]:内存占用已超标,请检查!' + '\n' + ' [' + mem_all_use + ' ' + mem_surplus_use + ' ' + mem_mem_mem + ']')
else:
print(time.strftime('%Y-%m-%d %H:%M:%S', time.localtime()),'[INFO]:内存使用正常,请放心运行!' + '\n' + ' [' + mem_all_use_mem_mem + ' ' + mem_use + ' ' + mem_swap_use + ']')
if __name__ == '__main__':
get_usage(),\
get_uscpu(),\
get_usmem()
run.sh代码如下:
#!/bin/bash
#执行py脚本启动监控
while true
do
python3 MonitorOS.py >> ./monitoros.log
sleep 600
done
此脚本作者还在持续优化中......