极狐极光日志自动下载处理
This commit is contained in:
@@ -10,7 +10,7 @@ logs:
|
|||||||
ansible_user: root
|
ansible_user: root
|
||||||
ansible_ssh_pass: adjg9815...
|
ansible_ssh_pass: adjg9815...
|
||||||
|
|
||||||
123.6.147.9:
|
43.226.59.253:
|
||||||
name: jglog
|
name: jglog
|
||||||
ansible_user: wyk
|
ansible_user: wyk
|
||||||
ansible_ssh_pass: adjg9815...
|
ansible_ssh_pass: adjg9815...
|
||||||
|
|||||||
@@ -3,9 +3,8 @@
|
|||||||
hosts: logs
|
hosts: logs
|
||||||
gather_facts: false
|
gather_facts: false
|
||||||
vars:
|
vars:
|
||||||
# 默认值,可以通过命令行参数覆盖
|
# year, month, start_day, end_day 通过命令行提供
|
||||||
log_base_path: "/data/roslog/roslog"
|
log_base_path: "/data/roslog/roslog"
|
||||||
# year, month, start_day, end_day 必须通过命令行参数提供
|
|
||||||
local_download_path: "/mnt/d/logs"
|
local_download_path: "/mnt/d/logs"
|
||||||
remove_after_compress: true
|
remove_after_compress: true
|
||||||
download_to_local: true
|
download_to_local: true
|
||||||
@@ -13,28 +12,28 @@
|
|||||||
|
|
||||||
tasks:
|
tasks:
|
||||||
- name: 验证必填参数
|
- name: 验证必填参数
|
||||||
fail:
|
ansible.builtin.fail:
|
||||||
msg: "缺少必填参数!必须提供: year, month, start_day, end_day"
|
msg: "缺少必填参数!必须提供: year, month, start_day, end_day"
|
||||||
when: year is not defined or month is not defined or start_day is not defined or end_day is not defined
|
when: year is not defined or month is not defined or start_day is not defined or end_day is not defined
|
||||||
|
|
||||||
- name: 显示将要处理的日志路径
|
- name: 显示将要处理的日志路径
|
||||||
debug:
|
ansible.builtin.debug:
|
||||||
msg: "处理路径: {{ log_base_path }}/{{ year }}/{{ month }}, 日期范围: {{ start_day }}-{{ end_day }}"
|
msg: "处理路径: {{ log_base_path }}/{{ year }}/{{ month }}, 日期范围: {{ start_day }}-{{ end_day }}"
|
||||||
|
|
||||||
- name: 确保日志目录存在
|
- name: 确保日志目录存在
|
||||||
stat:
|
ansible.builtin.stat:
|
||||||
path: "{{ log_base_path }}/{{ year }}/{{ month }}"
|
path: "{{ log_base_path }}/{{ year }}/{{ month }}"
|
||||||
register: log_dir
|
register: log_dir
|
||||||
|
|
||||||
- name: 检查日志目录是否存在
|
- name: 检查日志目录是否存在
|
||||||
fail:
|
ansible.builtin.fail:
|
||||||
msg: "日志目录 {{ log_base_path }}/{{ year }}/{{ month }} 不存在"
|
msg: "日志目录 {{ log_base_path }}/{{ year }}/{{ month }} 不存在"
|
||||||
when: not log_dir.stat.exists
|
when: not log_dir.stat.exists
|
||||||
|
|
||||||
- name: 压缩指定日期范围的日志文件夹
|
- name: 压缩指定日期范围的日志文件夹
|
||||||
shell: |
|
ansible.builtin.shell: |
|
||||||
cd {{ log_base_path }}/{{ year }}/{{ month }}
|
cd {{ log_base_path }}/{{ year }}/{{ month }}
|
||||||
for day in {{{ start_day }}..{{ end_day }}}; do
|
for day in $(seq -f "%02g" {{ start_day }} {{ end_day }}); do
|
||||||
if [ -d "$day" ]; then
|
if [ -d "$day" ]; then
|
||||||
echo "压缩目录: $day"
|
echo "压缩目录: $day"
|
||||||
tar -czf ${day}.tar.gz $day
|
tar -czf ${day}.tar.gz $day
|
||||||
@@ -44,80 +43,69 @@
|
|||||||
done
|
done
|
||||||
args:
|
args:
|
||||||
executable: /bin/bash
|
executable: /bin/bash
|
||||||
become: yes
|
become: true
|
||||||
register: compress_result
|
register: compress_result
|
||||||
|
|
||||||
- name: 显示压缩结果
|
- name: 显示压缩结果
|
||||||
debug:
|
ansible.builtin.debug:
|
||||||
var: compress_result.stdout_lines
|
var: compress_result.stdout_lines
|
||||||
|
|
||||||
- name: 列出已生成的压缩文件
|
- name: 列出已生成的压缩文件
|
||||||
find:
|
ansible.builtin.find:
|
||||||
paths: "{{ log_base_path }}/{{ year }}/{{ month }}"
|
paths: "{{ log_base_path }}/{{ year }}/{{ month }}"
|
||||||
patterns: "*.tar.gz"
|
patterns: "*.tar.gz"
|
||||||
register: compressed_files
|
register: compressed_files
|
||||||
|
|
||||||
- name: 显示压缩文件列表
|
|
||||||
debug:
|
|
||||||
msg: "已生成 {{ compressed_files.files | length }} 个压缩文件"
|
|
||||||
|
|
||||||
- name: 删除已压缩的原始文件夹
|
- name: 删除已压缩的原始文件夹
|
||||||
shell: |
|
ansible.builtin.shell: |
|
||||||
cd {{ log_base_path }}/{{ year }}/{{ month }}
|
cd {{ log_base_path }}/{{ year }}/{{ month }}
|
||||||
for day in {{{ start_day }}..{{ end_day }}}; do
|
for day in $(seq -f "%02g" {{ start_day }} {{ end_day }}); do
|
||||||
if [ -d "$day" ] && [ -f "${day}.tar.gz" ]; then
|
if [ -d "$day" ] && [ -f "${day}.tar.gz" ]; then
|
||||||
echo "删除目录: $day"
|
echo "删除原始目录: $day"
|
||||||
rm -rf $day
|
rm -rf $day
|
||||||
fi
|
fi
|
||||||
done
|
done
|
||||||
args:
|
args:
|
||||||
executable: /bin/bash
|
executable: /bin/bash
|
||||||
become: yes
|
become: true
|
||||||
when: remove_after_compress
|
|
||||||
register: cleanup_result
|
|
||||||
|
|
||||||
- name: 显示清理结果
|
|
||||||
debug:
|
|
||||||
var: cleanup_result.stdout_lines
|
|
||||||
when: remove_after_compress
|
when: remove_after_compress
|
||||||
|
|
||||||
- name: 确保本地下载目录存在
|
- name: 确保本地下载目录存在
|
||||||
local_action:
|
ansible.builtin.file:
|
||||||
module: file
|
path: "{{ local_download_path }}/{{ label }}/{{ year }}/{{ month }}"
|
||||||
path: "{{ local_download_path }}/{{ name }}/{{ year }}/{{ month }}"
|
|
||||||
state: directory
|
state: directory
|
||||||
|
delegate_to: localhost
|
||||||
when: download_to_local
|
when: download_to_local
|
||||||
become: no
|
become: false
|
||||||
|
|
||||||
- name: 下载压缩文件到本地(支持断点续传)
|
- name: 下载压缩文件到本地
|
||||||
synchronize:
|
ansible.posix.synchronize:
|
||||||
src: "{{ log_base_path }}/{{ year }}/{{ month }}/{{ item.path | basename }}"
|
src: "{{ log_base_path }}/{{ year }}/{{ month }}/{{ item.path | basename }}"
|
||||||
dest: "{{ local_download_path }}/{{ name }}/{{ year }}/{{ month }}/"
|
dest: "{{ local_download_path }}/{{ label }}/{{ year }}/{{ month }}/"
|
||||||
mode: pull
|
mode: pull
|
||||||
rsync_opts:
|
rsync_opts:
|
||||||
- "--partial"
|
- "--partial"
|
||||||
- "--progress"
|
- "--progress"
|
||||||
- "--compress"
|
|
||||||
with_items: "{{ compressed_files.files }}"
|
with_items: "{{ compressed_files.files }}"
|
||||||
when: download_to_local
|
when: download_to_local
|
||||||
become: yes
|
become: true
|
||||||
|
|
||||||
- name: 删除服务器上的压缩文件
|
- name: 删除服务器上的压缩文件
|
||||||
file:
|
ansible.builtin.file:
|
||||||
path: "{{ item.path }}"
|
path: "{{ item.path }}"
|
||||||
state: absent
|
state: absent
|
||||||
with_items: "{{ compressed_files.files }}"
|
with_items: "{{ compressed_files.files }}"
|
||||||
when: remove_local_archives and download_to_local
|
when: remove_local_archives and download_to_local
|
||||||
become: yes
|
become: true
|
||||||
|
|
||||||
- name: 完成信息
|
- name: 完成信息
|
||||||
debug:
|
ansible.builtin.debug:
|
||||||
msg: |
|
msg: |
|
||||||
日志处理完成!
|
日志处理完成!
|
||||||
- 服务器: {{ name }} ({{ inventory_hostname }})
|
- 服务器: {{ label }} ({{ inventory_hostname }})
|
||||||
- 路径: {{ log_base_path }}/{{ year }}/{{ month }}
|
- 路径: {{ log_base_path }}/{{ year }}/{{ month }}
|
||||||
- 日期范围: {{ start_day }}-{{ end_day }}
|
- 日期范围: {{ start_day }}-{{ end_day }}
|
||||||
- 压缩文件数: {{ compressed_files.files | length }}
|
- 压缩文件数: {{ compressed_files.files | length }}
|
||||||
{% if download_to_local %}
|
{% if download_to_local %}
|
||||||
- 本地下载路径: {{ local_download_path }}/{{ name }}/{{ year }}/{{ month }}
|
- 本地下载路径: {{ local_download_path }}/{{ label }}/{{ year }}/{{ month }}
|
||||||
{% endif %}
|
{% endif %}
|
||||||
|
|||||||
@@ -19,10 +19,10 @@ show_help() {
|
|||||||
用法: $0 -y YEAR -m MONTH -s START_DAY -e END_DAY [其他选项]
|
用法: $0 -y YEAR -m MONTH -s START_DAY -e END_DAY [其他选项]
|
||||||
|
|
||||||
必填选项:
|
必填选项:
|
||||||
-y, --year YEAR 年份 (必填)
|
-y, --year YEAR 年份
|
||||||
-m, --month MONTH 月份 (必填)
|
-m, --month MONTH 月份
|
||||||
-s, --start-day DAY 起始日期 (必填)
|
-s, --start-day DAY 起始日期
|
||||||
-e, --end-day DAY 结束日期 (必填)
|
-e, --end-day DAY 结束日期
|
||||||
|
|
||||||
可选选项:
|
可选选项:
|
||||||
-h, --host HOST 指定单个主机
|
-h, --host HOST 指定单个主机
|
||||||
@@ -43,12 +43,6 @@ show_help() {
|
|||||||
|
|
||||||
# 只压缩不删除原文件
|
# 只压缩不删除原文件
|
||||||
$0 -y 2025 -m 11 -s 21 -e 30 --no-rm-origin
|
$0 -y 2025 -m 11 -s 21 -e 30 --no-rm-origin
|
||||||
|
|
||||||
注意:
|
|
||||||
- 年月日参数必须显式指定
|
|
||||||
- 下载路径: /mnt/d/logs/<host_name>/<year>/<month>/
|
|
||||||
- 支持断点续传
|
|
||||||
|
|
||||||
EOF
|
EOF
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user