Python-Based Inspection System (solaris) Example

Source: Internet
Author: User
Tags pprint

Compiled using python + shell, is a simple solaris System Inspection Program

Copy codeThe Code is as follows:
#! /Usr/bin/python-u
#-*-Coding: UTF-8 -*-
'''
Program: solaris_status.py
Author: gyh9711
Function:
System Status Information Retrieval

Language:
Sh + python

Note:
Some invocation commands require the root permission

Test conditions:
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 ()]

# System status information:

# Basic System Information
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 cores: % s" % getCommand ("mpstat | awk 'end {print NR-1} '") [0])
Puts ("system load: % s" % getCommand ("uptime") [0])
Puts ("number of system processes: % s" % getCommand ("ps-ef | wc-l") [0])
Puts ("user running process ")
PsAll = getCommand ('''ps-ef | awk' {print $1} '| sort | uniq-c | grep-v "UID "''')
Puts ("\ n % 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" % (_ user [1]. ljust (20), _ user [0]. ljust (10 )))
Puts ("-" * 60)

Puts ("\ n ")
Puts ("% s" % ("=" * 30, "status information", "=" * 30 ))
Puts ("\ n ")
Puts ("processor usage: % s %" % 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 $3} ''') [0], getCommand (''' prtconf | grep-I "^ memory" | awk '{print $3} ''') [0])
Puts ("swap usage: % s" % (getCommand ("swap-s") [0])
Puts ("\ n ")
Puts ("% s \ n" % ("=" * 30, "disk space usage", "=" * 30 ))

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


Puts ("network interface status ")
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 network interface IP address and data access ")
For I in getCommand ("netstat-in "):
Puts (I)

Puts ("\ n network interface traffic ")
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 Network ARP cache information ")
For I in getCommand ("netstat-anp "):
Puts (I)
Puts ("\ n ")
Puts ("\ n route table condition ")
For I in getCommand ("netstat-rn "):
Puts (I)
Puts ("\ n ")

Puts ("\ n % s \ n" % ("=" * 30, "main system services and status [svcs]", "=" * 30 ))
For I in getCommand ('''svcs '''):
Puts (I)
Puts ("\ n % s \ n" % ("=" * 30, "service hardware status [prtdiag]", "=" * 30 ))
For I in getCommand (''' prtdiag-v '''):
Puts (I)

# Puts (getCommand ("showrev "))
# Hardware status information
# Puts (getCommand ("prtdiag-v "))

# Cpu usage
# Puts (getCommand ("mpstat "))
# Puts (getCommand ("sar-u "))

# Physical memory size
# Puts (getCommand ('''/usr/sbin/prtdiag-v | grep "^ Memory "'''))
# Virtual memory
# Puts (getCommand ('''vmstat 1 2 '''))
# Puts (getCommand ('''swap-S '''))


#========================== Disk I/O ============================
# Puts (getCommand ('''iostat-xtc '''))#

Related Article

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.