Monitor Linux system resources and the CPU memory consumption of custom processes.

Source: Internet
Author: User
Tags pprint

1 #Coding=utf82 Import Time3 ImportPsutil4  fromPprintImportPprint5 6  fromLogger_untilImportLoggeruntil7Logger = Loggeruntil (name="Monitor"). Getlog (logfilename='Monitor.log', loglevel=2, Add_streamhandler=1)8 9Need_monitor_procces_names = [Ten 'touna0627.py', One 'dailiip.py', A 'Redis-server', - 'Mongod', - ] the  -  - classMonitor (object): -     def __init__(self): +Self.specified_process_list =self.get_specified_process () -  +     defPrint_all_cmd_lines (self): A         Pass at  -     defget_specified_process (self): -All_pids =Psutil.pids () -Process_list = [] -          forPidinchAll_pids: -p =Psutil. Process (PID) inP_cmdline =P.cmdline () -              forArgxinchP_cmdline: to                  forNameinchNeed_monitor_procces_names: +                     ifArgx.find (name) >-1: -                         ifP.status ()! ='stopped': the process_list.append (P) *  $P_pid_set =set ()Panax NotoginsengProcess_list2 = [] -          forPinchprocess_list: the             ifP.pid not inchP_pid_set: + process_list2.append (P) A P_pid_set.add (p.pid) the         returnProcess_list2 +  -  $     defMonitor_system (self): $ psutil.cpu_percent () -Time.sleep (1) -MEM =psutil.virtual_memory () the  -Mem_total = mem.total/1000000WuyiMem_available =mem.available/1000000 themem_percent = str (mem.percent) +'%' -  WuCpu_count =Psutil.cpu_count () -Cpu_percent =psutil.cpu_percent () About  $msg ='The total native memory is: {0}m, native available memory is: {1}m, Native memory usage is: {2}, native CPU cores are: {3}, native CPU usage is: {4}'. Format (mem_total, mem_available, mem_percent,cpu_count,cpu_percent) - logger.info (msg) -  -  A     defmonitor_specified_process (self): +          forPinchself.specified_process_list: the p.cpu_percent (None) -Time.sleep (1) $          forPinchself.specified_process_list: the             #p = psutil. Process (0) the             """: Type:p Sutil. Process""" theCmdline_str =' '. Join (P.cmdline ()). Ljust (60,' ') theP_cpu_percent_str = str (round (P.cpu_percent (), 2) +'%' -P_memory_percent_str = str (round (P.memory_percent (), 2) +'%' inP_strated_time = Time.strftime ('%y-%m-%d%h:%m:%s', Time.localtime (P.create_time ())) theP_pid_str =Str (p.pid) the             Printp.status (), str (p.status ()) About  themsg ='Process'+ Cmdline_str +'The PID is:'+ P_pid_str +'The CPU utilization is:'+ P_cpu_percent_str +'Memory Utilization is:'+P_memory_percent_str the+'the start time of the process is:'+P_strated_time the  + logger.info (msg) -  the Bayi defmonitoring (): the      while1: theMonitor =Monitor () - Monitor.monitor_system () - monitor.monitor_specified_process () the         Print '\ n' theTime.sleep (10) the  the  - if __name__=="__main__": theMonitoring ()



Monitor system resources and custom processes for Linux.

Fill in the process you want to monitor, as long as the PS-EF command contains strings in Need_monitor_procces_names, and you can monitor the Java Njinx.

Monitor Linux system resources and the CPU memory consumption of custom processes.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.