Last active
December 30, 2024 23:04
-
-
Save Hitokun/f3a68ee8a950699d8b4ff1d1c6a85f0b to your computer and use it in GitHub Desktop.
Use the SevOne REST API (available for SevOne NMS 5.6+) to gather all the metrics (Indicators) from all devices. This is a starting point to develop more robust SevOne export solutions via REST
This file contains hidden or 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
import requests | |
import json | |
import time | |
# Log into SevOne API. | |
address = 'http://sevone.com/api/v1/' | |
creds = {'name': 'user', 'password':'pass'} | |
r = requests.post( address + "authentication/signin", | |
data=json.dumps( creds ), | |
headers = { 'content-type': 'application/json' }) | |
response = json.loads( r.text ) | |
# Open a session for credential handling. | |
session = requests.Session() | |
session.headers.update({ 'content-type': 'application/json', | |
'X-AUTH-TOKEN': response[ 'token' ]}) | |
# Time interval in linux epoch (up to miliseconds). | |
endTime = int( time.time() * 1000 ) | |
startTime = endTime - 1800000 | |
# Let's get the devices, objects and indicators to collect metrics from them. | |
# By default, you'll get the first twenty devices. | |
r = session.get(address + 'devices') | |
devices = json.loads(r.text) | |
for device in devices['content']: | |
print "Device: {} id: {}".format( device[ 'name' ], device[ 'id' ]) | |
r = session.get( address | |
+ 'devices/{}?includeIndicators=true'.format( device[ 'id' ])) | |
response = json.loads(r.text) | |
for object in response[ 'objects' ]: | |
print "* Object: {} id: {}".format( object[ 'name' ], object[ 'id' ]) | |
for indicator in object[ 'indicators' ]: | |
print "** indictorId: {}".format( | |
indicator[ 'id' ]) | |
indicatorDataUrl = address + "devices/{}/objects/{}/indicators/{}/data".format( | |
indicator[ 'deviceId' ], indicator[ 'objectId' ], indicator[ 'id' ]) | |
params = { 'startTime': startTime, 'endTime': endTime } | |
r = session.get( indicatorDataUrl, params=params ) | |
print r.url | |
print r.text |
This was for py27.
Error seems to indicate ca-sevone01 cannot be resolved? I don't know where did that came from.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hello, is any specific python version required, as i tried it with python 2.7.7 and it failed.
root@Graphite-test1:/home# python sevone_alarms2.py
Traceback (most recent call last):
File "sevone_alarms2.py", line 10, in
headers = { 'content-type': 'application/json' })
File "/usr/lib/python2.7/dist-packages/requests/api.py", line 88, in post
return request('post', url, data=data, **kwargs)
File "/usr/lib/python2.7/dist-packages/requests/api.py", line 44, in request
return session.request(method=method, url=url, **kwargs)
File "/usr/lib/python2.7/dist-packages/requests/sessions.py", line 455, in request
resp = self.send(prep, **send_kwargs)
File "/usr/lib/python2.7/dist-packages/requests/sessions.py", line 558, in send
r = adapter.send(request, **kwargs)
File "/usr/lib/python2.7/dist-packages/requests/adapters.py", line 378, in send
raise ConnectionError(e)
requests.exceptions.ConnectionError: HTTPConnectionPool(host='ca1-sevone01', port=80): Max retries exceeded with url: /api/v1/authentication/signin (Caused by <class 'socket.gaierror'>: [Errno -2] Name or service not known)
Where: modules json, request and time are installed.