国产宅男网站在线|亚洲A级性爱免费视频|亚洲中精品级在线|午夜福利AA毛

  • <dd id="gf5jf"><th id="gf5jf"></th></dd>

    <cite id="gf5jf"><label id="gf5jf"></label></cite>
  • <div id="gf5jf"><listing id="gf5jf"></listing></div>
    學(xué)習(xí)啦 > 學(xué)習(xí)電腦 > 電腦安全 > 局域網(wǎng)安全 > 局域網(wǎng)python封裝linux監(jiān)控模塊

    局域網(wǎng)python封裝linux監(jiān)控模塊

    時間: 林澤1002 分享

    局域網(wǎng)python封裝linux監(jiān)控模塊

      psutil是一個跨平臺庫,能夠輕松實現(xiàn)獲取系統(tǒng)運行的進程、系統(tǒng)利用率、CPU信息、內(nèi)存信息、磁盤信息、網(wǎng)絡(luò)信息。下面是學(xué)習(xí)啦小編收集整理的局域網(wǎng)python封裝linux監(jiān)控模塊,希望對大家有幫助~~

      局域網(wǎng)python封裝linux監(jiān)控模塊

      一、簡介

      psutil是一個跨平臺庫,能夠輕松實現(xiàn)獲取系統(tǒng)運行的進程、系統(tǒng)利用率、CPU信息、內(nèi)存信息、磁盤信息、網(wǎng)絡(luò)信息。它主要應(yīng)用于系統(tǒng)監(jiān)控,分析和限制系統(tǒng)資源及進程管理。以下是小編實現(xiàn)的網(wǎng)頁版效果。

      二、安裝步驟

      1.開發(fā)環(huán)境:centos 7

      2.開發(fā)語言:python 2.7.5

      3.依賴軟件

      (1).安裝setuptools

      下載地址:https://pypi.python.org/pypi/setuptools

      #wget https://pypi.python.org/packages/xxx/setuptools-xxx.tar.gz

      #tar xf setuptools-xxx.tar.gz

      #cd setuptools-xxx

      #python setup.py install

      (2).安裝pip

      下載地址:https://pypi.python.org/pypi/pip

      #wget https://pypi.python.org/packages/xxx/pip-xxx.tar.gz

      #tar xf pip-xxx.tar.gz

      #cd pip-xxx

      #python setup.py install

      (3).安裝psutil

      #pip install psutil

      4.封裝CPU信息

      info = psutil.cpu_times()

      res = dict(

      user=info.user, # 執(zhí)行用戶進程的時間百分比

      system=info.system, # 執(zhí)行內(nèi)核進程和中斷的時間百分比

      iowait=info.iowait, # 由于IO等待而使CPU處于idle(空閑)狀態(tài)的時間百分比

      idle=info.idle, # CPU處于idle狀態(tài)的時間百分比

      cpucount1=psutil.cpu_count(), # 獲取CPU的邏輯個數(shù)

      cpucount2=psutil.cpu_count(logical=False) # 獲取CPU的物理個數(shù)

      )

      5.封裝內(nèi)存信息

      meminfo = psutil.virtual_memory()

      swapinfo = psutil.swap_memory()

      res = dict(

      mem=dict(

      total=round(meminfo.total / (1024 ** 3), 2), # 內(nèi)存總數(shù)

      available=round(meminfo.available / (1024 ** 3), 2), # 可用內(nèi)存數(shù)

      percent=meminfo.percent,

      used=round(meminfo.used / (1024 ** 3), 2), # 已使用的內(nèi)存數(shù)

      free=round(meminfo.free / (1024 ** 3), 2), # 空閑內(nèi)存數(shù)

      active=round(meminfo.active / (1024 ** 3), 2), # 活躍內(nèi)存數(shù)

      inactive=round(meminfo.inactive / (1024 ** 3), 2), # 不活躍內(nèi)存數(shù)

      buffers=round(meminfo.buffers / (1024 ** 3), 2), # 緩沖使用數(shù)

      cached=round(meminfo.cached / (1024 ** 3), 2), # 緩存使用數(shù)

      shared=round(meminfo.shared / (1024 ** 3), 2) # 共享內(nèi)存數(shù)

      ),

      swap=dict(

      total=round(swapinfo.total / (1024 ** 3), 2), # 交換分區(qū)總數(shù)

      used=round(swapinfo.used / (1024 ** 3), 2), # 已使用的交換分區(qū)數(shù)

      free=round(swapinfo.free / (1024 ** 3), 2), # 空閑交換分區(qū)數(shù)

      percent=swapinfo.percent,

      sin=round(swapinfo.sin / (1024 ** 3), 2), # 輸入數(shù)

      sout=round(swapinfo.sout / (1024 ** 3), 2) # 輸出數(shù)

      )

      )

      6.封裝磁盤信息

      partinfo = psutil.disk_usage("/") #獲取磁盤完整信息

      diskinfo = dict(

      free=round(partinfo.free / (1024 ** 3), 2), #磁盤剩余量

      used=round(partinfo.used / (1024 ** 3), 2),#磁盤使用量

      total=round(partinfo.total / (1024 ** 3), 2),#磁盤總量

      )

      7.封裝網(wǎng)絡(luò)信息

      allnetio = psutil.net_io_counters() # 獲取網(wǎng)絡(luò)總的IO信息

      onenetio = psutil.net_io_counters(pernic=True) # 輸出每個網(wǎng)絡(luò)接口的IO信息

      res = dict(

      allnetio=dict(

      bytes_sent=allnetio.bytes_sent, # 發(fā)送字節(jié)數(shù)

      bytes_recv=allnetio.bytes_recv, # 接受字節(jié)數(shù)

      packets_sent=allnetio.packets_sent, # 發(fā)送數(shù)據(jù)包數(shù)

      packets_recv=allnetio.packets_recv, # 接受數(shù)據(jù)包數(shù)

      errin=allnetio.errin,

      errout=allnetio.errout,

      dropin=allnetio.dropin,

      dropout=allnetio.dropout

      ),

      onenetio=[

      dict(

      name=v[0],

      bytes_sent=v[1].bytes_sent, # 發(fā)送字節(jié)數(shù)

      bytes_recv=v[1].bytes_recv, # 接受字節(jié)數(shù)

      packets_sent=v[1].packets_sent, # 發(fā)送數(shù)據(jù)包數(shù)

      packets_recv=v[1].packets_recv, # 接受數(shù)據(jù)包數(shù)

      errin=v[1].errin,

      errout=v[1].errout,

      dropin=v[1].dropin,

      dropout=v[1].dropout

      )

      for v in onenetio.iteritems()

      ]

      )

      8.用戶信息

      usersinfo = psutil.users() # 當(dāng)前登錄系統(tǒng)的用戶信息

      res = dict(

      usersinfo=[

      dict(

      name=v.name, # 當(dāng)前登錄用戶名

      terminal=v.terminal, # 打開終端

      host=v.host, # 登錄IP地址

      started=datetime.datetime.fromtimestamp(v.started).strftime("%Y-%m-%d %H:%M:%S") # 登錄時間

      )

      for v in usersinfo

      ],

      boottime=datetime.datetime.fromtimestamp(psutil.boot_time()).strftime("%Y-%m-%d %H:%M:%S") # 開機時間

      )

      9.進程信息

      pidsinfo = psutil.pids() # 列出所有進程pid

      pinfo = map(lambda v: psutil.Process(v), pidsinfo) # 實例化進程狀態(tài)

      res = dict(

      pinfo=[

      dict(

      pid=v[0], # 進程pid

      name=v[1].name(), # 進程名稱

      exe=v[1].exe(), # 進程bin路徑

      cwd=v[1].cwd(), # 進程工作目錄絕對路徑

      status=v[1].status(), # 進程狀態(tài)

      create_time=datetime.datetime.fromtimestamp(v[1].create_time()).strftime("%Y-%m-%d %H:%M:%S"),

      # 進程創(chuàng)建時間

      uids=dict(

      real=v[1].uids().real,

      effective=v[1].uids().effective,

      saved=v[1].uids().saved,

      ), # 進程uid信息

      gids=dict(

      real=v[1].gids().real,

      effective=v[1].gids().effective,

      saved=v[1].gids().saved,

      ), # 進程gid信息

      cpu_times=dict(

      user=v[1].cpu_times().user, # 用戶cpu時間

      system=v[1].cpu_times().system, # 系統(tǒng)cpu時間

      ), # 進程cpu時間

      cpu_affinity=v[1].cpu_affinity(), # 進程cpu親和度

      memory_percent=round(v[1].memory_percent(), 2), # 進程內(nèi)存利用率

      memory_info=dict(

      rss=v[1].memory_info().rss, # 進程內(nèi)存rss信息

      vms=v[1].memory_info().vms # 進程內(nèi)存vms信息

      ), # 進程內(nèi)存信息

      io_counters=dict(

      read_count=v[1].io_counters().read_count, # 讀IO數(shù)

      write_count=v[1].io_counters().write_count, # 寫IO數(shù)

      read_bytes=v[1].io_counters().read_bytes, # IO讀字節(jié)數(shù)

      write_bytes=v[1].io_counters().write_bytes, # IO寫字節(jié)數(shù)

      ), # 進程IO信息

      num_threads=v[1].num_threads(), # 進程開啟的線程數(shù)

      # connections = v[1].connections()#打開進程socket的namedutples列表

      )

      for v in zip(pidsinfo, pinfo)

      ]

      )

      res["pinfo"] = sorted(res["pinfo"], key=lambda v: v["memory_percent"], reverse=True)


    局域網(wǎng)python相關(guān)文章:

    1.局域網(wǎng)python快速上手

    2.怎么搭建局域網(wǎng)YUM服務(wù)器

    3.局域網(wǎng)怎么搭建YUM服務(wù)器

    4.網(wǎng)絡(luò)管理員無工作經(jīng)驗簡歷怎么寫

    5.遠(yuǎn)程操作Mysql數(shù)據(jù)庫

    2900375