python工程,kafka消息对接转换成配置文件并完成类似看门狗监控linux系统进程,定期清理日志
wangrong
2025-04-05 88f9100edcba9581214c1a0acc23cd33a821a9c6
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
import json
import configparser
import os
import sys
import subprocess
import logging
import threading
import argparse
import time
import datetime
import signal
import functools
from kafka import KafkaConsumer
from datetime import datetime, timedelta
import schedule
 
# 创建日志记录器
logger = logging.getLogger(__name__)
 
# 设置日志级别
logger.setLevel(logging.INFO)
 
# 创建文件处理器,日志写入到文件
file_handler = logging.FileHandler('./parkwatch.log')
file_handler.setLevel(logging.INFO)  # 文件输出的日志级别
 
# 创建控制台处理器,日志输出到控制台
console_handler = logging.StreamHandler()
console_handler.setLevel(logging.INFO)  # 控制台输出的日志级别
 
# 定义日志格式
formatter = logging.Formatter('%(asctime)s - %(levelname)s - %(message)s')
file_handler.setFormatter(formatter)
console_handler.setFormatter(formatter)
 
# 将处理器添加到日志记录器
logger.addHandler(file_handler)
logger.addHandler(console_handler)
 
# 记录日志
logger.info("parkwatch Program started")
 
process_data = {}
 
def read_config(config_path):
    """读取配置文件"""
    config = configparser.ConfigParser()
    config.read(config_path)
    return config
 
def subscribe_kafka(config):
    """订阅Kafka topic"""
    kafka_server = config['KAFKA']['server']
    topic = config['TRANSFER']['access_topic']
 
    consumer = KafkaConsumer(
        topic,
        bootstrap_servers=kafka_server,
        value_deserializer=lambda m: json.loads(m.decode('utf-8'))
        #auto_offset_reset='earliest'
    )
    logger.info(f"subscribe Kafka topic : {topic} success!")
    return consumer
 
def process_kafka_message(message, config, file_index):
    """解析Kafka消息并按channel_size拆分写入JSON"""
    output_dir = os.path.dirname(config['TRANSFER']['outfile'])
    os.makedirs(output_dir, exist_ok=True)
    file_name, file_extension = os.path.splitext(os.path.basename(config['TRANSFER']['outfile']))
    channel_size = int(config['TRANSFER']['channel_size'])
    default_skip_interval = int(config['TRANSFER']['default_skip_interval'])
    default_gate_interval = int(config['TRANSFER']['default_gate_interval'])
    source_type = config['TRANSFER']['source_type']
    video_resize_ratio = round(float(config['TRANSFER']['video_resize_ratio']), 1)
    kfk_server = config['KAFKA']['server']
    public_topic = config['KAFKA']['public_topic']
    need_independent = config.getboolean('TRANSFER', 'need_independent')
    independent_action = [item.strip() for item in config['TRANSFER']['independent_action'].split(',')]
    independent_channel_size = int(config['TRANSFER']['independent_channel_size'])
    independent_channels = []  # 用于存储需要单独处理的通道
    # 读取允许的 actions
    allowed_actions = set(config['TRANSFER']['actions'].split(','))
    
    # 解析通道信息
    channels = message.get("channels", [])
    logger.info(f"get Kafka message, contains {len(channels)} channels")
 
    # 读取配置模板
    config_template = {
        "version": "1.0",
        "show_screen": False,
        "show_board": False,
        "kafka_server": kfk_server,
        "kafka_topic": public_topic,
        "max_input_per_detection": channel_size,
        "ba_jam_config": message.get("ba_jam_config", {}),
        "ba_stop_config": message.get("ba_stop_config", {}),
        "ba_person_gathering_config": message.get("ba_person_gathering_config", {}),
        "ba_person_fall_down_config": message.get("ba_person_fall_down_config", {}),
        "ba_wrong_direction_config": message.get("ba_wrong_direction_config", {}),
        "ba_barrier_gate_config": message.get("ba_barrier_gate_config", {}),
        "ba_fire_smoke_config": message.get("ba_fire_smoke_config", {}),
        "ba_junk_stack_config": message.get("ba_junk_stack_config", {}),
        "channels": []
    }
 
    # 拆分通道,每channel_size个写入一个新文件
    for i in range(0, len(channels), channel_size):
        config_template["channels"] = []
        for channel in channels[i:i + channel_size]:
            processed_channel = {
                "id": str(channel["id"]),
                "url": channel["url"],
                "source_type": channel.get("source_type", source_type),
                "skip_interval": channel.get("skip_interval", default_skip_interval),
                "resize_ratio": channel.get("resize_ratio", video_resize_ratio),
                "pix_width": channel.get("pix_width", 1280),
                "pix_height": channel.get("pix_height", 720),
                "actions": []
            }
 
            # 处理 actions,仅保留 STOP 和 JAM
            for act in channel.get("actions", []):
                act["action"] = act.get("action", "").upper()
                if act["action"] not in allowed_actions:
                    continue  # 过滤掉不在配置文件中的 action
                if act["action"] == independent_action[0] and need_independent:
                    processed_channel["skip_interval"] = default_gate_interval
                    # 将该通道添加到 independent_channels 中,而不是 config_template["channels"]
                    independent_channels.append(processed_channel)
                    processed_channel["actions"].append(act)
                    # 不将该通道加入到 config_template["channels"]
                    continue
                processed_channel["actions"].append(act)
 
            # 仅当 actions 不为空时才加入 channels
            if processed_channel["actions"]:
                config_template["channels"].append(processed_channel)
 
        # 写入 JSON 文件
        file_path = os.path.join(output_dir, f"{file_name}_{file_index}{file_extension}")
        write_config_file(config_template, file_path)
        file_index += 1
 
    # 如果 independent_channels 不为空,将其写入一个新的文件
    if independent_channels:
        # 过滤掉 independent_channels 中的 actions
        for channel in independent_channels:
            channel["actions"] = [act for act in channel["actions"] if act["action"] in independent_action]
 
        # 更新 config_template,设置最大通道数为 independent_channel_size
        config_template["channels"] = independent_channels
        config_template["max_input_per_detection"] = independent_channel_size
        
        # 写入文件
        file_path = os.path.join(output_dir, f"{file_name}_gate{file_extension}")
        write_config_file(config_template, file_path)
 
    return file_index
 
def write_config_file(config_template, file_path):
    try:
        with open(file_path, 'w') as f:
            json.dump(config_template, f, indent=4)
        logger.info(f"created file: {file_path}")
    except IOError as e:
        logger.error(f"write file: {file_path} faild: {e}")
 
 
def get_process_pids(process_name):
    """获取进程名称匹配的所有PID"""
    try:
        output = subprocess.check_output(["pgrep", "-x", process_name], text=True)
        return [int(pid) for pid in output.strip().split("\n") if pid]
    except subprocess.CalledProcessError:
        return []
    
def get_process_info(config):
    """
    获取进程信息,从配置文件中的process_file加载数据。
    :param config: 配置字典,包含PROCESS配置
    :return: 进程数据字典
    """
    process_data = {}
    # 从配置中获取process_file路径
    process_file = config['PROCESSES']['process_file']
    if not process_file:
        logger.error("Configuration missing 'process_file' in PROCESSES section.")
        return process_data
    try:
        # 打开文件并加载JSON数据
        with open(process_file, 'r') as file:
            process_data = json.load(file)
            logger.info(f"Successfully loaded process data from {process_file}")
    except FileNotFoundError:
        logger.error(f"Process file '{process_file}' not found.")
    except json.JSONDecodeError:
        logger.error(f"Error decoding JSON from the process file '{process_file}'.")
    except Exception as e:
        logger.error(f"Unexpected error when reading process file '{process_file}': {e}")
    
    return process_data
 
def manage_processes(process_data):
    """定期检查进程状态并在进程停止时重新启动"""
    if not process_data:
        logging.error("process_data is empty, not starting the monitor thread")
        return    
    # 停止进程
    process_list = process_data.get("process_list", [])
    for process in process_list:
        pids = get_process_pids(process.get("process_name"))
        for pid in pids:
            try:
                if os.path.exists(f"/proc/{pid}"):
                    os.kill(pid, 9)
                    logger.info(f"stop success {process} (PID: {pid})")
                else:
                    logger.warning(f"Process {process} (PID: {pid}) does not exist")
            except ProcessLookupError:
                logger.warning(f"process {process} (PID: {pid}) is not exists")
            except Exception as e:
                logger.error(f"stop process {process} faild: {e}")
 
    # 启动进程
    for process in process_data.get("process_list", []):
        start_process(process)
 
def start_process(proc):
    """启动单个进程"""
    program_path = proc.get('program_path')
    command_ = proc.get('commands')
 
    try:
        if program_path == "gnome-terminal":
            subprocess.Popen([program_path, '--', 'bash', '-c', command_])
        elif program_path == "xterm":
            subprocess.Popen([program_path, '-e', 'bash -c "' + command_ + '"'])
        elif program_path == "konsole":
            subprocess.Popen([program_path, '--hold', '-e', 'bash -c "' + command_ + '"'])
        else:
            subprocess.Popen([program_path, '-c', command_])
        
        logger.info(f"start process : {command_}")
    except FileNotFoundError as e:
        logger.error(f"terminel programe not find: {e}")
    except Exception as e:
        logger.error(f"start process faild: {e}")
 
def stop_process(pids):
    """停止指定PID的进程"""
    for pid in pids:
        try:   
            os.kill(pid, signal.SIGTERM)  # Try graceful stop
            logging.info(f"Sent SIGTERM to process with PID: {pid}")
            time.sleep(1)  # Wait for process to terminate
            if os.path.exists(f"/proc/{pid}"):
                os.kill(pid, signal.SIGKILL)  # Force kill if still running
                logging.info(f"Process {pid} killed with SIGKILL")
        except ProcessLookupError:
            logging.warning(f"Process with PID {pid} not found, might have exited already.")
        except PermissionError:
            logging.error(f"Permission denied while stopping process {pid}. Try running as root.")
        except Exception as e:
            logging.error(f"Error stopping process with PID {pid}: {e}")
 
 
def monitor_processes(process_data):
    """定期检查进程状态并在进程停止时重新启动"""
    if not process_data:
        logging.error("process_data is null, not starting the monitor thread")
        return
 
    process_list = process_data.get("process_list", [])
    interval_seconds = process_data.get("interval_monitor_seconds", 10)
    stop_start_hour = process_data.get("stop_start_hour", 0)
    stop_end_hour = process_data.get("stop_end_hour", 5)
    
    if not isinstance(interval_seconds, (int, float)) or interval_seconds <= 0:
        logging.warning(f"the interval_seconds is not effective {interval_seconds}, defaulting to 10 seconds")
        interval_seconds = 10  # 兜底策略
 
    while True:
        try:
            current_time = datetime.now()
            current_hour = current_time.hour
            for process in process_list:
                process_name = process.get("process_name")
                if stop_start_hour <= current_hour < stop_end_hour and process_name!="Gate":
                    pids = get_process_pids(process_name)
                    if pids:
                        stop_process(pids)                       
                else:
                    pids = get_process_pids(process_name)
                    if not pids:
                        logging.warning(f"Process {process_name} is not running, starting...")
                        start_process(process)
        except Exception as e:
            logging.error(f"Monitoring processes encountered an exception: {e}")
            
        time.sleep(interval_seconds)  # 按配置时间间隔检查   
 
 
def stop_all_processes(process_data):
    """Stop all processes listed in the process_data."""
    process_list = process_data.get("process_list", [])
    for process in process_list:
        process_name = process.get("process_name")
        if process_name:
            pids = get_process_pids(process_name)
            for pid in pids:
                try:
                    os.kill(pid, signal.SIGTERM)  # Try graceful stop
                    logger.info(f"Sent SIGTERM to process: {process_name} (PID: {pid})")
                    time.sleep(1)  # Wait for process to terminate
                    if os.path.exists(f"/proc/{pid}"):
                        os.kill(pid, signal.SIGKILL)  # Force kill if still running
                        logger.info(f"Sent SIGKILL to process: {process_name} (PID: {pid})")
                except ProcessLookupError:
                    logger.warning(f"Process {process_name} (PID: {pid}) not found.")
                except Exception as e:
                    logger.error(f"Error stopping process {process_name} (PID: {pid}): {e}")
 
def restart_processes(process_data):
    """Restart all processes listed in the process_data."""
    process_list = process_data.get("process_list", [])
    for process in process_list:
        start_process(process)  # Restart the process using existing start_process function
 
def exit_handler(signum, frame, process_data):
    """Exit handler to stop all processes on script exit."""
    logger.info("Exiting program, stopping all processes...")
    stop_all_processes(process_data)  # Stop all processes
    sys.exit(0)    
 
def clean_files(log_dir,log_expiry_time,file_dir,file_expiry_time):
    """清理过期的日志文件和录制文件"""
    logger.info(f"[{datetime.now()}] Starting cleanup process...")
 
    # 清理日志文件
    for filename in os.listdir(log_dir):
        file_path = os.path.join(log_dir, filename)
        if os.path.isfile(file_path):
            file_mtime = os.path.getmtime(file_path)  # 获取文件的修改时间
            if file_mtime < log_expiry_time:  # 如果文件超过指定的清理时间
                logger.info(f"Deleting log file: {file_path}")
                os.remove(file_path)
 
    # 清理录制文件
    for filename in os.listdir(file_dir):
        file_path = os.path.join(file_dir, filename)
        if os.path.isfile(file_path):
            file_mtime = os.path.getmtime(file_path)  # 获取文件的修改时间
            if file_mtime < file_expiry_time:  # 如果文件超过指定的清理时间
                logger.info(f"Deleting file: {file_path}")
                os.remove(file_path)
 
    logger.info(f"[{datetime.now()}] Cleanup process completed.")
 
def schedule_cleanup(config):
    """计划清理任务"""
    try:
        # 读取配置
        cleanup_time = config['clean'].get('cleanup_time')
        log_dir = config['clean'].get('log_dir')
        log_expiry_days = int(config['clean'].get('log_expiry_days', 7))  # Default to 7 days if not set
        file_dir = config['clean'].get('file_dir')
        file_expiry_days = int(config['clean'].get('file_expiry_days', 7))  # Default to 7 days if not set
 
        if not cleanup_time or not log_dir or not file_dir:
            raise ValueError("Missing required configuration for cleanup: cleanup_time, log_dir, or file_dir.")
 
        # 计算过期时间
        log_expiry_time = time.time() - (log_expiry_days * 24 * 60 * 60)
        file_expiry_time = time.time() - (file_expiry_days * 24 * 60 * 60)
 
        # 设置清理任务
        schedule_time = datetime.strptime(cleanup_time, '%H:%M:%S').time()
 
        # 安排每天在指定时间清理文件
        schedule.every().day.at(cleanup_time).do(clean_files, log_dir=log_dir, log_expiry_time=log_expiry_time, 
                                                 file_dir=file_dir, file_expiry_time=file_expiry_time)
 
        # 获取当前时间
        now = datetime.now()
        target_time = now.replace(hour=schedule_time.hour, minute=schedule_time.minute, second=schedule_time.second, microsecond=0)
 
        # 如果目标时间已经过去,计划任务将推迟到明天
        if now > target_time:
            target_time += timedelta(days=1)
 
        wait_time = (target_time - now).total_seconds()
        logger.info(f"Waiting {wait_time} seconds until the next cleanup task.")
        time.sleep(wait_time)
 
        # 进入调度循环
        while True:
            schedule.run_pending()
            time.sleep(60*60)  # 等待下一次任务
 
    except ValueError as ve:
        logger.error(f"Configuration error: {ve}")
    except KeyError as ke:
        logger.error(f"Missing configuration key: {ke}")
    except Exception as e:
        logger.error(f"Unexpected error occurred in schedule_cleanup: {e}")
 
def main():
    """主函数"""
    # 设置命令行参数解析
    parser = argparse.ArgumentParser(description="Process some integers.")
    parser.add_argument('--config', type=str, help="Path to the config file", default='msg_config.ini')
    args = parser.parse_args()
 
    config_path = args.config  # 从命令行获取配置文件路径
    config = read_config(config_path)
    process_data = get_process_info(config)
    signal.signal(signal.SIGINT, functools.partial(exit_handler, process_data=process_data))
    signal.signal(signal.SIGTERM, functools.partial(exit_handler, process_data=process_data))    
 
    # 启动进程监控线程
    monitor_thread = threading.Thread(target=monitor_processes, args=(process_data,), daemon=True)
    monitor_thread.start()
 
    # 启动清理任务调度线程
    cleanup_thread = threading.Thread(target=schedule_cleanup, args=(config,), daemon=True)
    cleanup_thread.start()   
 
    consumer = subscribe_kafka(config)
    if not consumer:
        return
        
    for message in consumer:
        try:
            process_kafka_message(message.value, config, 1)
            manage_processes(process_data)
        except Exception as e:
            logger.error(f"deal the Kafka messages exception: {e}")
 
if __name__ == "__main__":
    main()