Collect metric to post to vRealize Operations HTTP Post adapter
When I present vRealize Operations to potential customers, I often let them know that vRealize Operations can accept any data. Whether it's a property or a metric. To show that capability, I thought I would post my office temperature. I have a Raspberry Pi Zero that I use for a desktop clock that collects weather data.
It's fairly straightforward to post the data. In another blog I create the object. The creation process will create a unique identifier in vROps. That ID is then used to access the resource to update metrics or properties. The easiest way to find the ID is within the vROps URL. Once you're logged into vROps, search for the object and click on it. In the web browser's url, you will see the ID of the selected resource.
Once you have the resource id, you can update the code below with your value. When I manually execute the script, vROps will display the posted data within a couple of minutes. Once I have the script working, I add a cron job to execute on a 5 minute interval.
Cron entry:
In the code below, the statKey "Room|Temperature(F)" is category and attribute name we will add and update with a value. If the category or attribute does not exist, vRealize Operations will create them on the resource when the data is posted.
When I present vRealize Operations to potential customers, I often let them know that vRealize Operations can accept any data. Whether it's a property or a metric. To show that capability, I thought I would post my office temperature. I have a Raspberry Pi Zero that I use for a desktop clock that collects weather data.
It's fairly straightforward to post the data. In another blog I create the object. The creation process will create a unique identifier in vROps. That ID is then used to access the resource to update metrics or properties. The easiest way to find the ID is within the vROps URL. Once you're logged into vROps, search for the object and click on it. In the web browser's url, you will see the ID of the selected resource.
Once you have the resource id, you can update the code below with your value. When I manually execute the script, vROps will display the posted data within a couple of minutes. Once I have the script working, I add a cron job to execute on a 5 minute interval.
Cron entry:
*/5 * * * * /home/pi/bin/montemp.py
The code (montemp.py):
#!/usr/bin/python
import sys, os, time, signal, json
import urllib3
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
import requests
from requests.packages.urllib3.exceptions import InsecureRequestWarning
requests.packages.urllib3.disable_warnings(InsecureRequestWarning)
os.environ["SDL_FBDEV"] = "/dev/fb1"
resID = "a982ad91-21b4-4966-93ac-8dc254e6ec6a"
usrName = "<user with write permission>"
usrPass = "<user password>"
srvName = "<vrops URL or IP>"
baseURL = "https://" + srvName
ds18b20 = ''
def signal_handler (signal, frame):
sys.exit(0)
def setup():
global ds18b20
for i in os.listdir('/sys/bus/w1/devices'):
if i != 'w1_bus_master1':
ds18b20 = i
def readTemp():
# location = '/sys/bus/w1/devices/28-00000a423922/w1_slave'
location = '/sys/bus/w1/devices/' + ds18b20 + '/w1_slave'
tfile = open(location)
text = tfile.read()
tfile.close()
secondline = text.split("\n")[1]
temperaturedata = secondline.split(" ")[9]
temperature = float(temperaturedata[2:])
temperature = temperature / 1000
return temperature
def Authenticate():
tokenURL = baseURL + "/suite-api/api/auth/token/acquire"
authJSON = {"username": usrName,"authSource": "Local","password": usrPass,"others": [],"otherAttributes": {}}
authHeaders = {"Content-Type":"application/json","Accept":"application/json"}
authResponse = requests.post(tokenURL,data=json.dumps(authJSON),headers=authHeaders,verify=False)
if (authResponse.status_code != 200):
print('probably invalid credentials')
print('Returned Status Code: ' + authResponse.status_code)
exit
else:
authToken = "vRealizeOpsToken " + authResponse.json()['token']
return authToken
def UpdateMetric(token):
setup()
currenttemp = readTemp()
temp_f = float((currenttemp*9/5)+32)
resData = { "stat-content" : [ {
"statKey" : "Room|Temperature(F)",
"timestamps" : [int(time.time()*1000)],
"data" : [temp_f],
"others" : [],
"otherAttributes" : {}
} ]
}
vropsURL = baseURL+"/suite-api/api/resources/"+resID+"/stats"
postHeaders = {"Content-Type":"application/json","Authorization":token,"Accept":"application/json"}
response = requests.post(vropsURL,headers=postHeaders,data=json.dumps(resData),verify=False)
status = str(response.status_code)
print (response.text)
print ("Update Metric exited with status: " + status)
def Logout(token):
releaseURL = baseURL + "/suite-api/api/auth/token/release"
authHeaders = {"Content-Type":"application/json","Authorization":token,"Accept":"application/json"}
authResponse = requests.post(releaseURL,headers=authHeaders,verify=False)
## Script Start
signal.signal(signal.SIGINT, signal_handler)
authToken=Authenticate()
UpdateMetric(authToken)
Logout(authToken)
No comments:
Post a Comment