python腳本監控獲取當前Linux操作系統[內存]/[cpu]/[硬盤]/[登錄用戶]


此腳本應用在linux, 前提是需要有python和python的psutil模塊

腳本

#!/usr/bin/env python
# coding=utf-8
import sys
import psutil
import time
import os
time_str =  time.strftime( "%Y-%m-%d", time.localtime( ) )
file_name = "./" + time_str + ".log"

if os.path.exists ( file_name ) == False :
   os.mknod( file_name )
   handle = open ( file_name , "w" )
else :
   handle = open ( file_name , "a" )

if len( sys.argv ) == 1 :
   print_type = 1
else :
   print_type = 2

def isset ( list_arr , name ) :
    if name in list_arr :
       return True
    else :
       return False

print_str = "";
if ( print_type == 1 ) or isset( sys.argv,"mem" )  :
 memory_convent = 1024 * 1024
 mem = psutil.virtual_memory()
 print_str +=  " 內存狀態如下:\n" 
 print_str = print_str + "   系統的內存容量為: "+str( mem.total/( memory_convent ) ) + " MB\n" 
 print_str = print_str + "   系統的內存以使用容量為: "+str( mem.used/( memory_convent ) ) + " MB\n" 
 print_str = print_str + "   系統可用的內存容量為: "+str( mem.total/( memory_convent ) - mem.used/( 1024*1024 )) + "MB\n"
 print_str = print_str + "   內存的buffer容量為: "+str( mem.buffers/( memory_convent ) ) + " MB\n" 
 print_str = print_str + "   內存的cache容量為:" +str( mem.cached/( memory_convent ) ) + " MB\n"

if ( print_type == 1 ) or isset( sys.argv,"cpu" ) :
 print_str += " CPU狀態如下:\n"
 cpu_status = psutil.cpu_times()
 print_str = print_str + "   user = " + str( cpu_status.user ) + "\n" 
 print_str = print_str + "   nice = " + str( cpu_status.nice ) + "\n"
 print_str = print_str + "   system = " + str( cpu_status.system ) + "\n"
 print_str = print_str + "   idle = " + str ( cpu_status.idle ) + "\n"
 print_str = print_str + "   iowait = " + str ( cpu_status.iowait ) + "\n"
 print_str = print_str + "   irq = " + str( cpu_status.irq ) + "\n"
 print_str = print_str + "   softirq = " + str ( cpu_status.softirq ) + "\n" 
 print_str = print_str + "   steal = " + str ( cpu_status.steal ) + "\n"
 print_str = print_str + "   guest = " + str ( cpu_status.guest ) + "\n"

if ( print_type == 1 ) or isset ( sys.argv,"disk" ) :
 print_str +=  " 硬盤信息如下:\n" 
 disk_status = psutil.disk_partitions()
 for item in disk_status :
     print_str = print_str + "   "+ str( item ) + "\n"

if ( print_type == 1 ) or isset ( sys.argv,"user" ) :
 print_str +=  " 登錄用戶信息如下:\n " 
 user_status = psutil.users()
 for item in  user_status :
     print_str = print_str + "   "+ str( item ) + "\n"

print_str += "---------------------------------------------------------------\n"
print ( print_str )
handle.write( print_str )
handle.close()

此時如果直接啟動的話會報錯(因為沒有psutil模塊)

下面安裝psutil模塊

下載

wget https://pypi.python.org/packages/source/p/psutil/psutil-2.1.3.tar.gz

解壓

tar -zxvf psutil-2.1.3.tar.gz

安裝

cd psutil-2.1.3/

python setup.py install

安裝過程中報錯

原因:缺少python-dev的依賴環境, 安裝它

yum -y install python-devel.x86_64

再次安裝psutil

python setup.py install

安裝成功

測試psutil模塊是否安裝成功

[root@localhost psutil-2.1.3]# python
>>> import psutil
>>> psutil.pids()

安裝成功

運行腳本

./system.py 

顯示

[root@localhost test]# ./system.py 
 內存狀態如下:
   系統的內存容量為: 1863 MB
   系統的內存以使用容量為: 445 MB
   系統可用的內存容量為: 1418MB
   內存的buffer容量為: 32 MB
   內存的cache容量為:247 MB
 CPU狀態如下:
   user = 5.9
   nice = 0.0
   system = 8.34
   idle = 1125.86
   iowait = 2.2
   irq = 0.02
   softirq = 0.48
   steal = 0.0
   guest = 0.0
 硬盤信息如下:
   sdiskpart(device='/dev/mapper/VolGroup-lv_root', mountpoint='/', fstype='ext4', opts='rw')
   sdiskpart(device='/dev/sda1', mountpoint='/boot', fstype='ext4', opts='rw')
   sdiskpart(device='/dev/sr0', mountpoint='/mnt/redhat', fstype='iso9660', opts='ro')
 登錄用戶信息如下:
    suser(name='root', terminal='pts/0', host='192.168.145.1', started=1510234880.0)
---------------------------------------------------------------

成功!


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM