Wednesday, September 14, 2016

SwimLane/NITRO Python script Query for existing devices

import base64
import requests
import json
from requests import session
import getpass
#This script will add data sources into Nitro. Works with Windows and Linux devices. Can easily be modified to add more types
#By changing the typeId in the script. There's over 500 typeId's though so only add commonly used ones.
#Created by Gil Turner (Turner_Gil@bah.com)

#Gets the a devices unique id a device
def getDSid(dataName):
params = {"types": ["THIRD_PARTY"],"filterByRights" : "false"}
params_json = json.dumps(params)
DeviceTreeURL = "https://ashbnitroesm.bah.com/rs/esm/devGetDeviceList"
response = requests.post(DeviceTreeURL, data=params_json, headers=payloadID, verify=False)
data = response.json()
DSid = ""
for item in data.get('return'):
if item.get('name').lower() == dataName.lower():
DSid = item.get('id').get('id')
return DSid

#username and password hard coded for now so its easier to test
username1 = ""
password1 = ""

#Later will have user login with their creds
username = input("Enter your username: ")
password = getpass.getpass("Enter your password: ")


Authoritize = 'Basic (' + str(base64.b64encode(bytearray(username + ":" + password, 'ascii')), 'ascii') + ')'
payload = {"Authorization": Authoritize}

#Actually will login to nitro with the credientials as well as get the sessionID
res = requests.post('https://ashbnitroesm.bah.com/rs/esm/login', headers=payload, verify=False)
x = res.text.index('sessionID') + 10
id = ""
while res.text[x] != "<":
id += res.text[x]
x = x + 1
#print(id)
payloadID = {"Authorization": "Session " + id}

#user inputted information about the data source they want to add.
dsType = input("Type either windows or linux for the type of device you want to add: ")
dsID = input("Enter either no or the name of the Sub group you want to input the data source. If no is typed, the data source will default to Receiver02:  ")
idsID = ""
if dsID == "no":
idsID = 144117387099111424
else:
idsID = getDSid(dsID)

#adding windows data source
if dsType == "windows":
dsNameW = input("Enter the name of the data source you want to create: ")
dsIPW = input("Enter the ip address of the data source you want to create: ")
datarW = {"datasource": {
"name": dsNameW,
"childEnabled": False,
"childCount": 0,
"childType": 1,
"ipAddress": dsIPW,
"url": "",
"enabled": True,
"idmId": 0,
"parameters": [{"key": "collector", "value": "npp"}, {"key": "elm_logging", "value": "no"}, {"key": "encrypt", "value": "F"}, {"key": "keepme", "value": ""}, {"key": "parsing", "value": "yes"}, {"key": "snmp_trap_id", "value": "0"}],
"parentId": {"id": idsID},
"typeId": {"id": 43},
"zoneId": 0
}}

jsonDatarW = json.dumps(datarW)
responsesW = requests.post('https://ashbnitroesm.bah.com/rs/esm/dsAddDataSource', headers=payloadID, data=jsonDatarW, verify=False)
print(responsesW.text)

#adding linux data source.
elif dsType == "linux":
dsNameL = input("Enter the name of the data source you want to create: ")
dsIPL = input("Enter the ip address of the data source you want to create: ")
datarL = {"datasource": {
"name": dsNameL,
"childEnabled": False,
"childCount": 0,
"childType": 1,
"ipAddress": dsIPL,
"url": "",
"enabled": True,
"idmId": 0,
"parameters": [{"key": "parsing", "value": "yes"}],
"parentId": {"id": idsID},
"typeId": {"id": 65},
"zoneId": 0
}}

jsonDatarL = json.dumps(datarL)
responsesL = requests.post('https://ashbnitroesm.bah.com/rs/esm/dsAddDataSource', headers=payloadID, data=jsonDatarL, verify=False)
print(responsesL.text)

#Error message if invalid type of data source is selected.
else:
print("Invalid type of data source. Please rerun the program and enter a valid data source type.")


No comments:

Post a Comment