Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Token authentication #7

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion cloudflare-analytics.sh
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ InfluxDBUser="USER" #User for Database
InfluxDBPassword="PASSWORD" #Password for Database

# Endpoint URL for login action
cloudflareauthmethod="TOKEN/APIKEY" #Choose Authentication method. Either TOKEN (single token with specific rights) or APIKEY (Global API KEY with Email)
cloudflareapikey="YOURAPIKEY"
cloudflarezone="YOURZONEID"
cloudflareemail="YOUREMAIL"
Expand Down Expand Up @@ -105,7 +106,19 @@ PAYLOAD="$PAYLOAD
##
# Cloudflare Analytics. This part will check on your Cloudflare Analytics, extracting the data from the last 24 hours
##
## URL with API email and key
case $cloudflareauthmethod in
APIKEY)
cloudflareUrl=$(curl -s -X POST -H "Content-Type: application/json" -H "X-Auth-Email: $cloudflareemail" -H "X-Auth-Key: $cloudflareapikey" --data "$(echo $PAYLOAD)" https://api.cloudflare.com/client/v4/graphql/ 2>&1 -k --silent)
;;
## URL with API Token
TOKEN)
cloudflareUrl=$(curl -s -X POST -H "Content-Type: application/json" -H "Authorization: Bearer $cloudflareapitoken" --data "$(echo $PAYLOAD)" https://api.cloudflare.com/client/v4/graphql/ 2>&1 -k --silent)
;;
*)
echo "No or Wrong Input in Config for 'cloudflareauthmethod'. Current value is $cloudflareauthmethod"
;;
esac

declare -i arraydays=0
for requests in $(echo "$cloudflareUrl" | jq -r '.data.viewer.zones[0].httpRequests1dGroups[].sum.requests'); do
Expand Down Expand Up @@ -162,4 +175,4 @@ cloudflareUrl=$(curl -s -X POST -H "Content-Type: application/json" -H "X-Auth-E

arraydays=$arraydays+1
fi
done
done