python/System_Monitoring_Script/cpu.py

17 lines
606 B
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/usr/bin/python3
#-*-coding: utf-8-*-
# 获取cpu负载信息并调用钉钉接口发送
import subprocess,sys,datetime
import dingding
def cpu_load(): # 定义函数获取系统cpu负载
a = subprocess.getoutput('uptime |cut -d":" -f4')
upload = a.split() # 将结果转换为列表
return upload # 返回 这里要注意return和print的区别
a = cpu_load() # 调用方法
time = datetime.datetime.now() # 定义当前时间
data = 'cpu 负载为:{}\n当前时间:{}'.format(a,time)
dingding.msg(data) # 调用钉钉
if __name__ == '__main__':
print(data) # 测试数据