#!/usr/bin/python3
# file: log.py
# Created by Guang at 19-9-19
# description: 按照 (2019-09-01) 每日生成一个 类似2019-09-01.log这样命名的文件, 并把磁盘使用情况写入;
"""
python 代码监控 磁盘; 监控 cup; 监控内存; 监控网卡 的使用情况, 并定时发送结果到 email;
延伸:  如何监控远程主机的 上述项目呢?
"""

# *-* coding:utf8 *-*

import os
from collections import namedtuple

disk_ntuple = namedtuple('partition', 'device mountpoint fstype')
usage_ntuple = namedtuple('usage', 'total used free percent')

# 获取当前操作系统下所有磁盘


def disk_partitions(all=False):
    """Return all mountd partitions as a nameduple.
    If all == False return phyisical partitions only.
    """
    phydevs = []
    f = open("/proc/filesystems", "r")
    for line in f:
        if not line.startswith("nodev"):
            phydevs.append(line.strip())

    retlist = []
    f = open('/etc/mtab', "r")
    for line in f:
        if not all and line.startswith('none'):
            continue
        fields = line.split()
        device = fields[0]
        mountpoint = fields[1]
        fstype = fields[2]
        if not all and fstype not in phydevs:
            continue
        if device == 'none':
            device = ''
        ntuple = disk_ntuple(device, mountpoint, fstype)
        retlist.append(ntuple)
    return retlist

# 统计某磁盘使用情况，返回对象


def disk_usage(path):
    """Return disk usage associated with path."""
    st = os.statvfs(path)
    free = (st.f_bavail * st.f_frsize)
    total = (st.f_blocks * st.f_frsize)
    used = (st.f_blocks - st.f_bfree) * st.f_frsize
    try:
        percent = ret = (float(used) / total) * 100
    except ZeroDivisionError:
        percent = 0
    # NB: the percentage is -5% than what shown by df due to
    # reserved blocks that we are currently not considering:
    # http://goo.gl/sWGbH
    return usage_ntuple(total, used, free, round(percent, 1))


#!/usr/bin/python3
# file: log.py
# Created by Guang at 19-9-19
# description: 按照 (2019-09-01) 每日生成一个 类似2019-09-01.log这样命名的文件, 并把磁盘使用情况写入;
"""
python 代码监控 磁盘; 监控 cup; 监控内存; 监控网卡 的使用情况, 并定时发送结果到 email;
延伸:  如何监控远程主机的 上述项目呢?
"""

# *-* coding:utf8 *-*

import os
from collections import namedtuple

disk_ntuple = namedtuple('partition', 'device mountpoint fstype')
usage_ntuple = namedtuple('usage', 'total used free percent')

# 获取当前操作系统下所有磁盘


def disk_partitions(all=False):
    """Return all mountd partitions as a nameduple.
    If all == False return phyisical partitions only.
    """
    phydevs = []
    f = open("/proc/filesystems", "r")
    for line in f:
        if not line.startswith("nodev"):
            phydevs.append(line.strip())

    retlist = []
    f = open('/etc/mtab', "r")
    for line in f:
        if not all and line.startswith('none'):
            continue
        fields = line.split()
        device = fields[0]
        mountpoint = fields[1]
        fstype = fields[2]
        if not all and fstype not in phydevs:
            continue
        if device == 'none':
            device = ''
        ntuple = disk_ntuple(device, mountpoint, fstype)
        retlist.append(ntuple)
    return retlist

# 统计某磁盘使用情况，返回对象


def disk_usage(path):
    """Return disk usage associated with path."""
    st = os.statvfs(path)
    free = (st.f_bavail * st.f_frsize)
    total = (st.f_blocks * st.f_frsize)
    used = (st.f_blocks - st.f_bfree) * st.f_frsize
    try:
        percent = ret = (float(used) / total) * 100
    except ZeroDivisionError:
        percent = 0
    # NB: the percentage is -5% than what shown by df due to
    # reserved blocks that we are currently not considering:
    # http://goo.gl/sWGbH
    return usage_ntuple(total, used, free, round(percent, 1))


#!/usr/bin/python3 # file: log.py # Created by Guang at 19-9-19 # description: 按照 (2019-09-01) 每日生成一个 类似2019-09-01.log这样命名的文件, 并把磁盘使用情况写入; python 代码监控 磁盘; 监控 cup; 监控内存; 监控网卡 的使用情况, 并定时发送结果到 email; 延伸:  如何监控远程主机的 上述项目呢? # *-* coding:utf8 *-* # 获取当前操作系统下所有磁盘 Return all mountd partitions as a nameduple.     If all == False return phyisical partitions only. # 统计某磁盘使用情况，返回对象 Return disk usage associated with path. # NB: the percentage is -5% than what shown by df due to # reserved blocks that we are currently not considering: # http://goo.gl/sWGbH