學習網站彙總

1.C++:

2 Linux:

linux監控m命令整理

2.1 操作系統:

2.1.1 CPU、內存、IO監控

python->psutil模塊介紹
進程監控腳本
python實現內存監控
python實現linux進程性能消耗

2.2 計算機網絡:

3 數據結構:

4 數據庫:

5 shell:

top詳解
linux系統監控shell腳本

https://blog.csdn.net/weixin_33881041/article/details/87550815
https://www.cnblogs.com/feeland/p/4514771.html

demo

http://code.sangfor.org/SIP/SIS/sip-master/commit/2388299c6b11be655a8f54e0fb887fd3cb9f14a9

{
  "CPU":
  {
    "period":
    {
      "val":10000,
      "path":"/sys/fs/cgroup/cpu/g1/cpu.cfs_period_us"
    },
    "quota":
    {
      "val":5000,
      "path":"/sys/fs/cgroup/cpu/g1/cpu.cfs_quota_us"
    },
    "task_path":"/sys/fs/cgroup/cpu/g1/cgroup.procs"
  },
  "MEMORY":
  {
    "limit":
    {
      "val":2147483648,
      "path":"/sys/fs/cgroup/memory/g1/memory.limit_in_bytes"
    },
    "soft_limit":
    {
      "val":2147483648,
      "path":"/sys/fs/cgroup/memory/g1/memory.soft_limit_in_bytes"
    },
    "oom_control":
    {
      "val":1,
      "path":"/sys/fs/cgroup/memory/g1/memory.oom_control"
    },
    "task_path":"/sys/fs/cgroup/memory/g1/tasks"
  },
  "CPUSET":
  {
    "cpus":
    {
      "val":"0-1",
      "path":"/sys/fs/cgroup/cpuset/g1/cpuset.cpus"
    },
    "mems":
    {
      "val":"1",
      "path":"/sys/fs/cgroup/cpuset/g1/cpuset.mems"
    },
    "task_path":"/sys/fs/cgroup/cpuset/g1/tasks"
  }
}


import os
import json

ID_PATH = "/data/czk/project/id.txt"
CONFIG_PATH = "/data/czk/project/con.json"
PROCESS_KEYWORD = "highcpu"
class limit:
  fd_con = 1
  cresult = ''
  id_path = ID_PATH
  config_path = CONFIG_PATH
  process_keyword = PROCESS_KEYWORD

  def __init__(self):
    '''
    construct function
    '''
    with open(self.config_path,'r') as self.fd_con:
      self.cresult=json.load(self.fd_con)

  def getpidtofile(self):
    '''
    get process_keyword PID and dup to file
    '''
    os.system("ps -ef|grep %s|grep -v 'grep'|awk '{print $2}' > %s" %(self.process_keyword,self.id_path))

  def loadcpu_con(self):
    '''
    loading CPU configuration
    '''
    try:
      os.system("echo %s > %s" %(self.cresult["CPU"]["period"]["val"],self.cresult["CPU"]["period"]["path"]))
      os.system("echo %s > %s" %(self.cresult["CPU"]["quota"]["val"],self.cresult["CPU"]["quota"]["path"]))
    except IOError:
      print "cpu load system called error"
    else:
      pass

  def loadmemory_con(self):
    '''
    loading MEMORY configuration
    '''
    try:
      os.system("echo %s > %s" %(self.cresult["MEMORY"]["limit"]["val"],self.cresult["MEMORY"]["limit"]["path"]))
      os.system("echo %s > %s" %(self.cresult["MEMORY"]["soft_limit"]["val"],self.cresult["MEMORY"]["soft_limit"]["path"]))
      os.system("sudo sh -c 'echo %s >> %s'" %(self.cresult["MEMORY"]["oom_control"]["val"],self.cresult["MEMORY"]["oom_control"]["path"]))
    except IOError:
      print "memory load system called error"
    else:
      pass

  def loadcpuset_con(self):
    '''
    loading CPUSET configuration
    '''
    try:
      os.system("echo %s > %s" %(self.cresult["CPUSET"]["cpus"]["val"],self.cresult["CPUSET"]["cpus"]["path"]))
      #os.system("echo %s > %s" %(self.cresult["CPUSET"]["mems"]["val"],self.cresult["CPUSET"]["mems"]["path"]))
    except IOError:
      print "cpuset load system called error"
    else:
      pass

  def limitcpu(self):
    '''add PID to process cpu Cgroup'''
    try:
      self.loadcpu_con()
      fd = open(self.id_path,'r')
      while True:
        line = fd.readline()
        if line:
          os.system("echo %d > %s" %(int(line),self.cresult["CPU"]["task_path"]))
        else:
          break
    except IOError:
      print "Error(limitcpu): no such file or open failed"
    else:
      fd.close()

  def limitmemory(self):
    '''add PID to process memory Cgroup'''
    try:
      self.loadmemory_con()
      fd = open(self.id_path,'r')
      while True:
        line = fd.readline()
        if line:
          os.system("echo %d > %s" %(int(line),self.cresult["MEMORY"]["task_path"]))
        else:
          break
    except IOError:
      print "Error(limitmemory): no such file or open failed"
    finally:
      fd.close()

  def limitcpuset(self):
    "add PID to process memory Cgroup"
    try:
      self.loadcpuset_con()
      fd = open(self.id_path,'r')
      while True:
        line = fd.readline()
        if line:
          os.system("echo %s > %s" %(line,self.cresult["CPUSET"]["task_path"]))
        else:
          break
    except IOError:
      print "Error(limitcpuset): no such file or open failed"
    finally:
      fd.close()

  def limitcputest(self):
    self.getpidtofile()
    self.limitcpu()
    self.limitcpuset()

  def limitmemorytest(self):
    self.getpidtofile()
    self.limitmemory()

  def __del__(self):
    self.fd_con.close()

if __name__ == '__main__':
  try:
    limit = limit()
    limit.limitcputest()
  except ZeroDivisionError as err:
    print('Handling run-time error:', err)

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章