Skip to content
Snippets Groups Projects
Commit 2b03f86b authored by Robin Sonnabend's avatar Robin Sonnabend
Browse files

Add cleanup script for db partitions

parent d778ad60
No related branches found
No related tags found
No related merge requests found
#!/usr/bin/env python
# encoding: utf-8
from __future__ import print_function
import psycopg2
import re
from datetime import datetime, date, timedelta
DATE_PATTERN = r'(?P<date>\d{6})'
def get_date(name):
match = re.search(DATE_PATTERN, name)
date_str = match.group("date")
year_part, month_part = date_str[:4], date_str[4:]
return date(year=int(year_part), month=int(month_part), day=1)
def get_partitions(connection, tablename):
cursor = connection.cursor()
cursor.execute("SELECT relname FROM pg_inherits i "
"JOIN pg_class c on c.oid = inhrelid "
"WHERE inhparent = '{}'::regclass".format(tablename))
return cursor.fetchall()
def delete_partition(connection, partition_name, partitions_schema="partitions"):
cursor = connection.cursor()
cursor.execute("DROP TABLE {}.{}".format(partitions_schema, partition_name))
return connection.commit()
if __name__ == "__main__":
today = datetime.now().date()
import argparse
parser = argparse.ArgumentParser("Helper script to keep the zabbix "
"database s̶m̶a̶l̶l̶ not tooo large.")
parser.add_argument("table", help="Tablename to remove partitions from "
"(e.g. „public.history“).")
parser.add_argument("--db", default="zabbix",
help="Name of the database to operate on.")
parser.add_argument("--months", default=3, type=int,
help="Number of months after which partitions should be dropped.")
parser.add_argument("--dryrun", action="store_true", default=False,
help="Do not actually delete partitions.")
parser.add_argument("--partitions-schema", default="partitions",
help="Schema name of partitions.")
arguments = parser.parse_args()
min_drop_delta = timedelta(days=31*arguments.months)
connection = psycopg2.connect(dbname=arguments.db)
for partitions in get_partitions(connection, arguments.table):
partition_name, = partitions
partition_date = get_date(partition_name)
should_delete = today - partition_date > min_drop_delta
if not should_delete:
continue
if arguments.dryrun:
print("Would drop partition {}.".format(partition_name))
else:
print("Dropping partition {}.".format(partition_name))
delete_partition(connection, partition_name,
partitions_schema=arguments.partitions_schema)
connection.close()
......@@ -126,6 +126,33 @@
- database
- zabbix
- name: upload the partition cleanup script
copy:
src: partition-cleanup.py
dest: /usr/local/bin/
owner: root
group: root
mode: 0755
delegate_to: "{{ zabbix_db_host }}"
tags:
- database
- zabbix
- name: let's clean the history partition up once per month
cron:
name: "clean zabbix-db {{item.table}} partition"
special_time: monthly
job: "/usr/local/bin/partition-cleanup.py public.{{item.table}} --months {{item.months}}"
state: present
user: postgres
delegate_to: "{{ zabbix_db_host }}"
with_items:
- { table: "history", months: 4 }
- { table: "trends", months: 13 }
tags:
- database
- zabbix
- name: include snmp-features
include: snmp.yml
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment