Added status file parsing and writing telemetry file
@@ -23,6 +23,8 @@ import time
import string
import traceback
import shutil
+import sys
+import json
class Watcher:
"""
@@ -52,50 +54,79 @@ class Watcher:
except IOError as e:
self._hutil_error('Error writing to console. Exception={0}'.format(e))
- def write_waagent_event(event):
- offset = ustr(int(time.time() * 1000000))
+ def write_waagent_event(self, event):
+ offset = str(int(time.time() * 1000000))
fn = '/var/lib/waagent/events/{}.tld'.format(offset)
+ self._hutil_log(fn)
- with open(fn + '.tmp') as fh:
+ with open(fn + '.tmp','w+') as fh:
fh.write(event)
shutil.move(fn + '.tmp', fn)
- def create_telemetry_event(self):
- template =
- """ {
+ def create_telemetry_event(self, operation, operation_success, message, duration):
+ template = """ {{
"eventId": 1,
"providerId": "69B669B9-4AF8-4C50-BDC4-6006FA76E975",
"parameters": [
- {
+ {{
"name": "Name",
"value": "Microsoft.EnterpriseCloud.Monitoring.OmsLinuxAgent"
- },
- {
+ }},
+ {{
"name": "Version",
"value": "1.6"
- },
- {
+ }},
+ {{
"name": "Operation",
"value": "{}"
- },
- {
+ }},
+ {{
"name": "OperationSuccess",
"value": {}
- },
- {
+ }},
+ {{
"name": "Message",
"value": "{}"
- },
- {
+ }},
+ {{
"name": "Duration",
"value": {}
- }
+ }}
]
- }"""
+ }}"""
- return template.format("ODSIngestion","true","Success","300000")
+ return template.format(operation, operation_success, message, duration)
def upload_telemetry(self):
+ status_files = [
+ "/var/opt/microsoft/omsagent/log/ODSIngestion.status",
+ "/var/opt/microsoft/omsagent/log/ODSIngestionBlob.status",
+ "/var/opt/microsoft/omsagent/log/ODSIngestionAPI.status"
+ ]
+ for sf in status_files:
+ if os.path.isfile(sf):
+ mod_time = os.path.getmtime(sf)
+ curr_time = int(time.time())
+ if (curr_time - mod_time < 300):
+ with open(sf) as json_file:
+ try:
+ status_data = json.load(json_file)
+ operation = status_data["operation"]
+ operation_success = status_data["success"]
+ message = status_data["message"]
+
+ event = self.create_telemetry_event(operation,operation_success,message,"300000")
+ self._hutil_log("Writing telemetry event: "+event)
+ self.write_waagent_event(event)
+ self._hutil_log("Successfully processed telemetry status file: "+sf)
+
+ except Exception as e:
+ self._hutil_log("Error parsing telemetry status file: "+sf)
+ self._hutil_log("Exception info: "+traceback.format_exc())
+ else:
+ self._hutil_log("Telemetry status file not updated in last 5 mins: "+sf)
+ else:
+ self._hutil_log("Telemetry status file does not exist: "+sf)
pass
def watch(self):
