]>
Commit | Line | Data |
---|---|---|
059469a8 MT |
1 | #!/bin/bash |
2 | ############################################################################### | |
3 | # # | |
4 | # IPFire.org - A linux based firewall # | |
5 | # Copyright (C) 2010 Michael Tremer & Christian Schmidt # | |
6 | # # | |
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. # | |
11 | # # | |
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. # | |
16 | # # | |
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/>. # | |
19 | # # | |
20 | ############################################################################### | |
21 | ||
c041b631 MT |
22 | _db_key2path() { |
23 | local key="${1}" | |
24 | assert isset key | |
25 | ||
26 | print "${NETWORK_DB_DIR}/${key}" | |
27 | } | |
28 | ||
29 | db_exists() { | |
30 | local key="${1}" | |
31 | ||
32 | local path="$(_db_key2path "${key}")" | |
33 | ||
34 | if [ -e "${path}" ]; then | |
35 | return ${EXIT_TRUE} | |
36 | fi | |
37 | ||
38 | return ${EXIT_FALSE} | |
39 | } | |
40 | ||
41 | db_get() { | |
42 | local key="${1}" | |
43 | ||
44 | local path="$(_db_key2path "${key}")" | |
45 | fread "${path}" | |
46 | } | |
47 | ||
48 | db_set() { | |
49 | local key="${1}" | |
50 | shift | |
51 | ||
52 | local value="$@" | |
53 | local path="$(_db_key2path "${key}")" | |
54 | ||
55 | if [ -r "${path}" ]; then | |
56 | log DEBUG "Setting '${key}' = '${value}'" | |
57 | else | |
58 | log DEBUG "Adding '${key}' = '${value}'" | |
59 | fi | |
60 | ||
46954be3 | 61 | make_parent_directory "${path}" |
c041b631 MT |
62 | print "${value}" > "${path}" |
63 | } | |
64 | ||
65 | db_delete() { | |
66 | local key="${1}" | |
67 | ||
68 | local path="$(_db_key2path "${key}")" | |
69 | ||
70 | log DEBUG "Deleting key = '${key}'" | |
71 | ||
72 | if [ -d "${path}" ]; then | |
73 | _db_delete_recursive "${path}" | |
74 | fi | |
75 | ||
76 | rm -f - "${path}" | |
77 | } | |
78 | ||
79 | _db_delete_recursive() { | |
80 | local path="${1}" | |
81 | ||
82 | local key | |
83 | for key in $(_db_list_path "${path}"); do | |
84 | db_delete "${key}" | |
85 | done | |
86 | ||
87 | rmdir "${path}" | |
88 | } | |
89 | ||
90 | db_dump() { | |
91 | _db_dump_recursive "${NETWORK_DB_DIR}" | |
92 | } | |
93 | ||
94 | _db_dump_key() { | |
95 | local key="${1}" | |
96 | local val="$(db_get "${key}")" | |
97 | ||
98 | printf "%-38s = %s\n" "${key}" "${val}" | |
99 | } | |
100 | ||
101 | _db_dump_recursive() { | |
102 | local path="${1}" | |
103 | ||
104 | local key | |
105 | for key in $(_db_list_path "${path}"); do | |
106 | path="$(_db_key2path "${key}")" | |
107 | ||
108 | if [ -d "${path}" ]; then | |
109 | _db_dump_recursive "${path}" | |
110 | else | |
111 | _db_dump_key "${key}" | |
112 | fi | |
113 | done | |
114 | } | |
115 | ||
116 | _db_list_path() { | |
117 | local path="${1}" | |
118 | ||
119 | local element | |
120 | for element in ${path}/*; do | |
121 | [ -e "${element}" ] || continue | |
122 | ||
123 | print "${element#${NETWORK_DB_DIR}/}" | |
124 | done | |
125 | } | |
126 | ||
1c6a4e30 | 127 | db_connection_init() { |
059469a8 MT |
128 | if [ -e "${DB_CONNECTION_FILE}" ]; then |
129 | return ${EXIT_OK} | |
130 | fi | |
131 | ||
132 | log DEBUG "Creating connection database ${DB_CONNECTION_FILE}." | |
133 | ||
134 | sqlite3 -batch ${DB_CONNECTION_FILE} <<EOF | |
135 | CREATE TABLE log( | |
136 | id INTEGER PRIMARY KEY AUTOINCREMENT, | |
137 | zone TEXT, | |
138 | time INTEGER, | |
139 | state TEXT | |
140 | ); | |
141 | ||
142 | CREATE VIEW current as | |
143 | SELECT zone, time, state FROM log GROUP BY zone; | |
144 | ||
145 | EOF | |
146 | } | |
147 | ||
1c6a4e30 | 148 | db_connection_update() { |
059469a8 MT |
149 | local zone=${1} |
150 | local action=${2} | |
151 | shift 2 | |
152 | ||
153 | db_connection_init | |
154 | ||
155 | log DEBUG "Writing connection to database: zone=${zone} action=${action}." | |
156 | ||
157 | sqlite3 -batch ${DB_CONNECTION_FILE} <<EOF | |
158 | INSERT INTO log(zone, time, state) | |
159 | VALUES('${zone}', strftime('%s', 'now', 'utc'), '${action}'); | |
160 | EOF | |
161 | } | |
162 | ||
1c6a4e30 | 163 | db_ppp_init() { |
059469a8 MT |
164 | local file=${1} |
165 | ||
166 | if [ -e "${file}" ]; then | |
167 | return ${EXIT_OK} | |
168 | fi | |
169 | ||
170 | log DEBUG "Creating ppp database ${file}." | |
171 | ||
172 | sqlite3 -batch ${file} <<EOF | |
173 | CREATE TABLE accounting( | |
174 | id INTEGER PRIMARY KEY AUTOINCREMENT, | |
175 | time INTEGER, | |
176 | duration INTEGER, | |
177 | rcvd INTEGER, | |
178 | sent INTEGER | |
179 | ); | |
180 | EOF | |
181 | } | |
182 | ||
1c6a4e30 | 183 | db_ppp_update() { |
059469a8 MT |
184 | local zone=${1} |
185 | shift | |
186 | ||
187 | local rcvd | |
188 | local sent | |
189 | local duration | |
190 | ||
191 | while [ $# -gt 0 ]; do | |
192 | case "${1}" in | |
193 | --rcvd=*) | |
194 | rcvd=${1#--rcvd=} | |
195 | ;; | |
196 | --sent=*) | |
197 | sent=${1#--sent=} | |
198 | ;; | |
199 | --duration=*) | |
200 | duration=${1#--duration=} | |
201 | ;; | |
202 | esac | |
203 | shift | |
204 | done | |
205 | ||
206 | local file="${LOG_DIR}/ppp_${zone}.db" | |
207 | ||
208 | db_ppp_init ${file} | |
209 | ||
210 | local time=$(( $(date -u +"%s") - ${duration} )) | |
211 | ||
212 | log DEBUG "Writing accounting data: time=${time} duration=${duration} rcvd=${rcvd} sent=${sent}." | |
213 | ||
214 | sqlite3 -batch ${file} <<EOF | |
215 | INSERT INTO accounting(time, duration, rcvd, sent) | |
216 | VALUES('${time}', '${duration}', '${rcvd}', '${sent}'); | |
217 | EOF | |
218 | } |