-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
dfa9246
commit 95c4cf1
Showing
5 changed files
with
138 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
# http://editorconfig.org | ||
|
||
root = true | ||
|
||
[*] | ||
indent_style = space | ||
indent_size = 4 | ||
insert_final_newline = true | ||
trim_trailing_whitespace = true | ||
end_of_line = lf | ||
charset = utf-8 | ||
|
||
# Docstrings and comments use max_line_length = 79 | ||
[*.py] | ||
max_line_length = 119 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,9 @@ | ||
# telegraf-cloudflare | ||
Plugin for Telegraf for gathering statistics from Cloudflare | ||
|
||
This script is in beta version, use with caution | ||
|
||
## Installation | ||
``` | ||
$ pip install telegraf-cloudflare | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
requests>=2.19 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import setuptools | ||
|
||
with open("README.md", "r") as fh: | ||
long_description = fh.read() | ||
|
||
|
||
def read_file(file_name): | ||
with open(file_name, 'r') as f: | ||
return f.read() | ||
|
||
|
||
setuptools.setup(name='telegraf-cloudflare', | ||
description='Plugin for Telegraf for gathering statistics from Cloudflare', | ||
long_description=long_description, | ||
version='0.1.0', | ||
url='https://github.com/SebastianCzoch/telegraf-cloudflare', | ||
author='Sebastian Czoch', | ||
author_email='[email protected]', | ||
license='MIT', | ||
classifiers=[ | ||
'Development Status :: 4 - Beta', | ||
'Intended Audience :: System Administrators', | ||
'License :: OSI Approved :: MIT License' | ||
'Programming Language :: Python :: 2' | ||
'Operating System :: OS Independent', | ||
], | ||
packages=setuptools.find_packages(), | ||
install_requires=read_file('requirements.txt').splitlines(), | ||
scripts=['telegraf-cloudflare'] | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
#!/usr/bin/env python | ||
|
||
import sys | ||
import argparse | ||
import requests | ||
|
||
CLOUDFLARE_API_URL = 'https://api.cloudflare.com/client/v4' | ||
|
||
|
||
def __handle_cli(args): | ||
parser = argparse.ArgumentParser( | ||
description='Plugin for Telegraf for gathering statistics from Cloudflare') | ||
|
||
parser.add_argument( | ||
'--zone-id', | ||
help='Cloudflare zone ID', | ||
default=None, | ||
required=True | ||
) | ||
|
||
parser.add_argument( | ||
'--email', | ||
help='Cloudflare email', | ||
default=None, | ||
required=True | ||
) | ||
|
||
parser.add_argument( | ||
'--api-key', | ||
help='Cloudflare API key', | ||
default=None, | ||
required=True | ||
) | ||
|
||
parser.add_argument( | ||
'--interval', | ||
help='Interval', | ||
default=360, | ||
) | ||
|
||
return parser.parse_args(args) | ||
|
||
|
||
def __get_stats(cli): | ||
url = '%s/zones/%s/analytics/dashboard?since=-%d&continuous=true' % ( | ||
CLOUDFLARE_API_URL, cli.zone_id, cli.interval) | ||
|
||
headers = { | ||
'X-Auth-Email': cli.email, | ||
'X-Auth-Key': cli.api_key, | ||
'Content-Type': 'application/json', | ||
} | ||
|
||
resp = requests.get(url, headers=headers) | ||
if resp.status_code != 200: | ||
raise Exception( | ||
'Invalid status code, got %s, expected 200' % resp.status_code) | ||
|
||
return resp.json()['result']['totals'] | ||
|
||
|
||
def __format_to_telegraf(zone_id, values): | ||
line = ( | ||
'cloudflare,zone_id=%s ' | ||
'requests_all=%d,requests_cached=%d,requests_uncached=%d,' | ||
'requests_encrypted=%d,requests_unencrypted=%d,' | ||
'bandwidth_all=%d,bandwidth_cached=%d,bandwidth_uncached=%d' | ||
) | ||
|
||
requests = values['requests'] | ||
bandwidth = values['bandwidth'] | ||
|
||
return line % (zone_id, requests['all'], requests['cached'], requests['uncached'], | ||
requests['ssl']['encrypted'], requests['ssl']['unencrypted'], bandwidth['all'], bandwidth['cached'], | ||
bandwidth['uncached']) | ||
|
||
|
||
def main(args): | ||
cli = __handle_cli(args) | ||
stats = __get_stats(cli) | ||
sys.stdout.write(__format_to_telegraf(cli.zone_id, stats)) | ||
|
||
|
||
if __name__ == '__main__': | ||
main(sys.argv[1:]) |