2
0
mirror of https://gitlab.isc.org/isc-projects/kea synced 2025-08-31 22:15:23 +00:00

[#2038] kea-admin lease-dump mysql outputs a memfile-ready CSV

This commit is contained in:
Andrei Pavel
2021-12-16 16:09:34 +02:00
parent 15bb806651
commit 9832c56ad6
8 changed files with 231 additions and 10 deletions

View File

@@ -20,4 +20,5 @@
/upgrade_009.6_to_010.0.sh
/upgrade_010_to_011.sh
/upgrade_011_to_012.sh
/upgrade_012_to_013.sh
/wipe_data.sh

View File

@@ -31,6 +31,7 @@ mysql_SCRIPTS += upgrade_009.5_to_009.6.sh
mysql_SCRIPTS += upgrade_009.6_to_010.0.sh
mysql_SCRIPTS += upgrade_010_to_011.sh
mysql_SCRIPTS += upgrade_011_to_012.sh
mysql_SCRIPTS += upgrade_012_to_013.sh
mysql_SCRIPTS += wipe_data.sh
DISTCLEANFILES = ${mysql_SCRIPTS}

View File

@@ -303,7 +303,7 @@ ALTER TABLE lease6
ADD CONSTRAINT fk_lease6_hwaddr_source FOREIGN KEY (hwaddr_source)
REFERENCES lease_hwaddr_source (hwaddr_source);
# FUNCTION that returns a result set containing the column names for lease4 dumps
# Procedure that returns a result set containing the column names for lease4 dumps
DROP PROCEDURE IF EXISTS lease4DumpHeader;
DELIMITER $$
CREATE PROCEDURE lease4DumpHeader()
@@ -312,7 +312,7 @@ SELECT 'address,hwaddr,client_id,valid_lifetime,expire,subnet_id,fqdn_fwd,fqdn_r
END $$
DELIMITER ;
# FUNCTION that returns a result set containing the data for lease4 dumps
# Procedure that returns a result set containing the data for lease4 dumps
DROP PROCEDURE IF EXISTS lease4DumpData;
DELIMITER $$
CREATE PROCEDURE lease4DumpData()
@@ -335,7 +335,7 @@ ORDER BY l.address;
END $$
DELIMITER ;
# FUNCTION that returns a result set containing the column names for lease6 dumps
# Procedure that returns a result set containing the column names for lease6 dumps
DROP PROCEDURE IF EXISTS lease6DumpHeader;
DELIMITER $$
CREATE PROCEDURE lease6DumpHeader()
@@ -344,7 +344,7 @@ SELECT 'address,duid,valid_lifetime,expire,subnet_id,pref_lifetime,lease_type,ia
END $$
DELIMITER ;
# FUNCTION that returns a result set containing the data for lease6 dumps
# Procedure that returns a result set containing the data for lease6 dumps
DROP PROCEDURE IF EXISTS lease6DumpData;
DELIMITER $$
CREATE PROCEDURE lease6DumpData()
@@ -691,7 +691,7 @@ SELECT 'address,hwaddr,client_id,valid_lifetime,expire,subnet_id,fqdn_fwd,fqdn_r
END $$
DELIMITER ;
# FUNCTION that returns a result set containing the data for lease4 dumps
# Procedure that returns a result set containing the data for lease4 dumps
DROP PROCEDURE IF EXISTS lease4DumpData;
DELIMITER $$
CREATE PROCEDURE lease4DumpData()
@@ -723,7 +723,7 @@ SELECT 'address,duid,valid_lifetime,expire,subnet_id,pref_lifetime,lease_type,ia
END $$
DELIMITER ;
# FUNCTION that returns a result set containing the data for lease6 dumps
# Procedure that returns a result set containing the data for lease6 dumps
DROP PROCEDURE IF EXISTS lease6DumpData;
DELIMITER $$
CREATE PROCEDURE lease6DumpData()
@@ -4076,7 +4076,86 @@ ALTER TABLE dhcp6_options
UPDATE schema_version
SET version = '12', minor = '0';
# This line concludes database upgrade to version 12.
-- Create a procedure that separates groups of two hexadecimals
-- with colons.
DROP FUNCTION IF EXISTS colonSeparatedHex;
DELIMITER $$
CREATE FUNCTION colonSeparatedHex(hex VARCHAR(64))
RETURNS VARCHAR(64)
DETERMINISTIC
BEGIN
DECLARE i INT;
DECLARE length INT;
DECLARE output VARCHAR(64);
SET i = 3;
SET length = LENGTH(hex);
SET output = SUBSTR(hex, 1, 2);
label: WHILE i < length DO
SET output = CONCAT(output, ':', SUBSTR(hex, i, 2));
SET i = i + 2;
END WHILE label;
SET output = LOWER(output);
RETURN output;
END $$
DELIMITER ;
-- Modify the procedure to output a memfile-ready CSV file.
DROP PROCEDURE IF EXISTS lease4DumpData;
DELIMITER $$
CREATE PROCEDURE lease4DumpData()
BEGIN
SELECT
INET_NTOA(l.address),
IFNULL(colonSeparatedHex(HEX(l.hwaddr)), ''),
IFNULL(colonSeparatedHex(HEX(l.client_id)), ''),
l.valid_lifetime,
UNIX_TIMESTAMP(l.expire),
l.subnet_id,
l.fqdn_fwd,
l.fqdn_rev,
l.hostname,
l.state,
IFNULL(l.user_context, '')
FROM
lease4 l
ORDER BY l.address;
END $$
DELIMITER ;
-- Modify the procedure to output a memfile-ready CSV file.
DROP PROCEDURE IF EXISTS lease6DumpData;
DELIMITER $$
CREATE PROCEDURE lease6DumpData()
BEGIN
SELECT
l.address,
IFNULL(colonSeparatedHex(HEX(l.duid)), ''),
l.valid_lifetime,
UNIX_TIMESTAMP(l.expire),
l.subnet_id,
l.pref_lifetime,
l.lease_type,
l.iaid,
l.prefix_len,
l.fqdn_fwd,
l.fqdn_rev,
l.hostname,
IFNULL(colonSeparatedHex(HEX(l.hwaddr)), ''),
IFNULL(l.hwtype, ''),
IFNULL(h.name, ''),
l.state,
IFNULL(l.user_context, '')
FROM lease6 l
LEFT OUTER JOIN lease_hwaddr_source h ON l.hwaddr_source = h.hwaddr_source
ORDER BY l.address;
END $$
DELIMITER ;
-- Update the schema version number.
UPDATE schema_version
SET version = '13', minor = '0';
-- This line concludes database upgrade to version 13.
# Notes:
#

View File

@@ -0,0 +1,135 @@
#!/bin/sh
# Copyright (C) 2021 Internet Systems Consortium, Inc. ("ISC")
#
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
# shellcheck disable=SC1091
# SC1091: Not following: ... was not specified as input (see shellcheck -x).
# Exit with error if commands exit with non-zero and if undefined variables are
# used.
set -eu
# shellcheck disable=SC2034
# SC2034: ... appears unused. Verify use (or export if used externally).
prefix="@prefix@"
# Include utilities. Use installed version if available and
# use build version if it isn't.
if [ -e @datarootdir@/@PACKAGE_NAME@/scripts/admin-utils.sh ]; then
. "@datarootdir@/@PACKAGE_NAME@/scripts/admin-utils.sh"
else
. "@abs_top_builddir@/src/bin/admin/admin-utils.sh"
fi
# Check version.
version=$(mysql_version "${@}")
if test "${version}" != "12.0"; then
printf 'This script upgrades 12.0 to 13.0. '
printf 'Reported version is %s. Skipping upgrade.\n' "${version}"
exit 0
fi
# Get the schema name from database argument. We need this to
# query information_schema for the right database.
for arg in "${@}"
do
if ! printf '%s' "${arg}" | grep -Eq '^\-\-'
then
schema="$arg"
break
fi
done
# Make sure we have the schema.
if [ -z "$schema" ]
then
printf "Could not find database schema name in cmd line args: %s\n" "${*}"
exit 255
fi
mysql "$@" <<EOF
-- Create a procedure that separates groups of two hexadecimals
-- with colons.
DROP FUNCTION IF EXISTS colonSeparatedHex;
DELIMITER $$
CREATE FUNCTION colonSeparatedHex(hex VARCHAR(64))
RETURNS VARCHAR(64)
DETERMINISTIC
BEGIN
DECLARE i INT;
DECLARE length INT;
DECLARE output VARCHAR(64);
SET i = 3;
SET length = LENGTH(hex);
SET output = SUBSTR(hex, 1, 2);
label: WHILE i < length DO
SET output = CONCAT(output, ':', SUBSTR(hex, i, 2));
SET i = i + 2;
END WHILE label;
SET output = LOWER(output);
RETURN output;
END $$
DELIMITER ;
-- Modify the procedure to output a memfile-ready CSV file.
DROP PROCEDURE IF EXISTS lease4DumpData;
DELIMITER $$
CREATE PROCEDURE lease4DumpData()
BEGIN
SELECT
INET_NTOA(l.address),
IFNULL(colonSeparatedHex(HEX(l.hwaddr)), ''),
IFNULL(colonSeparatedHex(HEX(l.client_id)), ''),
l.valid_lifetime,
UNIX_TIMESTAMP(l.expire),
l.subnet_id,
l.fqdn_fwd,
l.fqdn_rev,
l.hostname,
l.state,
IFNULL(l.user_context, '')
FROM
lease4 l
ORDER BY l.address;
END $$
DELIMITER ;
-- Modify the procedure to output a memfile-ready CSV file.
DROP PROCEDURE IF EXISTS lease6DumpData;
DELIMITER $$
CREATE PROCEDURE lease6DumpData()
BEGIN
SELECT
l.address,
IFNULL(colonSeparatedHex(HEX(l.duid)), ''),
l.valid_lifetime,
UNIX_TIMESTAMP(l.expire),
l.subnet_id,
l.pref_lifetime,
l.lease_type,
l.iaid,
l.prefix_len,
l.fqdn_fwd,
l.fqdn_rev,
l.hostname,
IFNULL(colonSeparatedHex(HEX(l.hwaddr)), ''),
IFNULL(l.hwtype, ''),
IFNULL(h.name, ''),
l.state,
IFNULL(l.user_context, '')
FROM lease6 l
LEFT OUTER JOIN lease_hwaddr_source h ON l.hwaddr_source = h.hwaddr_source
ORDER BY l.address;
END $$
DELIMITER ;
-- Update the schema version number.
UPDATE schema_version
SET version = '13', minor = '0';
-- This line concludes database upgrade to version 13.
EOF