Linux server CPU, memory, disk space, load condition view python script

Source: Internet
Author: User
Tags spl python script

[This article is from the Sky Cloud-owned blog Park]

Online search, patchwork, assembled a Python script that can check Linux server CPU usage, memory usage, disk space occupancy, and load conditions.

The script reads as follows:

#-*-coding:utf-8-*--ImportOS, Timelast_worktime=0last_idletime=0defget_cpu ():GlobalLast_worktime, Last_idletime f=open ("/proc/stat","R") Line=""         while  not "CPU" inchLine:line=f.readline () f.close () SPL=line.split (" ") Worktime=int (spl[2]) +int (spl[3]) +int (spl[4]) Idletime=int (spl[5]) Dworktime= (worktime-last_worktime) Didletime= (idletime-last_idletime) rate=float (dworktime)/(didletime+dworktime) Last_worktime=worktime Last_idletime=Idletimeif(last_worktime==0):return0return Ratedefget_mem_usage_percent ():Try: F= Open ('/proc/meminfo','R')         forLineinchF:ifLine.startswith ('memtotal:'): Mem_total= Int (Line.split () [1])            elifLine.startswith ('Memfree:'): Mem_free= Int (Line.split () [1])            elifLine.startswith ('buffers:'): Mem_buffer= Int (Line.split () [1])            elifLine.startswith ('Cached:'): Mem_cache= Int (Line.split () [1])            elifLine.startswith ('swaptotal:'): Vmem_total= Int (Line.split () [1])            elifLine.startswith ('Swapfree:'): Vmem_free= Int (Line.split () [1])            Else:                Continuef.close ()except:        returnNone physical_percent= Usage_percent (Mem_total-(mem_free + Mem_buffer +Mem_cache), mem_total) virtual_percent=0ifVmem_total >0:virtual_percent= Usage_percent ((Vmem_total-vmem_free), vmem_total)returnphysical_percent, Virtual_percentdefusage_percent (use, total):Try: Ret= (float (use)/total) * 100exceptZerodivisionerror:RaiseException ("Error-zero Division ERROR")    returnRetstatvfs= Os.statvfs ('/') Total_disk_space= Statvfs.f_frsize *Statvfs.f_blocksfree_disk_space= Statvfs.f_frsize *Statvfs.f_bfreedisk_usage= (total_disk_space-free_disk_space) * 100.0/Total_disk_spacedisk_usage=Int (disk_usage) Disk_tip="Hard disk space utilization (Max 100%):"+str (disk_usage) +"%"Print(disk_tip) mem_usage=get_mem_usage_percent () mem_usage=Int (mem_usage[0]) Mem_tip="Physical Memory Utilization (Max 100%):"+str (mem_usage) +"%"Print(mem_tip) cpu_usage= Int (get_cpu () *100) Cpu_tip="CPU Usage (max 100%):"+str (cpu_usage) +"%"Print(cpu_tip) load_average=os.getloadavg () Load_tip="system Load (one of three values is more than 3 is high):"+Str (load_average)Print(Load_tip)

Touch a py file on the Linux server and paste the above into it and save it. Run the Python script with the following effect:

Glance. In the future to access the background interface 502, no return situation, in the background server to execute a script to see if this is caused by the problem, is not memory consumption is too high, is not disk full and so on. Facilitates the positioning of background server environment issues in order to contact related development or operations to assist with problem resolution.

Linux server CPU, memory, disk space, load condition view python script

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.