threading和queue监控两个log的python脚本

# coding:utf-8

__author__ = ‘admin‘

# --------------------------------

# Created by admin  on 2015/5/29.

# ---------------------------------

#/usr/bin/python

import redis,re,subprocess,threading,Queue

host="192.168.8.137"

wiki_log="/home/nginx/logs/wiki.log"

other_log="/home/nginx/logs/other.log"

_popen={}

queue=Queue.Queue()

#获取log的一行数据

def get_one_line(logpath):

"get one line from log,logpath mast be a str"

global  _popen,state

if not _popen.has_key(logpath):

_popen[logpath]=subprocess.Popen("tail -f %s"%(logpath,),stdout=subprocess.PIPE,stderr=subprocess.PIPE,shell=True)

a=_popen[logpath].stdout.readline()

return a

#获取一次访问的IP

def get_guest_ip_info(log):

"get guest ip,this fun return a string"

while 1:

info=get_one_line(log)

ip=re.match("^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}",info)

return ip.group()

def ip_count():

global  queue

r = redis.Redis(host=host, port=6379, db=0)

while 1:

item=queue.get()

if not r.exists(item[0]):

r.zadd(item[0],item[1],1)

else:

#直接写也可以,不存在key值的话会自动创建

r.zincrby(item[0],item[1],1)

def start_thread(target,args):

"start a theard"

t=threading.Thread(target=target,args=args)

#加上setDaemon(True)的话,主线程不等待子线程结束就关闭所有线程,所有在子线程里print调试的内容都不会再前台显示出来

# t.setDaemon(True)

t.start()

def put_ip(log_name,log):

global  queue

while 1:

ip=get_guest_ip_info(log)

queue.put([log_name,ip])

def handle():

#为避免put_ip陷入死循环(while 1:)无法执行后面的代码,所以每个函数都用一个线程单独运行。

#target目标函数不能带括号,args为空时,用()表示,一个参数时用(agrs,)表示

start_thread(put_ip,("wiki",wiki_log))

start_thread(put_ip,("other",other_log))

start_thread(ip_count,())

def main():

#主线程

start_thread(handle,())

if __name__ == ‘__main__‘:

main()

#放到后台运行ps -ef |grep python 可以看到#python ip_count.py &#远程连接到192.168.8.137查看#redis-cli -h 192.168.8.137#keys "wiki"#ZRANGE wiki 0 -1 withscores
时间: 2024-08-03 22:31:15

threading和queue监控两个log的python脚本的相关文章

Zabbix LLD监控之创建发现的Python脚本

#!/usr/bin/python import requests,json ip_list=[] data_json={} url = 'http://cmdb.cheyaoshicorp.com/api/ecs' r = requests.get(url) ecss = json.loads(r.text) for I in ecss:     ip_dic={}     host_ip = I.get('ip')     ip_dic["{#IP}"]=host_ip     i

监控无线AP是否在线python脚本

由于工作需要,编写了一个自动检查办公区无线AP是否掉线的python脚本,我这里用的是python3环境,请大家注意还有要注意的是我这里用的是锐捷的无线AC及无线AP.其它品牌只需要替换相关命令即可,就是脚本内容的中的command内容更改成你的品牌无线AC命令即可.下面是实际脚本内容: #!/usr/local/python3/bin/python3 import telnetlib,time,os def do_telnet(Host,password,finish,commands): i

用 Python 脚本实现对 Linux 服务器的监控

hon 分享到:8 原文出处: 曹江华 目前 Linux 下有一些使用 Python 语言编写的 Linux 系统监控工具 比如 inotify-sync(文件系统安全监控软件).glances(资源监控工具)在实际工作中,Linux 系统管理员可以根据自己使用的服务器的具体情况编写一下简单实用的脚本实现对 Linux 服务器的监控. 本文介绍一下使用 Python 脚本实现对 Linux 服务器 CPU 内存 网络的监控脚本的编写. Python 版本说明 Python 是由 Guido va

python 多线程并发threading & 任务队列Queue

https://docs.python.org/3.7/library/concurrency.htmlpython程序默认是单线程的,也就是说在前一句语句执行完之前后面的语句不能继续执行先感受一下线程,一般情况下: def testa(): sleep(1) print "a" def testb(): sleep(1) print "b" testa() testb()#先隔出一秒打印出a,再过一秒打出b 但是如果用了threading的话: ta = thre

用threading和Queue模块实现多线程的端口扫描器

一.Queue模块基础 q = Queue.Queue()    q.qsize()           返回队列的大小  q.empty()         如果队列为空,返回True,反之False  q.full()        如果队列满了,返回True,反之False q.full            与 maxsize 大小对应  q.get([block[, timeout]]) 获取队列,timeout等待时间  q.get_nowait()         相当q.get(

shell vs python脚本监控http请求

各写一个shell和python脚本来监控http请求,并在服务不可用的时候重启服务. 监控的连接为: http://192.168.1.101:5022/product http://192.168.1.101:5024/module shell脚本如下,配合crontab计划任务每一分钟执行一次检查: #!/bin/bash # This shell is used to moniter 192.168.1.101 port 5022 & 5024 date  #在crontab里用来记录l

两个月学习Python的胡言乱语

露珠是一名爱好养花的测试员,熟悉python,减少了很多重复性的工作.扯一扯这两个月的学习总结: 基础 语法,数据结构什么的翻一遍手册足够了,用到的时候不会了再查,相信只有你想不到的没有python不支持的(此处忽略老牛). 文件 嘛,作为一名测试,起初最需要用到的就是python的文件操作,各种读写文件,分析数据神马的..这里需要用到的就是python file操作.以及各种数据结构的用法了,比如List,dict 等等,,至于后台数据生成的文件,大多是json啦,excel啦,cpickle

监控HP服务器CPU温度的脚本

监控HP服务器CPU温度的脚本: #!/bin/bash Name=`hostname` IP=`/sbin/ifconfig eth0 | grep "inet addr" | awk -F[:" "]+ '{print $4}'` Date=`date +%m%d%y` Date2=`date +%H:%M` Num=`/usr/sbin/dmidecode | grep -i 'serial number' | head -n 1` CPU1=`/sbin/h

监控Oracle数据库的常用shell脚本-转

8个DBA最常用的监控Oracle数据库的常用shell脚本--转 分类: Linux 一.8个重要的脚本来监控Oracle数据库: 1.检查实例的可用性 2.检查监听器的可用性 3.检查alert日志文件中的错误信息 4.在存放log文件的地方满以前清空旧的log文件 5.分析table和index以获得更好的性能 6.检查表空间的使用情况 7.找出无效的对象 8.监控用户和事务 二.DBA需要的Unix基本知识 基本的UNIX命令,以下是一些常用的Unix命令: ps--显示进程 grep-