当前位置: 首页 > wzjs >正文

泗洪网站wordpress 主题删除

泗洪网站,wordpress 主题删除,网站死链修复,如何做线上推广监控系统资源使用率并钉钉报警 当Linux系统 cpu 内存 SWap交换空间 使用率超过80% 给钉钉发送报警。 #python3版本 [rootlocalhost 11:35:16]# python3 --version Python 3.6.8#pip3版本 [rootlocalhost 11:35:22]# pip3 --version pip 9.0.3 from /usr/lib/python3.6/site-p…

监控系统资源使用率并钉钉报警

当Linux系统 cpu 内存 SWap交换空间 使用率超过80% 给钉钉发送报警。

#python3版本
[root@localhost 11:35:16]# python3 --version
Python 3.6.8#pip3版本
[root@localhost 11:35:22]# pip3 --version
pip 9.0.3 from /usr/lib/python3.6/site-packages (python 3.6)#安装requests模块
pip3 install requests#创建脚本文件
vim monitor.py#赋予脚本执行权限
chmod +x monitor.py#运行脚本:
python3 monitor.py#设置定时任务
#打开 crontab 编辑器:
crontab -e
#添加以下内容,每分钟运行一次脚本:
* * * * * /usr/bin/python3 /path/to/monitor.py
#将 /path/to/monitor.py 替换为脚本的实际路径。

monitor.py脚本内容

import os
import requests
import json
import socket# 钉钉 Webhook URL
webhook_url = "钉钉 Webhook URL"# 获取主机名称
def get_hostname():return socket.gethostname()# 获取 IP 地址
def get_ip_address():try:# 使用 ifconfig 获取 IP 地址cmd = "ifconfig ens160 | grep 'inet ' | awk '{ print $2 }'"ip_address = os.popen(cmd).read().strip()if ip_address:return ip_addresselse:return "未知"except Exception as e:print(f"获取 IP 地址失败: {e}")return "未知"# 获取 CPU 使用率
def get_cpu_usage():cmd = "top -bn1 | grep '%Cpu' | awk '{print $2}'"result = os.popen(cmd).read().strip()print(f"CPU 使用率命令输出: {result}")  # 调试信息return float(result)# 获取内存使用率
def get_memory_usage():cmd = "free -m | grep 'Mem'"result = os.popen(cmd).read().split()memory_total = float(result[1])memory_used = float(result[2])memory_usage = (memory_used / memory_total) * 100print(f"内存使用率: {memory_usage:.2f}%")  # 调试信息return memory_usage# 获取 SWAP 使用率
def get_swap_usage():cmd = "free -m | grep 'Swap'"result = os.popen(cmd).read().split()swap_total = float(result[1])swap_used = float(result[2])swap_usage = (swap_used / swap_total) * 100print(f"SWAP 使用率: {swap_usage:.2f}%")  # 调试信息return swap_usage# 发送钉钉报警
def send_dingding_alert(message):headers = {'Content-Type': 'application/json'}data = {"msgtype": "text","text": {"content": f"报警:{message}"  # 加入关键词 "报警"}}print(f"发送消息到钉钉: {message}")  # 调试信息response = requests.post(webhook_url, headers=headers, data=json.dumps(data))print(f"钉钉响应状态码: {response.status_code}")  # 调试信息print(f"钉钉响应内容: {response.text}")  # 调试信息return response.status_code# 主函数
def main():print("脚本开始运行...")  # 调试信息hostname = get_hostname()ip_address = get_ip_address()cpu_usage = get_cpu_usage()memory_usage = get_memory_usage()swap_usage = get_swap_usage()if cpu_usage > 80:message = f"主机: {hostname} (IP: {ip_address})\n⚠️ CPU 使用率超过 80%: {cpu_usage:.2f}%"send_dingding_alert(message)if memory_usage > 80:message = f"主机: {hostname} (IP: {ip_address})\n⚠️ 内存使用率超过 80%: {memory_usage:.2f}%"send_dingding_alert(message)if swap_usage > 80:message = f"主机: {hostname} (IP: {ip_address})\n⚠️ SWAP 使用率超过 80%: {swap_usage:.2f}%"send_dingding_alert(message)if __name__ == "__main__":main()

注意事项1:钉钉Webhook URL

注意事项2:网络接口名称

 注意事项3:报警关键字。

 注意事项4:报警控制 资源利用率。

 运行结果:

python3 monitor.py

 

 Windows 在pycharm 上面执行脚本。

import os
import requests
import json
import socket
import psutil# 钉钉 Webhook URL
webhook_url = " 钉钉 Webhook URL"# 获取主机名称
def get_hostname():return socket.gethostname()# 获取 IP 地址
def get_ip_address():try:hostname = socket.gethostname()ip_address = socket.gethostbyname(hostname)return ip_addressexcept Exception as e:print(f"获取 IP 地址失败: {e}")return "未知"# 获取 CPU 使用率
def get_cpu_usage():return psutil.cpu_percent(interval=1)# 获取内存使用率
def get_memory_usage():memory_info = psutil.virtual_memory()return memory_info.percent# 获取 SWAP 使用率
def get_swap_usage():swap_info = psutil.swap_memory()return swap_info.percent# 发送钉钉报警
def send_dingding_alert(message):headers = {'Content-Type': 'application/json'}data = {"msgtype": "text","text": {"content": f"报警:{message}"  # 加入关键词 "报警"}}print(f"发送消息到钉钉: {message}")  # 调试信息try:response = requests.post(webhook_url, headers=headers, data=json.dumps(data), timeout=10)print(f"钉钉响应状态码: {response.status_code}")  # 调试信息print(f"钉钉响应内容: {response.text}")  # 调试信息return response.status_codeexcept requests.exceptions.RequestException as e:print(f"发送消息到钉钉失败: {e}")return None# 主函数
def main():print("脚本开始运行...")  # 调试信息hostname = get_hostname()ip_address = get_ip_address()cpu_usage = get_cpu_usage()memory_usage = get_memory_usage()swap_usage = get_swap_usage()print(f"CPU 使用率: {cpu_usage}%")print(f"内存使用率: {memory_usage}%")print(f"SWAP 使用率: {swap_usage}%")if cpu_usage > 50:message = f"主机: {hostname} (IP: {ip_address})\n⚠️ CPU 使用率超过 50%: {cpu_usage:.2f}%"send_dingding_alert(message)if memory_usage > 50:message = f"主机: {hostname} (IP: {ip_address})\n⚠️ 内存使用率超过 50%: {memory_usage:.2f}%"send_dingding_alert(message)if swap_usage > 50:message = f"主机: {hostname} (IP: {ip_address})\n⚠️ SWAP 使用率超过 50%: {swap_usage:.2f}%"send_dingding_alert(message)if __name__ == "__main__":main()

运行结果:


文章转载自:

http://TL77elIT.zycLL.cn
http://ZVRs5eEd.zycLL.cn
http://jQXidZl2.zycLL.cn
http://QqmXsezz.zycLL.cn
http://KrDjJpK9.zycLL.cn
http://867PTy0F.zycLL.cn
http://JuF5Xzzl.zycLL.cn
http://nRJu7HzV.zycLL.cn
http://MGOzt5uJ.zycLL.cn
http://SIWsUbYC.zycLL.cn
http://nWG4MZir.zycLL.cn
http://4KRFyQ6v.zycLL.cn
http://8ZDyaIw3.zycLL.cn
http://jtef6RgX.zycLL.cn
http://gum7EV3v.zycLL.cn
http://gJbIz86q.zycLL.cn
http://z8xEKrrN.zycLL.cn
http://YSdTHrWh.zycLL.cn
http://7eUFy2uz.zycLL.cn
http://YkrnSG3H.zycLL.cn
http://3fkRCMAf.zycLL.cn
http://BiAWGMT1.zycLL.cn
http://7RNWyneL.zycLL.cn
http://j2NES35r.zycLL.cn
http://ar6HCUkX.zycLL.cn
http://dG8tSJ0E.zycLL.cn
http://SDNhDlUP.zycLL.cn
http://HFVmdKtR.zycLL.cn
http://XpmCbEg2.zycLL.cn
http://5o71SzoH.zycLL.cn
http://www.dtcms.com/wzjs/725973.html

相关文章:

  • 网站备案需要多长时间企业内部的网站系统
  • 如何做自己官方网站学校网站建设方法
  • 山东住房和城乡建设局网站首页wordpress商店
  • WordPress大前端top攀枝花seo
  • 网站没有流量怎么办模板网页设计视频
  • 毕业设计做课程网站好vi设计都包括什么
  • 英铭长沙网站建设手机网站建设知识
  • 网站app开发费用毕业去设计公司还是企业
  • WordPress如何禁止游客访问专业做网站优化需要多久
  • 响应式网站怎么改万网怎样做网站调试
  • 营业执照包含网站开发建设品牌型网站
  • 做书的网站有哪些用asp做的网站
  • 建设银行征信中心网站单位做网站支出应怎么核算
  • 企?I网站建站 ?V州 ??lwordpress社交系统
  • 一流的镇江网站建设做网站一定要效果图吗
  • 网站开发遇到的风险潍坊网站公司网络科技
  • 软件开发培训中心重庆谷歌seo关键词优化
  • 网站建设的最终目标网站推广效益怎么分析
  • 浙江省住建和城乡建设厅官方网站wordpress 2.0漏洞
  • 晋中市两学一做网站html5 音乐网站
  • 淘金企业网站建设服务商丘seo快速排名
  • 营销网站做推广公司关键词seo培训
  • 上海婚恋网站排名长春网站公司
  • 网站服务器租赁需要什么手续吉林省建设工程质量监督站网站
  • 网站交互性网站弹窗怎么做
  • 网站建设公司推广方案网站建设关键词
  • 购物网站html济阳做网站哪家好
  • ui做交互式网站吗免费网站风格
  • 小白怎样建设公司网站wordpress保护原创
  • 越秀网站建设哪家好为了 门户网站建设