Python implementation patrol System (Solaris) sample _python

Source: Internet
Author: User
Tags memory usage pprint egrep

Written using Python + shell, is a simple Solaris system patrol procedure

Copy Code code as follows:

#!/usr/bin/python-u
#-*-Coding:utf-8-*-
'''
Program: solaris_status.py
author:gyh9711
Function:
System State Information Acquisition

Language:
SH + python

Attention:
The partial invoke command requires root permission

Test situation:
System version: SOLARIS10 system test OK
Test server Model: Sun 6900 6800 v445 v440 M3000 M5000
Content:

'''
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 (check time%s% Getnow ())
puts ("-" *60)
puts ("\ n system basic Information")
for I in GetCommand ( "Showrev"):
 puts (i)
puts ("=" *60)
puts ("\ n")
puts ("Host name:%s"% GetCommand ("hostname") [0])
Puts ("Processor core:%s"% GetCommand ("Mpstat |awk ' end{print NR-1} '") [0])
puts ("System load Condition:%s"% GetCommand ("uptime") [0]) puts ("System process:%s"% GetCommand ("Ps-ef |wc-l") [0])
puts ("User run process condition")
Psall = GetCommand ("' ps-ef  |awk ' {pri NT "|sort |uniq-c |grep-v" UID "" "
Puts (" \n%s%s "% (" user ". Ljust (20)," Number of processes ". Ljust))
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)
puts ("-*60)

Puts ("\ n")
Puts ("%s%s%s"% ("=" *30, "status info", "=" *30))
Puts ("\ n")
Puts ("Processor usage:%s%%" GetCommand ("Vmstat 1 2 |tail-1 |awk ' {print 100-$22} '") [0])
Puts ("Memory usage:%s%%%:%s [M]"% (GetCommand (' ' Vmstat 1 2 |tail-1 |awk ' {printf ("%d", $5/1024/memall*100);} ' Memall= ' prtconf |grep-i ' ^memory ' |awk ' {print $} ' ' [0],getcommand ' prtconf |grep-i ' ^memory ' |awk ' {print $} '] [0]))
Puts ("Swap:%s"% (GetCommand ("swap-s") [0])
Puts ("\ n")
Puts ("%s%s%s\n"% ("=" *30, "Disk space usage", "=" *30))

For I in GetCommand (' awk ' {if ($!= ") {print $}} '/etc/vfstab |egrep-v mount|to|-' |xargs df-h '):
Puts (i)
Puts ("\n%s%s%s\n"% ("=" *30, "network condition", "=" *30))


Puts ("Network interface State")
For I in GetCommand (' For i ' Ifconfig-a |egrep ' ^{hme|qfe|ge|ce|eri|bge|nge|e1000g} ' |cut-d:-f1 '; Do/sbin/dladm sh Ow-dev $i; Done ""):
Puts (i)
Puts ("\ n network interface IP and data access")
For I in GetCommand ("Netstat-in"):
Puts (i)

Puts ("\ n Network interface traffic conditions")
For I in GetCommand (' ' For I ' Ifconfig-a |egrep ' ^{hme|qfe|ge|ce|eri|bge|nge|e1000g} ' |cut-d:-f1 '; do netstat-i $i; Done ""):
Puts (i)
Puts ("\ n network arp cache information")
For I in GetCommand ("NETSTAT-ANP"):
Puts (i)
Puts ("\ n")
Puts ("\ n routing Table condition")
For I in GetCommand ("Netstat-rn"):
Puts (i)
Puts ("\ n")

Puts ("\n%s%s%s\n"% ("=" *30, "system main service and status [Svcs]", "=" *30))
For I in GetCommand (' Svcs '):
Puts (i)
Puts ("\n%s%s%s\n"% ("=" *30, "Service hardware status [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 '))


#================= Disk IO situation =================
#puts (GetCommand (' IOSTAT-XTC ')) #

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.