#!/usr/bin/python # Description: Check Solaris processes by type # Results are displayed on stdout or redirected to a file # # If you obtain this script via Web, convert it to Unix format. For example: # dos2unix -n Solaris-check-ps.py.txt Solaris-check-ps.py # # Last Update: 23 June 2014 # Designed by: Dusan U. Baljevic (dusan.baljevic@ieee.org) # Coded by: Dusan U. Baljevic (dusan.baljevic@ieee.org) # # Copyright 2014 Dusan Baljevic # # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation, either version 3 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program. If not, see . import subprocess import shlex import sys import os import getopt import platform os.environ['PATH'] = "/bin:/usr/bin:/sbin:" + os.environ['PATH'] def main(argv): try: opts, args = getopt.getopt(argv,"h") except getopt.GetoptError: print 'Solaris-check-ps.py [-h]' sys.exit(1) for opt, arg in opts: if opt == '-h': print 'Solaris-check-ps.py [-h]' sys.exit() if __name__ == "__main__": main(sys.argv[1:]) psys = platform.system() prel = platform.release() # Command to run to list all processes # if psys == "SunOS": if prel > "5.10": psext = "Z" myprocess = "ps -elf" myprocess += psext # Initialise lists for each process type # lsleep = [] lruncpu = [] lrun = [] lstop = [] lpage = [] lzombie = [] linecnt = 0 p = subprocess.Popen(myprocess, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT) for line in p.stdout.readlines(): if linecnt == 0: lrun.append(line) lruncpu.append(line) lstop.append(line) lpage.append(line) lsleep.append(line) lzombie.append(line) linecnt += 1 else: psent = shlex.split(line) if psent[1].startswith('S') == True: lsleep.append(line) if psent[1].startswith('O') == True: lruncpu.append(line) if psent[1].startswith('R') == True: lrun.append(line) if psent[1].startswith('T') == True: lstop.append(line) if psent[1].startswith('W') == True: lpage.append(line) if psent[1].startswith('Z') == True: lzombie.append(line) retval = p.wait() if retval == 0: if len(lsleep) > 1: print "INFO: Processes in interruptible sleep" print ''.join(lsleep) if len(lrun) > 1: print "INFO: Processes running on processor" print ''.join(lrun) if len(lruncpu) > 1: print "INFO: Processes on run queue" print ''.join(lruncpu) if len(lpage) > 1: print "INFO: Processes waiting for CPU usage to drop to CPU-caps enforced limits" print ''.join(lpage) if len(lstop) > 1: print "INFO: Processes stopped by job control signal or being traced" print ''.join(lstop) if len(lzombie) > 1: print "INFO: Defunct (zombie) processes" print ''.join(lzombie) else: print "ERROR: Command failed:", myprocess exit(1) exit(0)