Skip to content
This repository was archived by the owner on Jun 12, 2019. It is now read-only.

Commit b2ee32b

Browse files
committed
Merge pull request #9 from mathieupassenaud/add-list-metrics
Add list metrics
2 parents 9780f51 + e775eeb commit b2ee32b

10 files changed

+179
-132
lines changed

Bash/README.md

+6
Original file line numberDiff line numberDiff line change
@@ -49,3 +49,9 @@ Or send key:value list with `kv_to_put_json` function:
4949
"tags":{}
5050
}]
5151
}' | delete
52+
53+
### List Metrics
54+
55+
listMetrics
56+
57+
returns all metrics for the current application

Bash/lib/iot-lib.sh

+11-1
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,19 @@ delete() {
4444
$OPTS
4545
}
4646

47+
# Get metrics
48+
#
49+
# @param none
50+
#
51+
listMetrics() {
52+
curl -s \
53+
-u $READ_TOKEN_ID:$READ_TOKEN_KEY \
54+
-XGET "$IOT_API/api/suggest?type=metrics" \
55+
$OPTS
56+
}
57+
4758
# Utils
4859

49-
# Transform a key/value list in JSON 'OpenTSDB put' data.
5060
# (http://opentsdb.net/docs/build/html/api_http/put.html)
5161
#
5262
# @param tag optional JSON object for the tags (example: {"host": localhost})

Bash/list-metrics

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#!/bin/bash
2+
set -euo pipefail
3+
4+
source lib/iot-lib.sh
5+
6+
#
7+
# @return a JSON object representing an OpenTSDB suggest metrics query
8+
#
9+
10+
listMetrics

Python/Metric.py

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# -*- coding: utf8 -*-
2+
3+
import json
4+
from json import JSONEncoder
5+
6+
#
7+
# A metric for an OpenTSDB container
8+
#
9+
class Metric:
10+
11+
metric = ""
12+
value = 0.0
13+
timestamp = 0
14+
tags = {}
15+
16+
def __init__(self, metricName, value, timestamp, tags):
17+
self.metric = metricName
18+
self.value = value
19+
self.timestamp = timestamp
20+
self.tags = tags
21+
22+
def getAsJsonString(self):
23+
return MetricEncoder().encode(self)
24+
#return json.dumps(self)
25+
26+
class MetricEncoder(JSONEncoder):
27+
def default(self, o):
28+
return o.__dict__

Python/OpenTSDBProfile.py

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# -*- coding: utf8 -*-
2+
3+
#
4+
# Your OpenTSDB Account
5+
#
6+
# Only use the default constructor with your tokens
7+
# and server URI
8+
#
9+
10+
class OpenTSDBProfile:
11+
12+
token_id = ""
13+
token_password = ""
14+
url = ""
15+
port = 0
16+
17+
def __init__(self, token_id, token_password, url, port):
18+
self.token_id = token_id
19+
self.token_password = token_password
20+
self.url = url
21+
self.port = port

Python/OpenTSDBPusherHttp.py

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# -*- coding: utf8 -*-
2+
3+
###############################################################################################
4+
# OpenTSDB REST API session in a HTTPS connection library #
5+
# from https://github.com/runabove/iot-push-examples/blob/master/Python/python-client-http.py #
6+
###############################################################################################
7+
8+
from __future__ import print_function, unicode_literals
9+
import json
10+
11+
# Requests library that hides much of the complexity of dealing with
12+
# HTTP requests in Python. Can either be installed with pip:
13+
# pip install requests
14+
# or with your package manager:
15+
# apt-get install python-requests
16+
# yum install python-requests
17+
import requests
18+
from Metric import Metric
19+
from OpenTSDBProfile import OpenTSDBProfile
20+
21+
class OpenTSDBPusher:
22+
23+
def pushData(self, openTSDBProfile, metric):
24+
try:
25+
# Send request and fetch response
26+
response = requests.post(openTSDBProfile.url, data=metric.getAsJsonString(),
27+
auth=(openTSDBProfile.token_id, openTSDBProfile.token_password))
28+
29+
# Raise error if any
30+
response.raise_for_status()
31+
32+
# Print the http response code on success
33+
print('Send successful\nResponse code from server: {}'.
34+
format(response.status_code))
35+
36+
except requests.exceptions.HTTPError as e:
37+
print('HTTP code is {} and reason is {}'.format(e.response.status_code,
38+
e.response.reason))
39+
print('data : ' + metric.getAsJsonString())

Python/OpenTSDBPusherTelnet.py

+64
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
# -*- coding: utf8 -*-
2+
3+
###############################################################################################
4+
# OpenTSDB REST API session in a HTTPS connection library #
5+
# from https://github.com/runabove/iot-push-examples/blob/master/Python/python-client-http.py #
6+
###############################################################################################
7+
8+
from __future__ import print_function, unicode_literals
9+
from socket import socket, AF_INET, SOCK_STREAM, SHUT_RDWR
10+
from ssl import wrap_socket, PROTOCOL_TLSv1
11+
import json
12+
13+
# Requests library that hides much of the complexity of dealing with
14+
# HTTP requests in Python. Can either be installed with pip:
15+
# pip install requests
16+
# or with your package manager:
17+
# apt-get install python-requests
18+
# yum install python-requests
19+
import requests
20+
from Metric import Metric
21+
from OpenTSDBProfile import OpenTSDBProfile
22+
23+
class OpenTSDBPusher:
24+
25+
def pushData(self, openTSDBProfile, metric):
26+
client_socket = socket(AF_INET, SOCK_STREAM)
27+
client_socket.settimeout(1)
28+
29+
tls_client = wrap_socket(client_socket, ssl_version=PROTOCOL_TLSv1)
30+
31+
print('Opening connection')
32+
# Connect to the echo server
33+
tls_client.connect((openTSDBProfile.url, openTSDBProfile.port))
34+
35+
print('Authenticating')
36+
# Send auth command
37+
auth_command = ('auth {}:{}\n'.format(openTSDBProfile.token_id, openTSDBProfile.token_password))
38+
tls_client.send(auth_command.encode('utf-8'))
39+
40+
# Read received data
41+
data_in = tls_client.recv(1024)
42+
43+
# Decode and print message
44+
response = data_in.decode()
45+
print('Read response: ' + response)
46+
47+
if response == 'ok\n':
48+
# Send data
49+
print('Sending data')
50+
tagsAsString = ""
51+
for key, value in metric.tags.iteritems():
52+
tagsAsString = tagsAsString + " " + key + "=" + value
53+
54+
tls_client.send(b'put '+ metric.metric +' '+ metric.timestamp +' '+ metric.value + ' ' + tagsAsString + '\n')
55+
print('Data sent')
56+
else:
57+
print('Auth failed, not sending data.')
58+
59+
# Send exit command to close connection
60+
tls_client.send(b'exit\n')
61+
62+
# Close the socket
63+
tls_client.shutdown(SHUT_RDWR)
64+
tls_client.close()

Python/__init__.py

Whitespace-only changes.

Python/python-client-http.py

-77
This file was deleted.

Python/python-client-telnet.py

-54
This file was deleted.

0 commit comments

Comments
 (0)