How to Rename Agents Using AWS Lambda and the NetBeez API

Automatically Rename Remote Worker Agents based on the employee’s HR profile

This notebook showcases a use-case suitable for anyone managing more than 100 remote worker agents on their BeezKeeper. It actually came out from a request from one of our customers who is managing more than 1000 remote worker agents. They wanted to name the agents with the employee’s first name, last name and office number. The remote worker agent software reports the employee’s ID, which is the same as the laptop’s name, to the NetBeez BeezKeeper server.

Then there is a script running on AWS Lambda, which periodically checks for new agents whose name matches a regular expression representing the ID format, using the NetBeez API. When it finds some, it cross-references that ID with the employee’s profile on the HR software and generates a string with the ID, first name, last name and office number for each agent. It finally submits those names back to the NetBeez server API and they get updated.

This diagram represents the workflow:

API AWS Lambda

In the following sections we will describe how the python script running in the Lambda function can be written to implement the functionality we just described.

Setup your script for NetBeez BeezKeeper API access

The following code will initialize everything needed to enable your script to access the API. If you would like to learn exactly how this setup works, please refer to our previous blog post about using the NetBeez API.

In [1]:

baseUrl='https://' + beezkeeper_fqdn


import requests
import urllib3
import json


nbHeaders = {
    'Cache-Control': 'no-store',
    'Content-type': 'application/json',
    'Authorization': 'Bearer ' + apiKey

Function to get the agents records

In this section we implement the function that will get the agents which only have the employee ID in their name.

The format of the employee id is id1234567 so a regular expression that matches names which have the ID at the end of it would be id[0-9]{7}$.

Documentation for the endpoint used can be found here:

In [2]:

def getNBAgents():
    payload = ''
    url = baseUrl + '/agents?filter[name][regex]=id[0-9]{7}

amp;stubbed=true&page[offset]=1&page[limit]=1000&type=beta' response = requests.request("GET", url, headers=nbHeaders, verify=False) return response.json()['data'] print(getNBAgents()) [{'id': '395', 'type': 'agent_stub', 'attributes': {'description': None, 'agent_class': 'mac', 'active': True, 'category': 'remote_worker_agent', 'agent_type_name': 'mac', 'open_incident_id': None, 'reached_target_test_limit': False, 'reached_scheduled_test_limit': None, 'name': 'id5647382'}}]

Function to get the employee information

In this section we implement the HR database lookup. For the sake of demonstration we’ll just use a public API that is generating random names. When you are implementing this workflow you should check the software’s documentation for information on how to get employee names from your HR software.

In [3]:

def getEmployeeName(employeeId):
    url = '' + employeeId
    response = requests.get(url)
    return response.json()[0]


John Evans

Function to generate an array of objects containing agent ids and employee names

We now implement the function that generates an array of objects with agent ids and names that will be used as the data pushed back to the NetBeez server to update the agent record names.

In [4]:

def generatePayloadFromAgents(agents):
    agentsToUpdate = []
    for agent in agents:
        employeeName = getEmployeeName(agent['attributes']['name'])
        agentId = int(agent['id'])
        newAgentName = agent['attributes']['name'] + ' - ' + employeeName
        agentsToUpdate.append({ 'id': agentId, 'name': newAgentName})
    payload = { 'agents': agentsToUpdate }
    return payload


{'agents': [{'id': 395, 'name': 'id5647382 - Kimberly Green'}]}

Function to push the new agent names back to the NetBeez API

Now that we have all the data transformation function done, we can implement the final step which is the PUT request back to the NetBeez API, which takes as input the output from the generatePayloadFromAgents() function.

Documentation for the endpoint used can be found here:

In [5]:

def pushNBAgentsWithEmployeeNames(newAgentNamesPayload):
    url = baseUrl + '/agents?type=beta'
    response = requests.request('PUT', url, headers=nbHeaders, data=json.dumps(newAgentNamesPayload), verify=False)
    return response

Final glue for running on AWS Lambda

We finally add some code that will invoke the functions above whenever the Lambda function is invoked. Invoking the Lambda function can be done on a schedule.

More information on how you can install and setup an AWS Lamda function can be found here:

In [6]:

def lambda_handler(event, context):

if __name__ == '__main__':
    lambda_handler('', '')

State Before and After running this script

Here is what the agent looked like before running this script: 

before script

Here it is after running this script: 

after script


This article describes a real use-case developed with one of our customers who is managing thousands of remote worker agents on a NetBeez dashboard. Manually tracking new agents deployed and figuring out how to name them is time consuming, let alone almost impossible. With this workflow the process is automated to keep your dashboard organized at all times. Currently there is no triggering event available from NetBeez, but we will eventually provide functionality where the NetBeez server can be used to trigger these kinds of automation workflows. For now it needs to be periodically scheduled to run and discover new agents.

More documentation on the NetBeez API can be found here: Other API examples can be found here and here.

The code contained herein is available as a Jupyter Notebook on the NetBeez GitHub account.

Lastly, are there other examples that you want me to share about using the NetBeez API? Just drop a note in the comments section!

decoration image

Get your free trial now

Monitor your network from the user perspective

You can share

Twitter Linkedin Facebook

Let's keep in touch

decoration image