Skip to content

Commit

Permalink
Add many updates. More info on README.md#Changelog.
Browse files Browse the repository at this point in the history
  • Loading branch information
C1299593 Phillipe Smith Carvalho Chaves committed Aug 6, 2020
1 parent f9c3957 commit 6864ee8
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 16 deletions.
68 changes: 68 additions & 0 deletions .github/workflows/docker-publish.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
name: Docker

on:
push:
tags:
- v*

env:
# TODO: Change variable to your image's name.
IMAGE_NAME: rundeck-exporter

jobs:
# Run tests.
# See also https://docs.docker.com/docker-hub/builds/automated-testing/
test:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v2

- name: Run tests
run: |
if [ -f docker-compose.test.yml ]; then
docker-compose --file docker-compose.test.yml build
docker-compose --file docker-compose.test.yml run sut
else
docker build . --file Dockerfile
fi
# Push image to GitHub Packages.
# See also https://docs.docker.com/docker-hub/builds/
push:
# Ensure test job passes before pushing image.
needs: test

runs-on: ubuntu-latest
if: github.event_name == 'push'

steps:
- uses: actions/checkout@v2

- name: Build image
run: docker build . --file Dockerfile --tag $IMAGE_NAME

- name: Log into registry
run: echo "${{ secrets.GITHUB_TOKEN }}" | docker login docker.pkg.github.com -u ${{ github.actor }} --password-stdin

- name: Push image
run: |
IMAGE_ID=docker.pkg.github.com/${{ github.repository }}/$IMAGE_NAME
# Change all uppercase to lowercase
IMAGE_ID=$(echo $IMAGE_ID | tr '[A-Z]' '[a-z]')
# Strip git ref prefix from version
VERSION=$(echo "${{ github.ref }}" | sed -e 's,.*/\(.*\),\1,')
# Strip "v" prefix from tag name
[[ "${{ github.ref }}" == "refs/tags/"* ]] && VERSION=$(echo $VERSION | sed -e 's/^v//')
# Use Docker `latest` tag convention
[ "$VERSION" == "master" ] && VERSION=latest
echo IMAGE_ID=$IMAGE_ID
echo VERSION=$VERSION
docker tag $IMAGE_NAME $IMAGE_ID:$VERSION
docker push $IMAGE_ID:$VERSION
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ This exporter uses the prometheus_client and requests Python module to expose Ru
* RUNDECK_URL/api/*version*/system/info
* RUNDECK_URL/api/*version*/metrics/metrics

Where *version* represents the Rundeck API version, like: 29,30,31,etc.
Where *version* represents the Rundeck API version, like: 31,32,33,34,etc.

This code was tested on Rundeck API version 31.

Expand Down Expand Up @@ -160,7 +160,8 @@ docker run --rm -d -p 9620:9620 rundeck_exporter \
* --rundeck.projects.filter: Get executions only from listed projects (delimiter = space)
* --rundeck.projects.executions.limit: Limit project executions metrics query. Default: 20
* --rundeck.cached.requests.ttl: Rundeck cached requests (by now, only for rundeck.projects.executions) expiration time. Default: 120
* Add log messages through logging module
* Add code improvements
* Add cachetools to pip install on Dockerfile
* Add logging module to replace print calls
* Add better error handling
* Change args location, now located at class RundeckMetricsCollector
31 changes: 17 additions & 14 deletions rundeck_exporter.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,31 +104,32 @@ def __init__(self):
self.rundeck_node = ''

if not self.args.rundeck_url or not self.args.rundeck_token:
self.exit_with_msg('Rundeck URL and Token are required.')
self.exit_with_msg(msg='Rundeck URL and Token are required.', level='critical')

"""
Method to manage requests on Rundeck API Endpoints
"""
def request_data_from(self, endpoint: str) -> dict:
response = requests.get(
response = None

try:
response = requests.get(
f'{self.args.rundeck_url}/api/{self.args.rundeck_api_version}/{endpoint}',
headers={
'Accept': 'application/json',
'X-Rundeck-Auth-Token': self.args.rundeck_token
},
verify=not self.args.rundeck_skip_ssl
)
response_json = response.json()
self.client_requests_count += 1

try:
if response:
self.client_requests_count += 1
return response.json()
except requests.exceptions.SSLError:
self.exit_with_msg('SSL Certificate Verify Failed.')
except (OSError, requests.exceptions.ConnectionError):
self.exit_with_msg('Connection error.')
if response_json and response_json.get('error') == True:
raise Exception(response_json.get('message'))

return response_json
except Exception as error:
self.exit_with_msg(response.text if response.text else str(error))
self.exit_with_msg(msg=response.text if response else str(error), level='critical')

@cached(cache=TTLCache(maxsize=1024, ttl=args.rundeck_cached_requests_ttl))
def cached_request_data_from(self, endpoint: str) -> dict:
Expand Down Expand Up @@ -259,6 +260,7 @@ def get_counters(self, metrics: dict):
Method to collect Rundeck metrics
"""
def collect(self):
self.client_requests_count = 0
metrics = self.request_data_from('/metrics/metrics')
system_info = self.request_data_from('/system/info')
self.rundeck_node = system_info['system']['rundeck']['node']
Expand Down Expand Up @@ -299,8 +301,9 @@ def collect(self):
yield(execution)

@staticmethod
def exit_with_msg(msg: str):
raise SystemExit(f'\nError:\n {msg}\n')
def exit_with_msg(msg: str, level: str):
getattr(logging, level)(msg)
exit(getattr(logging, level.upper()))

@classmethod
def run(cls):
Expand All @@ -312,7 +315,7 @@ def run(cls):
while True:
sleep(1)
except OSError as os_error:
cls.exit_with_msg(str(os_error))
cls.exit_with_msg(msg=str(os_error), level='critical')


if __name__ == "__main__":
Expand Down

0 comments on commit 6864ee8

Please sign in to comment.