Skip to content

Commit 089e84c

Browse files
Sanjay ChopraSanjay Chopra
authored andcommitted
Updated example and fixed authentication bug
1 parent 98a7e76 commit 089e84c

File tree

3 files changed

+54
-36
lines changed

3 files changed

+54
-36
lines changed

example/log_non_batch.py

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,25 @@
1-
2-
import logging
1+
# Sample Program to send Logs to LogicMonitor Platform
2+
#
33
import os
4-
import time
5-
6-
import psutil as psutil
7-
84
import logicmonitor_data_sdk
95

106
from logicmonitor_data_sdk.api.logs import Logs
117
from logicmonitor_data_sdk.models import Resource
128

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

16-
configuration = logicmonitor_data_sdk.Configuration(company='yourcompany',
17-
id='accessID',
18-
key='accessKey')
19-
15+
# The resource which is already present on LM Platform. Use a unique property to match
16+
# the resource and send log for that.
17+
resource = Resource(ids={"system.hostname": 'your_system'})
2018

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

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

25+
print(return_value)

example/simple_example.py

Lines changed: 37 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,44 @@
22
from random import seed, random
33

44
import logicmonitor_data_sdk
5+
6+
# LogicMonitor metric data model is as below
7+
#
8+
#Company
9+
# |--- Resource (like device/service. Ex: VM)
10+
# |--- Data Source (Ex. CPU)
11+
# |--- Instance (of a Data Source on a resource. Ex. CPU-1)
12+
# |--- Data Point (the metric which is being monitored. Ex. %Used)
13+
# |- <Time> : <Metric Value>
14+
# |- <Time> : <Metric Value>
15+
# |...
16+
#
517
from logicmonitor_data_sdk.api.metrics import Metrics
618
from logicmonitor_data_sdk.models import DataSource, \
719
Resource, DataSourceInstance, DataPoint
820

9-
# Configure API key authorization: LMv1
10-
configuration = logicmonitor_data_sdk.Configuration(company='COMPANY',
11-
id='ACCESS_ID',
12-
key='ACCESS_KEY')
13-
# create an instance of the API class
14-
metric_api = Metrics(batch=True)
15-
seed(1)
16-
while True:
17-
metric_api.send_metrics(resource=Resource(
18-
ids={"system.hostname": "SampleDevice"}, create=True, name="SampleDevice",
19-
properties={"using.sdk": "true"}), datasource=DataSource(
20-
name="PusMetricsDS"), instance=DataSourceInstance(name="instance"),
21-
datapoint=DataPoint(name="dataPoint"),
22-
values={str(int(time.time())): str(random())})
23-
time.sleep(10)
21+
# Configure SDK with Account and access information
22+
# On your LogicMonitor portal, create API token (LMv1) for user and get
23+
# Access Id and Access Key
24+
configuration = logicmonitor_data_sdk.Configuration(company='your_company',
25+
id='API_ACCESS_ID',
26+
key='API_ACCESS_KEY')
27+
28+
# Create api handle for Metrics use case (we also support Logs)
29+
metric_api = Metrics()
30+
31+
return_val = metric_api.send_metrics(
32+
resource=Resource(
33+
ids={"system.hostname": "SampleDevice"}, #Core Properties of the Resource
34+
create=True, #Auto-create resource if does not exist
35+
name="SampleDevice", #Name of the resource
36+
properties={"using.sdk": "true"}), #Additional Properties [Optional]
37+
datasource=DataSource(
38+
name="SampleDS"), #Name of data source is must. Rest optional
39+
instance=DataSourceInstance(
40+
name="SampleInstance"), #Name of instance is must. Rest optional
41+
datapoint=DataPoint(
42+
name="SampleDataPoint"), #The metric
43+
values={str(int(time.time())): str(random())} #Values at specific time(s)
44+
)
45+
print("Return Value = ",return_val)

logicmonitor_data_sdk/configuration.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,12 +74,12 @@ def __init__(self, company=None, authentication=None, id=None, key=None):
7474
raise ValueError(
7575
'Authentication must provide the `id` and `key`'
7676
)
77-
if not objectNameValidator.is_valid_auth_id(id):
77+
if not objectNameValidator.is_valid_auth_id(authentication.get('id', None)):
7878
raise ValueError(
7979
'Invalid Access ID'
8080
)
81-
if key:
82-
if not objectNameValidator.is_valid_auth_key(key):
81+
if authentication.get('key', None):
82+
if not objectNameValidator.is_valid_auth_key(authentication.get('key', None)):
8383
raise ValueError(
8484
'Invalid Access Key'
8585
)

0 commit comments

Comments
 (0)