Forum Discussion

Tanvir's avatar
6 years ago

Update resource's custom property with a csv file

Hi All

 

I have written a script which would take csv file with column and update the device custom property's value.

This script works like displayname=~  but If you provide full display name, please change the line 39 to queryParameters = '?fields=id&filter=displayName:'+devicename+'&size=300'

CSV file have to have heads and the first column is resource name, the second column is property name and the third column have to have  value.

 

 

#!/bin/ehave value

mport pandas as pd
import requests
import json
import hashlib
import base64
import time
import hmac
import csv

#Account Info
AccessId ='XXX'
AccessKey ='YYY'
Company = 'abc'

filename = raw_input("Enter a CSV name with extention: ")

# open the file to read number of rowa.
file = open(filename)
row = len(file.readlines())


df=pd.read_csv (filename)

for r in range (0, row-1):
    devicename=df.iloc[r,0]
    print(devicename)
        
        
    #Request Info
    httpVerb ='GET'
    resourcePath = '/device/devices'
    
    queryParameters = '?fields=id&filter=displayName~'+devicename+'&size=300'
    data = ''

    #Construct URL 
    url = 'https://'+ Company +'.logicmonitor.com/santaba/rest' + resourcePath + queryParameters

    #Get current time in milliseconds
    epoch = str(int(time.time() * 1000))

    #Concatenate Request details
    requestVars = httpVerb + epoch + data + resourcePath

    #Construct signature
    signature = base64.b64encode(hmac.new(AccessKey,msg=requestVars,digestmod=hashlib.sha256).hexdigest())

    #Construct headers
    auth = 'LMv1 ' + AccessId + ':' + signature + ':' + epoch
    headers = {'Content-Type':'application/json','Authorization':auth}

    #Make request
    response = requests.get(url, data=data, headers=headers)

    #Parse response
    jsonResponse = json.loads(response.content)
    #print 'Response Body:',jsonResponse

    for i in jsonResponse['data']['items']:
        #print  str(i['id'])
        deviceId = str(i['id'])
        j=len(deviceId)-3
        ##print ('j',j)
        for i in range (0, j):
                
                            
            #Request Info
    
            httpVerb ='PUT'
            proertyname=df.iloc[r,1]
            resourcePath = '/device/devices/'+deviceId+'/properties/'+proertyname
            queryParams =''
            propertyvalue= df.iloc[r,2]
            data = '{"value":"'+propertyvalue+'"}'
                
            #Construct URL 
            url = 'https://'+ Company +'.logicmonitor.com/santaba/rest' + resourcePath + queryParams

            #Get current time in milliseconds
            epoch = str(int(time.time() * 1000))

            #Concatenate Request details
            requestVars = httpVerb + epoch + data + resourcePath

            #Construct signature
            signature = base64.b64encode(hmac.new(AccessKey,msg=requestVars,digestmod=hashlib.sha256).hexdigest())

            #Construct headers
            auth = 'LMv1 ' + AccessId + ':' + signature + ':' + epoch
            headers = {'Content-Type':'application/json','Authorization':auth}

            #Make request
            response = requests.put(url, data=data, headers=headers)
            #print ('Response Status:',response.status_code)
            print ('Response Body:', str(response.content))