python3實現(xiàn)磁盤空間監(jiān)控
本文實例為大家分享了python3磁盤空間監(jiān)控的具體代碼,供大家參考,具體內容如下
軟硬件環(huán)境
python3
apscheduler
前言
在做頻繁操作磁盤的python項目時,經(jīng)常會碰到磁盤空間不足的情況,這個時候,工程應該要有自己的處理模塊,當磁盤利用率到達某個點時,發(fā)出警告并停止程序的運行。本文就利用Python3中的apscheduler模塊來處理這樣的問題。
代碼實踐
import os
import sys
import signal
import logging
from apscheduler.schedulers.background import BackgroundScheduler
from apscheduler.triggers.interval import IntervalTrigger
# 開啟磁盤空間檢測
sched = BackgroundScheduler()
# 間隔5分鐘開啟一個檢查
intervalTrigger = IntervalTrigger(minutes=5)
# 給檢查任務設個id,方便任務的取消
sched.add_job(spaceMonitorJob, trigger=intervalTrigger, id='id_space_monitor')
sched.start()
# 禁止apscheduler相關信息屏幕輸出
logging.getLogger('apscheduler.executors.default').propagate = False
方法spaceMonitorJob代碼如下
def spaceMonitorJob():
'''
當磁盤(切片存儲的目錄)利用率超過90%,程序退出
:return:
'''
try:
st = os.statvfs('/')
total = st.f_blocks * st.f_frsize
used = (st.f_blocks - st.f_bfree) * st.f_frsize
except FileNotFoundError:
print('check webroot space error.')
logger.error('check webroot space error.')
# 移除任務,病關閉sched任務
sched.remove_job(job_id='id_space_monitor')
sched.shutdown(wait=False)
sys.exit(-3)
if used / total > 0.9:
print('No enough space.')
logger.debug('No enough space.')
sched.remove_job(job_id='id_space_monitor')
sched.shutdown(wait=False)
# 殺掉進程
os.killpg(os.getpgid(os.getpid()), signal.SIGKILL)
# 退出
exit(-3)
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
Seaborn數(shù)據(jù)分析NBA球員信息數(shù)據(jù)集
這篇文章主要為大家介紹了Seaborn數(shù)據(jù)分析處理NBA球員信息數(shù)據(jù)集案例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-09-09
探索Python fcntl模塊文件鎖和文件控制的強大工具使用實例
這篇文章主要介紹了Python fcntl模塊文件鎖和文件控制的強大工具使用實例探索,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2024-01-01
一小時學會TensorFlow2之Fashion Mnist
這篇文章主要介紹了TensorFlow2之Fashion Mnist,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-09-09
在python中利用最小二乘擬合二次拋物線函數(shù)的方法
今天小編就為大家分享一篇在python中利用最小二乘擬合二次拋物線函數(shù)的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-12-12

