python實現(xiàn)巡檢系統(tǒng)(solaris)示例
使用python + shell 編寫,是一個簡易solaris系統(tǒng)巡檢程序
#!/usr/bin/python -u
#-*- coding:utf-8 -*-
'''
程序:solaris_status.py
author: gyh9711
功能:
系統(tǒng)狀態(tài)信息獲取
語言:
sh + python
注意:
部分調(diào)用命令需要用到root權(quán)限
測試情況:
系統(tǒng)版本:solaris10 系統(tǒng)測試ok
測試服務(wù)器型號:sun 6900 6800 v445 v440 M3000 M5000
內(nèi)容:
'''
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()]
#通過系統(tǒng)狀態(tài)信息:
#系統(tǒng)基本情況
puts("="*60)
puts("檢查時間 %s" % getNow())
puts("-"*60)
puts("\n系統(tǒng)基本信息")
for i in getCommand("showrev"):
puts(i)
puts("="*60)
puts("\n")
puts("主機名稱: %s " % getCommand("hostname")[0])
puts("處理器core數(shù): %s " % getCommand("mpstat |awk 'END{print NR-1}'")[0])
puts("系統(tǒng)負載情況: %s " % getCommand("uptime")[0])
puts("系統(tǒng)進程數(shù): %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),"進程數(shù)".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,"狀態(tài)信息","="*30))
puts("\n")
puts("處理器使用情況: %s %%" % getCommand("vmstat 1 2 |tail -1 |awk '{print 100-$22}'")[0])
puts("內(nèi)存使用情況: %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,"網(wǎng)絡(luò)情況","="*30))
puts("網(wǎng)絡(luò)接口狀態(tài)")
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網(wǎng)絡(luò)接口IP及數(shù)據(jù)進出情況")
for i in getCommand("netstat -in"):
puts(i)
puts("\n網(wǎng)絡(luò)接口流量情況")
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 網(wǎng)絡(luò)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,"系統(tǒng)主要服務(wù)及狀態(tài)[svcs]","="*30))
for i in getCommand('''svcs'''):
puts(i)
puts("\n%s%s%s\n"%("="*30,"服務(wù)硬件狀態(tài)[prtdiag]","="*30))
for i in getCommand('''prtdiag -v'''):
puts(i)
# puts(getCommand("showrev"))
#硬件狀態(tài)信息
#puts(getCommand("prtdiag -v"))
#cpu情況
#puts(getCommand("mpstat"))
#puts(getCommand("sar -u"))
#物理內(nèi)存大小
#puts(getCommand('''/usr/sbin/prtdiag -v |grep "^Memory"'''))
#虛擬內(nèi)存
#puts(getCommand('''vmstat 1 2'''))
#puts(getCommand('''swap -s'''))
#=================磁盤IO情況=================
#puts(getCommand('''iostat -xtc''')) #
相關(guān)文章
手把手教你使用Django + Vue.js 快速構(gòu)建項目
本篇將基于Django + Vue.js,手把手教大家快速的實現(xiàn)一個前后端分離的Web項目。文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-08-08
python如何標(biāo)準化日期時間格式轉(zhuǎn)化成非標(biāo)準化格式
這篇文章主要介紹了python如何標(biāo)準化日期時間格式轉(zhuǎn)化成非標(biāo)準化格式問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-06-06
Python用于學(xué)習(xí)重要算法的模塊pygorithm實例淺析
這篇文章主要介紹了Python用于學(xué)習(xí)重要算法的模塊pygorithm,結(jié)合實例形式簡單分析了pygorithm模塊的功能、算法調(diào)用、源碼獲取、時間復(fù)雜度計算等相關(guān)操作技巧,需要的朋友可以參考下2018-08-08

