forked from DataDog/oracle-cloud-integration
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
108 additions
and
94 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
*.pyc | ||
**/__pycache__/** |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,67 +1,72 @@ | ||
import io | ||
import oci | ||
import re | ||
import os | ||
import gzip | ||
import json | ||
import sys | ||
import requests | ||
import logging | ||
import time | ||
import gzip | ||
|
||
from fdk import response | ||
import oci | ||
import requests | ||
|
||
|
||
def handler(ctx, data: io.BytesIO=None): | ||
def handler(ctx, data: io.BytesIO = None): | ||
try: | ||
body = json.loads(data.getvalue()) | ||
data = body.get("data", {}) | ||
additional_details = data.get("additionalDetails", {}) | ||
namespace = additional_details.get("namespace") | ||
bucket = additional_details.get("bucketName") | ||
obj = data.get("resourceName") | ||
eventtime = body.get("eventTime") | ||
except (Exception, ValueError) as ex: | ||
logging.getLogger().info(str(ex)) | ||
return | ||
|
||
source = "Oracle Cloud" #adding a source name. | ||
service = "OCI Logs" #adding a servicen name. | ||
data = body.get("data", {}) | ||
additional_details = data.get("additionalDetails", {}) | ||
|
||
datafile = request_one_object(namespace, bucket, obj) | ||
data = str(datafile,'utf-8') | ||
namespace = additional_details.get("namespace") | ||
if not namespace: | ||
logging.getLogger().error("No namespace provided") | ||
return | ||
|
||
bucket = additional_details.get("bucketName") | ||
if not bucket: | ||
logging.getLogger().error("No bucket provided") | ||
return | ||
|
||
datadoghost = os.environ['DATADOG_HOST'] | ||
datadogtoken = os.environ['DATADOG_TOKEN'] | ||
resource_name = data.get("resourceName") | ||
if not resource_name: | ||
logging.getLogger().error("No obj provided") | ||
return | ||
|
||
for lines in data.splitlines(): | ||
logging.getLogger().info("lines " + lines) | ||
payload = {} | ||
payload.update({"host":obj}) | ||
payload.update({"time": eventtime}) | ||
event_time = body.get("eventTime") | ||
|
||
payload.update({"ddsource":source}) | ||
payload.update({"service":service}) | ||
source = "Oracle Cloud" # Adding a source name. | ||
service = "OCI Logs" # Adding a service name. | ||
|
||
payload.update({"event":lines}) | ||
datafile = request_one_object(namespace, bucket, resource_name) | ||
data = str(datafile, 'utf-8') | ||
|
||
|
||
|
||
headers = {'Content-type': 'application/json', 'DD-API-KEY': datadogtoken} | ||
x = requests.post(datadoghost, data = json.dumps(payload), headers=headers) | ||
logging.getLogger().info(x.text) | ||
print(x.text) | ||
dd_host = os.environ['DATADOG_HOST'] | ||
dd_token = os.environ['DATADOG_TOKEN'] | ||
|
||
except (Exception, ValueError) as ex: | ||
# print(str(ex)) | ||
logging.getLogger().info(str(ex)) | ||
return | ||
for lines in data.splitlines(): | ||
logging.getLogger().info("lines %s", lines) | ||
payload = {} | ||
payload.update({"host": resource_name}) | ||
payload.update({"time": event_time}) | ||
|
||
payload.update({"ddsource": source}) | ||
payload.update({"service": service}) | ||
|
||
payload.update({"event": lines}) | ||
|
||
headers = {'Content-type': 'application/json', 'DD-API-KEY': dd_token} | ||
req = requests.post(dd_host, data=json.dumps(payload), headers=headers) | ||
logging.getLogger().info(req.text) | ||
|
||
|
||
def request_one_object(namespace, bucket, obj): | ||
assert bucket and obj | ||
signer = oci.auth.signers.get_resource_principals_signer() | ||
object_storage_client = oci.object_storage.ObjectStorageClient(config={}, signer=signer) | ||
namespace = namespace | ||
bucket_name = bucket | ||
object_name = obj | ||
get_obj = object_storage_client.get_object(namespace, bucket_name, object_name) | ||
def request_one_object(namespace: str, bucket: str, resource_name: str): | ||
""" | ||
Calls OCI to request object from Object Storage Client and decompress | ||
""" | ||
oci_signer = oci.auth.signers.get_resource_principals_signer() | ||
os_client = oci.object_storage.ObjectStorageClient(config={}, | ||
signer=oci_signer) | ||
get_obj = os_client.get_object(namespace, bucket, resource_name) | ||
bytes_read = gzip.decompress(get_obj.data.content) | ||
return bytes_read | ||
return bytes_read |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,2 @@ | ||
# Oracle_Logs_Integration | ||
Houses code for OCI's log collections pipeline. | ||
Houses code for OCI's log collection pipeline. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters