Python implementation patrol System (Solaris) sample

Source: Internet
Author: User
Tags root access pprint egrep
Written using Python + shell, is a simple Solaris system patrol Program

Copy the 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 access

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 ("\ nthe system basic information")
For I in GetCommand ("Showrev"):
Puts (i)
Puts ("=" *60)
Puts ("\ n")
Puts ("Host name:%s"% GetCommand ("hostname") [0])
Puts ("Processor core number:%s"% GetCommand ("Mpstat |awk ' end{print NR-1} '") [0])
Puts ("System load Condition:%s"% GetCommand ("uptime") [0])
Puts ("Number of system processes:%s"% GetCommand ("Ps-ef |wc-l") [0])
Puts ("User run process condition")
Psall = GetCommand (' ps-ef |awk ' {print $} ' |sort |uniq-c |grep-v ' UID ' ')
Puts ("\n%s%s"% ("user". Ljust (20), "Number of processes". Ljust (10)))
Puts ("-" *60)
For I in Psall:
_user = Re.split (r "\s+", i) [1:]
#print _user
Puts ("%s%s"% (_user[1].ljust), _user[0].ljust (10))
Puts ("-" *60)

Puts ("\ n")
Puts ("%s%s%s"% ("=" *30, "status information", "=" *30))
Puts ("\ n")
Puts ("Processor usage:%s percent"% GetCommand ("Vmstat 1 2 |tail-1 |awk ' {print 100-$22} '") [0])
Puts ("Memory usage:%s Total size:%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 usage:%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 Status")
For I in GetCommand ("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 ingress and egress conditions")
For I in GetCommand ("Netstat-in"):
Puts (i)

Puts ("\ n network interface traffic condition")
For I in GetCommand ("Ifconfig-a |egrep" ^{hme|qfe|ge|ce|eri|bge|nge|e1000g} "|cut-d:-f1 '; do netstat-i $i; Done "):
Puts (i)
Puts ("\ nthe 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.