在python中使用linux命令写一个监控脚本

编写python监控脚本,监控/和/boot分区的使用率,/ 大于60%就告警,在屏幕上输出内容,具体自己定义

/boot分区大于50%就告警
脚本名monitor_partition.py
将磁盘的使用率写到日志文件里到/var/log/root_boot_partition.txt,具体内容自己定义
2022-4-26 22:01:01 /boot used 45%
2022-4-26 22:01:01 / used 70%
软件工程:
1.需求分析
monitor_partition.py
1.1.如何在python里执行linux命令?
模块: os subprocess
标准库: 自带的不需要安装
pip install **
1.2 如果将内容保存到文件 open()
1.3 如何在python里获得当前的时间?
datetime
时间的格式化输出: 按照我需要的格式输出,或者指定格式输出
date +%Y%m%d

2.写文档
3.设计
python
1. 执行linux命令df
2. 判断
3.写日志

4.开发

datetime模块

>>> import datetime
>>> datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
'2022-05-07 20:46:00'

subprocess

subprocess 执行系统(linux,windows、mac os)的命令

>>> import subprocess  导入库(模块)
>>> subprocess.run('ls',shell=True)  --》run方法去执行ls命令
monitor_partition.sh
>>> num=subprocess.run('ls',shell=True).returncode  将命令的返回值赋值给num
monitor_partition.sh
>>> root_part = subprocess.run("df |grep /$|awk '{print $5}'|tr -d % ",shell=True)
83
>>> root_part = subprocess.run("df |grep /$|awk '{print $5}'|tr -d % ",shell=True,stdout=subprocess.PIPE,stderr=subprocess.PIPE)
>>> root_part.
root_part.args               root_part.stderr
root_part.check_returncode(  root_part.stdout
root_part.returncode         
>>> root_part.stdout
b'83\n'

stdout:标准输出

>>> lei = subprocess.run('ls /lianxi',shell=True,stdout=subprocess.PIPE,stderr=subprocess.PIPE)
>>> lei.stdout
b'410\n412\n417\n419\n426\n57\nchen.yaml\ndy.yaml\nfeng\nfeng2.yaml\nfeng.yaml.xz\nhejin.yaml.gz\niptables.rules\nluoyawei\npod+service\nsanchuang\nscfeng.py\nservice.yaml\ntang.yaml\ntangyuhao.yaml\nwang.yaml\nxiong.yaml\n'
>>> print(lei.stdout)
b'410\n412\n417\n419\n426\n57\nchen.yaml\ndy.yaml\nfeng\nfeng2.yaml\nfeng.yaml.xz\nhejin.yaml.gz\niptables.rules\nluoyawei\npod+service\nsanchuang\nscfeng.py\nservice.yaml\ntang.yaml\ntangyuhao.yaml\nwang.yaml\nxiong.yaml\n'
>>> 

stdout=subprocess.PIPE 定义subprocess执行命令的标准输出到subprocess.PIPE里

标准输入 stdinput standard input -->从键盘
标准输出 stdoutput standard output --》屏幕
标准错误输出 stderr standard error output --》屏幕

在这里插入图片描述

脚本:

import datetime

num_root = subprocess.run("df -h|grep '/$'|awk '{print $5}'|tr -d % ", shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)

num_boot = subprocess.run("df -h|grep 'boot$'|awk '{print $5}'|tr -d % ", shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)

ctime = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")

num_root.stdout = int(num_root.stdout)
num_boot.stdout = int(num_boot.stdout)

if (num_root.stdout > 60):
	print("root usage is not enough!")
	print("root_usage:", num_root.stdout)
if (num_boot.stdout > 50):
	print("boot usage is not enough!")
	print("root_usage:", num_boot.stdout)

with open("/var/log/root_boot_partition.txt", "a+") as fp:
	fp.write(f"{ctime} /boot used {num_boot.stdout}%\n")
	fp.write(f"{ctime} /root used {num_root.stdout}%\n")
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值