python實現巡檢系統(solaris)樣本_python

來源:互聯網
上載者:User

使用python + shell 編寫,是一個簡易solaris系統巡檢程式

複製代碼 代碼如下:

#!/usr/bin/python -u
#-*- coding:utf-8 -*-
'''
程式:solaris_status.py
author: gyh9711
功能:
 系統狀態資訊擷取

語言: 
 sh + python

注意:
 部分調用命令需要用到root許可權

測試情況:
 系統版本:solaris10 系統測試ok 
 測試伺服器型號:sun 6900 6800 v445 v440 M3000 M5000
內容:

'''
import time
import sys
import os
import re
# import pprint

def getNow():
 return time.strftime("%Y-%m-%d %H:%M:%S",time.localtime())

def puts(mess,_type="print"):

 if _type == "log":
  sys.stdout.write("[%s] %s\n" % (getNow(),mess))
 else:
  sys.stdout.write("%s\n" % mess)
 #pprint.pprint(mess)

def getCommand(cmd):
 return [t.rstrip() for t in os.popen(cmd).readlines()]

#通過系統狀態資訊:

#系統基本情況
puts("="*60)
puts("檢查時間 %s" % getNow())
puts("-"*60)
puts("\n系統基本資料")
for i in getCommand("showrev"):
 puts(i)
puts("="*60)
puts("\n")
puts("主機名稱: %s " % getCommand("hostname")[0])
puts("處理器core數: %s " % getCommand("mpstat |awk 'END{print NR-1}'")[0])
puts("系統負載情況: %s " % getCommand("uptime")[0])
puts("系統進程數: %s " % getCommand("ps -ef |wc -l")[0])
puts("使用者運行進程情況")
psAll = getCommand('''ps -ef  |awk '{print $1}' |sort |uniq -c |grep -v "UID"''')
puts("\n%s%s" %("使用者".ljust(20),"進程數".ljust(10)))
puts("-"*60)
for i in psAll:
 _user = re.split(r"\s+",i)[1:]
 #print _user
 puts("%s%s" %(_user[1].ljust(20),_user[0].ljust(10)))
puts("-"*60)

puts("\n")
puts("%s%s%s"%("="*30,"狀態資訊","="*30))
puts("\n")
puts("處理器使用方式: %s %%" % getCommand("vmstat 1 2 |tail -1 |awk '{print 100-$22}'")[0])
puts("記憶體使用量情況: %s %% 總大小:%s [M]" % (getCommand('''vmstat 1 2 |tail -1 |awk '{printf("%d",$5/1024/MEMALL*100);}' MEMALL=`prtconf |grep -i "^memory" |awk '{print $3}'`''')[0],getCommand('''prtconf |grep -i "^memory" |awk '{print $3}''')[0]))
puts("swap使用方式: %s" % (getCommand("swap -s")[0]))
puts("\n")
puts("%s%s%s\n"%("="*30,"磁碟空間使用方式","="*30))

for i in getCommand('''awk '{if($3 != "") {print $3}}' /etc/vfstab |egrep -v "mount|to|-" |xargs df -h'''):
 puts(i)
puts("\n%s%s%s\n"%("="*30,"網路情況","="*30))


puts("網路介面狀態")
for i in getCommand('''for i in `ifconfig -a |egrep "^{hme|qfe|ge|ce|eri|bge|nge|e1000g}" |cut -d: -f1`; do /sbin/dladm show-dev $i; done'''):
 puts(i)
puts("\n網路介面IP及資料進出情況")
for i in getCommand("netstat -in"):
 puts(i)

puts("\n網路介面流量情況")
for i in getCommand('''for i in `ifconfig -a |egrep "^{hme|qfe|ge|ce|eri|bge|nge|e1000g}" |cut -d: -f1`; do netstat -I $i; done'''):
 puts(i)
puts("\n 網路ARP緩衝資訊")
for i in getCommand("netstat -anp"):
 puts(i)
puts("\n")
puts("\n 路由表情況")
for i in getCommand("netstat -rn"):
 puts(i)
puts("\n")

puts("\n%s%s%s\n"%("="*30,"系統主要服務及狀態[svcs]","="*30))
for i in getCommand('''svcs'''):
 puts(i)
puts("\n%s%s%s\n"%("="*30,"服務硬體狀態[prtdiag]","="*30))
for i in getCommand('''prtdiag -v'''):
 puts(i)

# puts(getCommand("showrev"))
#硬體狀態資訊
#puts(getCommand("prtdiag -v"))

#cpu情況
#puts(getCommand("mpstat"))
#puts(getCommand("sar -u"))

#實體記憶體大小
#puts(getCommand('''/usr/sbin/prtdiag -v |grep "^Memory"'''))
#虛擬記憶體
#puts(getCommand('''vmstat 1 2'''))
#puts(getCommand('''swap -s'''))


#=================磁碟IO情況=================
#puts(getCommand('''iostat -xtc'''))  #

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.