Showing
5 changed files
with
201 additions
and
53 deletions
@@ -25,18 +25,26 @@ influxdb_port = 8086 | @@ -25,18 +25,26 @@ influxdb_port = 8086 | ||
25 | --[[ | 25 | --[[ |
26 | 前端监听配置 | 26 | 前端监听配置 |
27 | --]] | 27 | --]] |
28 | -http_mq_port = 9503 | 28 | +http_mq_port = 9500 |
29 | worker_num = 8 | 29 | worker_num = 8 |
30 | daemonize = false | 30 | daemonize = false |
31 | 31 | ||
32 | 32 | ||
33 | --[[ | 33 | --[[ |
34 | - 日志文件 | 34 | + 路径配置 |
35 | --]] | 35 | --]] |
36 | +--working directory工作目录 | ||
37 | +pwd = '/www/yoho-phplog' | ||
38 | +--pwd = '/Data/code/yoho-phplog' | ||
39 | +--python路径 | ||
40 | +python = "/usr/bin/python" | ||
41 | +--php路径 | ||
42 | +php = "/usr/bin/php56" | ||
43 | +--php = "/Data/local/php/bin/php" | ||
44 | +--日志文件 | ||
36 | --log_file = nil | 45 | --log_file = nil |
37 | log_file = '/Data/code/yoho-phplog/access.log' | 46 | log_file = '/Data/code/yoho-phplog/access.log' |
38 | 47 | ||
39 | - | ||
40 | --[[ | 48 | --[[ |
41 | 通道配置 | 49 | 通道配置 |
42 | --]] | 50 | --]] |
@@ -52,31 +60,3 @@ rabbitmq_ack_interval = 10 | @@ -52,31 +60,3 @@ rabbitmq_ack_interval = 10 | ||
52 | info_channel = 'YHInfo' | 60 | info_channel = 'YHInfo' |
53 | debug_channel = 'YHDebug' | 61 | debug_channel = 'YHDebug' |
54 | shutdown_channel = 'YHShutdown' | 62 | shutdown_channel = 'YHShutdown' |
55 | - | ||
56 | - | ||
57 | ---[[ | ||
58 | - 执行检测代码 | ||
59 | ---]] | ||
60 | -if log_file then | ||
61 | - fp = io.open(log_file,'rb') | ||
62 | - if fp ~= nil then | ||
63 | - fp:close() | ||
64 | - else | ||
65 | - dir = string.match(log_file,"(.+)[/\\][^/\\]+") | ||
66 | - dp = io.open(dir,'rb') | ||
67 | - if dp ~= nil then | ||
68 | - dp:close() | ||
69 | - else | ||
70 | - os.execute('mkdir -p '..dir..' 2>&1 > /dev/null') | ||
71 | - end | ||
72 | - | ||
73 | - dp = io.open(dir,'rb') | ||
74 | - if dp ~= nil then | ||
75 | - dp:close() | ||
76 | - fp = io.open(log_file,'w') | ||
77 | - if fp ~= nil then | ||
78 | - fp:close() | ||
79 | - end | ||
80 | - end | ||
81 | - end | ||
82 | -end |
GoMqClient.py
0 → 100644
1 | +#!/usr/bin/env python | ||
2 | +# -*- coding: utf-8 -*- | ||
3 | +# @Author: wuxiao | ||
4 | +# @Date: 2016-05-24 13:37:45 | ||
5 | +# @Last Modified by: anchen | ||
6 | +# @Last Modified time: 2016-05-24 18:40:09 | ||
7 | + | ||
8 | +import os | ||
9 | +import sys | ||
10 | +__FILE__ = os.path.abspath(sys.argv[0]) | ||
11 | +__DIR__ = os.path.dirname(os.path.abspath(sys.argv[0])) | ||
12 | + | ||
13 | +import json | ||
14 | +import time | ||
15 | +import pika | ||
16 | +from influxdb import InfluxDBClient | ||
17 | +from lupa import LuaRuntime | ||
18 | + | ||
19 | +''' | ||
20 | +读取配置信息 | ||
21 | +''' | ||
22 | +lua = LuaRuntime(unpack_returned_tuples=True) | ||
23 | +lua.execute(open(__DIR__+'/Config.lua').read(-1)) | ||
24 | +Config = lua.globals() | ||
25 | + | ||
26 | +q_name = 'log' #队列名 | ||
27 | +ack_interval = Config.rabbitmq_ack_interval #批量ack响应阀值 | ||
28 | + | ||
29 | +#连接rabbitmq | ||
30 | +credentials = pika.PlainCredentials(Config.rabbitmq_user, Config.rabbitmq_pass) | ||
31 | +connection = pika.BlockingConnection(pika.ConnectionParameters( | ||
32 | + Config.rabbitmq_host,Config.rabbitmq_port,Config.rabbitmq_vhost,credentials)) | ||
33 | +if not connection: | ||
34 | + exit("Cannot connect to the broker!\n") | ||
35 | +channel = connection.channel() | ||
36 | + | ||
37 | +#连接influxdb | ||
38 | +influxdb = InfluxDBClient(Config.influxdb_host, Config.influxdb_port, timeout=1) | ||
39 | + | ||
40 | +print "MqClient:Start.\n"; | ||
41 | + | ||
42 | +#开始消费消息数据 | ||
43 | +for method_frame, properties, body in channel.consume(q_name): | ||
44 | + try: | ||
45 | + | ||
46 | + #print method_frame.delivery_tag, method_frame, properties | ||
47 | + print " [x] Received %r" % (body,) | ||
48 | + data = json.loads(body) | ||
49 | + #print data | ||
50 | + | ||
51 | + #决定库表 | ||
52 | + if data['group'] == 'error': | ||
53 | + db = 'error' | ||
54 | + measurement = Config.debug_channel+time.strftime('_%Y%m%d') | ||
55 | + elif data['group'] == 'shutdown': | ||
56 | + db = 'error' | ||
57 | + measurement = Config.shutdown_channel+time.strftime('_%Y%m%d') | ||
58 | + else: | ||
59 | + db = 'log' | ||
60 | + measurement = Config.info_channel+time.strftime('_%Y%m%d') | ||
61 | + | ||
62 | + #拼装入库数据 | ||
63 | + json_body = [ | ||
64 | + { | ||
65 | + "measurement": measurement, | ||
66 | + "tags": data['tags'], | ||
67 | + #"time": "2009-11-10T23:00:00Z", | ||
68 | + "fields": data['data'] | ||
69 | + } | ||
70 | + ] | ||
71 | + | ||
72 | + #选择库,写入 | ||
73 | + influxdb.switch_database(db) | ||
74 | + influxdb.write_points(json_body) | ||
75 | + | ||
76 | + #ack回应 | ||
77 | + #channel.basic_ack(method_frame.delivery_tag) | ||
78 | + if method_frame.delivery_tag % ack_interval == 0: | ||
79 | + channel.basic_ack(multiple=True) | ||
80 | + | ||
81 | + except Exception as err: | ||
82 | + channel.basic_ack(method_frame.delivery_tag) | ||
83 | + print err | ||
84 | + continue | ||
85 | + | ||
86 | + | ||
87 | +channel.cancel() | ||
88 | +connection.close() |
@@ -7,15 +7,6 @@ | @@ -7,15 +7,6 @@ | ||
7 | class Rabbitmq | 7 | class Rabbitmq |
8 | { | 8 | { |
9 | 9 | ||
10 | - //配置信息 | ||
11 | - static $conn_args = array( | ||
12 | - 'host' => '54.222.219.223', | ||
13 | - 'port' => '5672', | ||
14 | - 'login' => 'guest', | ||
15 | - 'password' => 'guest', | ||
16 | - 'vhost' => '/' | ||
17 | - ); | ||
18 | - | ||
19 | static $conn; | 10 | static $conn; |
20 | 11 | ||
21 | static $channel; | 12 | static $channel; |
@@ -30,7 +21,15 @@ class Rabbitmq | @@ -30,7 +21,15 @@ class Rabbitmq | ||
30 | //创建连接 | 21 | //创建连接 |
31 | $conn = self::$conn; | 22 | $conn = self::$conn; |
32 | if (!is_object($conn) || !$conn->isConnected()){ | 23 | if (!is_object($conn) || !$conn->isConnected()){ |
33 | - $conn = new AMQPConnection(self::$conn_args); | 24 | + //配置信息 |
25 | + $conn_args = array( | ||
26 | + 'host' => $Config->rabbitmq_host, | ||
27 | + 'port' => $Config->rabbitmq_port, | ||
28 | + 'login' => $Config->rabbitmq_user, | ||
29 | + 'password' => $Config->rabbitmq_pass, | ||
30 | + 'vhost' => $Config->rabbitmq_vhost, | ||
31 | + ); | ||
32 | + $conn = new AMQPConnection($conn_args); | ||
34 | if (!$conn->connect()) { | 33 | if (!$conn->connect()) { |
35 | echo ("Cannot connect to the broker!\n"); | 34 | echo ("Cannot connect to the broker!\n"); |
36 | return; | 35 | return; |
bak/Config.lua
0 → 100644
1 | +--[[ | ||
2 | + 配置文件 | ||
3 | +--]] | ||
4 | + | ||
5 | + | ||
6 | +--[[ | ||
7 | + rabbitmq服务器 | ||
8 | +--]] | ||
9 | +--rabbitmq_host = '127.0.0.1' | ||
10 | +rabbitmq_host = '54.222.219.223' | ||
11 | +rabbitmq_port = 5672 | ||
12 | +rabbitmq_user = 'guest' | ||
13 | +rabbitmq_pass = 'guest' | ||
14 | +rabbitmq_vhost = '/' | ||
15 | + | ||
16 | + | ||
17 | +--[[ | ||
18 | + influxdb服务器 | ||
19 | +--]] | ||
20 | +--influxdb_host = '127.0.0.1' | ||
21 | +influxdb_host = '54.222.219.223' | ||
22 | +influxdb_port = 8086 | ||
23 | + | ||
24 | + | ||
25 | +--[[ | ||
26 | + 前端监听配置 | ||
27 | +--]] | ||
28 | +http_mq_port = 9503 | ||
29 | +worker_num = 8 | ||
30 | +daemonize = false | ||
31 | + | ||
32 | + | ||
33 | +--[[ | ||
34 | + 日志文件 | ||
35 | +--]] | ||
36 | +--log_file = nil | ||
37 | +log_file = '/Data/code/yoho-phplog/access.log' | ||
38 | + | ||
39 | + | ||
40 | +--[[ | ||
41 | + 通道配置 | ||
42 | +--]] | ||
43 | +--交换机名前缀 | ||
44 | +rabbitmq_exchange_name = 'yohoExchange_' | ||
45 | +--路由键名 | ||
46 | +rabbitmq_route_key = 'phplog' | ||
47 | +--队列名 | ||
48 | +rabbitmq_queue_name = 'phplog' | ||
49 | +--批量ack响应阀值 | ||
50 | +rabbitmq_ack_interval = 10 | ||
51 | + | ||
52 | +info_channel = 'YHInfo' | ||
53 | +debug_channel = 'YHDebug' | ||
54 | +shutdown_channel = 'YHShutdown' | ||
55 | + | ||
56 | + | ||
57 | +--[[ | ||
58 | + 执行检测代码 | ||
59 | +--]] | ||
60 | +if log_file then | ||
61 | + fp = io.open(log_file,'rb') | ||
62 | + if fp ~= nil then | ||
63 | + fp:close() | ||
64 | + else | ||
65 | + dir = string.match(log_file,"(.+)[/\\][^/\\]+") | ||
66 | + dp = io.open(dir,'rb') | ||
67 | + if dp ~= nil then | ||
68 | + dp:close() | ||
69 | + else | ||
70 | + os.execute('mkdir -p '..dir..' 2>&1 > /dev/null') | ||
71 | + end | ||
72 | + | ||
73 | + dp = io.open(dir,'rb') | ||
74 | + if dp ~= nil then | ||
75 | + dp:close() | ||
76 | + fp = io.open(log_file,'w') | ||
77 | + if fp ~= nil then | ||
78 | + fp:close() | ||
79 | + end | ||
80 | + end | ||
81 | + end | ||
82 | +end |
1 | #!/bin/sh | 1 | #!/bin/sh |
2 | 2 | ||
3 | -#working directory | ||
4 | -pwd="/Data/code/yoho-phplog" | ||
5 | - | ||
6 | -pgrep="/usr/bin/pgrep" | ||
7 | -wc="/usr/bin/wc" | ||
8 | -lsof="/usr/sbin/lsof" | ||
9 | -python="/usr/bin/python" | ||
10 | -php="/Data/local/php/bin/php" | ||
11 | - | ||
12 | -log_file=$pwd'/access.log' | ||
13 | -if [ ! -f $log_file ];then | 3 | +#配置读取 |
4 | +vars=`sed -n '/^pwd\|^python\|^php\|^log_file/s/ \{1,\}= \{1,\}/=/p' Config.lua` | ||
5 | +eval $vars | ||
6 | + | ||
7 | +if [ ! -f "$log_file" ];then | ||
8 | + dir=`dirname $log_file` | ||
9 | + if [ ! -d $dir ];then | ||
10 | + mkdir -p $dir | ||
11 | + fi | ||
14 | touch $log_file | 12 | touch $log_file |
15 | chmod 777 $log_file | 13 | chmod 777 $log_file |
16 | fi | 14 | fi |
17 | 15 | ||
18 | monitor(){ | 16 | monitor(){ |
19 | #pid=`ps aux|grep $1.php|$wc -l` | 17 | #pid=`ps aux|grep $1.php|$wc -l` |
20 | - pid=`ps aux|grep "$1"|$wc -l` | 18 | + pid=`ps aux|grep "$1"|wc -l` |
21 | 19 | ||
22 | if [ $pid -le 1 ];then | 20 | if [ $pid -le 1 ];then |
23 | msg="At time: `date` :$1 is stop ." | 21 | msg="At time: `date` :$1 is stop ." |
@@ -40,6 +38,7 @@ while true;do | @@ -40,6 +38,7 @@ while true;do | ||
40 | 38 | ||
41 | #mqclient | 39 | #mqclient |
42 | monitor "$python $pwd/MqClient.py" | 40 | monitor "$python $pwd/MqClient.py" |
41 | + monitor "$python $pwd/GoMqClient.py.py" | ||
43 | 42 | ||
44 | sleep 5 | 43 | sleep 5 |
45 | 44 |
-
Please register or login to post a comment