]>
git.ipfire.org Git - people/ms/network.git/blob - src/functions/functions.db
2 ###############################################################################
4 # IPFire.org - A linux based firewall #
5 # Copyright (C) 2010 Michael Tremer & Christian Schmidt #
7 # This program is free software: you can redistribute it and/or modify #
8 # it under the terms of the GNU General Public License as published by #
9 # the Free Software Foundation, either version 3 of the License, or #
10 # (at your option) any later version. #
12 # This program is distributed in the hope that it will be useful, #
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of #
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
15 # GNU General Public License for more details. #
17 # You should have received a copy of the GNU General Public License #
18 # along with this program. If not, see <http://www.gnu.org/licenses/>. #
20 ###############################################################################
26 print
"${NETWORK_DB_DIR}/${key}"
32 local path
="$(_db_key2path "${key}")"
34 if [ -e "${path}" ]; then
44 local path
="$(_db_key2path "${key}")"
53 local path
="$(_db_key2path "${key}")"
55 if [ -r "${path}" ]; then
56 log DEBUG
"Setting '${key}' = '${value}'"
58 log DEBUG
"Adding '${key}' = '${value}'"
61 make_parent_directory
"${path}"
62 print
"${value}" > "${path}"
68 local path
="$(_db_key2path "${key}")"
70 log DEBUG
"Deleting key = '${key}'"
72 if [ -d "${path}" ]; then
73 _db_delete_recursive
"${path}"
79 _db_delete_recursive
() {
83 for key
in $
(_db_list_path
"${path}"); do
91 _db_dump_recursive
"${NETWORK_DB_DIR}"
96 local val
="$(db_get "${key}")"
98 printf "%-38s = %s\n" "${key}" "${val}"
101 _db_dump_recursive
() {
105 for key
in $
(_db_list_path
"${path}"); do
106 path
="$(_db_key2path "${key}")"
108 if [ -d "${path}" ]; then
109 _db_dump_recursive
"${path}"
111 _db_dump_key
"${key}"
120 for element
in ${path}/*; do
121 [ -e "${element}" ] ||
continue
123 print
"${element#${NETWORK_DB_DIR}/}"
127 db_connection_init
() {
128 if [ -e "${DB_CONNECTION_FILE}" ]; then
132 log DEBUG
"Creating connection database ${DB_CONNECTION_FILE}."
134 sqlite3
-batch ${DB_CONNECTION_FILE} <<EOF
136 id INTEGER PRIMARY KEY AUTOINCREMENT,
142 CREATE VIEW current as
143 SELECT zone, time, state FROM log GROUP BY zone;
148 db_connection_update
() {
155 log DEBUG
"Writing connection to database: zone=${zone} action=${action}."
157 sqlite3
-batch ${DB_CONNECTION_FILE} <<EOF
158 INSERT INTO log(zone, time, state)
159 VALUES('${zone}', strftime('%s', 'now', 'utc'), '${action}');
166 if [ -e "${file}" ]; then
170 log DEBUG
"Creating ppp database ${file}."
172 sqlite3
-batch ${file} <<EOF
173 CREATE TABLE accounting(
174 id INTEGER PRIMARY KEY AUTOINCREMENT,
191 while [ $# -gt 0 ]; do
200 duration
=${1#--duration=}
206 local file="${LOG_DIR}/ppp_${zone}.db"
210 local time=$
(( $
(date -u +"%s") - ${duration} ))
212 log DEBUG
"Writing accounting data: time=${time} duration=${duration} rcvd=${rcvd} sent=${sent}."
214 sqlite3
-batch ${file} <<EOF
215 INSERT INTO accounting(time, duration, rcvd, sent)
216 VALUES('${time}', '${duration}', '${rcvd}', '${sent}');