Skip to main content
kitkat09811
Explorer
Oct 25, 2024 3:18 PM UTC
Question

Need help creating a custom dataset for Fortianalyzer

FYI, im not a coder of sql expert by any means...

here is my ChatGPT generated Fortianalyzer SQL query:

-- Main query: Count occurrences of srcip, dstport, and policyid SELECT     srcip,     dstport,     policyid,     COUNT(*) AS event_count FROM $log  -- Replace with the appropriate log source WHERE srcip IS NOT NULL AND dstport IS NOT NULL AND policyid IS NOT NULL GROUP BY srcip, dstport, policyid ORDER BY policyid, srcip;  -- Order by policyid and srcip  -- Summary query: Unique dstports per policyid SELECT      CONCAT('PolicyID ', CAST(policyid AS STRING), ' unique dstports') AS srcip,     STRING_AGG(DISTINCT dstport, ', ') AS unique_dstports,     policyid,     NULL AS event_count FROM $log WHERE srcip IS NOT NULL AND dstport IS NOT NULL AND policyid IS NOT NULL GROUP BY policyid ORDER BY policyid;

Wen i paste this code in the SQL query dataset window, I get this error and don't know how to fix this.

Validate Result
ERROR: 'group by' or 'order by' clause is expected in hcache.

Ultimately, i am trying to do a report that will output a list of policyID's and the unique destination ports being used on each policy in order to clamp down on the service ports required for each policy.

4 replies

spoojary
Staff
Staff
Oct 25, 2024 8:26 PM UTC
kitkat09811
Explorer
Oct 28, 2024 12:29 PM UTC

i dont need a link to document which I already read. As i stated, im trying to get the SQL to give me the output as described

JudiFulo
Staff
Staff
Apr 30, 2025 9:05 AM UTC

You need to add GROUP BY every field from SELECT which doesn't part of an aggregate function like count(). In your example the GROUP BY of summary query part is missing srcip, unique_dstports and event_count as well

farhanahmed
Staff
Staff
May 2, 2025 12:12 AM UTC

Hi,

 

It seems that you are trying to create a report for policy hit count including the dstport.
You can refer to the article below and may be modify it to add dstport as well.
https://community.fortinet.com/t5/FortiAnalyzer/Technical-Tip-Policy-Hit-Count-Report/ta-p/316140

SELECT policyid, dstport, count (*) AS policyhit FROM $log WHERE $filter GROUP BY policyid, dstport ORDER BY policyhit desc


But for the error you are receiving, @JudiFulo has explained how to fix it.

 

www.farhan.ch
Thought Leadership. Security Summit. Thursday, November 12th, PGA National Resort, Palm Beach Gardens, FL.
Security Operations Summit: Modernize SecOps. Operate AI-Native.AMER: November 4 | 9:00 AM PST. India and SAARC: November 5 | 10:00 AM IST. EMEA: November 5 | 10:30 AM CET. APAC: November 5 | 11:00 AM SGT