{"id":3364,"date":"2021-03-07T15:01:50","date_gmt":"2021-03-07T15:01:50","guid":{"rendered":"https:\/\/wiki.thomasandsofia.com\/?p=3364"},"modified":"2021-03-07T18:11:38","modified_gmt":"2021-03-07T18:11:38","slug":"sending-data-using-python","status":"publish","type":"post","link":"https:\/\/wiki.thomasandsofia.com\/?p=3364","title":{"rendered":"Sending data using Python"},"content":{"rendered":"<h1>Python v3 changes vs v2<\/h1>\n<ul>\n<li>Print statements are now functions\n<ul>\n<li>v2: <code>print 'The moon is made of green cheese.'<\/code><\/li>\n<li>v3: <code>print('The moon is made of green cheese.')<\/code><\/li>\n<\/ul>\n<\/li>\n<li>Formatted string literals\n<ul>\n<li>v2: <code>print 'Hello, {0}.'.format(name)'<\/code><\/li>\n<li>v3: <code>print(f'Hello, {name}.')<\/code><\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<p>&nbsp;<\/p>\n<h1>Setup a Python Virtual Environment<\/h1>\n<h2>Install python3-venv<\/h2>\n<ul>\n<li><code>sudo apt install python3-venv<\/code><\/li>\n<\/ul>\n<h2>Create the environment<\/h2>\n<p><strong>Command: python3 -m venv environment-name<\/strong><\/p>\n<p><code>python3 -m venv mypython3<\/code><\/p>\n<p>This will create a directory off your current working directory.<\/p>\n<h3><strong>Verify the environment has been created<\/strong><\/h3>\n<p><code>$ ls mypython3<\/code><\/p>\n<h3><strong>Activate the environment<\/strong><\/h3>\n<p><strong>Show the activate environmental variables file.<\/strong><\/p>\n<pre>ls mypython3\/bin\/activate\r\nmypython3\/bin\/activate\r\n<\/pre>\n<h3>Activate the environment<\/h3>\n<p>The prompt will change showing activation was successful.<\/p>\n<pre>source mypython3\/bin\/activate\r\n(mypython3) user@hostname: $\r\n<\/pre>\n<h2>Configure the system to automatically load the environment at boot<\/h2>\n<p><strong>Add the code to your .bash_profile<\/strong><\/p>\n<p><code>echo \"source ~\/mypython3\/bin\/activate\" &gt;&gt; ~\/.bash_profile<\/code><\/p>\n<h2>Update the environment and install modules<\/h2>\n<p><strong>Update pip<\/strong><\/p>\n<p><code>pip install --upgrade pip<\/code><\/p>\n<p><strong>Install requests module<\/strong><\/p>\n<p><code>pip install requests<\/code><\/p>\n<p><strong>Install pyyaml<\/strong><\/p>\n<p><code>pip install pyyaml<\/code><\/p>\n<p>&nbsp;<\/p>\n<h1>Scripts<\/h1>\n<p>example.py<\/p>\n<pre># Usage\r\n# python3 example.py\r\n\r\nimport getpass\r\nimport json\r\nimport os\r\nimport os.path\r\nimport random\r\nimport socket\r\nimport sys\r\nimport time\r\n\r\nimport requests\r\n\r\n#USER_NAME = os.environ.get('USER') or getpass.getuser()\r\nUSER_NAME = 'troberts'\r\nDEBUG_ENABLED = True #False\r\n\r\nZENOSS_API_BASE_ADDRESS =  'https:\/\/api.zenoss.io\/v1\/data-receiver'\r\nZENOSS_METRIC_SERVICE = 'metrics'\r\nZENOSS_MODEL_SERVICE = 'models'\r\nZENOSS_API_KEY_HEADER = 'zenoss-api-key'\r\nZENOSS_API_KEY = None\r\n# ZENOSS_API_KEY = \"khHtq1e0Tf2rUqqM3t1W1drcBkzBFYubWj6X7fAs33HJ7\"\r\nAPI_KEY_ENV_VAR = 'ZENOSS_API_KEY'\r\nAPI_KEY_FILE_NAME = '~\/.zenoss.key'\r\nSOURCE = USER_NAME\r\nSOURCE_TYPE = 'com.zenoss.training'\r\n\r\ndef debug(*args):\r\n\tif DEBUG_ENABLED:\r\n\t\tprint(*args)\r\n\r\ndef get_api_key(env_var_name = API_KEY_ENV_VAR, file_name = API_KEY_FILE_NAME):\r\n\t\r\n\tif ZENOSS_API_KEY is not None:\r\n\t\tapi_key = ZENOSS_API_KEY\r\n\telif os.environ.get(env_var_name):\r\n\t\tapi_key = os.environ[env_var_name]\r\n\t\tdebug(f'API key read from \"${env_var_name}\".')\r\n\telse:\r\n\t\tfile_path = os.path.expanduser(file_name)\r\n\t\ttry:\r\n\t\t\twith open(file_path, 'r') as key_file:\r\n\t\t\t\tapi_key = key_file.readline().strip()\r\n\t\t\t\tdebug(f'API key read from file \"{file_path}\".')\r\n\t\texcept OSError as err:\r\n\t\t\tprint(f'Could not read API key from file {file_path}: ',\r\n\t\t\t\t\t\tf'{err.sererror}', file=sys.stderr)\r\n\t\t\tsys.exit(1)\r\n\treturn api_key\r\n\r\ndef send_request(api_key, service, json_request, count):\r\n\tdebug(f'{service} = ', json.dumps(json_request, indent=4))\r\n\turl = f'{ZENOSS_API_BASE_ADDRESS}\/{service}'\r\n\theaders = {ZENOSS_API_KEY_HEADER: api_key}\r\n\tresponse = requests.post(url, headers=headers, json=json_request)\r\n\t\r\n\tif not response.ok:\r\n\t\tresponse.raise_for_status()\r\n\t\t\r\n\tresponse_json = response.json()\r\n\tstatus = ('Succeeded' if response_json['succeeded'] == count else 'Failed')\r\n\tmessage = response_json.get('message')\r\n\tif message:\r\n\t\tprint(f'{status}: {message}.')\r\n\telse:\r\n\t\tprint(f'{status}.')\r\n\t\t\r\ndef send_models(api_key, timestamp, hostname):\r\n\tmodels = {\r\n\t\t'models': [\r\n\t\t\t{\r\n\t\t\t\t'timestamp': timestamp,\r\n\t\t\t\t'dimensions': {\r\n\t\t\t\t\t'source': SOURCE,\r\n\t\t\t\t\t'source-type': SOURCE_TYPE,\r\n\t\t\t\t},\r\n\t\t\t\t'metadataFields': {\r\n\t\t\t\t\t'name': f\"{SOURCE}-{hostname}\"\r\n\t\t\t\t}\r\n\t\t\t}\r\n\t\t]\r\n\t}\r\n\tprint(f'Sending model for {SOURCE}.')\r\n\tsend_request(api_key, ZENOSS_MODEL_SERVICE, models, len(models['models']))\t\r\n\r\ndef send_metrics(api_key, timestamp):\r\n\tmetrics = {\r\n\t\t'metrics': [\r\n\t\t\t{\r\n\t\t\t\t'metric': f'{SOURCE}.random.number',\r\n\t\t\t\t'timestamp': timestamp,\r\n\t\t\t\t'value': random.randint(0,127),\r\n\t\t\t\t'dimensions': {\r\n\t\t\t\t\t'source': SOURCE,\r\n\t\t\t\t\t'source-type': SOURCE_TYPE\r\n\t\t\t\t}\r\n\t\t\t}\r\n\t\t]\r\n\t}\r\n\tprint(f'Sending {SOURCE}.random.number metric.')\r\n\tsend_request(api_key, ZENOSS_METRIC_SERVICE, metrics, len(metrics['metrics']))\r\n\t\r\ndef main():\r\n\thostname = socket.getfqdn()\r\n\ttimestamp = int(time.time()*1000)\r\n\tapi_key = get_api_key()\r\n\tsend_models(api_key, timestamp, hostname)\r\n\tsend_metrics(api_key, timestamp)\r\n\t\r\n\tsys.exit(0)\r\n\r\nmain()\r\n<\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Python v3 changes vs v2 Print statements are now functions v2: print &#8216;The moon is made of green cheese.&#8217; v3: print(&#8216;The moon is made of green cheese.&#8217;) Formatted string literals v2: print &#8216;Hello, {0}.&#8217;.format(name)&#8217; v3: print(f&#8217;Hello, {name}.&#8217;) &nbsp; Setup a Python Virtual Environment Install python3-venv sudo apt install python3-venv Create the environment Command: python3 -m ..<\/p>\n<div class=\"clear-fix\"><\/div>\n<p><a href=\"https:\/\/wiki.thomasandsofia.com\/?p=3364\" title=\"read more...\">Read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[64,59],"tags":[],"class_list":["post-3364","post","type-post","status-publish","format-standard","hentry","category-streaming-data","category-zenoss"],"_links":{"self":[{"href":"https:\/\/wiki.thomasandsofia.com\/index.php?rest_route=\/wp\/v2\/posts\/3364","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/wiki.thomasandsofia.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/wiki.thomasandsofia.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/wiki.thomasandsofia.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/wiki.thomasandsofia.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=3364"}],"version-history":[{"count":3,"href":"https:\/\/wiki.thomasandsofia.com\/index.php?rest_route=\/wp\/v2\/posts\/3364\/revisions"}],"predecessor-version":[{"id":3367,"href":"https:\/\/wiki.thomasandsofia.com\/index.php?rest_route=\/wp\/v2\/posts\/3364\/revisions\/3367"}],"wp:attachment":[{"href":"https:\/\/wiki.thomasandsofia.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3364"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/wiki.thomasandsofia.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3364"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/wiki.thomasandsofia.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3364"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}