Skip to content

Commit

Permalink
Merge pull request #4 from logicmonitor/validation-fix
Browse files Browse the repository at this point in the history
Updated example and fixed authentication bug
  • Loading branch information
choprasan authored Apr 22, 2021
2 parents 91abcc9 + 089e84c commit 70d980e
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 36 deletions.
32 changes: 14 additions & 18 deletions example/log_non_batch.py
Original file line number Diff line number Diff line change
@@ -1,29 +1,25 @@

import logging
# Sample Program to send Logs to LogicMonitor Platform
#
import os
import time

import psutil as psutil

import logicmonitor_data_sdk

from logicmonitor_data_sdk.api.logs import Logs
from logicmonitor_data_sdk.models import Resource

logger = logging.getLogger('lmdata.api')
logger.setLevel(logging.INFO)
# Initialize LM SDK and provide required authentication parameters
# On LM Portal, create 'API Token' for the user to get access Id and access Key
configuration = logicmonitor_data_sdk.Configuration( company='your_company',
id='API access id',
key='API access key')

configuration = logicmonitor_data_sdk.Configuration(company='yourcompany',
id='accessID',
key='accessKey')

# The resource which is already present on LM Platform. Use a unique property to match
# the resource and send log for that.
resource = Resource(ids={"system.hostname": 'your_system'})

resource = Resource(ids={"System.hostname": "192.168.1.33"})
#Create an api handle for sending the logs
# "batch" would club logs for 8MB size or 30 Sec - whichever is earlier. Its default is "True".
log_api = Logs(batch = False)
log_api.send_logs(resource = resource,msg= "this is smaple log")





return_value = log_api.send_logs(resource=resource, msg= "this is sample log")

print(return_value)
52 changes: 37 additions & 15 deletions example/simple_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,44 @@
from random import seed, random

import logicmonitor_data_sdk

# LogicMonitor metric data model is as below
#
#Company
# |--- Resource (like device/service. Ex: VM)
# |--- Data Source (Ex. CPU)
# |--- Instance (of a Data Source on a resource. Ex. CPU-1)
# |--- Data Point (the metric which is being monitored. Ex. %Used)
# |- <Time> : <Metric Value>
# |- <Time> : <Metric Value>
# |...
#
from logicmonitor_data_sdk.api.metrics import Metrics
from logicmonitor_data_sdk.models import DataSource, \
Resource, DataSourceInstance, DataPoint

# Configure API key authorization: LMv1
configuration = logicmonitor_data_sdk.Configuration(company='COMPANY',
id='ACCESS_ID',
key='ACCESS_KEY')
# create an instance of the API class
metric_api = Metrics(batch=True)
seed(1)
while True:
metric_api.send_metrics(resource=Resource(
ids={"system.hostname": "SampleDevice"}, create=True, name="SampleDevice",
properties={"using.sdk": "true"}), datasource=DataSource(
name="PusMetricsDS"), instance=DataSourceInstance(name="instance"),
datapoint=DataPoint(name="dataPoint"),
values={str(int(time.time())): str(random())})
time.sleep(10)
# Configure SDK with Account and access information
# On your LogicMonitor portal, create API token (LMv1) for user and get
# Access Id and Access Key
configuration = logicmonitor_data_sdk.Configuration(company='your_company',
id='API_ACCESS_ID',
key='API_ACCESS_KEY')

# Create api handle for Metrics use case (we also support Logs)
metric_api = Metrics()

return_val = metric_api.send_metrics(
resource=Resource(
ids={"system.hostname": "SampleDevice"}, #Core Properties of the Resource
create=True, #Auto-create resource if does not exist
name="SampleDevice", #Name of the resource
properties={"using.sdk": "true"}), #Additional Properties [Optional]
datasource=DataSource(
name="SampleDS"), #Name of data source is must. Rest optional
instance=DataSourceInstance(
name="SampleInstance"), #Name of instance is must. Rest optional
datapoint=DataPoint(
name="SampleDataPoint"), #The metric
values={str(int(time.time())): str(random())} #Values at specific time(s)
)
print("Return Value = ",return_val)
6 changes: 3 additions & 3 deletions logicmonitor_data_sdk/configuration.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,12 +74,12 @@ def __init__(self, company=None, authentication=None, id=None, key=None):
raise ValueError(
'Authentication must provide the `id` and `key`'
)
if not objectNameValidator.is_valid_auth_id(id):
if not objectNameValidator.is_valid_auth_id(authentication.get('id', None)):
raise ValueError(
'Invalid Access ID'
)
if key:
if not objectNameValidator.is_valid_auth_key(key):
if authentication.get('key', None):
if not objectNameValidator.is_valid_auth_key(authentication.get('key', None)):
raise ValueError(
'Invalid Access Key'
)
Expand Down

0 comments on commit 70d980e

Please sign in to comment.