六月丁香五月婷婷,丁香五月婷婷网,欧美激情网站,日本护士xxxx,禁止18岁天天操夜夜操,18岁禁止1000免费,国产福利无码一区色费

學(xué)習(xí)啦 > 學(xué)習(xí)電腦 > 電腦安全 > 局域網(wǎng)安全 > 局域網(wǎng)python封裝linux監(jiān)控模塊

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

時(shí)間: 林澤1002 分享

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

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

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

  一、簡介

  psutil是一個(gè)跨平臺庫,能夠輕松實(shí)現(xiàn)獲取系統(tǒng)運(yùn)行的進(jìn)程、系統(tǒng)利用率、CPU信息、內(nèi)存信息、磁盤信息、網(wǎng)絡(luò)信息。它主要應(yīng)用于系統(tǒng)監(jiān)控,分析和限制系統(tǒng)資源及進(jìn)程管理。以下是小編實(shí)現(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í)行用戶進(jìn)程的時(shí)間百分比

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

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

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

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

  cpucount2=psutil.cpu_count(logical=False) # 獲取CPU的物理個(gè)數(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) # 輸出每個(gè)網(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") # 登錄時(shí)間

  )

  for v in usersinfo

  ],

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

  )

  9.進(jìn)程信息

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

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

  res = dict(

  pinfo=[

  dict(

  pid=v[0], # 進(jìn)程pid

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

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

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

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

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

  # 進(jìn)程創(chuàng)建時(shí)間

  uids=dict(

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

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

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

  ), # 進(jìn)程uid信息

  gids=dict(

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

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

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

  ), # 進(jìn)程gid信息

  cpu_times=dict(

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

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

  ), # 進(jìn)程cpu時(shí)間

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

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

  memory_info=dict(

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

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

  ), # 進(jìn)程內(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ù)

  ), # 進(jìn)程IO信息

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

  # connections = v[1].connections()#打開進(jìn)程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)驗(yàn)簡歷怎么寫

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

2900375