python
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()