Cloudflare Zone Analytics API Script (GraphQL)
This Cloudflare Zone Analytics API script, cf-analytics-graphql.sh uses the new Cloudflare GraphQL API as the old zone analytics API has been deprecated and supports both traditional Cloudflare Global API Token authentication (CF_GLOBAL_TOKEN='y') and newer non-global Cloudflare permission based API Token authentication (CF_GLOBAL_TOKEN='n') which is currently in beta testing. The cf-analytics-graphql.sh script is currently default to CF_GLOBAL_TOKEN='n' for Cloudflare API Token based authentication.
If you have Cloudflare Argo enabled on your CF zone, also set CF_ARGO='y' - currently enabled by default for testing purposes.
Required Cloudflare API Token Permissions
By default the cf-analytics-graphql.sh script sets CF_GLOBAL_TOKEN='n' to use Cloudflare API Token. If you intend to use Cloudflare API Token, you'll need the Account and Zone level permissions for Logs:Read and Zone level permissions for Analytics:Read generated at https://dash.cloudflare.com/profile/api-tokens.
Usage
Populate your SSH session variables Where YOUR_CLOUDFLARE_API_KEY can either be your Cloudflare Global API Token or your generated Cloudflare API Token.
Sessions variables
export zid=YOUR_CLOUDFLARE_DOMAIN_ZONE_ID
export cfkey=YOUR_CLOUDFLARE_API_KEY
export cfemail=YOUR_CLOUDFLARE_ACCOUNT_EMAIL
Settings file
Or create a settings file cf-analytics-graphql.ini in same directory as cf-analytics-graphql.sh and populate your Cloudflare related variables within cf-analytics-graphql.ini with the following. Then you'll be able to run cf-analytics-graphql.sh.
zid=YOUR_CLOUDFLARE_DOMAIN_ZONE_ID
cfkey=YOUR_CLOUDFLARE_API_KEY
cfemail=YOUR_CLOUDFLARE_ACCOUNT_EMAIL
You can populate the settings file cf-analytics-graphql.ini with other overriding variables too:
If you have Cloudflare Argo enabled, you may want to also get Argo analytic stats
CF_ARGO='y'
zid=YOUR_CLOUDFLARE_DOMAIN_ZONE_ID
cfkey=YOUR_CLOUDFLARE_API_KEY
cfemail=YOUR_CLOUDFLARE_ACCOUNT_EMAIL
If you on Cloudflare Enterprise plan and enabled Enterprise Bot Management, you can enable additional stats for Bot Management score (botScore) and bot sore (botScoreSrcName) etc:
CF_ENTERPRISE='y'
zid=YOUR_CLOUDFLARE_DOMAIN_ZONE_ID
cfkey=YOUR_CLOUDFLARE_API_KEY
cfemail=YOUR_CLOUDFLARE_ACCOUNT_EMAIL
If you have Cloudflare Argo + Enterprise Botmanagement enabled:
CF_ARGO='y'
CF_ENTERPRISE='y'
zid=YOUR_CLOUDFLARE_DOMAIN_ZONE_ID
cfkey=YOUR_CLOUDFLARE_API_KEY
cfemail=YOUR_CLOUDFLARE_ACCOUNT_EMAIL
Switch from default Cloudflare API Token authentication to using Cloudflare Global API Key and Cloudflare Account Email
CF_GLOBAL_TOKEN='y'
zid=YOUR_CLOUDFLARE_DOMAIN_ZONE_ID
cfkey=YOUR_CLOUDFLARE_GLOBAL_API_KEY
cfemail=YOUR_CLOUDFLARE_ACCOUNT_EMAIL
Save JSON log output
JSON_OUTPUT_SAVE='y'
CF_GLOBAL_TOKEN='y'
zid=YOUR_CLOUDFLARE_DOMAIN_ZONE_ID
cfkey=YOUR_CLOUDFLARE_GLOBAL_API_KEY
cfemail=YOUR_CLOUDFLARE_ACCOUNT_EMAIL
The output would show something like:
JSON log saved: /home/cf-graphql-json-output/cf-graphql-ip.json
Save JSON log output and JSON to CSV converted log output
JSON_TO_CSV='y'
JSON_OUTPUT_SAVE='y'
CF_GLOBAL_TOKEN='y'
zid=YOUR_CLOUDFLARE_DOMAIN_ZONE_ID
cfkey=YOUR_CLOUDFLARE_GLOBAL_API_KEY
cfemail=YOUR_CLOUDFLARE_ACCOUNT_EMAIL
The output would show something like:
JSON log saved: /home/cf-graphql-json-output/cf-graphql-ip.json
CSV converted log saved: /home/cf-graphql-json-output/cf-graphql-ip.csv
Supported options include querying the Cloudflare Firewall GraphQL API filtering by ruleId, rayID and client visitor IP address.
./cf-analytics-graphql.sh
Usage:
---------------------------------------------
Zone Analytics
---------------------------------------------
./cf-analytics-graphql.sh hrs 72
./cf-analytics-graphql.sh days 3
---------------------------------------------
Firewall Events
---------------------------------------------
./cf-analytics-graphql.sh ruleid-mins 60 cfruleid
./cf-analytics-graphql.sh ruleid-hrs 72 cfruleid
./cf-analytics-graphql.sh ruleid-days 3 cfruleid
./cf-analytics-graphql.sh rayid-mins 60 cfrayid
./cf-analytics-graphql.sh rayid-hrs 72 cfrayid
./cf-analytics-graphql.sh rayid-days 3 cfrayid
./cf-analytics-graphql.sh ip-mins 60 request-ip|all
./cf-analytics-graphql.sh ip-hrs 72 request-ip|all
./cf-analytics-graphql.sh ip-days 3 request-ip|all
---------------------------------------------
Firewall Events filter by action
---------------------------------------------
./cf-analytics-graphql.sh ruleid-mins 60 cfruleid {block|log|challenge|challenge_solved|managed_block|managed_challenge|jschallenge|allow}
./cf-analytics-graphql.sh ruleid-hrs 72 cfruleid {block|log|challenge|challenge_solved|managed_block|managed_challenge|jschallenge|allow}
./cf-analytics-graphql.sh ruleid-days 3 cfruleid {block|log|challenge|challenge_solved|managed_block|managed_challenge|jschallenge|allow}
./cf-analytics-graphql.sh rayid-mins 60 cfrayid {block|log|challenge|challenge_solved|managed_block|managed_challenge|jschallenge|allow}
./cf-analytics-graphql.sh rayid-hrs 72 cfrayid {block|log|challenge|challenge_solved|managed_block|managed_challenge|jschallenge|allow}
./cf-analytics-graphql.sh rayid-days 3 cfrayid {block|log|challenge|challenge_solved|managed_block|managed_challenge|jschallenge|allow}
./cf-analytics-graphql.sh ip-mins 60 request-ip|all {block|log|challenge|challenge_solved|managed_block|managed_challenge|jschallenge|allow}
./cf-analytics-graphql.sh ip-hrs 72 request-ip|all {block|log|challenge|challenge_solved|managed_block|managed_challenge|jschallenge|allow}
./cf-analytics-graphql.sh ip-days 3 request-ip|all {block|log|challenge|challenge_solved|managed_block|managed_challenge|jschallenge|allow}
---------------------------------------------
Firewall Events filter by action + limit XX
---------------------------------------------
./cf-analytics-graphql.sh ruleid-mins 60 cfruleid {block|log|challenge|challenge_solved|managed_block|managed_challenge|jschallenge|allow} 100
./cf-analytics-graphql.sh ruleid-hrs 72 cfruleid {block|log|challenge|challenge_solved|managed_block|managed_challenge|jschallenge|allow} 100
./cf-analytics-graphql.sh ruleid-days 3 cfruleid {block|log|challenge|challenge_solved|managed_block|managed_challenge|jschallenge|allow} 100
./cf-analytics-graphql.sh rayid-mins 60 cfrayid {block|log|challenge|challenge_solved|managed_block|managed_challenge|jschallenge|allow} 100
./cf-analytics-graphql.sh rayid-hrs 72 cfrayid {block|log|challenge|challenge_solved|managed_block|managed_challenge|jschallenge|allow} 100
./cf-analytics-graphql.sh rayid-days 3 cfrayid {block|log|challenge|challenge_solved|managed_block|managed_challenge|jschallenge|allow} 100
./cf-analytics-graphql.sh ip-mins 60 request-ip|all {block|log|challenge|challenge_solved|managed_block|managed_challenge|jschallenge|allow} 100
./cf-analytics-graphql.sh ip-hrs 72 request-ip|all {block|log|challenge|challenge_solved|managed_block|managed_challenge|jschallenge|allow} 100
./cf-analytics-graphql.sh ip-days 3 request-ip|all {block|log|challenge|challenge_solved|managed_block|managed_challenge|jschallenge|allow} 100
---------------------------------------------
Firewall Events filter by action + limit XX + hostname
---------------------------------------------
./cf-analytics-graphql.sh ruleid-mins 60 cfruleid {block|log|challenge|challenge_solved|managed_block|managed_challenge|jschallenge|allow} 100 hostname
./cf-analytics-graphql.sh ruleid-hrs 72 cfruleid {block|log|challenge|challenge_solved|managed_block|managed_challenge|jschallenge|allow} 100 hostname
./cf-analytics-graphql.sh ruleid-days 3 cfruleid {block|log|challenge|challenge_solved|managed_block|managed_challenge|jschallenge|allow} 100 hostname
./cf-analytics-graphql.sh rayid-mins 60 cfrayid {block|log|challenge|challenge_solved|managed_block|managed_challenge|jschallenge|allow} 100 hostname
./cf-analytics-graphql.sh rayid-hrs 72 cfrayid {block|log|challenge|challenge_solved|managed_block|managed_challenge|jschallenge|allow} 100 hostname
./cf-analytics-graphql.sh rayid-days 3 cfrayid {block|log|challenge|challenge_solved|managed_block|managed_challenge|jschallenge|allow} 100 hostname
./cf-analytics-graphql.sh ip-mins 60 request-ip|all {block|log|challenge|challenge_solved|managed_block|managed_challenge|jschallenge|allow} 100 hostname
./cf-analytics-graphql.sh ip-hrs 72 request-ip|all {block|log|challenge|challenge_solved|managed_block|managed_challenge|jschallenge|allow} 100 hostname
./cf-analytics-graphql.sh ip-days 3 request-ip|all {block|log|challenge|challenge_solved|managed_block|managed_challenge|jschallenge|allow} 100 hostname
---------------------------------------------
Firewall Events filter by action + limit XX + hostname + referrer
---------------------------------------------
./cf-analytics-graphql.sh ruleid-mins 60 cfruleid {block|log|challenge|challenge_solved|managed_block|managed_challenge|jschallenge|allow} 100 hostname|none referrer|none|empty|notempty
./cf-analytics-graphql.sh ruleid-hrs 72 cfruleid {block|log|challenge|challenge_solved|managed_block|managed_challenge|jschallenge|allow} 100 hostname|none referrer|none|empty|notempty
./cf-analytics-graphql.sh ruleid-days 3 cfruleid {block|log|challenge|challenge_solved|managed_block|managed_challenge|jschallenge|allow} 100 hostname|none referrer|none|empty|notempty
./cf-analytics-graphql.sh rayid-mins 60 cfrayid {block|log|challenge|challenge_solved|managed_block|managed_challenge|jschallenge|allow} 100 hostname|none referrer|none|empty|notempty
./cf-analytics-graphql.sh rayid-hrs 72 cfrayid {block|log|challenge|challenge_solved|managed_block|managed_challenge|jschallenge|allow} 100 hostname|none referrer|none|empty|notempty
./cf-analytics-graphql.sh rayid-days 3 cfrayid {block|log|challenge|challenge_solved|managed_block|managed_challenge|jschallenge|allow} 100 hostname|none referrer|none|empty|notempty
./cf-analytics-graphql.sh ip-mins 60 request-ip|all {block|log|challenge|challenge_solved|managed_block|managed_challenge|jschallenge|allow} 100 hostname|none referrer|none|empty|notempty
./cf-analytics-graphql.sh ip-hrs 72 request-ip|all {block|log|challenge|challenge_solved|managed_block|managed_challenge|jschallenge|allow} 100 hostname|none referrer|none|empty|notempty
./cf-analytics-graphql.sh ip-days 3 request-ip|all {block|log|challenge|challenge_solved|managed_block|managed_challenge|jschallenge|allow} 100 hostname|none referrer|none|empty|notempty
Example Web Analytics For Past 72hrs
Example web traffic analytics for Cloudflare Zone site where Cloudflare Argo is enabled and thus `CF_ARGO='y' is set in script.