Skip to content

Commit

Permalink
Add first version of script
Browse files Browse the repository at this point in the history
  • Loading branch information
SebastianCzoch committed Aug 21, 2018
1 parent dfa9246 commit 95c4cf1
Show file tree
Hide file tree
Showing 5 changed files with 138 additions and 0 deletions.
15 changes: 15 additions & 0 deletions .editorconfig
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
7 changes: 7 additions & 0 deletions README.md
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
```
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
requests>=2.19
30 changes: 30 additions & 0 deletions setup.py
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']
)
85 changes: 85 additions & 0 deletions telegraf-cloudflare
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:])

0 comments on commit 95c4cf1

Please sign in to comment.