python 写一个类似于top的监控脚本
最近老板给提出一个需要,项目需求大致如下:
1、用树莓派作为网关,底层接多个ZigBee传感节点,网关把ZigBee传感节点采集到的信息通过串口接收汇总,并且发送给上层的HTTP Server;2、要有数据的反向控制通道,即网关与Server间要保持长连接,采用websocket实现,以此实现给ZigBee传感节点发送控制命令,来实现对ZigBee节点的远程配置操作;3、树莓派网关本身要与上层Server实现交互,上层Server能够看到网关实时的cpu、内存以及网络上行与下行的带宽等等;
前两条需求在前一段时间已经基本实现,等后续有时间完善之后在整理,今天记录一下第三条的实现过程。
感觉第三条需求很像目前公司用到的监控系统的一个小的底层实现,因为前几天无聊刚好搭了个zabbix的环境玩了玩,感觉老板的需求在前端上好像就是类似于zabbix Server上的那种展现形式,但是用zabbix实在感觉不够灵活,其实我也用不明白,只能实现一个类似于top工具的监控脚本吧,先把实时的cpu、内存、网络流量等信息在本地表现出来,等待后续和Server端的朋友联调再说,代码如下:
#!/usr/bin/env python
# -*- coding:utf-8 -*-
#############################
#__author__ = 'webber' #
#create at 2016/12/12 #
#############################
import os
import sys
import time def cpuinfo():
"""
get cpuinfo from '/proc/stat' and
calculate the percentage of the cpu occupy.
"""
f = open('/proc/stat','r')
cpu = f.readline()
f.close()
#print "cpuinfo: ", cpu
cpu = cpu.split(" ")
total = 0
usr = float(cpu[2]) #用户态cpu占用率
_sys = float(cpu[4]) #内核态cpu占用率
for info in cpu:
if info.isdigit():
total += float(info)
print '\033[31mcpu info: \033[0m',
print 'usr: %.5f%%' % ((usr/total)*100),
print ' sys: %.5f%%' % ((_sys/total)*100) def meminfo():
"""
get meminfo from '/proc/meminfo' and
calculate the percentage of the mem occupy
used = total - free - buffers - cached
"""
f = open('/proc/meminfo','r')
mem = f.readlines()
f.close()
#print "meminfo", mem
total, free, buffers, cached = 0, 0, 0, 0
for info in mem:
mem_item = info.lower().split()
#print mem_item
if mem_item[0] == 'memtotal:':
total = float(mem_item[1])
if mem_item[0] == 'memfree:':
free = float(mem_item[1])
if mem_item[0] == 'buffers:':
buffers = float(mem_item[1])
if mem_item[0] == 'cached:':
cached = float(mem_item[1])
used = total - free - buffers - cached
print "\033[31mmeminfo: \033[0m",
print "total: %.2f GB" % (total/1024/1024),
print " used: %.5f%%" % (used/total) def netinfo():
"""
get real-time bandwidth
"""
f = open('/proc/net/dev','r')
net = f.readlines()
f.close() net_item = []
for info in net:
if info.strip().startswith("eth0"):
net_item = info.strip().split()
break
# print net_item
recv = float(net_item[1])
send = float(net_item[9])
#print "recv:%s " % recv
#print "send:%s " % send
time.sleep(1) f2 = open('/proc/net/dev','r')
_net = f2.readlines()
f2.close()
_net_item = []
for info in _net:
if info.strip().startswith("eth0"):
_net_item = info.strip().split()
break
recv_2 = float(_net_item[1])
send_2 = float(_net_item[9]) #print "recv_2:%s " % recv_2
#print "send_2:%s " % send_2
print "\033[31m network info: \033[0m"
print "received: %.3f Kb/s" % (recv_2/1024 - recv/1024)
print "transmit: %.3f kb/s" % (send_2/1024 - send/1024) def loadavg():
pass def disk():
pass if __name__ == '__main__':
while True:
try:
os.system('clear')
cpuinfo()
print "=============================================="
meminfo()
print "##############################################"
netinfo()
time.sleep(5)
except KeyboardInterrupt, e: # 这里也可以用信号函数来处理
print ''
print "BYE-BYE"
sys.exit(0)
python 写一个类似于top的监控脚本的更多相关文章
- 用python写一个类似于linux中的tree
import os filePath = 'g:/File' j = 0 # 查找的深度计数 def tree(filePath,j): dir_now = os.listdir(filePath) ...
- 用python写一个自动化盲注脚本
前言 当我们进行SQL注入攻击时,当发现无法进行union注入或者报错等注入,那么,就需要考虑盲注了,当我们进行盲注时,需要通过页面的反馈(布尔盲注)或者相应时间(时间盲注),来一个字符一个字符的进行 ...
- 【Python】如何基于Python写一个TCP反向连接后门
首发安全客 如何基于Python写一个TCP反向连接后门 https://www.anquanke.com/post/id/92401 0x0 介绍 在Linux系统做未授权测试,我们须准备一个安全的 ...
- 用Python写一个简单的Web框架
一.概述 二.从demo_app开始 三.WSGI中的application 四.区分URL 五.重构 1.正则匹配URL 2.DRY 3.抽象出框架 六.参考 一.概述 在Python中,WSGI( ...
- 十行代码--用python写一个USB病毒 (知乎 DeepWeaver)
昨天在上厕所的时候突发奇想,当你把usb插进去的时候,能不能自动执行usb上的程序.查了一下,发现只有windows上可以,具体的大家也可以搜索(搜索关键词usb autorun)到.但是,如果我想, ...
- [py]python写一个通讯录step by step V3.0
python写一个通讯录step by step V3.0 参考: http://blog.51cto.com/lovelace/1631831 更新功能: 数据库进行数据存入和读取操作 字典配合函数 ...
- Python写一个自动点餐程序
Python写一个自动点餐程序 为什么要写这个 公司现在用meican作为点餐渠道,每天规定的时间是早7:00-9:40点餐,有时候我经常容易忘记,或者是在地铁/公交上没办法点餐,所以总是没饭吃,只有 ...
- python写一个能变身电光耗子的贪吃蛇
python写一个不同的贪吃蛇 写这篇文章是因为最近课太多,没有精力去挖洞,记录一下学习中的收获,python那么好玩就写一个大一没有完成的贪吃蛇(主要还是跟课程有关o(╥﹏╥)o,课太多好烦) 第一 ...
- 无监控不运维——使用 Python 写一个小小的项目监控
在公司里做的一个接口系统,主要是对接第三方的系统接口,所以,这个系统里会和很多其他公司的项目交互.随之而来一个很蛋疼的问题,这么多公司的接口,不同公司接口的稳定性差别很大,访问量大的时候,有的不怎么行 ...
随机推荐
- react.js Warning: Failed form propType: You provided a value prop to a form field without an onChange handler. This will render a read-only field.
错误信息: eact.js:20483 Warning: Failed form propType: You provided a value prop to a form field without ...
- Shell--变量键盘读取、数组与声明:read,array,declare
1.read read [-pt] variable -P:后面可以接提示信息 -t:后面可以接等待的秒数,时间到后等待结束 read后面不加任何参数,直接加变量名称,那么就会主动出现一个空白行等待你 ...
- this的四种绑定规则总结
一.默认绑定 1.全局环境中,this默认绑定到window 2.函数独立调用时,this默认绑定到window console.log(this === window);//true functio ...
- ES,ZK,Mysql相关参数优化
1.ES 内存调优: vi config/jvm.options -Xms16g -Xmx16g 2.Zookeeper参数配置调优 2.1\在conf目录下 vi java.env export J ...
- 2017.4.28 SSM框架搭建与配置
1.项目结构 2.配置文件 对配置文件进行总结: pom.xml web.xml -> 配置web相关 -> 读取application*.xml 5 -> 读取logback.xm ...
- debug模式下dlgdata.cpp line 43 断言失败
我在VC6下显示Line 43, Line 624行失败 网上有Line 40行猜测是其他版本 运行程序出错,定位如下: HWND CDataExchange::PrepareCtrl(int nID ...
- Linux 命令 indent 用法
此命令用于调整C源码的格式. 在LKD中的例子: indent -kr -i8 -ts8 -sob -l80 -ss -bs -psl filename 参数解释如下: -i :设置缩进的格数 - ...
- react-native 调用第三方 SDK
步骤一:android 文件修改 (1)In android/settings.gradle ... include ':VoiceModule', ':app' project(':VoiceMod ...
- springMVC 前后台日期格式传值解决方式之一(共二) @DateTimeFormat的使用和配置
无意中发现对于时间字符串转Date类,根本不用自己去写转换类,spring mvc已经实现了该功能,还是基于注解的,轻松省事,使用 org.springframework.format.support ...
- declare @t table
DECLARE @t TABLE(date char(21))INSERT @t SELECT '1900-1-1 00:00:00.000'INSERT @t SELECT '1900-1-1 00 ...