前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >python2 监控服务器资源

python2 监控服务器资源

作者头像
用户5760343
发布2019-08-26 17:53:05
1K0
发布2019-08-26 17:53:05
举报
文章被收录于专栏:sktjsktj

******************************内存监控********************************* *******************时间: 2019-08-24 16:50:20 ****************** 总内存: 65958920 空闲内存: 37296548 给文件的缓冲大小: 310288 高速缓冲存储器使用的大小: 8991808 被高速缓冲存储用的交换空间大小: 7160 给文件的缓冲大小: 310288 交换内存利用率: 4.35586183351 内存利用率: 29.35 % ****************************内核线程、虚拟内存、磁盘、陷阱和 CPU 活动的统计信息监控**************************** *******************时间: 2019-08-24 16:50:22 ****************** 等待运行进程的数量: 1 处于不间断状态的进程: 0 使用虚拟内存(swap)的总量: 365396 空闲的内存总量: 37298132 用作缓冲的内存总量: 310288 用作缓存的内存总量: 8991816 交换出内存总量 : 0 交换入内存总量 : 0 从一个块设备接收: 0 发送到块设备: 0 每秒的中断数: 9751 每秒的上下文切换数: 7514 用户空间上进程运行的时间百分比: 3 内核空间上进程运行的时间百分比: 2 闲置时间百分比: 95 等待IO的时间百分比: 0 从虚拟机偷取的时间百分比: 0 ***************************************cpu监控*************************************** *******************时间: 2019-08-24 16:50:23 ****************** CPU数目: 24 ************************负载均衡监控**************************** *******************时间: 2019-08-24 16:50:24 ****************** 系统5分钟前的平均负载: 0.16 系统10分钟前的平均负载: 0.08 系统15分钟前的平均负载: 0.06 分子是正在运行的进程数,分母为总进程数: 3/968 最近运行的进程id: 25222 ************************磁盘空间监控**************************** *******************时间: 2019-08-24 16:50:24 ****************** 文件系统: /dev/sda3 容量: 1.8T 已用: 133G 可用: 1.7T 已用%挂载点: 8% 文件系统: tmpfs 容量: 32G 已用: 12K 可用: 32G 已用%挂载点: 1% 文件系统: /dev/sda1 容量: 477M 已用: 57M 可用: 395M 已用%挂载点: 13% 文件系统: /dev/sdb1 容量: 440G 已用: 71M 可用: 436G 已用%挂载点: 1% ******************************端口监控********************************* *******************时间: 2019-08-24 16:50:25 ****************** 1 1 #################################################

!/usr/bin/env python

-- coding: utf-8 --

@Time : 2017/11/27 15:59

@Desc : 服务器监控代码

@File : monitorserver.py

@Software: PyCharm

import commands import re

import time import threading

"""

内存监控

""" def mem_info(ip): cm="ssh -q root@{0} 'cat /proc/meminfo'".format(ip) status,output = commands.getstatusoutput(cm) mem = output mem_values = re.findall("(\d+)\ kB", mem) MemTotal = mem_values[0] MemFree = mem_values[1] Buffers = mem_values[2] Cached = mem_values[3] SwapCached=mem_values[4] SwapTotal = mem_values[13] SwapFree = mem_values[14] print '******************************内存监控*********************************' print "*******************时间:", time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()), "******************" print "总内存:",MemTotal print "空闲内存:", MemFree print "给文件的缓冲大小:",Buffers print "高速缓冲存储器使用的大小:", Cached print "被高速缓冲存储用的交换空间大小:", SwapCached print "给文件的缓冲大小:", Buffers if int(SwapTotal) == 0: print "交换内存总共为:0" else: Rate_Swap = 100 - 100int(SwapFree)/float(SwapTotal) print "交换内存利用率:", Rate_Swap Free_Mem = int(MemFree) + int(Buffers) + int(Cached) Used_Mem = int(MemTotal) - Free_Mem Rate_Mem = 100Used_Mem/float(MemTotal) print "内存利用率:", str("%.2f" % Rate_Mem), "%"

""" 内核线程、虚拟内存、磁盘、陷阱和 CPU 活动的统计信息 """ def vm_stat_info(ip): cm="ssh -q root@{0} 'vmstat 1 2 | tail -n 1'".format(ip) status,output = commands.getstatusoutput(cm) vmstat_info = output.strip().split() processes_waiting = vmstat_info[7] processes_sleep = vmstat_info[8] swpd = vmstat_info[9] free = vmstat_info[10] buff = vmstat_info[11] cache = vmstat_info[12] si = vmstat_info[13] so = vmstat_info[14] io_bi = vmstat_info[15] io_bo = vmstat_info[16] system_interrupt = vmstat_info[17] system_context_switch = vmstat_info[18] cpu_user = vmstat_info[19] cpu_sys = vmstat_info[20] cpu_idle = vmstat_info[21] cpu_wait = vmstat_info[22] st=vmstat_info[23] print '****************************内核线程、虚拟内存、磁盘、陷阱和 CPU 活动的统计信息监控****************************' print "*******************时间:", time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()), "******************" print "等待运行进程的数量:", processes_waiting print "处于不间断状态的进程:", processes_sleep print "使用虚拟内存(swap)的总量:", swpd print "空闲的内存总量:", free print "用作缓冲的内存总量:", buff print "用作缓存的内存总量:", cache print "交换出内存总量 :", si print "交换入内存总量 :", so print "从一个块设备接收:", io_bi print "发送到块设备:", io_bo print "每秒的中断数:", system_interrupt print "每秒的上下文切换数:", system_context_switch print "用户空间上进程运行的时间百分比:", cpu_user print "内核空间上进程运行的时间百分比:", cpu_sys print "闲置时间百分比:", cpu_idle print "等待IO的时间百分比:", cpu_wait print "从虚拟机偷取的时间百分比:", st

''' cpu监控 ''' def cpu_info(ip): cm="ssh -q root@{0} 'cat /proc/cpuinfo'".format(ip) status,output = commands.getstatusoutput(cm) cpuinfo = output cpu_num = re.findall('processor.*?(\d+)', cpuinfo)[-1] cpu_num = str(int(cpu_num) + 1) print '***************************************cpu监控***************************************' print "*******************时间:", time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()), "******************" print u"CPU数目:", cpu_num

""" 负载均衡 """ def load_stat(ip): cm="ssh -q root@{0} 'cat /proc/loadavg'".format(ip) status,output = commands.getstatusoutput(cm) loadavgs = output.strip().split() print '************************负载均衡监控****************************' print "*******************时间:",time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()),"******************" print "系统5分钟前的平均负载:", loadavgs[7] print "系统10分钟前的平均负载:", loadavgs[8] print "系统15分钟前的平均负载:", loadavgs[9] print "分子是正在运行的进程数,分母为总进程数:",loadavgs[10] print "最近运行的进程id:", loadavgs[11]

""" 磁盘空间监控 """ def disk_stat(ip): cm="ssh -q root@{0} 'df -h'".format(ip) status,output = commands.getstatusoutput(cm) disk = output disklist = disk.strip().split('\n') disklists=[] for disk in disklist[3:]: disklists.append(disk.strip().split()) print '************************磁盘空间监控****************************' print "*******************时间:", time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()), "******************" for i in disklists: print "\t文件系统:", i[0], print "\t容量:", i[1], print "\t已用:", i[2], print "\t可用:", i[3], print "\t已用%挂载点:", i[4]

""" 端口监控 一般是远程服务器用户名用户 """ def getComStr(ip,port,pro): cm="ssh -q root@{0} 'netstat -tpln|grep 60001|wc -l;ps aux|grep -v grep|grep java|wc -l'".format(ip) status,output = commands.getstatusoutput(cm) com = output.split("\n") print '******************************端口监控*********************************' print "*******************时间:", time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()), "******************" print com[2],com[3]

""" 第一种执行 """ def alltask(): try: threads = [] t1 = threading.Thread(target=mem_info) threads.append(t1) t2 = threading.Thread(target=vm_stat_info) threads.append(t2) t3 = threading.Thread(target=cpu_info) threads.append(t3) t4 = threading.Thread(target=load_stat) threads.append(t4) t5 = threading.Thread(target=ionetwork) threads.append(t5) t6 = threading.Thread(target=disk_stat) threads.append(t6) t7 = threading.Thread(target=getComStr) threads.append(t7) for n in range(len(threads)): threads[n].start() except Exception, e: print str(e)

""" 第二种执行 """ if name == 'main': try: mem_info('113.219.129.32') vm_stat_info('113.219.129.32') cpu_info('113.219.129.32') load_stat('113.219.129.32') disk_stat('113.219.129.32') getComStr('113.219.129.32',"","") except Exception, e: print str(e)

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2019.08.25 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • !/usr/bin/env python
  • -- coding: utf-8 --
  • @Time : 2017/11/27 15:59
  • @Desc : 服务器监控代码
  • @File : monitorserver.py
  • @Software: PyCharm
  • 内存监控
相关产品与服务
负载均衡
负载均衡(Cloud Load Balancer,CLB)提供安全快捷的流量分发服务,访问流量经由 CLB 可以自动分配到云中的多台后端服务器上,扩展系统的服务能力并消除单点故障。负载均衡支持亿级连接和千万级并发,可轻松应对大流量访问,满足业务需求。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档