{"id":28718793,"url":"https://github.com/lolo1580/cloudflare-influxdbv2","last_synced_at":"2026-04-09T07:03:35.292Z","repository":{"id":297488899,"uuid":"996947756","full_name":"lolo1580/cloudflare-influxdbv2","owner":"lolo1580","description":"Collect and send Cloudflare GraphQL metrics to InfluxDB v2 for Grafana dashboards.","archived":false,"fork":false,"pushed_at":"2025-06-14T18:01:45.000Z","size":11,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":0,"default_branch":"main","last_synced_at":"2025-06-14T19:18:52.212Z","etag":null,"topics":["analytics","bash","cloudflare","grafana","graphql","influxdb2","metrics"],"latest_commit_sha":null,"homepage":"","language":"Shell","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/lolo1580.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2025-06-05T17:55:26.000Z","updated_at":"2025-06-14T18:00:50.000Z","dependencies_parsed_at":"2025-06-05T19:24:31.395Z","dependency_job_id":"06c52eab-42fb-4efe-a0c7-ad3ebbc17f5a","html_url":"https://github.com/lolo1580/cloudflare-influxdbv2","commit_stats":null,"previous_names":["lolo1580/cloudflare-influxdbv2"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/lolo1580/cloudflare-influxdbv2","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lolo1580%2Fcloudflare-influxdbv2","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lolo1580%2Fcloudflare-influxdbv2/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lolo1580%2Fcloudflare-influxdbv2/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lolo1580%2Fcloudflare-influxdbv2/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/lolo1580","download_url":"https://codeload.github.com/lolo1580/cloudflare-influxdbv2/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lolo1580%2Fcloudflare-influxdbv2/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":259924674,"owners_count":22932782,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["analytics","bash","cloudflare","grafana","graphql","influxdb2","metrics"],"created_at":"2025-06-15T05:04:30.699Z","updated_at":"2025-12-30T22:33:13.590Z","avatar_url":"https://github.com/lolo1580.png","language":"Shell","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Cloudflare Analytics to InfluxDB v2\r\n\r\nThis script collects Cloudflare metrics using the GraphQL API and pushes them to an InfluxDB v2 bucket. It is designed to feed a Grafana dashboard (e.g., Jorge de la Cruz’s dashboard) with the following metrics:\r\n\r\n- Bandwidth (`bytes`, `cached_bytes`)\r\n- Number of requests (`requests`, `cached_requests`)\r\n- Number of threats (`threats`)\r\n- Page views (`pageviews`)\r\n- Unique visitors (`uniques`)\r\n- Request distribution by country (`country_requests`)\r\n\r\n## Prerequisites\r\n\r\n1. **InfluxDB v2** installed and reachable (URL, port, organization, bucket, token).\r\n2. **Cloudflare**: an API token with read permissions for the GraphQL API on the desired zone.\r\n3. **Tools** on the machine where the script will run:\r\n   - `bash` (version 4+)\r\n   - `curl` (for HTTP requests)\r\n   - `jq` (for JSON parsing)\r\n   - Network access to `api.cloudflare.com` and the InfluxDB instance\r\n\r\n## Installation\r\n\r\n```bash\r\nmkdir -p ~/script/cloudflare\r\ncd ~/script/cloudflare\r\n# Copy cloudflare-analytics.sh here\r\nchmod +x cloudflare-analytics.sh\r\n```\r\n\r\nInstall dependencies:\r\n\r\n```bash\r\nsudo apt update\r\nsudo apt install -y curl jq\r\n```\r\n\r\n## Configuration\r\n\r\nEdit the variables at the top of the script to match your environment:\r\n\r\n```bash\r\nInfluxDBURL=\"http://YOUR_INFLUXDB_IP\"\r\nInfluxDBPort=\"8086\"\r\nInfluxDBBucket=\"cloudflare\"\r\nInfluxDBOrg=\"YourOrgName\"\r\nInfluxDBToken=\"your_influxdb_token\"\r\n\r\ncloudflareapikey=\"your_cloudflare_token\"\r\ncloudflarezone=\"your_cloudflare_zone_id\"\r\ncloudflareemail=\"your_cloudflare_email\"\r\n```\r\n\r\n## Script Workflow\r\n\r\n1. Defines the last 24 hours as the time range.\r\n2. Sends a GraphQL request to Cloudflare to retrieve:\r\n   - Daily metrics: `bytes`, `cachedBytes`, `requests`, `cachedRequests`, `threats`, `pageViews`, and `countryMap`\r\n   - Unique visitors (`uniques`)\r\n3. Parses and transforms data into InfluxDB Line Protocol.\r\n4. Pushes each metric and country-specific data to InfluxDB.\r\n5. Sends a test point for debugging.\r\n\r\n## Manual Execution\r\n\r\n```bash\r\ncd ~/script/cloudflare\r\n./cloudflare-analytics.sh\r\n```\r\n\r\n## Automating with systemd (Optional)\r\n\r\nCreate a service file at `/etc/systemd/system/cloudflare-analytics.service`:\r\n\r\n```ini\r\n[Unit]\r\nDescription=Cloudflare Analytics to InfluxDB\r\nAfter=network.target\r\n\r\n[Service]\r\nType=oneshot\r\nUser=YOUR_USERNAME\r\nWorkingDirectory=/home/YOUR_USERNAME/script/cloudflare\r\nExecStart=/home/YOUR_USERNAME/script/cloudflare/cloudflare-analytics.sh\r\n```\r\n\r\nCreate a timer at `/etc/systemd/system/cloudflare-analytics.timer`:\r\n\r\n```ini\r\n[Unit]\r\nDescription=Run cloudflare-analytics.service every 5 minutes\r\n\r\n[Timer]\r\nOnBootSec=5min\r\nOnUnitActiveSec=5min\r\nAccuracySec=1s\r\nPersistent=true\r\n\r\n[Install]\r\nWantedBy=timers.target\r\n```\r\n\r\nEnable and start the timer:\r\n\r\n```bash\r\nsudo systemctl daemon-reload\r\nsudo systemctl enable cloudflare-analytics.timer\r\nsudo systemctl start cloudflare-analytics.timer\r\n```\r\n\r\n## Verify in InfluxDB\r\n\r\n### CLI:\r\n\r\n```bash\r\ninflux query 'from(bucket: \"cloudflare\") |\u003e range(start: -7d) |\u003e limit(n: 10)' \\\r\n  --org YourOrgName \\\r\n  --host http://YOUR_INFLUXDB_IP:8086 \\\r\n  --token \"your_token\"\r\n```\r\n\r\n### UI:\r\n\r\nUse the InfluxDB Data Explorer:\r\n- Select the `cloudflare` bucket\r\n- Pick the last 7 days as time range\r\n- Use measurement `cloudflare`, fields (e.g., bytes, requests)\r\n- For per-country stats, use the tag `country` and field `country_requests`\r\n\r\n## Grafana Dashboard\r\n\r\nImport Jorge de la Cruz’s dashboard or your custom one.\r\n- Data source: InfluxDB v2 with `http://\u003chost\u003e:8086`, org, bucket, token.\r\n- Variable `zone`: value is your `zone_id`\r\n- Recommended time range: last 7 days or 24 hours\r\n\r\n## Customization\r\n\r\n- Adjust `back_seconds` to collect a longer range\r\n- Extend GraphQL query for more fields (HTTP status, browsers)\r\n- Change timer interval via `OnUnitActiveSec` in the `.timer` file\r\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flolo1580%2Fcloudflare-influxdbv2","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Flolo1580%2Fcloudflare-influxdbv2","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flolo1580%2Fcloudflare-influxdbv2/lists"}