python subprocess模块

本文介绍了一个使用Python实现的日志管理系统,该系统支持文件日志、控制台日志和系统日志输出,并能为特定任务(如监测CPLD硬件监控偏移)提供日志记录功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一、

import logging
import sys
import logging.config
import logging.handlers
import subprocess

global log_file
global log_level

log_file = '%s.log' % FUNCTION_NAME
log_level = logging.INFO

class monitor(object):

    alarm = 0
    base_cpld_path = ""
    def __init__(self, log_file, log_level):
    
        """Needs a logger and a logger level."""
        # set up logging to file
        logging.basicConfig(
            filename=log_file,
            filemode='a',   # filemode='a' will append context into logfile, filemode='w' will truncated logfile 
            level=log_level,
            format= '[%(asctime)s] {%(pathname)s:%(lineno)d} %(levelname)s - %(message)s',
            datefmt='%Y-%m-%d %H:%M:%S'
        )
        # set up logging to console
        if log_level == logging.DEBUG:
            console = logging.StreamHandler()
            console.setLevel(log_level)
            formatter = logging.Formatter('%(name)-12s: %(levelname)-8s %(message)s')
            console.setFormatter(formatter)
            logging.getLogger('').addHandler(console)

        sys_handler = logging.handlers.SysLogHandler(address = '/dev/log')
        #sys_handler.setLevel(logging.WARNING)       
        sys_handler.setLevel(logging.INFO)       
        logging.getLogger('').addHandler(sys_handler)

        #logging.debug('SET. logfile:%s / loglevel:%d', log_file, log_level)

	def get_cpld_hwmon_offset(self):
        sysfs = '/sys/class/hwmon/hwmon{0}/name'
        
        offset = -1
        cpld_name = 'xc_cpld'
        for index in range(0, 10):
            filename=sysfs.format(index)
            
            if os.path.exists(filename) and os.path.isfile(filename):
                cmd = "cat " + filename
                proc1 = subprocess.Popen(cmd, shell=True, universal_newlines=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
        
                output = proc1.stdout.readlines()
                errput = proc1.stderr.readlines()
                (out, err) = proc1.communicate()
                
                if proc1.returncode > 0:
                    for line1 in errput:
                        logging.info("ipmitool sdr Error: %s", line1.strip())
                    return None
                
                
                #print("filename:%s  adaptername:%s find result:%s" % (filename, adaptername, adaptername.find(cpld_name)))
                for i in range(len(output)):
                    if output[0].find(cpld_name) >= 0:
                        offset = index       
                        break
                
        return  offset 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值