免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 4042 | 回复: 6
打印 上一主题 下一主题

为什么logging模块不打印日志 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2017-12-08 14:38 |只看该作者 |倒序浏览
系统centos6.8
Python2.7.13
问题:我写了一个监控服务器性能的模块,在另外一个模块使用任务框架,同时import监控模块,但很多时候模块不打印日志,对logging这块不是很理解,请大神帮忙指导下,谢谢!

host模块代码:
from SSH_D import ssh
import  logging,time
import threading
from logging.config import fileConfig
import ConfigParser

fileConfig('../conf/logging.conf',disable_existing_loggers=0)
logger = logging.getLogger()

conf_read = ConfigParser.ConfigParser()
conf_read.read("../conf/config_host.ini")

class host_status(ssh):
    def cpu(self, cmd):
        '''
        :param cmd: vmstat 1 5 |sed -n '3,$p'|awk '{x = x + $15} END {print x/5}' |awk -F. '{print $1}'
        :return: cpu utilization ratio
        '''
        threshold = int(conf_read.get("DEFAULT","cpu"))
        use_ratio = 100 - float(self.get_reponse(cmd)[1].read().decode().strip())
        # use_ratio = '%.3f' % (use_ratio)

        if use_ratio > threshold:
            logger.warning("%s : cpu used - %s%%" % (self.host, use_ratio))
        else:
            logger.info("%s : cpu used - %s%%" % (self.host, use_ratio))

    def disk(self, cmd):
        '''
        :param cmd: df -h|sed '1d'|awk '{print $NF,$(NF-1)}'
        :return: disk utilization ratio list
        '''
        threshold = int(conf_read.get("DEFAULT","disk"))
        for l in self.get_reponse(cmd)[1].read().decode().strip().split("\n"):
            k, v = l.split()
            v = int(v.replace("%", ""))

            if v > threshold:
                logger.warning("%s : filesystem %s used - %s%%" % (self.host, k, v))
            else:
                logger.info("%s : filesystem %s used - %s%%" % (self.host, k, v))

    def mem(self, cmd):
        '''
        :param cmd: free -m|grep 'buffers/cache'|awk '{print ($3/($3+$4))*100}'
        :return: memory utilization ratio
        '''
        threshold = int(conf_read.get("DEFAULT","mem"))
        use_ratio = float(self.get_reponse(cmd)[1].read().decode().strip())

        if use_ratio > threshold:
            logger.warning("%s : mem use - %s%%" % (self.host, use_ratio))
        else:
            logger.info("%s : mem used - %s%%" % (self.host, use_ratio))

    def iowait(self, cmd):
        '''
        :param cmd: iostat|grep -A 1 'avg-cpu'|grep -v 'avg-cpu'|awk '{print $4}'
        :return: the percentage of time that the CPU or CPUs were idle during which the system had an outstanding disk I/O request.
        '''
        threshold = int(conf_read.get("DEFAULT","iowait"))
        use_ratio = float(self.get_reponse(cmd)[1].read().decode().strip())

        if use_ratio > threshold:
            logger.warning("%s iowait - %s%%" % (self.host, use_ratio))
        else:
            logger.info("%s iowait - %s%%" % (self.host, use_ratio))

    def ioutil(self, cmd):
        '''
        :param cmd: iostat -xndk|grep -A 1 'Device'|grep -v 'Device'|awk '{print $1, $NF}'
        :return:
        '''
        threshold = int(conf_read.get("DEFAULT","ioutil"))
        for l in self.get_reponse(cmd)[1].read().decode().strip().split("\n"):
            k, v = l.split()
            v = float(v)

            if v > threshold:
                logger.warning("%s - %s ioutil - %s%%" % (self.host, k, v))
            else:
                logger.info("%s - %s ioutil - %s%%" % (self.host, k, v))

    def run_thread(self):
        cmd_cpu = "vmstat 1 5 |sed -n '3,$p'|awk '{x = x + $15} END {print x/5}' |awk -F. '{print $1}'"
        cmd_disk = "df -h|sed '1d'|awk '{print $NF,$(NF-1)}'"
        cmd_mem = "free -m|grep 'buffers/cache'|awk '{print ($3/($3+$4))*100}'"
        cmd_iowait = "iostat|grep -A 1 'avg-cpu'|grep -v 'avg-cpu'|awk '{print $4}'"
        cmd_ioutil = "iostat -xndk|grep -A 1 'Device'|grep -v 'Device'|awk '{print $1, $NF}'"
        jobs = [threading.Thread(target=self.cpu, name='cpu', args=(cmd_cpu,)),
                threading.Thread(target=self.disk, name='disk', args=(cmd_disk,)),
                threading.Thread(target=self.mem, name='mem', args=(cmd_mem,)),
                threading.Thread(target=self.iowait, name='iowait', args=(cmd_iowait,)),
                threading.Thread(target=self.ioutil, name='ioutil', args=(cmd_ioutil,))]
        for job in jobs:
            job.start()
            job.join()


调用host模块的job管理模块:
import ConfigParser
import logging
from logging.config import fileConfig
from HOST import host_status
from apscheduler.schedulers.blocking import BlockingScheduler
from NetMonitor import clientip
from MySQLMonitor import db
from Process import process
from PageCheck import pageCheck

fileConfig('../conf/logging.conf',disable_existing_loggers=0)
logger = logging.getLogger()

def host_job():
    '''host status'''
    # Read configuration
    conf_read = ConfigParser.ConfigParser()
    conf_read.read("../conf/config_host.ini")

    if  len(conf_read.sections()):
        # process pool
        num = len(conf_read.sections())

        # Multi process handle
        for section in conf_read.sections():
            # every server in configuration
            if len(conf_read.options(section)):
                server = host_status(conf_read.get(section,"host"),
                                          conf_read.getint(section,"port"),
                                          conf_read.get(section,"user"),
                                          conf_read.get(section,"password"))
                server.run_thread()

系统启动接口模块:
from JOB_Run import job_Scheduler
import logging,sys
from logging.config import fileConfig

# log
fileConfig('../conf/logging.conf',disable_existing_loggers=0)
logger = logging.getLogger()



# add job scheduler
job_name=sys.argv[1]
job_Scheduler(job_name)


现在使用 python27 ../MonApp/startup_job.py host_job >> /dev/null 2>&1 &启动,手动执行可以打印日志,如果系统BlockingScheduler调用则不能打印模块

论坛徽章:
33
荣誉会员
日期:2011-11-23 16:44:17天秤座
日期:2014-08-26 16:18:20天秤座
日期:2014-08-29 10:12:18丑牛
日期:2014-08-29 16:06:45丑牛
日期:2014-09-03 10:28:58射手座
日期:2014-09-03 16:01:17寅虎
日期:2014-09-11 14:24:21天蝎座
日期:2014-09-17 08:33:55IT运维版块每日发帖之星
日期:2016-04-17 06:23:27操作系统版块每日发帖之星
日期:2016-04-18 06:20:00IT运维版块每日发帖之星
日期:2016-04-24 06:20:0015-16赛季CBA联赛之天津
日期:2016-05-06 12:46:59
2 [报告]
发表于 2017-12-09 01:10 |只看该作者
回复 1# 白浪巨沙

  1. fileConfig('../conf/logging.conf',disable_existing_loggers=0)
复制代码

哪有用相对路径的,又没有指定base。
你的程序跑起来,找不到配置了吧?

论坛徽章:
0
3 [报告]
发表于 2017-12-11 17:30 |只看该作者
可以跑起来,能找到路径,我是将相对路径的节点加入到系统环境中的,现在就是日志打印有问题,如:
20171211 14:33:31 INFO: Job "client_job (trigger: interval[0:05:00], next run at: 2017-12-11 14:38:20 CST)" execu
ted successfully
只打印任务是否成功,不具体打印任务过程,我想看到过程情况

论坛徽章:
0
4 [报告]
发表于 2017-12-11 17:30 |只看该作者
可以跑起来,能找到路径,我是将相对路径的节点加入到系统环境中的,现在就是日志打印有问题,如:
20171211 14:33:31 INFO: Job "client_job (trigger: interval[0:05:00], next run at: 2017-12-11 14:38:20 CST)" execu
ted successfully
只打印任务是否成功,不具体打印任务过程,我想看到过程情况

论坛徽章:
0
5 [报告]
发表于 2017-12-15 17:43 |只看该作者
是不是多进程输出同一个日志文件 ?  以前使用这好像有问题  你自己测试看看   

论坛徽章:
11
2015年迎新春徽章
日期:2015-03-04 09:55:282017金鸡报晓
日期:2017-02-08 10:39:4215-16赛季CBA联赛之辽宁
日期:2016-12-15 10:24:1715-16赛季CBA联赛之佛山
日期:2016-11-30 09:04:2015-16赛季CBA联赛之江苏
日期:2016-04-29 15:56:1215-16赛季CBA联赛之同曦
日期:2016-04-12 13:21:182016猴年福章徽章
日期:2016-02-18 15:30:3415-16赛季CBA联赛之山东
日期:2016-02-16 11:37:52每日论坛发贴之星
日期:2016-02-07 06:20:00程序设计版块每日发帖之星
日期:2016-02-07 06:20:0015-16赛季CBA联赛之新疆
日期:2018-01-09 16:25:37
6 [报告]
发表于 2017-12-22 14:47 |只看该作者
回复 5# remark
多线程导致的, 要放到一个队列,由专门线程来写日志才行

论坛徽章:
0
7 [报告]
发表于 2018-03-05 11:46 |只看该作者
好的,谢谢各位,5楼6楼正解
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP