Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
O
OpnSense
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Kulya
OpnSense
Commits
88a0b222
Commit
88a0b222
authored
May 08, 2016
by
Ad Schellevis
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
(network insight) check if target table exists before fetching data
parent
0e916fb2
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
78 additions
and
76 deletions
+78
-76
aggregate.py
src/opnsense/scripts/netflow/lib/aggregate.py
+78
-76
No files found.
src/opnsense/scripts/netflow/lib/aggregate.py
View file @
88a0b222
...
@@ -355,60 +355,61 @@ class BaseFlowAggregator(object):
...
@@ -355,60 +355,61 @@ class BaseFlowAggregator(object):
:return: iterator returning dict records (start_time, end_time, [fields], octets, packets)
:return: iterator returning dict records (start_time, end_time, [fields], octets, packets)
"""
"""
result
=
list
()
result
=
list
()
select_fields
=
self
.
_valid_fields
(
fields
)
if
self
.
is_db_open
()
and
'timeserie'
in
self
.
_known_targets
:
filter_fields
=
[]
select_fields
=
self
.
_valid_fields
(
fields
)
query_params
=
{}
filter_fields
=
[]
if
value_field
==
'octets'
:
query_params
=
{}
value_sql
=
'sum(octets)'
if
value_field
==
'octets'
:
elif
value_field
==
'packets'
:
value_sql
=
'sum(octets)'
value_sql
=
'sum(packets)'
elif
value_field
==
'packets'
:
else
:
value_sql
=
'sum(packets)'
value_sql
=
'0'
else
:
value_sql
=
'0'
# query filters, correct start_time for resolution
query_params
[
'start_time'
]
=
self
.
_parse_timestamp
((
int
(
start_time
/
self
.
resolution
))
*
self
.
resolution
)
# query filters, correct start_time for resolution
query_params
[
'end_time'
]
=
self
.
_parse_timestamp
(
end_time
)
query_params
[
'start_time'
]
=
self
.
_parse_timestamp
((
int
(
start_time
/
self
.
resolution
))
*
self
.
resolution
)
if
data_filters
:
query_params
[
'end_time'
]
=
self
.
_parse_timestamp
(
end_time
)
for
data_filter
in
data_filters
.
split
(
','
):
if
data_filters
:
tmp
=
data_filter
.
split
(
'='
)[
0
]
.
strip
()
for
data_filter
in
data_filters
.
split
(
','
):
if
tmp
in
self
.
agg_fields
and
data_filter
.
find
(
'='
)
>
-
1
:
tmp
=
data_filter
.
split
(
'='
)[
0
]
.
strip
()
filter_fields
.
append
(
tmp
)
if
tmp
in
self
.
agg_fields
and
data_filter
.
find
(
'='
)
>
-
1
:
query_params
[
tmp
]
=
'='
.
join
(
data_filter
.
split
(
'='
)[
1
:])
filter_fields
.
append
(
tmp
)
query_params
[
tmp
]
=
'='
.
join
(
data_filter
.
split
(
'='
)[
1
:])
if
len
(
select_fields
)
>
0
:
# construct sql query to filter and select data
if
len
(
select_fields
)
>
0
:
sql_select
=
'select
%
s'
%
','
.
join
(
select_fields
)
# construct sql query to filter and select data
sql_select
+=
',
%
s as total, max(last_seen) last_seen
\n
'
%
value_sql
sql_select
=
'select
%
s'
%
','
.
join
(
select_fields
)
sql_select
+=
'from timeserie
\n
'
sql_select
+=
',
%
s as total, max(last_seen) last_seen
\n
'
%
value_sql
sql_select
+=
'where mtime >= :start_time and mtime < :end_time
\n
'
sql_select
+=
'from timeserie
\n
'
for
filter_field
in
filter_fields
:
sql_select
+=
'where mtime >= :start_time and mtime < :end_time
\n
'
sql_select
+=
' and
%
s = :
%
s
\n
'
%
(
filter_field
,
filter_field
)
for
filter_field
in
filter_fields
:
sql_select
+=
'group by
%
s
\n
'
%
','
.
join
(
select_fields
)
sql_select
+=
' and
%
s = :
%
s
\n
'
%
(
filter_field
,
filter_field
)
sql_select
+=
'order by
%
s desc '
%
value_sql
sql_select
+=
'group by
%
s
\n
'
%
','
.
join
(
select_fields
)
sql_select
+=
'order by
%
s desc '
%
value_sql
# execute select query
cur
=
self
.
_db_connection
.
cursor
()
# execute select query
cur
.
execute
(
sql_select
,
query_params
)
cur
=
self
.
_db_connection
.
cursor
()
cur
.
execute
(
sql_select
,
query_params
)
# fetch all data, to a max of [max_hits] rows.
field_names
=
(
map
(
lambda
x
:
x
[
0
],
cur
.
description
))
# fetch all data, to a max of [max_hits] rows.
for
record
in
cur
.
fetchall
():
field_names
=
(
map
(
lambda
x
:
x
[
0
],
cur
.
description
))
result_record
=
dict
()
for
record
in
cur
.
fetchall
():
for
field_indx
in
range
(
len
(
field_names
)):
result_record
=
dict
()
if
len
(
record
)
>
field_indx
:
for
field_indx
in
range
(
len
(
field_names
)):
result_record
[
field_names
[
field_indx
]]
=
record
[
field_indx
]
if
len
(
record
)
>
field_indx
:
if
len
(
result
)
<
max_hits
:
result_record
[
field_names
[
field_indx
]]
=
record
[
field_indx
]
result
.
append
(
result_record
)
if
len
(
result
)
<
max_hits
:
else
:
result
.
append
(
result_record
)
if
len
(
result
)
==
max_hits
:
else
:
# generate row for "rest of data"
if
len
(
result
)
==
max_hits
:
result
.
append
({
'total'
:
0
})
# generate row for "rest of data"
for
key
in
result_record
:
result
.
append
({
'total'
:
0
})
if
key
not
in
result
[
-
1
]:
for
key
in
result_record
:
result
[
-
1
][
key
]
=
""
if
key
not
in
result
[
-
1
]:
result
[
-
1
][
'total'
]
+=
result_record
[
'total'
]
result
[
-
1
][
key
]
=
""
# close cursor
result
[
-
1
][
'total'
]
+=
result_record
[
'total'
]
cur
.
close
()
# close cursor
cur
.
close
()
return
result
return
result
...
@@ -418,25 +419,26 @@ class BaseFlowAggregator(object):
...
@@ -418,25 +419,26 @@ class BaseFlowAggregator(object):
:param end_time: end timestamp
:param end_time: end timestamp
:return: iterator
:return: iterator
"""
"""
query_params
=
{}
if
self
.
is_db_open
()
and
'timeserie'
in
self
.
_known_targets
:
query_params
[
'start_time'
]
=
self
.
_parse_timestamp
((
int
(
start_time
/
self
.
resolution
))
*
self
.
resolution
)
query_params
=
{}
query_params
[
'end_time'
]
=
self
.
_parse_timestamp
(
end_time
)
query_params
[
'start_time'
]
=
self
.
_parse_timestamp
((
int
(
start_time
/
self
.
resolution
))
*
self
.
resolution
)
sql_select
=
'select mtime start_time, '
query_params
[
'end_time'
]
=
self
.
_parse_timestamp
(
end_time
)
sql_select
+=
'
%
s, octets, packets, last_seen as "last_seen [timestamp]"
\n
'
%
','
.
join
(
self
.
agg_fields
)
sql_select
=
'select mtime start_time, '
sql_select
+=
'from timeserie
\n
'
sql_select
+=
'
%
s, octets, packets, last_seen as "last_seen [timestamp]"
\n
'
%
','
.
join
(
self
.
agg_fields
)
sql_select
+=
'where mtime >= :start_time and mtime < :end_time
\n
'
sql_select
+=
'from timeserie
\n
'
cur
=
self
.
_db_connection
.
cursor
()
sql_select
+=
'where mtime >= :start_time and mtime < :end_time
\n
'
cur
.
execute
(
sql_select
,
query_params
)
cur
=
self
.
_db_connection
.
cursor
()
cur
.
execute
(
sql_select
,
query_params
)
# fetch all data, to a max of [max_hits] rows.
field_names
=
(
map
(
lambda
x
:
x
[
0
],
cur
.
description
))
# fetch all data, to a max of [max_hits] rows.
while
True
:
field_names
=
(
map
(
lambda
x
:
x
[
0
],
cur
.
description
))
record
=
cur
.
fetchone
()
while
True
:
if
record
is
None
:
record
=
cur
.
fetchone
()
break
if
record
is
None
:
else
:
break
result_record
=
dict
()
else
:
for
field_indx
in
range
(
len
(
field_names
)):
result_record
=
dict
()
if
len
(
record
)
>
field_indx
:
for
field_indx
in
range
(
len
(
field_names
)):
result_record
[
field_names
[
field_indx
]]
=
record
[
field_indx
]
if
len
(
record
)
>
field_indx
:
yield
result_record
result_record
[
field_names
[
field_indx
]]
=
record
[
field_indx
]
yield
result_record
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment