zoukankan      html  css  js  c++  java
  • python实时监控服务器性能

    监控服务器各项性能指标,以下为监控测试流程

    1、mysql数据库插入数据

    1.1 大家要首先在服务器上安装一个mysql,此项略。。。

    1.2 登录mysql新建一个cpu库和cpu表

    1.3  连接数据库,代码如下

     1 import pymysql
     2 class Conn:
     3     def __init__(self):
     4         self.connect = pymysql.connect(
     5             host = "192.168.1.124",
     6             port = 3306,
     7             user = "root",
     8             passwd = "testmysql",
     9             db = "cpu",
    10             charset = "utf8"
    11         )
    12         self.cur = self.connect.cursor()
    13 
    14     def insert(self,data):
    15         sql = "insert into cpu(cpu_idle,memory_used,memory_free,disk_C_free) values ('%s','%s','%s','%s')"
    16         self.cur.execute(sql%data)
    17         # self.connect.close()
    18         self.connect.commit()
    19 
    20 # if __name__ == '__main__':
    21 #     v = Conn()
    22 #     data = (23,45,56,67)
    23 #     print(1)
    24 #     v.insert(data)
    25 #     print(2)
    连接mysql代码

    2、收集服务器数据(此测试本机为样本)

     1 import  time
     2 from TEST.test import Conn
     3 import psutil
     4 def Getdata():
     5     while 1:
     6         cpu_idle = psutil.cpu_times().idle
     7         cpu_idle = cpu_idle/1000
     8 
     9         memory_used = psutil.virtual_memory().used
    10         memory_used = memory_used/(1000**3)
    11 
    12         memory_free = psutil.virtual_memory().free
    13         memory_free = memory_free/(1000**3)
    14 
    15         disk_C_free = psutil.disk_usage("C://").free
    16         disk_C_free = disk_C_free/(1000**3)
    17 
    18         date = (round(cpu_idle,2),round(memory_used,2),round(memory_free,2),round(disk_C_free,2))
    19         v = Conn()
    20         v.insert(date)
    21         time.sleep(5)
    22         print(date)
    23 Getdata()
    收集服务器数据

    3、验证

    可以在数据库中查看是否能正常插入数据

  • 相关阅读:
    SpringBoot--实现Mybatis的多数据源切换和动态数据源切换
    云服务器查看外网IP地址方法
    Linux性能调优方法总结
    rsyslog配置
    Linux网络优化
    Reversoir sampling
    SQL 中的 NULL 小结
    Go by Example: HTTP Servers
    Linux 在 TOP 命令中切换内存的显示单位
    kubernetes informer 原理解析二
  • 原文地址:https://www.cnblogs.com/eddycomeon/p/11960664.html
Copyright © 2011-2022 走看看