IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> Python知识库 -> python 编程 远程监控进程数据 -> 正文阅读

[Python知识库]python 编程 远程监控进程数据

配置文件:

###############################################################################
# Config file to monitor CHM processes, YAML format
#
# procTask DataFile Record fields:
#  Date Time Node Command PID PCPU VSZ(KB) RSS(KB) StartTime Info
#
# fileTask DataFile Record fields:
#  FileName CreateTime Size(KB) INFO
#
###############################################################################

#common setting for monitor
logLevel: debug         # debug, info, warn, error, fatal, default: debug
logFile: monitor.log    # log filename, default: monitor.log
allProc: "ASM|asm|MGMT|mgmt|APX|apx|d\.bin|cssdagent|cssdmonitor|orarootagent.bin|oraagent.bin|scriptagent.bin|tnslsnr"
asmProc: "ASM|asm"
mgmtProc: "MGMT|mgmt"
apxProc: "APX|apx"
crsProc: "d\.bin|cssdagent|cssdmonitor|orarootagent.bin|oraagent.bin|scriptagent.bin|tnslsnr"

# monitoring process settings
OCSSDProcTask:
  type: processTask     # processTask - process monitor task, fileTask - file monitor task
  exec: true            # true - execute this task, false - don't execute this task, default true
  process: ocssd.bin|ohasd.bin   # process name list to be monitored, seperated by '|', used by grep to select
  nodes: [rws00fys, rws00fyt, rws00fyu, rws00fyv] #optioanl, default is local host
  checkInterval: 1      # interval seconds to check status, default: 1 seconds
  statCount: 5          # count of checkInterval to statistic, default: 5
  cpuThreshold: 10      # cpu usage percent threshold, default: 10.0%
  memThreshold: 409600  # rss memory threshold, in KB, default: 400MB
  DataFile: procmonitor.dat
  actions: ~

# monitoring file settings: use regular expression to match filenames
OCSSDFileTask:
  type: fileTask        # processTask - process monitor task, fileTask - file monitor task
  exec: true            # true - execute this task, false - don't execute this task
  file:                 # file to be monitored
    - '/u01/app/crsusr/diag/crs/<LOCAL_HOST>/crs/trace/ohasd[_0-9]*.trc'
    - '/u01/app/crsusr/diag/crs/<LOCAL_HOST>/crs/trace/ocssd[_0-9]*.trc'
  checkInterval: 60     # interval seconds to check status, default: 60 seconds
  statCount: 5          # count of checkInterval to statistic, default: 5
  wrapTime: 600
  wrapCount: 20         # in wrapInterval seconds, no more than wrapCount files generated
  DataFile: filemonitor.dat
  actions: ~

# monitoring process settings
CHAMProcTask:
  type: processTask     # processTask - process monitor task, fileTask - file monitor task
  exec: false            # true - execute this task, false - don't execute this task
  process: osysmond|ologgerd|mdb_pmon   # process name list to be monitored, seperated by '|', used by grep to select
  nodes: [rws00fys, rws00fyt, rws00fyu, rws00fyv] #optioanl, default is local host
  checkInterval: 1      # interval seconds to check status, default: 1 seconds
  statCount: 5          # count of checkInterval to statistic, default: 5
  cpuThreshold: 10      # cpu usage percent threshold, default: 10.0%
  memThreshold: 409600  # rss memory threshold, in KB, default: 400MB
  DataFile: procmonitor.dat
  actions: ~

CHAMFileTask:
  type: fileTask        # processTask - process monitor task, fileTask - file monitor task
  exec: false            # true - execute this task, false - don't execute this task
  file:                 # file to be monitored
    - '/u01/app/crsusr/diag/crs/<LOCAL_HOST>/crs/trace/osysmond[_0-9]*.trc'
    - '/u01/app/crsusr/diag/crs/<LOCAL_HOST>/crs/trace/ologgerd[_0-9]*.trc'
    - '/u01/app/12.2.0/grid/crf/db/<LOCAL_HOST>/json/jsondump.log'
  checkInterval: 60     # interval seconds to check status, default: 60 seconds
  statCount: 5          # count of checkInterval to statistic, default: 5
  wrapTime: 600
  wrapCount: 20         # in wrapInterval seconds, no more than wrapCount files generated
  DataFile: filemonitor.dat
  actions: ~

运行代码

#!/usr/bin/env python

"""
This script runs a sequence of commands on a remote host using SSH, it will monitor assigned 
processes, it will report a warning if the process reboot or quit, or CPU/MEMORY exceed assigned
limits. 
It also perform some simple system health checks such as open file descriptors number, 
port status and memory usage etc.

"""

import sys
import os
import platform
import time
from datetime import datetime,timedelta
import getopt
import getpass
import logging
import traceback
import subprocess
import yaml
import socket
import signal
import time
import threading 
import re

global LOGGER, OS_NAME

TIME_OUT                    = 20
DEFAULT_LOGFILE             = "ractest.log";
DEFAULT_LOGLEVEL            = "debug"

EXPR_NAME_ESC               = '\\'
EXPR_NAME_BEGIN             = '<'
EXPR_NAME_END               = '>'

#platform.system()
#platform.release()
SYSTEM_LINUX                = 'Linux'
SYSTEM_SOLARIS              = 'SunOS'
SYSTEM_WINDOWS              = 'Windows'
SYSTEM_MACOS                = 'Darwin'
SYSTEM_AIX                  = 'AIX'
SYSTEM_HPUX                 = 'HPUX'

LOCAL_HOST                  = socket.gethostname().split(".")[0]

g_bRunApplication           = True

def findString(file_name, value):
    file1 = file(file_name, 'r')
    for line in file1:
        if value in line:
            return line
        else:
            return None

def find_in_list(array_list, value):
    index = []
    i = 0
    for l in array_list:
        if cmp(l, value) == 0:
            index.append(i)
        i = i + 1
    return index    

def strpTimeDelta(strDayTime):
    '''
    translate a format string to a timedelta object.
    strDayTime: string, format: [days-][hour:]minutes:seconds
    return a timedelta object by format string
    '''
    listDayTime = strDayTime.split('-')
    if (len(listDayTime) > 1):
        nDays = int(listDayTime[0])
        listTime = listDayTime[1].split(':')
    else:
        nDays = 0
        listTime = listDayTime[0].split(':')

    if (len(listTime) > 2):
        nHour = int(listTime[0])
        nMin = int(listTime[1])
        nSec = int(listTime[2])
    else:
        nHour = 0
        nMin = int(listTime[0])
        nSec = int(listTime[1])

    return timedelta(days=nDays, hours=nHour, minutes=nMin, seconds=nSec)

# Run command over ssh or in local shell
def runSSHOneCmd(host, command):
    if (host != LOCAL_HOST):
        ssh_cmd = "ssh %s %s" % (host, command)
    else:
        ssh_cmd = "%s" % (command)

    proc = subprocess.Popen(ssh_cmd, shell=True, stderr=subprocess.STDOUT, stdout=subprocess.PIPE)
    output = proc.communicate()
    #print "runSSHOneCmd: %s\nOutput: \n%s" % (ssh_cmd, output[0])
    lines = output[0].split('\n')
    
    #remove blank line
    if len(lines) > 1 and not lines[-1].split():
        lines.pop()

    return lines      

def readPropertyFile(strFileName, sep='=', comment_char='#'):
    """
    Read the file passed as parameter as a properties file.
    """
    dictProps = {}
    with open(strFileName, "rt") as fPorpFile:
        for strLine in fPorpFile:
            strLine = strLine.strip()
            if strLine and not strLine.startswith(comment_char):
                tupKeyValue = strLine.split(sep)
                strKey = tupKeyValue[0].strip()
                strValue = sep.join(tupKeyValue[1:]).strip().strip('"') 
                dictProps[strKey] = strValue 
    return dictProps

def parseString(strExp, dict = None):
    """
    Parse the expressions in strExp, find expression value in dict and replace the expression with the value
    Example: 
    if LOCAL_HOST = "rws00fys", 
    /u01/app/crsusr/diag/<LOCAL_HOST>/... 
    will be parsed as:
    /u01/app/crsusr/diag/rws00fys/...
    """
    if (dict is None):
        dict = {"LOCAL_HOST":LOCAL_HOST}

    #print "parseString0: %s, dict=%s" % (strExp, dict)

    nNormal = 0
    strResult = ""
    # find the first begin mark
    nBegin = strExp.find(EXPR_NAME_BEGIN, nNormal)

    while (nBegin >= 0):
        if (nBegin >= 0 and strExp[nBegin-1] == EXPR_NAME_ESC):
            # escape of begin mark
            strResult += strExp[nNormal:nBegin-1]   # add string before ESC to result
            strResult += EXPR_NAME_BEGIN            # add escaped EXPR_NAME_BEGIN to result
            #print "parseString1: RESULT=%s" % (strResult)

            nNormal = nBegin+1
            nBegin = strExp.find(EXPR_NAME_BEGIN, nNormal)
            continue;

        # find end mark
        nName = nBegin+1
        strName = ""
        nEnd = strExp.find(EXPR_NAME_END, nName)
        while (nEnd > 0 and strExp[nEnd-1] == EXPR_NAME_ESC):
            # escape of end mark
            strName += strExp[nName:nEnd-1]         # add string before ESC to name
            strName += EXPR_NAME_END                # add escaped EXPR_NAME_END to name
            nName = nEnd+1
            nEnd = strExp.find(EXPR_NAME_END, nName)

        if (nEnd < 0):
            break;

        strName += strExp[nName:nEnd]
        strName = strName.replace(EXPR_NAME_ESC+EXPR_NAME_BEGIN, EXPR_NAME_BEGIN)
        strName = strName.replace(EXPR_NAME_ESC+EXPR_NAME_END, EXPR_NAME_END)
        strResult += strExp[nNormal:nBegin]
        #print "parseString2: NAME=%s" % (strName)

        if (strName is not None and len(strName) > 0):
            if strName not in dict:
                #print "parseString: '%s' not found." % (strName)
                #strResult += EXPR_NAME_BEGIN + strName + EXPR_NAME_END
                strResult += strName
            else:
                strResult += str(dict[strName])

        #print "parseString3: RESULT=%s" % (strResult)

        # find next begin mark
        nNormal = nEnd+1
        nBegin = strExp.find(EXPR_NAME_BEGIN, nNormal)

    # end of string parse
    strResult += strExp[nNormal:]
    #print "parseString4: RESULT=%s" % (strResult)
    strResult = strResult.replace(EXPR_NAME_ESC+EXPR_NAME_BEGIN, EXPR_NAME_BEGIN)
    strResult = strResult.replace(EXPR_NAME_ESC+EXPR_NAME_END, EXPR_NAME_END)
    #print "parseString5: RESULT=%s" % (strResult)
    return strResult

def sigHandler(sig, frame):
    global g_bRunApplication
    logging.info("sigHandler: Caught signal: %s", sig)
    g_bRunApplication = False
    logging.info("sigHandler: g_bRunApplication: %s", g_bRunApplication)

def exitWithUsage():
    print globals()['__doc__']
    os._exit(0)

def initLogging(nLevel = logging.DEBUG, strLogFile = DEFAULT_LOGFILE):
    # create logger
    strScriptDir = os.path.dirname(os.path.realpath(__file__))
    #time_now = time.strftime("%Y-%m-%d_%H-%M-%S", time.localtime())
    #strLogFileName = os.path.join(strScriptDir, 'monitor_' + time_now + '.log')
    strLogFileName = os.path.join(strScriptDir, strLogFile)
    #strFmt = "%(asctime)-15s : %(levelname)s: %(filename)s-%(lineno)d: %(message)s"
    strFmt = "%(asctime)s : %(levelname)s: %(message)s"
    print "initLogging:\n filename = %s\n level = %d DEBUG=%d\n format = %s" % (strLogFileName, nLevel, logging.DEBUG, strFmt)
    logging.basicConfig(filename = strLogFileName, level = nLevel, format = strFmt)

###############################################################################
# class RACAppBase

class RACAppBase():
    def __init__(self):
        self.strLogFile = DEFAULT_LOGFILE
        self.nLogLevel = logging.DEBUG
        self.dictTask = {}

    def readConfig(self, strFileName):
        """
        readConfig: read config items from config file file_name
        config items:
          outFile:    log file name, default is DEFAULT_PROC_DATAFILE
          nodes:      node list to be monitored, if None, monitor local node
          process:    process command name list to be monitored, seperated by '|', can't be empty
        """
        with open(strFileName, 'r') as stream:
            try:
                yamlObj = yaml.load(stream)
            except yaml.YAMLError as ex:
                print(ex)
                return False;

        self.strLogFile = yamlObj.get("logFile", DEFAULT_LOGFILE)
        strLogLevel = yamlObj.get("logLevel", DEFAULT_LOGLEVEL)
        if (strLogLevel == "info"):
            self.nLogLevel = logging.INFO
        elif (strLogLevel == "warn"):
            self.nLogLevel = logging.WARN
        elif (strLogLevel == "error"):
            self.nLogLevel = logging.ERROR
        elif (strLogLevel == "fatal"):
            self.nLogLevel = logging.FATAL
        else:
            self.nLogLevel = logging.DEBUG

        print "Read Config file OK:\n logFile = %s\n logLevel = %s(%d)\n" % (self.strLogFile, strLogLevel, self.nLogLevel)
        return True

    def StartAllTasks(self):
        logging.info("Starting all tasks ...")
        logging.info("Start all tasks OK.")

    def StopAllTasks(self):
        logging.info("Stopping all tasks ...")
        logging.info("Stop all tasks OK.")


#def main():
if __name__ == "__main__":
    #global g_bRunMonitor
    appTest = RACAppBase()

    #global g_bRunApplication
    configFile = "process.yml"

    ######################################################################
    ## Parse the options, arguments, get ready, etc.
    ######################################################################
    try:
        optlist, args = getopt.getopt(sys.argv[1:], 'h?f:', ['help','h','?'])
    except Exception, e:
        print str(e)
        exitWithUsage()
        
    if len(args) > 1:
        exitWithUsage()

    options = dict(optlist)
    if [elem for elem in options if elem in ['-h','--h','-?','--?','--help']]:
        print "Help:"
        exitWithUsage()

    if '-f' in options:
        configFile = options['-f']
        #print "filename=", configFile
        if not configFile:
            print "The value of \'-f\' is empty, please enter a valid file name!!!"
            sys.exit(0)

    print "config file: %s" % (configFile)
    if not appTest.readConfig(configFile):
        print "Read config file '%s' failed!" % (configFile)
        sys.exit(-1)

    initLogging(appTest.nLogLevel, appTest.strLogFile)
    signal.signal(signal.SIGTERM, sigHandler)
    signal.signal(signal.SIGINT, sigHandler)
    logging.info("Begin testing racutil.")

    appTest.StartAllTasks()

    while g_bRunApplication:
        time.sleep(0.5)

    appTest.StopAllTasks()

    logging.info("End test racutil.")

if __name__ == "__main__":
??? #global g_bRunMonitor
??? appTest = RACAppBase()

??? #global g_bRunApplication
??? configFile = "process.yml"

??? ######################################################################
??? ## Parse the options, arguments, get ready, etc.
??? ######################################################################
??? try:
??????? optlist, args = getopt.getopt(sys.argv[1:], 'h?f:', ['help','h','?'])
??? except Exception, e:
??????? print str(e)
??????? exitWithUsage()
?????? ?
??? if len(args) > 1:
??????? exitWithUsage()

??? options = dict(optlist)
??? if [elem for elem in options if elem in ['-h','--h','-?','--?','--help']]:
??????? print "Help:"
??????? exitWithUsage()

??? if '-f' in options:
??????? configFile = options['-f']
??????? #print "filename=", configFile
??????? if not configFile:
??????????? print "The value of \'-f\' is empty, please enter a valid file name!!!"
??????????? sys.exit(0)

?

  Python知识库 最新文章
Python中String模块
【Python】 14-CVS文件操作
python的panda库读写文件
使用Nordic的nrf52840实现蓝牙DFU过程
【Python学习记录】numpy数组用法整理
Python学习笔记
python字符串和列表
python如何从txt文件中解析出有效的数据
Python编程从入门到实践自学/3.1-3.2
python变量
上一篇文章      下一篇文章      查看所有文章
加:2021-10-17 11:57:20  更:2021-10-17 11:58:22 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年11日历 -2024/11/15 20:49:27-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码