Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
Z
zabbix
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Iterations
Wiki
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Container registry
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
infra
ansible-shared
zabbix
Commits
2b03f86b
Commit
2b03f86b
authored
7 years ago
by
Robin Sonnabend
Browse files
Options
Downloads
Patches
Plain Diff
Add cleanup script for db partitions
parent
d778ad60
No related branches found
No related tags found
No related merge requests found
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
zabbix-server/files/partition-cleanup.py
+60
-0
60 additions, 0 deletions
zabbix-server/files/partition-cleanup.py
zabbix-server/tasks/main.yml
+27
-0
27 additions, 0 deletions
zabbix-server/tasks/main.yml
with
87 additions
and
0 deletions
zabbix-server/files/partition-cleanup.py
0 → 100755
+
60
−
0
View file @
2b03f86b
#!/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
()
This diff is collapsed.
Click to expand it.
zabbix-server/tasks/main.yml
+
27
−
0
View file @
2b03f86b
...
...
@@ -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
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment