]> git.ipfire.org Git - people/arne_f/network.git/commitdiff
network: Add some small databases for status monitoring and accounting.
authorMichael Tremer <michael.tremer@ipfire.org>
Fri, 4 Jun 2010 01:06:47 +0000 (03:06 +0200)
committerMichael Tremer <michael.tremer@ipfire.org>
Fri, 4 Jun 2010 01:06:47 +0000 (03:06 +0200)
functions.constants
functions.db [new file with mode: 0644]
functions.ppp
functions.zone

index 7521e6e802d9f01b2efff012d6e42b3d008692ed..2030dc9b5e9c111773590278994974ee02afbfc3 100644 (file)
@@ -25,15 +25,18 @@ COLORS=1
 BASE_DIR=/lib/network
 CONFIG_DIR=/etc/network
 HOOKS_DIR=${BASE_DIR}/hooks
+LOG_DIR=/var/log/network
 RUN_DIR=/var/run/network
 ZONE_DIR=${CONFIG_DIR}
 
-RED_RUN=/var/run/network/red
+RED_RUN=${RUN_DIR}/red
 PPP_SECRETS=/etc/ppp/secrets
 
 CONFIG_FILE=${CONFIG_DIR}/network_config
 CONFIG_FILE_PARAMS="COLORS DEBUG SHELL"
 
+DB_CONNECTION_FILE="${LOG_DIR}/connections.db"
+
 # Proper error codes
 EXIT_OK=0
 EXIT_ERROR=1
diff --git a/functions.db b/functions.db
new file mode 100644 (file)
index 0000000..23e0e35
--- /dev/null
@@ -0,0 +1,113 @@
+#!/bin/bash
+###############################################################################
+#                                                                             #
+# IPFire.org - A linux based firewall                                         #
+# Copyright (C) 2010  Michael Tremer & Christian Schmidt                      #
+#                                                                             #
+# This program is free software: you can redistribute it and/or modify        #
+# it under the terms of the GNU General Public License as published by        #
+# the Free Software Foundation, either version 3 of the License, or           #
+# (at your option) any later version.                                         #
+#                                                                             #
+# This program is distributed in the hope that it will be useful,             #
+# but WITHOUT ANY WARRANTY; without even the implied warranty of              #
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               #
+# GNU General Public License for more details.                                #
+#                                                                             #
+# You should have received a copy of the GNU General Public License           #
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.       #
+#                                                                             #
+###############################################################################
+
+function db_connection_init() {
+       if [ -e "${DB_CONNECTION_FILE}" ]; then
+               return ${EXIT_OK}
+       fi
+
+       log DEBUG "Creating connection database ${DB_CONNECTION_FILE}."
+
+       sqlite3 -batch ${DB_CONNECTION_FILE} <<EOF
+CREATE TABLE log(
+       id INTEGER PRIMARY KEY AUTOINCREMENT,
+       zone TEXT,
+       time INTEGER,
+       state TEXT
+);
+
+CREATE VIEW current as
+       SELECT zone, time, state FROM log GROUP BY zone;
+
+EOF
+}
+
+function db_connection_update() {
+       local zone=${1}
+       local action=${2}
+       shift 2
+
+       db_connection_init
+
+       log DEBUG "Writing connection to database: zone=${zone} action=${action}."
+
+       sqlite3 -batch ${DB_CONNECTION_FILE} <<EOF
+INSERT INTO log(zone, time, state)
+       VALUES('${zone}', strftime('%s', 'now', 'utc'), '${action}');
+EOF
+}
+
+function db_ppp_init() {
+       local file=${1}
+
+       if [ -e "${file}" ]; then
+               return ${EXIT_OK}
+       fi
+
+       log DEBUG "Creating ppp database ${file}."
+
+       sqlite3 -batch ${file} <<EOF
+CREATE TABLE accounting(
+       id INTEGER PRIMARY KEY AUTOINCREMENT,
+       time INTEGER,
+       duration INTEGER,
+       rcvd INTEGER,
+       sent INTEGER
+);
+EOF
+} 
+
+function db_ppp_update() {
+       local zone=${1}
+       shift
+
+       local rcvd
+       local sent
+       local duration
+
+       while [ $# -gt 0 ]; do
+               case "${1}" in
+                       --rcvd=*)
+                               rcvd=${1#--rcvd=}
+                               ;;
+                       --sent=*)
+                               sent=${1#--sent=}
+                               ;;
+                       --duration=*)
+                               duration=${1#--duration=}
+                               ;;
+               esac
+               shift
+       done
+
+       local file="${LOG_DIR}/ppp_${zone}.db"
+
+       db_ppp_init ${file}
+
+       local time=$(( $(date -u +"%s") - ${duration} ))
+
+       log DEBUG "Writing accounting data: time=${time} duration=${duration} rcvd=${rcvd} sent=${sent}."
+
+       sqlite3 -batch ${file} <<EOF
+INSERT INTO accounting(time, duration, rcvd, sent)
+       VALUES('${time}', '${duration}', '${rcvd}', '${sent}');
+EOF
+}
index 958b6d2107a9a2fdeeb5ad911521d523f5b06ae0..9d4721158405d87667ab0586d57f115520cc9958 100644 (file)
@@ -58,6 +58,9 @@ function ppp_common_ip_down() {
                return ${EXIT_ERROR}
        fi
 
+       # Save accounting information
+       ppp_accounting ${zone}
+
        # Emit interface-up event
        event_interface_down ${zone}
 
@@ -83,31 +86,12 @@ function ppp_secret() {
        rm -f ${PPP_SECRETS}.tmp
 }
 
-function ppp_stat() {
-       local name=${1}
-       local time=${2}
-       local rcvd=${3}
-       local sent=${4}
-
-       local file="${LOG_DIR}/ppp_${name}.db"
-       if ! [ -e "${file}" ]; then
-       sqlite3 -batch ${file} <<EOF
-CREATE TABLE connections(date, duration, rcvd, sent);
-EOF
-       fi
-       ppp_stat_init ${file}
-
-       sqlite3 -batch ${file} <<EOF
-INSERT INTO connections(date, duration, rcvd, sent) VALUES('$(date -u '+%s')', '${time}', '${rcvd}', '${sent}');
-EOF
-}
+function ppp_accounting() {
+       local zone=${1}
+       shift
 
-function ppp_linkname_get() {
-       local config=${1}
-       (
-               . ${config}
-               echo "${NAME}"
-       )
+       db_ppp_update ${zone} --duration="${CONNECT_TIME}" \
+               --rcvd="${BYTES_RCVD}" --sent="${BYTES_SENT}"
 }
 
 function red_defaultroute_update() {
index 268993ae001abd97094c3f561297ba318c38ff7c..41b60aa02c1c3159dc0a468e8910723e67f7e73c 100644 (file)
@@ -154,7 +154,11 @@ function zone_up() {
                return ${EXIT_ERROR}
        fi
 
+       zone_db ${zone} starting
+
        hook_exec ${hook} up ${zone} $@
+       
+       zone_db ${zone} started
 }
 
 function zone_down() {
@@ -178,7 +182,11 @@ function zone_down() {
                return ${EXIT_ERROR}
        fi
 
+       zone_db ${zone} stopping
+
        hook_exec ${hook} down ${zone} $@
+
+       zone_db ${zone} stopped
 }
 
 function zone_status() {
@@ -415,3 +423,14 @@ function zone_has_ipv4() {
        device_has_ipv4 $@
 }
 
+function zone_db() {
+       local zone=${1}
+       local action=${2}
+       shift 2
+
+       case "${action}" in
+               starting|started|stopping|stopped)
+                       db_connection_update ${zone} ${action}
+                       ;;
+       esac
+}