```
#!/usr/bin/python
#########################################
# Function: sample linux performance indices
# Usage: python sampler.py
# Author: CMS DEV TEAM
# Company: Aliyun Inc.
# Version: 1.1
#########################################
import os
import os.path
import sys
import time
import operator
import httplib
import logging
import socket
import random
from shutil import copyfile
from subprocess import Popen, PIPE
from logging.handlers import RotatingFileHandler
logger = None
REMOTE_HOST = None
REMOTE_PORT = None
REMOTE_MONITOR_URI = None
UUID = None
def get_mem_usage_percent():
try:
f = open('/proc/meminfo', 'r')
for line in f:
if line.startswith('MemTotal:'):
mem_total = int(line.split()[1])
elif line.startswith('MemFree:'):
mem_free = int(line.split()[1])
elif line.startswith('Buffers:'):
mem_buffer = int(line.split()[1])
elif line.startswith('Cached:'):
mem_cache = int(line.split()[1])
elif line.startswith('SwapTotal:'):
vmem_total = int(line.split()[1])
elif line.startswith('SwapFree:'):
vmem_free = int(line.split()[1])
else:
continue
f.close()
except:
return None
physical_percent = usage_percent(mem_total - (mem_free + mem_buffer + mem_cache), mem_total)
virtual_percent = 0
if vmem_total > 0:
virtual_percent = usage_percent((vmem_total - vmem_free), vmem_total)
return physical_percent, virtual_percent
black_list = ('iso9660',)
def usage_percent(use, total):
try:
ret = (float(use) / total) * 100
except ZeroDivisionError:
raise Exception("ERROR - zero division error")
return ret
def get_disk_partition():
return_list = []
pd = []
try:
f = open("/proc/filesystems", "r")
for line in f:
if not line.startswith("nodev"):
fs_type = line.strip()
if fs_type not in black_list:
pd.append(fs_type)
f.close()
f = open('/etc/mtab', "r")
for line in f:
if line.startswith('none'):
continue
tmp = line.strip().split()
ft = tmp[2]
if ft not in pd:
continue
return_list.append(tmp[1])
f.close()
except:
return None
return return_list
def check_disk():
try:
return_dict = {}
p_list = get_disk_partition()
for i in p_list:
dt = os.statvfs(i)
use = (dt.f_blocks - dt.f_bfree) * dt.f_frsize
all = dt.f_blocks * dt.f_frsize
return_dict[i] = ('%.2f' % (usage_percent(use, all),), ('%.2f' % (all * 1.0 / (1024 * 1000000))))
except:
return None
return return_dict
_CLOCK_TICKS = os.sysconf("SC_CLK_TCK")
def get_cpu_time():
need_sleep = False
if not os.path.isfile('/tmp/cpu_stat') or os.path.getsize('/tmp/cpu_stat') == 0:
copyfile('/proc/stat', '/tmp/cpu_stat')
need_sleep = True
try:
f1 = open('/tmp/cpu_stat', 'r')
values1 = f1.readline().split()
total_time1 = 0
for i in values1[1:]:
total_time1 += int(i)
idle_time1 = int(values1[4])
iowait_time1 = int(values1[5])
finally:
f1.close()
if need_sleep:
time.sleep(1)
f2 = open('/proc/stat', 'r')
try:
values2 = f2.readline().split()
total_time2 = 0
for i in values2[1:]:
total_time2 += int(i)
idle_time2 = int(values2[4])
iowait_time2 = int(values2[5])
finally:
f2.close()
idle_time = idle_time2 - idle_time1
iowait_time = iowait_time2 - iowait_time1
total_time = total_time2 - total_time1
cpu_percentage = int(100.0 * (total_time - idle_time - iowait_time) / total_time)
# compensate logic
if total_time < 0 or idle_time < 0 or iowait_time < 0 or cpu_percentage < 0 or cpu_percentage > 100:
time.sleep(1)
f3 = open('/proc/stat', 'r')
try:
values3 = f3.readline().split()
total_time3 = 0
for i in values3[1:]:
total_time3 += int(i)
idle_time3 = int(values3[4])
iowait_time3 = int(values3[5])
finally:
f3.close()
idle_time = idle_time3 - idle_time2
iowait_time = iowait_time3 - iowait_time2
total_time = total_time3 - total_time2
cpu_percentage = int(100.0 * (total_time - idle_time - iowait_time) / total_time)
copyfile('/proc/stat', '/tmp/cpu_stat')
return cpu_percentage
def network_io_kbitps():
"""Return network I/O statistics for every network interface
installed on the system as a dict of raw tuples.
"""
f1 = open("/proc/net/dev", "r")
try:
lines1 = f1.readlines()
finally:
f1.close()
retdict1 = {}
for line1 in lines1[2:]:
colon1 = line1.find(':')
assert colon1 > 0, line1
name1 = line1[:colon1].strip()
fields1 = line1[colon1 + 1:].strip().split()
bytes_recv1 = float('%.4f' % (float(fields1[0]) * 0.0078125))
bytes_sent1 = float('%.4f' % (float(fields1[8]) * 0.0078125))
retdict1[name1] = (bytes_recv1, bytes_sent1)
time.sleep(1)
f2 = open("/proc/net/dev", "r")
try:
lines2 = f2.readlines()
finally:
f2.close()
retdict2 = {}
for line2 in lines2[2:]:
colon2 = line2.find(':')
assert colon2 > 0, line2
name2 = line2[:colon2].strip()
fields2 = line2[colon2 + 1:].strip().split()
bytes_recv2 = float('%.4f' % (float(fields2[0]) * 0.0078125))
bytes_sent2 = float('%.4f' % (float(fields2[8]) * 0.0078125))
retdict2[name2] = (bytes_recv2, bytes_sent2)
retdict = merge_with(retdict2, retdict1)
return retdict
def disk_io_Kbps():
iostat = Popen("iostat -d -k 1 2 | sed '/Device\|Linux\|^$/d' > /tmp/disk_io", shell=True, stdout=PIPE, stderr=PIPE)
iostat_error = iostat.communicate()[1].strip()
if iostat_error:
logger.error("iostat not exists, %s" % iostat_error)
return None
retdict = {}
exception = None
try:
try:
f = open('/tmp/disk_io', 'r')
except Exception, ex:
exception = ex
logger.error(exception)
if exception:
return None
lines = f.readlines()
for line in lines:
name, _, readkps, writekps, _, _, = line.split()
if name:
readkps = float(readkps)
writekps = float(writekps)
retdict[name] = (readkps, writekps)
return retdict
finally:
f.close()
def merge_with(d1, d2, fn=lambda x, y: tuple(map(operator.sub, x, y))):
res = d1.copy() # "= dict(d1)" for lists of tuples
for key, val in d2.iteritems(): # ".. in d2" for lists of tuples
try:
res[key] = fn(res[key], val)
except KeyError:
res[key] = val
return res
def get_load():
try:
f = open('/proc/loadavg', 'r')
tmp = f.readline().split()
lavg_1 = float(tmp[0])
lavg_5 = float(tmp[1])
lavg_15 = float(tmp[2])
f.close()
except:
return None
return lavg_1, lavg_5, lavg_15
def get_tcp_status():
check_cmd = "command -v ss"
check_proc = Popen(check_cmd, shell=True, stdout=PIPE)
ss = check_proc.communicate()[0].rstrip('\n')
if ss:
cmd = "ss -ant | awk '{if(NR != 1) print $1}' | awk '{state=$1;arr[state]++} END{for(i in arr){printf \"%s=%s \", i,arr[i]}}' | sed 's/-/_/g' | sed 's/ESTAB=/ESTABLISHED=/g' | sed 's/FIN_WAIT_/FIN_WAIT/g'"
else:
cmd = "netstat -anp | grep tcp | awk '{print $6}' | awk '{state=$1;arr[state]++} END{for(i in arr){printf \"%s=%s \", i,arr[i]}}' | tail -n 1"
tcp_proc = Popen(cmd, shell=True, stdout=PIPE)
tcp_status = tcp_proc.communicate()[0].rstrip('\n')
return tcp_status
def get_proc_number():
cmd = "ps axu | wc -l | tail -n 1"
proc_func = Popen(cmd, shell=True, stdout=PIPE)
proc_number = proc_func.communicate()[0].rstrip('\n')
return proc_number
def all_index():
return (
int(time.time() * 1000),
get_cpu_time(),
get_mem_usage_percent(),
check_disk(),
disk_io_Kbps(),
network_io_kbitps(),
get_load(),
get_tcp_status(),
get_proc_number()
)
def collector():
timestamp, cpu, mem, disk, disk_io, net, load, tcp_status, process_number = all_index()
disk_utilization = ''
disk_io_read = ''
disk_io_write = ''
internet_networkrx = ''
internet_networktx = ''
tcp_status_count = ''
period_1 = ''
period_5 = ''
period_15 = ''
if UUID:
cpu_utilization = 'vm.CPUUtilization ' + str(timestamp) + ' ' + str(cpu) + ' ns=ACS/ECS unit=Percent instanceId=%s\n' % UUID
memory_utilization = 'vm.MemoryUtilization ' + str(timestamp) + ' ' + str(mem[0]) + ' ns=ACS/ECS unit=Percent instanceId=%s\n' % UUID
if load:
period_1 = 'vm.LoadAverage ' + str(timestamp) + ' ' + str(load[0]) + ' ns=ACS/ECS unit=count' + ' instanceId=%s period=1min\n' % UUID
period_5 = 'vm.LoadAverage ' + str(timestamp) + ' ' + str(load[1]) + ' ns=ACS/ECS unit=count' + ' instanceId=%s period=5min\n' % UUID
period_15 = 'vm.LoadAverage ' + str(timestamp) + ' ' + str(load[2]) + ' ns=ACS/ECS unit=count' + ' instanceId=%s period=15min\n' % UUID
if disk:
for name, value in disk.items():
disk_utilization = disk_utilization + 'vm.DiskUtilization ' + str(timestamp) + ' ' + str(value[0]) + ' ns=ACS/ECS unit=Percent instanceId=%s mountpoint=%s\n' % (UUID, name)
if disk_io:
for name, value in disk_io.items():
disk_io_read = disk_io_read + 'vm.DiskIORead ' + str(timestamp) + ' ' + str(value[0]) + ' ns=ACS/ECS unit=Kilobytes/Second instanceId=%s diskname=%s\n' % (UUID, name)
disk_io_write = disk_io_write + 'vm.DiskIOWrite ' + str(timestamp) + ' ' + str(value[1]) + ' ns=ACS/ECS unit=Kilobytes/Second instanceId=%s diskname=%s\n' % (UUID, name)
for name, value in net.items():
internet_networkrx = internet_networkrx + 'vm.InternetNetworkRX ' + str(timestamp) + ' ' + str(value[0]) + ' ns=ACS/ECS unit=Kilobits/Second instanceId=%s netname=%s\n' % (UUID, name)
internet_networktx = internet_networktx + 'vm.InternetNetworkTX ' + str(timestamp) + ' ' + str(value[1]) + ' ns=ACS/ECS unit=Kilobits/Second instanceId=%s netname=%s\n' % (UUID, name)
if tcp_status:
status_count = tcp_status.split()
for element in status_count:
key_value = element.split('=')
tcp_status_count = tcp_status_count + 'vm.TcpCount ' + str(timestamp) + ' ' + key_value[1] + ' ns=ACS/ECS unit=Count instanceId=%s state=%s\n' % (UUID, key_value[0])
process_count = 'vm.ProcessCount ' + str(timestamp) + ' ' + process_number + ' ns=ACS/ECS unit=Count instanceId=%s\n' % UUID
else:
cpu_utilization = 'vm.CPUUtilization ' + str(timestamp) + ' ' + str(cpu) + ' ns=ACS/ECS unit=Percent\n'
memory_utilization = 'vm.MemoryUtilization ' + str(timestamp) + ' ' + str(mem[0]) + ' ns=ACS/ECS unit=Percent\n'
if load:
period_1 = 'vm.LoadAverage ' + str(timestamp) + ' ' + str(load[0]) + ' ns=ACS/ECS unit=count period=1min\n'
period_5 = 'vm.LoadAverage ' + str(timestamp) + ' ' + str(load[1]) + ' ns=ACS/ECS unit=count period=5min\n'
period_15 = 'vm.LoadAverage ' + str(timestamp) + ' ' + str(load[2]) + ' ns=ACS/ECS unit=count period=15min\n'
if disk:
for name, value in disk.items():
disk_utilization = disk_utilization + 'vm.DiskUtilization ' + str(timestamp) + ' ' + str(value[0]) + ' ns=ACS/ECS unit=Percent mountpoint=%s\n' % name
if disk_io:
for name, value in disk_io.items():
disk_io_read = disk_io_read + 'vm.DiskIORead ' + str(timestamp) + ' ' + str(value[0]) + ' ns=ACS/ECS unit=Kilobytes/Second diskname=%s\n' % name
disk_io_write = disk_io_write + 'vm.DiskIOWrite ' + str(timestamp) + ' ' + str(value[1]) + ' ns=ACS/ECS unit=Kilobytes/Second diskname=%s\n' % name
for name, value in net.items():
internet_networkrx = internet_networkrx + 'vm.InternetNetworkRX ' + str(timestamp) + ' ' + str(value[0]) + ' ns=ACS/ECS unit=Kilobits/Second netname=%s\n' % name
internet_networktx = internet_networktx + 'vm.InternetNetworkTX ' + str(timestamp) + ' ' + str(value[1]) + ' ns=ACS/ECS unit=Kilobits/Second netname=%s\n' % name
if tcp_status:
status_count = tcp_status.split()
for element in status_count:
key_value = element.split('=')
tcp_status_count = tcp_status_count + 'vm.TcpCount ' + str(timestamp) + ' ' + key_value[1] + ' ns=ACS/ECS unit=Count state=%s\n' % key_value[0]
process_count = 'vm.ProcessCount ' + str(timestamp) + ' ' + process_number + ' ns=ACS/ECS unit=Count\n'
data_post = cpu_utilization + memory_utilization + period_1 + period_5 + period_15 + disk_utilization + disk_io_read + disk_io_write + internet_networkrx + internet_networktx + tcp_status_count + process_count
print data_post
interval = random.randint(0, 5000)
time.sleep(interval / 1000.0)
headers = {"Content-Type": "text/plain", "Accept": "text/plain"}
exception = None
http_client = None
try:
try:
http_client = httplib.HTTPConnection(REMOTE_HOST, REMOTE_PORT)
http_client.request(method="POST", url=REMOTE_MONITOR_URI, body=data_post, headers=headers)
response = http_client.getresponse()
if response.status == 200:
return
else:
logger.warn("response code %d" % response.status)
logger.warn("response code %s" % response.read())
except Exception, ex:
exception = ex
finally:
if http_client:
http_client.close()
if exception:
logger.error(exception)
if __name__ == '__main__':
REMOTE_HOST = 'open.cms.aliyun.com'
REMOTE_PORT = 80
# get report address
if not os.path.isfile("../cmscfg"):
pass
else:
props = {}
prop_file = file("../cmscfg", 'r')
for line in prop_file.readlines():
kv = line.split('=')
props[kv[0].strip()] = kv[1].strip()
prop_file.close()
if props.get('report_domain'):
REMOTE_HOST = props.get('report_domain')
if props.get('report_port'):
REMOTE_PORT = props.get('report_port')
# get uuid
if not os.path.isfile("../aegis_quartz/conf/uuid"):
pass
else:
uuid_file = file("../aegis_quartz/conf/uuid", 'r')
UUID = uuid_file.readline()
UUID = UUID.lower()
REMOTE_MONITOR_URI = "/metrics/putLines"
MONITOR_DATA_FILE_DIR = "/tmp"
LOG_FILE = "/tmp/" + "vm.log"
LOG_LEVEL = logging.INFO
LOG_FILE_MAX_BYTES = 1024 * 1024
LOG_FILE_MAX_COUNT = 3
logger = logging.getLogger('sampler')
logger.setLevel(LOG_LEVEL)
handler = RotatingFileHandler(filename=LOG_FILE, mode='a', maxBytes=LOG_FILE_MAX_BYTES,
backupCount=LOG_FILE_MAX_COUNT)
formatter = logging.Formatter(fmt='%(asctime)s - %(levelname)s - %(message)s')
handler.setFormatter(formatter)
logger.addHandler(handler)
socket.setdefaulttimeout(10)
try:
collector()
except Exception, e:
logger.error(e)
sys.exit(1)
————————————————
版权声明:本文为CSDN博主「iteye_7966」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/iteye_7966/java/article/details/82614427
```
- 常见功能
- 第三方授权登录
- 邮件发送
- 简易聊天室
- 获取各国汇率
- PHP获取服务器硬件指标
- 数据上报之
- web开发
- 开发规范
- 前端
- 踩坑
- 将footer固定在底部
- bootstrap
- Metronic
- 用到的jquery插件
- bootstrap-hover-dropdown
- jquery.slimscroll
- jquery.blockui
- bootstrap-switch
- js.cookie
- moment
- bootstrap-daterangepicker
- morris
- raphael
- jquery.waypoints
- jquery.counterup
- select2
- 取值和设置默认值
- vue
- axios
- 浏览器
- 谷歌浏览器
- 谷歌插件
- layui
- layui-表格
- layui-表单
- layui-弹窗
- layui-分页
- 后端
- 操作系统
- linux
- 用户管理
- 文件管理
- 目录管理
- 压缩和解压缩
- 进程查看
- 端口查看
- 开机自启动服务
- 定时任务
- shell脚本
- 杀掉运行超过指定时长指定服务的进程
- 获取服务器使用状态
- bash-shell连接socket
- 自定义快捷命令
- centos-踩坑
- 防火墙
- 软件
- yum
- vim
- screen
- window
- 语言
- PHP
- 配置优化
- 框架
- thinkphp5.1+
- think命令行
- laravel6.+
- 维护模式
- 根据环境读取不同配置
- laravel6.+采坑
- laravel坑位
- 数据库事务
- 任务调度
- 文件权限问题
- 增强框架
- larvel:elastic-search
- 图形验证码
- laravel获取ip
- 函数
- strtotime
- 正则匹配
- 类
- 接口类与抽象类
- 类相关的关键字 - abstract
- 类相关的关键字 - interface
- PHP有关类的调用方式"->"与"::"的区别
- 扩展
- 问题归纳
- json_encode和json_decode
- 字符串的运算
- curl
- 优化php效率
- 数组相加合并与array_merge
- 时区转换
- 不常用特性
- php反射
- 包管理器-composer
- GuzzleHttp
- Python
- Go
- 数据库
- Redis
- 安装
- 本地化-数据备份
- php-redis操作
- Mysql
- mysql-命令集合
- 设置终端可访问
- 数据库设计
- 用户基础信息表
- 踩坑集合
- mysql-2002
- mysql-2054
- 优化策略
- mysql-密码验证插件
- 一些牛逼的sql查询
- topN
- 无限级分类
- Memcache
- MongoDb
- 安装mongo-server
- 安装php-mongodb扩展
- 在laravel中使用mongoDB
- 客户端软件
- Hbase
- Elasticsearch
- elastic-search
- restfulApi操作es
- web服务器
- 1.nginx
- 配置语法规则
- 配置详解
- rewrite规则
- request_filename
- 2.apache
- 功能设计
- 加密解密
- Base64
- 对亚马逊SKU加密
- 兼职项目中的加解密
- 腾讯外包时的加密
- 接口设计
- 接口限流设计
- 分库分表
- 遍历展示文件目录结构
- 时区换算
- 文件切割
- 解析xml字符串
- 项目
- 博客后台管理
- 亚马逊广告API
- 官方指引文档
- 开发人员中心
- 应用商店
- 第三方库
- 申请API邮件记录
- 亚马逊MWS
- 付款报告
- 乱码
- 亚马逊管理库存报告
- 报告
- 商品
- 入库
- 履行
- 出库
- 财务
- 订单
- 异步任务处理
- 集群如何同步代码
- 基本开发流程
- 文档管理
- showdoc
- 运行环境
- 开发环境
- vagrant
- windows上配置安装
- vagrant安装插件缓慢
- 更换ssh默认端口映射
- 设置x-shell密码登录
- 使用市场的box-homestead
- homestead-7: Box 'lc/homestead'
- 常见问题
- 虚拟环境reboot
- 突然无法使用
- phpStudy
- wamp
- 压测性能
- VPN
- vultr
- 凌空图床
- 宝塔
- 自动化部署
- 版本管理软件钩子
- 线上环境-LNMP
- centos7
- nginx
- mysql
- mysql开机自启
- mysql-更换默认端口
- datetime字段类型默认值
- php
- php扩展安装
- redis
- swoole
- gd
- BCMath
- igbinary
- zstd
- 包管理器:composer
- 优化性能
- nodejs
- 更新gcc版本
- 版本控制
- git
- 常用命令
- gitlab
- 版本管理规范
- 使用阿里云创建远程仓库
- git自动化部署
- svn
- 忽略指定文件
- 拉取代码
- 自动化运维
- jekins
- 容器
- 集群
- 架构设计
- 设计原则
- 阅读参考
- 代码规划
- 架构实战
- 服务治理
- 权限控制设计
- 具体设计
- 计划
- 疑问知识点
- 读书笔记
- 高性能Mysql
- TCP-IP详解-卷一:协议
- 思考
- php如何实现并发执行
- 对接调用设计
- 如何在浏览器上实现插件
- 如何设计一个app结合业务告警
- mysql的where查询没有用到索引
- 为啥in查询比循环嵌套sql的查询还要慢
- 使用git来创建属于自己的composer包
- 翻页获取数据的时候又新增了数据
- 安全思路
- 月报
- PHP ?? 和 ?: 的区别
- PHP异步执行
- redis集群的目标是什么
- 大文件数据处理
- 性能瓶颈分析
- 命令行里输出带颜色的字体
- 面试问题合集
- 基础
- 安全
- 算法
- 冒泡排序
- 快速排序
- 二分法查询数组指定成员
- 字符查找匹配
- 令牌桶
- 漏桶
- 计数器
- 代理
- 协议
- http
- 状态码
- tcp
- udp
- Oauth2.0
- 设计模式
- 单例模式
- 适配器模式
- 工厂模式
- 观察者模式
- 流程化
- 地址栏输入网址到返回网页的流程
- 题目收集
- 工具
- rabbitMq
- rabbitMQ用户管理
- 生产者
- 消费者
- 支持TP5.*的think-queue
- 消息丢失
- 消费者报错
- rabbitMQ配置优化
- 磁盘满载导致服务挂掉
- PHP类库
- rabbitMQ踩坑
- navicat
- vscode
- phpstorm
- 激活码
- markdown
- PHP自定义类库
- 工具类
- 领导力
- 任务分配
- 代码组织
- 不要重复
- 避免污染
- 接口定义规范
- 小业务需求
- 获取充值面额组成
- 监控服务器CPU和内存
- shell脚本版本