Sending data using Python

  Streaming Data, Zenoss

Python v3 changes vs v2

  • Print statements are now functions
    • v2: print 'The moon is made of green cheese.'
    • v3: print('The moon is made of green cheese.')
  • Formatted string literals
    • v2: print 'Hello, {0}.'.format(name)'
    • v3: print(f'Hello, {name}.')

 

Setup a Python Virtual Environment

Install python3-venv

  • sudo apt install python3-venv

Create the environment

Command: python3 -m venv environment-name

python3 -m venv mypython3

This will create a directory off your current working directory.

Verify the environment has been created

$ ls mypython3

Activate the environment

Show the activate environmental variables file.

ls mypython3/bin/activate
mypython3/bin/activate

Activate the environment

The prompt will change showing activation was successful.

source mypython3/bin/activate
(mypython3) user@hostname: $

Configure the system to automatically load the environment at boot

Add the code to your .bash_profile

echo "source ~/mypython3/bin/activate" >> ~/.bash_profile

Update the environment and install modules

Update pip

pip install --upgrade pip

Install requests module

pip install requests

Install pyyaml

pip install pyyaml

 

Scripts

example.py

# Usage
# python3 example.py

import getpass
import json
import os
import os.path
import random
import socket
import sys
import time

import requests

#USER_NAME = os.environ.get('USER') or getpass.getuser()
USER_NAME = 'troberts'
DEBUG_ENABLED = True #False

ZENOSS_API_BASE_ADDRESS =  'https://api.zenoss.io/v1/data-receiver'
ZENOSS_METRIC_SERVICE = 'metrics'
ZENOSS_MODEL_SERVICE = 'models'
ZENOSS_API_KEY_HEADER = 'zenoss-api-key'
ZENOSS_API_KEY = None
# ZENOSS_API_KEY = "khHtq1e0Tf2rUqqM3t1W1drcBkzBFYubWj6X7fAs33HJ7"
API_KEY_ENV_VAR = 'ZENOSS_API_KEY'
API_KEY_FILE_NAME = '~/.zenoss.key'
SOURCE = USER_NAME
SOURCE_TYPE = 'com.zenoss.training'

def debug(*args):
	if DEBUG_ENABLED:
		print(*args)

def get_api_key(env_var_name = API_KEY_ENV_VAR, file_name = API_KEY_FILE_NAME):
	
	if ZENOSS_API_KEY is not None:
		api_key = ZENOSS_API_KEY
	elif os.environ.get(env_var_name):
		api_key = os.environ[env_var_name]
		debug(f'API key read from "${env_var_name}".')
	else:
		file_path = os.path.expanduser(file_name)
		try:
			with open(file_path, 'r') as key_file:
				api_key = key_file.readline().strip()
				debug(f'API key read from file "{file_path}".')
		except OSError as err:
			print(f'Could not read API key from file {file_path}: ',
						f'{err.sererror}', file=sys.stderr)
			sys.exit(1)
	return api_key

def send_request(api_key, service, json_request, count):
	debug(f'{service} = ', json.dumps(json_request, indent=4))
	url = f'{ZENOSS_API_BASE_ADDRESS}/{service}'
	headers = {ZENOSS_API_KEY_HEADER: api_key}
	response = requests.post(url, headers=headers, json=json_request)
	
	if not response.ok:
		response.raise_for_status()
		
	response_json = response.json()
	status = ('Succeeded' if response_json['succeeded'] == count else 'Failed')
	message = response_json.get('message')
	if message:
		print(f'{status}: {message}.')
	else:
		print(f'{status}.')
		
def send_models(api_key, timestamp, hostname):
	models = {
		'models': [
			{
				'timestamp': timestamp,
				'dimensions': {
					'source': SOURCE,
					'source-type': SOURCE_TYPE,
				},
				'metadataFields': {
					'name': f"{SOURCE}-{hostname}"
				}
			}
		]
	}
	print(f'Sending model for {SOURCE}.')
	send_request(api_key, ZENOSS_MODEL_SERVICE, models, len(models['models']))	

def send_metrics(api_key, timestamp):
	metrics = {
		'metrics': [
			{
				'metric': f'{SOURCE}.random.number',
				'timestamp': timestamp,
				'value': random.randint(0,127),
				'dimensions': {
					'source': SOURCE,
					'source-type': SOURCE_TYPE
				}
			}
		]
	}
	print(f'Sending {SOURCE}.random.number metric.')
	send_request(api_key, ZENOSS_METRIC_SERVICE, metrics, len(metrics['metrics']))
	
def main():
	hostname = socket.getfqdn()
	timestamp = int(time.time()*1000)
	api_key = get_api_key()
	send_models(api_key, timestamp, hostname)
	send_metrics(api_key, timestamp)
	
	sys.exit(0)

main()

 

LEAVE A COMMENT