import socket
import psutil
from dingtalkchatbot.chatbot import DingtalkChatbot
def get_cpu_usage():
return psutil.cpu_percent()
def get_memory_usage():
return psutil.virtual_memory().percent
def get_disk_usage():
return psutil.disk_usage('/').percent
def check_port(port):
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
return s.connect_ex(('localhost', port)) == 0
def send_message_to_dingtalk(message):
webhook = '钉钉机器人token'
bot = DingtalkChatbot(webhook)
bot.send_text(msg=message)
def generate_report():
cpu_usage = get_cpu_usage()
memory_usage = get_memory_usage()
disk_usage1 = get_disk_usage()
message = f"CPU利用率: {cpu_usage}%\nMemory利用率: {memory_usage}%\n根目录磁盘利用率: {disk_usage1}%"
if check_port(端口号):
message += "\n服务端口: UP"
else:
message += "\n服务端口: Down!"
send_message_to_dingtalk(message)
generate_report()
文章来源地址https://www.toymoban.com/news/detail-589294.html
文章来源:https://www.toymoban.com/news/detail-589294.html
到了这里,关于Python脚本----巡检服务器资源并发送钉钉的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!