python 系统监控

#!/usr/bin/python2.7
# -*- coding:utf-8 -*-
#检查网卡流量
from __future__ import division
import commands
from datetime import datetime

"""
初始化文件
"""

try:
    f=open('/tmp/rx.txt')
    f1=open('/tmp/tx.txt')
    f2=open('/tmp/rxt.txt')
    f3=open('/tmp/txt.txt')
except IOError:
    f=open('/tmp/rx.txt','w')
    f1=open('/tmp/tx.txt','w')
    f2=open('/tmp/rxt.txt','w')
    f3=open('/tmp/txt.txt','w')
finally:	
    f.close()
    f1.close()
    f2.close()
    f3.close()

def tt():
    """
计算时间节点
    """
    time_now=datetime.now()
    ff=open('/tmp/time.txt','r')
    time_before_str=ff.read()
    ff.close()
    if time_before_str=='':
        ffw=open('/tmp/time.txt','w')
        ffw.write(str(time_now))
        ffw.close()
    else:
        time_before=datetime.strptime(time_before_str,"%Y-%m-%d %H:%M:%S.%f")
        delay=(time_now-time_before).seconds
        ffw=open('/tmp/time.txt','w')
        ffw.write(str(time_now))
        ffw.close()
        return delay

t=tt()

def RX():
    """
计算网卡每秒接收数据包的个数
    """
    data1=commands.getoutput(''' ifconfig eth1| grep packets | grep RX | grep -Po "\d+" | awk 'NR==1' ''')
    data=open('/tmp/rx.txt').readlines()
    if len(data)==0:
        f=open('/tmp/rx.txt','w')
        f.write(data1)
        f.close()
        print "rx need next check"
    else:
        data=data[0]
        rx=(int(data1)-int(data))/int(t)
        f=open('/tmp/rx.txt','w')
        f.write(data1)
        f.close()
        return rx
    

def TX():
    """
计算网卡每秒转发数据包的个数
    """
    data1=commands.getoutput(''' ifconfig eth1| grep packets | grep TX | grep -Po "\d+" | awk 'NR==1' ''')
    data=open('/tmp/tx.txt').readlines()
    if len(data)==0:
        f=open('/tmp/tx.txt','w')
        f.write(data1)
        f.close()
        print "tx need next check"
    else:
        data=data[0]
        rx=(int(data1)-int(data))/int(t)
        f=open('/tmp/tx.txt','w')
        f.write(data1)
        f.close()
        return rx

def RXT():
    """
计算网卡接收的流量
    """
    data1=commands.getoutput(''' ifconfig eth1| grep 'RX bytes' | grep -Po '\d+' | awk 'NR==1' ''')
    data=open('/tmp/rxt.txt').readlines()
    if len(data)==0:
        f=open('/tmp/rxt.txt','w')
        f.write(data1)
        f.close()
        print "tx need next check"
    else:
        data=data[0]
        rxt=(int(data1)-int(data))*8/1024/1024/int(t)
        f=open('/tmp/rxt.txt','w')
        f.write(data1)
        f.close()
        return rxt

def TXT():
    """ 
计算网卡接收的流量
    """
    data1=commands.getoutput(''' ifconfig eth1| grep 'RX bytes' | grep -Po '\d+' | awk 'NR==4' ''')
    data=open('/tmp/txt.txt').readlines()
    if len(data)==0:
        f=open('/tmp/txt.txt','w')
        f.write(data1)
        f.close()
        print "tx need next check"
    else:
        data=data[0]
        txt=(int(data1)-int(data))*8/1024/1024/int(t)
        f=open('/tmp/txt.txt','w')
        f.write(data1)
        f.close()
        return txt

def END():
    rx=RX()
    tx=TX()
    rxt=RXT()
    txt=TXT()
    print "bond0 rx=%d packets %.2fMb,tx=%d packets %.2fMb,| RX=%d;RXT=%.2fMb;TX=%d;TXT=%.2fMb;" %(int(rx),rxt,int(tx),txt,int(rx),rxt,int(tx),txt)

if __name__ == '__main__':
    END()
       
[root@eddy ~]# python check_traffic.py 
eth1 rx=633 packets 0.22Mb,tx=6 packets 0.01Mb,| RX=633;RXT=0.22Mb;TX=6;TXT=0.01Mb;    


#!/usr/bin/env python
# encoding: utf-8
#检查网卡流量
"""
-----------------------------------------------------------
total bytes:           sent: 1.49 G       received: 4.82 G
total packets:         sent: 7338724      received: 8082712
wlan0                     TOTAL         PER-SEC
-----------------------------------------------------------
bytes-sent               1.29 G        0.00 B/s
bytes-recv               3.48 G        0.00 B/s
pkts-sent               7221782               0
pkts-recv               6753724               0
eth1                      TOTAL         PER-SEC
-----------------------------------------------------------
bytes-sent             131.77 M        0.00 B/s
bytes-recv               1.28 G        0.00 B/s
pkts-sent                     0               0
pkts-recv               1214470               0
"""

import atexit
import time
import sys
try:
    import curses
except ImportError:
    sys.exit('platform not supported')

import psutil


# --- curses stuff
def tear_down():
    win.keypad(0)
    curses.nocbreak()
    curses.echo()
    curses.endwin()

win = curses.initscr()
atexit.register(tear_down)
curses.endwin()
lineno = 0


def print_line(line, highlight=False):
    """A thin wrapper around curses's addstr()."""
    global lineno
    try:
        if highlight:
            line += " " * (win.getmaxyx()[1] - len(line))
            win.addstr(lineno, 0, line, curses.A_REVERSE)
        else:
            win.addstr(lineno, 0, line, 0)
    except curses.error:
        lineno = 0
        win.refresh()
        raise
    else:
        lineno += 1
# --- curses stuff


def bytes2human(n):
    """
    >>> bytes2human(10000)
    '9.8 K'
    >>> bytes2human(100001221)
    '95.4 M'
    """
    symbols = ('K', 'M', 'G', 'T', 'P', 'E', 'Z', 'Y')
    prefix = {}
    for i, s in enumerate(symbols):
        prefix[s] = 1 << (i + 1) * 10
    for s in reversed(symbols):
        if n >= prefix[s]:
            value = float(n) / prefix[s]
            return '%.2f %s' % (value, s)
    return '%.2f B' % (n)


def poll(interval):
    """Retrieve raw stats within an interval window."""
    tot_before = psutil.net_io_counters()
    pnic_before = psutil.net_io_counters(pernic=True)
    # sleep some time
    time.sleep(interval)
    tot_after = psutil.net_io_counters()
    pnic_after = psutil.net_io_counters(pernic=True)
    return (tot_before, tot_after, pnic_before, pnic_after)


def refresh_window(tot_before, tot_after, pnic_before, pnic_after):
    """Print stats on screen."""
    global lineno

    # totals
    print_line("total bytes:           sent: %-10s   received: %s" % (
        bytes2human(tot_after.bytes_sent),
        bytes2human(tot_after.bytes_recv))
    )
    print_line("total packets:         sent: %-10s   received: %s" % (
        tot_after.packets_sent, tot_after.packets_recv))

    # per-network interface details: let's sort network interfaces so
    # that the ones which generated more traffic are shown first
    print_line("")
    nic_names = list(pnic_after.keys())
    nic_names.sort(key=lambda x: sum(pnic_after[x]), reverse=True)
    for name in nic_names:
        stats_before = pnic_before[name]
        stats_after = pnic_after[name]
        templ = "%-15s %15s %15s"
        print_line(templ % (name, "TOTAL", "PER-SEC"), highlight=True)
        print_line(templ % (
            "bytes-sent",
            bytes2human(stats_after.bytes_sent),
            bytes2human(
                stats_after.bytes_sent - stats_before.bytes_sent) + '/s',
        ))
        print_line(templ % (
            "bytes-recv",
            bytes2human(stats_after.bytes_recv),
            bytes2human(
                stats_after.bytes_recv - stats_before.bytes_recv) + '/s',
        ))
        print_line(templ % (
            "pkts-sent",
            stats_after.packets_sent,
            stats_after.packets_sent - stats_before.packets_sent,
        ))
        print_line(templ % (
            "pkts-recv",
            stats_after.packets_recv,
            stats_after.packets_recv - stats_before.packets_recv,
        ))
        print_line("")
    win.refresh()
    lineno = 0


def main():
    try:
        interval = 0
        while True:
            args = poll(interval)
            refresh_window(*args)
            interval = 1
    except (KeyboardInterrupt, SystemExit):
        pass

if __name__ == '__main__':
    main()

total bytes:           sent: 90.69 M	  received: 11.28 G
total packets:         sent: 529772	  received: 256538131

eth1                      TOTAL         PER-SEC                                                                                                                                                           
bytes-sent              74.59 M      202.00 B/s
bytes-recv              10.68 G        3.31 K/s
pkts-sent                432165               1
pkts-recv             242330523              74

eth0                      TOTAL         PER-SEC                                                                                                                                                           
bytes-sent               6.23 M        0.00 B/s
bytes-recv             605.69 M      268.00 B/s
pkts-sent                 88934               0
pkts-recv              14198935               6

lo                        TOTAL         PER-SEC                                                                                                                                                           
bytes-sent               9.87 M        0.00 B/s
bytes-recv               9.87 M        0.00 B/s
pkts-sent                  8673               0
pkts-recv                  8673               0

检查swap
#!/usr/bin/env python
import re,os
#Swap: 0 kB
def check_swap(pid):
        try:
                f_name='/proc/'+pid+'/smaps'
                f = open(f_name)
                lines=f.readlines()
                f.close()
                size = 0
                pattern=r'Swap:\s+(\d+)\s+KB'
                for line in lines:
                        if re.compile(pattern).findall(line):
                                size += int(re.compile(pattern).findall(line)[0])
                return pid,size
        except:
                #print "Erro"
                pass
def get_name(pid):
        try:
                f_name='/proc/'+pid+'/status'
                f = open(f_name)
                line=f.readline()
                f.close()
                name = line.split(':')[-1].split()[0] 
                return name
        except:
                pass
if __name__=='__main__':
        print 'PID\tSWAP-SIZE\tNAME'
        print os.getpid()
        print '----------------'
        for pid in os.listdir('/proc'):
          if pid.isdigit(): 
                info = check_swap(pid)
                if info :#and info[-1]:
                        print "%s\t%s\t%s" % (info[0],info[1],get_name(pid))

root@eddy ~]# python check_swap.py 
PID	SWAP-SIZE	NAME
8907
----------------
1	0	init
2	0	kthreadd
3	0	migration/0
4	0	ksoftirqd/0
5	0	migration/0
6	0	watchdog/0
7	0	events/0
8	0	cgroup
9	0	khelper
10	0	netns
11	0	async/mgr
12	0	pm
13	0	xenwatch
14	0	xenbus                        
                        
检查cpu                        
#!/usr/bin/env python
# encoding: utf-8
# cpu
import psutil
import  datetime
import  time
#cpu逻辑数目
print "CPU逻辑数目%d个" %psutil.cpu_count()
#cpu物理数目
print "CPU物理数目%d个" %psutil.cpu_count(logical=False)
#用户,系统,空闲百分比
cpu = psutil.cpu_times_percent(interval=1,percpu=False)
print cpu[0]
print  "user=%d%%," %cpu[0],
print  "system=%d%%," %cpu[1],
print  "idle=%d%%" %cpu[2] 
[root@eddy ~]# python check_cpu.py 
CPU逻辑数目1个
CPU物理数目1个
0.0
user=0%, system=0%, idle=0% 

检查磁盘
#!/usr/bin/env python
# encoding: utf-8
import psutil
#disk
#查看分区
partitions = psutil.disk_partitions()
for i in partitions:
    print "partitions:%s" %i[0]
    #使用情况
    partitions_used =  psutil.disk_usage(i[0])
    print "使用率%s%%" %(partitions_used[3])
    print "空闲%sG" %(partitions_used[2]/1024/1024/1024)
    print "使用%sG" %(partitions_used[1]/1024/1024/1024)
    print "总共%sG" %(partitions_used[0]/1024/1024/1024)
    print ''
    ##io情况
    print psutil.disk_io_counters()
    print "读IO数:%s" %psutil.disk_io_counters()[0]
    print "写IO数:%s" %psutil.disk_io_counters()[1]
    print "读IO(MB):%sMB" %(psutil.disk_io_counters()[2]/1024/1024)
    print "写IO(MB):%sMB" %(psutil.disk_io_counters()[3]/1024/1024)
    print "读时间:%ss" %(psutil.disk_io_counters()[4]/1000)
    print "写时间:%ss" %(psutil.disk_io_counters()[5]/1000)
    print ''
    
[root@eddy ~]# python check_disk.py 
partitions:/dev/xvda1
使用率12.2%
空闲16G
使用2G
总共19G

sdiskio(read_count=56840, write_count=614877, read_bytes=762566656, write_bytes=4527808512, read_time=234642, write_time=7270899)
读IO数:56840
写IO数:614877
读IO(MB):727MB
写IO(MB):4318MB
读时间:234s
写时间:7270s

检查运行时间 
#!/usr/bin/env python
# encoding: utf-8
import psutil
import datetime
import time
##开机时间、用户登录
users_count =  len(psutil.users())
users_list = ",".join([u.name for u in psutil.users()])
print u"当前有%s个用户,分别是%s" %(users_count, users_list)
print u"开机时间:%s" %datetime.datetime.fromtimestamp(psutil.boot_time()).strftime("%Y-%m-%d %H:%M:%S")

today = datetime.datetime.strftime(datetime.datetime.now(), '%Y-%m-%d %H:%M:%S')
start_time = datetime.datetime.fromtimestamp(psutil.boot_time()).strftime("%Y-%m-%d %H:%M:%S")
run_time = (time.time() - psutil.boot_time())/3600
print "当前时间%s" %today
print "系统已运行%0.2f小时" % run_time

[root@eddy ~]# python runtime.py 
当前有3个用户,分别是eddy,eddy,eddy
开机时间:2015-10-31 12:50:52
当前时间2015-11-27 13:54:16
系统已运行649.06小时

检查内存
#!/usr/bin/env python
# encoding: utf-8
#mem
import psutil
SWAP = psutil.swap_memory()
print "total_swap=%0.2fM," %float(SWAP[0]/1024/1024),
print "used_swap=%0.2fM," %float(SWAP[1]/1024/1024),
print "free_swap=%0.2fM," %float(SWAP[2]/1024/1024),
print "used_precent=%0.2f%%" %float(SWAP[3])
MEM = psutil.virtual_memory()
print MEM
print "total_mem=%0.2fM," %float(MEM[0]/1024/1024),
print "used_mem=%0.2fM," %float(MEM[3]/1024/1024),
print "free_mem=%0.2fM," %float(MEM[1]/1024/1024),
print "used_precent=%0.2f%%" %float(MEM[2])

[root@eddy ~]# free -m
             total       used       free     shared    buffers     cached
Mem:           994        898         95          0        196        479
-/+ buffers/cache:        223        770
Swap:            0          0          0
[root@eddy ~]# python check_mem.py 
total_swap=0.00M, used_swap=0.00M, free_swap=0.00M, used_precent=0.00%
total_mem=994.00M, used_mem=902.00M, free_mem=767.00M, used_precent=22.00%

#查看某个进程占用内存
#!/usr/bin/env python
# encoding: utf-8
import sys
import psutil


def main():
    if len(sys.argv) != 2:
        sys.exit('usage: pmap <pid>')
    p = psutil.Process(int(sys.argv[1]))
    print("pid=%s, name=%s" % (p.pid, p.name()))
    templ = "%-16s %10s  %-7s %s"
    print(templ % ("Address", "RSS", "Mode", "Mapping"))
    total_rss = 0
    for m in p.memory_maps(grouped=False):
        total_rss += m.rss
        print(templ % (
            m.addr.split('-')[0].zfill(16),
            str(m.rss / 1024) + 'K',
            m.perms,
            m.path))
    print("-" * 33)
    print(templ % ("Total", str(total_rss / 1024 /1024) + 'M', '', ''))

if __name__ == '__main__':
    main()

[root@eddy ~]# python check_mem.py 11671    
pid=11671, name=epmd
Address                 RSS  Mode    Mapping
0000000000400000        32K  r-xp    /usr/lib64/erlang/erts-5.8.5/bin/epmd
000000000060a000         8K  rw-p    /usr/lib64/erlang/erts-5.8.5/bin/epmd
00000000014bf000        36K  rw-p    [heap]
0000003b7e000000         4K  r-xp    /lib64/ld-2.12.so
0000003b7e21f000         4K  r--p    /lib64/ld-2.12.so
0000003b7e220000         4K  rw-p    /lib64/ld-2.12.so

#邮件告警
#/usr/bin/python
#
#
def alarm():
    msg = MIMEText('<html><h1>alarm</h1></html>','html','utf-8')
    msg_to = ['[email protected]','[email protected]']
    msg['from'] = '[email protected]'
    msg['subject'] = 'XX内存告警'
    try:
        server = smtplib.SMTP()
        server.connect('mail.eddy.com')
        server.login('[email protected]','123456')
        server.sendmail(msg['from'], msg_to,msg.as_string())
        server.quit()
        print '发送成功'
    except Exception, e:
        print str(e)
if __name__ == '__main__':
    if int(MEM[2]) > 80:
        alarm()


你可能感兴趣的:(python 系统监控)