]> git.ipfire.org Git - thirdparty/kea.git/commitdiff
[#1196] Checkpoint: todo tests
authorFrancis Dupont <fdupont@isc.org>
Mon, 6 Jul 2020 12:03:13 +0000 (14:03 +0200)
committerFrancis Dupont <fdupont@isc.org>
Mon, 13 Jul 2020 13:05:42 +0000 (15:05 +0200)
doc/sphinx/man/kea-admin.8.rst
src/bin/admin/admin-utils.sh
src/bin/admin/kea-admin.in
src/bin/admin/kea-admin.rst

index bb99dac2209aad48d7fb7343bf932dda9bfbe034..357f8db4bcca9382de2955b5a9330dd39cdb8904 100644 (file)
@@ -1,5 +1,5 @@
 ..
-   Copyright (C) 2019 Internet Systems Consortium, Inc. ("ISC")
+   Copyright (C) 2019-2020 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
@@ -43,13 +43,16 @@ Arguments
    **db-upgrade**
       Conducts a database schema upgrade. This is useful when upgrading Kea.
 
-   **db-dump**
+   **lease-dump**
       Dumps the contents of the lease database (for MySQL, PostgreSQL,
       or CQL backends) to a CSV (comma-separated values) text file.
       The first line of the file contains the column names. This is meant
       to be used as a diagnostic tool, so it provides a portable,
       human-readable form of the lease data.
 
+   **lease-recount**
+      Recounts leases for MySQL or PostgreSQL database.
+
 ``backend``
    Specifies the backend type. Currently allowed backends are: memfile,
    mysql, and pgsql.
index 14348cefde7b10c406f14135460cec22d3473154..0f9dac4456ca4864ad81ca15680d04e382951ded 100644 (file)
@@ -1,6 +1,6 @@
 #!/bin/sh
 
-# Copyright (C) 2014-2019 Internet Systems Consortium, Inc. ("ISC")
+# Copyright (C) 2014-2020 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
@@ -150,3 +150,31 @@ cql_version() {
     echo "$version"
     return $error
 }
+
+recount4_query() {
+    # recount IPv4 leases from scratch
+    query="
+START TRANSACTION;
+DELETE FROM lease4_stat;
+INSERT INTO lease4_stat (subnet_id, state, leases)
+    SELECT subnet_id, state, COUNT(*)
+    FROM lease4 WHERE state = 0 OR state = 1
+    GROUP BY subnet_id, state;
+COMMIT;
+"
+    return $query
+}
+
+recount6_query() {
+    # recount IPv6 leases from scratch
+    query="
+START TRANSACTION;
+DELETE FROM lease6_stat;
+INSERT INTO lease6_stat (subnet_id, lease_type, state, leases)
+    SELECT subnet_id, lease_type, state, COUNT(*)
+    FROM lease6 WHERE state = 0 OR state = 1
+    GROUP BY subnet_id, lease_type, state;
+COMMIT;
+"
+    return $query
+}
index 0bd828a7c12fdbbf7c94450caa3cf113f4ff785a..9ddf878b44eed0dd7d912e0b7a9f580ff6e43689 100644 (file)
@@ -1,6 +1,6 @@
 #!/bin/sh
 
-# Copyright (C) 2014-2019 Internet Systems Consortium, Inc. ("ISC")
+# Copyright (C) 2014-2020 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
@@ -13,6 +13,7 @@
 # - database version check
 # - database version upgrade
 # - lease database dump
+# - lease database recount
 
 
 # Get the location of the kea-admin scripts
@@ -59,6 +60,7 @@ usage() {
     printf " -             for checking databaseB version when preparing for an upgrade.\n"
     printf " - db-upgrade: Upgrades your database scheme\n"
     printf " - lease-dump: Dump current leases to a CSV file\n"
+    printf " - lease-recount: Recount leases.
     printf "\n"
     printf "BACKEND - one of the supported backends: memfile|mysql|pgsql|cql\n"
     printf "\n"
@@ -622,6 +624,51 @@ cql_dump() {
     exit 0
 }
 
+### Functions used for recount
+mysql_recount() {
+
+    printf "Recount leases from database\n"
+    QUERY4=recount4_query()
+    RESULT=$(mysql_execute "$QUERY")
+    ERRCODE=$?
+    if [ $ERRCODE -ne 0 ]
+    then
+        log_error "mysql failed to recount IPv4 leases, mysql status = $ERRCODE"
+        exit 1
+    fi
+
+    QUERY6=recount6_query()
+    RESULT=$(mysql_execute "$QUERY")
+    ERRCODE=$?
+    if [ $ERRCODE -ne 0 ]
+    then
+        log_error "mysql failed to recount IPv6 leases, mysql status = $ERRCODE"
+        exit 1
+    fi
+}
+
+pgsql_recount() {
+
+    printf "Recount leases from database\n"
+    QUERY4=recount4_query()
+    RESULT=$(pgsql_execute "$QUERY")
+    ERRCODE=$?
+    if [ $ERRCODE -ne 0 ]
+    then
+        log_error "pgsql failed to recount IPv4 leases, pgsql status = $ERRCODE"
+        exit 1
+    fi
+
+    QUERY6=recount6_query()
+    RESULT=$(pgsql_execute "$QUERY")
+    ERRCODE=$?
+    if [ $ERRCODE -ne 0 ]
+    then
+        log_error "pgsql failed to recount IPv6 leases, pgsql status = $ERRCODE"
+        exit 1
+    fi
+}
+
 ### Script starts here ###
 
 # First, find what the command is
@@ -638,7 +685,7 @@ if test "${command}" = "-v" || test "${command}" = "--version" ; then
     exit 0
 fi
 
-is_in_list "${command}" "db-init db-version db-upgrade lease-dump"
+is_in_list "${command}" "db-init db-version db-upgrade lease-dump lease-recount"
 if [ ${_inlist} -eq 0 ]; then
     log_error "invalid command: ${command}"
     usage
@@ -817,6 +864,22 @@ case ${command} in
                 ;;
             esac
         ;;
+    lease-recount)
+        case ${backend} in
+            memfile)
+                log_info "memfile does not count leases"
+                ;;
+            mysql)
+                mysql_recount
+                ;;
+            pgsql)
+                pgsql_recount
+                ;;
+            cql)
+                log_info "cql does not count leases"
+                ;;
+            esac
+        ;;
 esac
 
 exit 0
index 06de3b221dbe5495e86a1bc4179d7afe8a324c20..dc1859f99deae8477c84578beab5b1d593cdf48a 100644 (file)
@@ -3,7 +3,7 @@ kea-admin
 Kea
 kea-admin
 Shell script for managing Kea databases
-2014-2018
+2014-2020
 Internet Systems Consortium, Inc. ("ISC")
 kea-admin
 command
@@ -54,6 +54,9 @@ OPTIONS
         meant to be used as a diagnostic tool, so it provides a portable,
         human-readable form of the lease data.
 
+   lease-recount
+        Recounts leases for MySQL or PostgreSQL database.
+
 ``backend``
    Specifies backend type. Currently allowed backends are: memfile,
    mysql and pgsql.