Fortianalyzer: SQL and Datasets (Supplementary Material)
Fortianalyzer: SQL and Datasets (Supplementary Material)
FortiAnalyzer 6.4
© Copyright Fortinet Inc. All rights reserved.
Last Modified:
Wednesday, February 16, 2022
Lesson Overview
5
SQL – The Declarative Language
6
Basic Data Manipulation Constructs (DML)
• SELECT
• Retrieve and display data from one This is the only query
or more database tables (read-only query)
• SELECT … FROM … WHERE
statement used by
FortiAnalyzer for reports
• INSERT
• Add new rows of data into a table
• INSERT INTO … VALUES …
• UPDATE
• Modify existing data in a table
• UPDATE … SET … WHERE
• DELETE
• Remove rows of data from a table
• DELETE FROM … WHERE
7
SELECT Statement
• The SELECT statement retrieves the log data you want from the database
• Must specify criteria using a recognized/supported clause
Clause Definition
FROM Selects the table or views.
WHERE Sets the conditions (all rows that do not satisfy the condition are eliminated)
GROUP BY Collects data across multiple records and groups the results by one or more columns.
8
SELECT and FROM
• Use the SELECT query to ask specific questions of the database
Column from database that contains The log type under which the data is
the value(s) you want to retrieve contained (ie. Traffic, Web filter, etc.)
• When designing queries for SQL reports on the FortiAnalyzer device, the Log Type
is assigned to a variable called $log
SELECT *
returns all
data
9
Multiple Log Types
• Search multiple log types
• Combine the data so that you can compare and contrast information
10
WHERE
• The WHERE clause requests data with certain characteristics
• The expression specifies a stored value in the database
11
GROUP BY
• GROUP BY statement is usually used in conjunction with aggregate functions to
group data by one or more columns.
• Returns one output row for each group
• Can form groups within groups
• Each item in the SELECT list produces a single value per set
12
ORDER BY
• By default, rows of an SQL query result table are not arranged in a particular order
13
LIMIT and OFFSET
• The LIMIT clause limits the number of records retrieved from the query result
• Useful in large deployments to help limit the CPU/memory usage for reports
• Can be combined with ORDER BY asc to get the “top <x> results”
Specify how many records to return Specify how many records to skip
14
Creating a Dataset in FortiAnalyzer
Reports > Report Definitions > Datasets
15
Analyzing the Dataset Test Results
Reports > Report Definitions > Datasets
16
Lesson Progress
19
NULLIF
• NULLIF function takes two arguments: if the first two arguments are equal, then
NULL is returned. Otherwise, the first argument is returned.
20
COALESCE
• Returns the first of its arguments that is not NULL. NULL is returned only if all
arguments are NULL
21
Aggregate Functions
• Aggregate functions perform a calculation on a set of values in a column and return
a single value
Aggregate functions
AVG(expression) Returns the average value
COUNT(expression) Returns the number of rows
COUNT(*) Returns all rows, even if some columns contain a NULL value
22
Operators
• Reserved word or character used primarily in the WHERE clause to perform various
operations
• Arithmetic operators
• Comparison operators
• Logical operators
23
Arithmetic Operators
• Perform mathematical operations on two expressions of one or more of the data
types of the numeric data type category
Operator Description
+ Addition – adds values on either side of the operator
- Subtraction – Subtracts right hand operand from left hand operand
* Multiplication – Multiplies values on either side of the operator
/ Division – Divides left hand operand by right hand operand
% Modulus – Divides left hand operand by right hand operand and returns
remainder
24
Comparison Operators
• Test whether two expressions are the same
• Can be used on all expressions except text, ntext, or image data types
Operator Description
= Equal to
> Greater than
< Less than
>= Greater than or equal to
<= Less than or equal to
<> Not equal to
!= Not equal to (not ISO standard)
!< Not less than (not ISO standard)
!> Not greater than (not ISO standard)
25
Logical Operators
• Test for the truth of some condition
• Return a boolean data type with a value of TRUE, FALSE, or UNKNOWN
Operator Description
ALL TRUE if all of a set of comparisons are TRUE.
AND TRUE if both Boolean expressions are TRUE.
ANY TRUE if any one of a set of comparisons are TRUE.
BETWEEN TRUE if the operand is within a range.
EXISTS TRUE if a subquery contains any rows.
IN TRUE if the operand is equal to one of a list of expressions.
LIKE TRUE if the operand matches a pattern.
NOT Reverses the value of any other Boolean operator.
OR TRUE if either Boolean expression is TRUE.
SOME TRUE if some of a set of comparisons are TRUE.
26
Lesson Progress
29
nullifa
• nullifna(expression)
• Inverse operation of COALESCE
• Can be used to filter out values with N/A and n/a from logs
• SQL syntax SELECT NULLIF(NULLIF(<value>, 'N/A'), 'n/a')
30
FortiAnalyzer Functions: email_domain, email_user
• email_domain: Retrieves anything after the @ symbol in an email address
• email_user: Retrieves anything before the @ symbol in an email address
31
FortiAnalyzer Functions: from_dtime, from_itime
• from_dtime(bigint): Returns device timestamp without time zone
• from_itime(bigint): Returns FortiAnalyzer’s timestamp without time zone
32
Macros
• FortiAnalyzer Date/Time macros
33
Lesson Progress