No one really like serial numbers, but keeping track of them is one of the “brushing your teeth” activities that everyone needs to take care of. It’s like eating your brussel sprouts. Or listening to your mom. You’re just better of if you do it quickly as it just gets more painful over time.
Not only is it just good hygene, but you may be subject to regulations, like eRate in the United States where you have to be able to report on the location of any device by serial number at any point in time.
Trust me, having to play hide-and-go seek with an SSH session is not something you want to do when government auditors are looking for answers.
I’m sure you’ve already guessed what I’m about to say, but I”ll say it anyway…
There’s an API for that!!!
HPE IMC base platform has a great network assets function that automatically gathers all the details of your various devices, assuming of course they supportRFC 4133, otherwise known as the Entity MIB. On the bright side, most vendors have chosen to support this standards based MIB, so chances are you’re in good shape.
And if they don’t support it, they really should. You should ask them. Ok?
So without further ado, let’s get started.
Importing the required libraries
I’m sure you’re getting used to this part, but it’s import to know where to look for these different functions. In this case, we’re going to look at a new library that is specifically designed to deal with network assets, including serial numbers.
from pyhpeimc.auth import *
from pyhpeimc.plat.netassets import *
import csv
auth = IMCAuth("http://", "10.101.0.203", "8080", "admin", "admin")
ciscorouter = get_dev_asset_details('10.101.0.1', auth.creds, auth.url)
How many assets in a Cisco Router?
As some of you may have heard, HPE IMC is a multi-vendor tool and offers support for many of the common devices you’ll see in your daily travels.
In this example, we’re going to use a Cisco 2811 router to showcase the basic function.
Routers, like chassis switches have multiple components. As any one who’s ever been the victem owner of a Smartnet contract, you’ll know that you have individual components which have serial numbers as well and all of them have to be reported for them to be covered. So let’s see if we managed to grab all of those by first checking out how many individual items we got back in the asset list for this cisco router.
len(ciscorouter)
What’s in the box???
Now we know that we’ve got an idea of how many assets are in here, let’s take a look to see exactly what’s in one of the asset records to see if there’s anything useful in here.
ciscorouter[0]
What can we do with this?
With some basic python string manipulation we could easily print out some of the attributes that we want into what could easily turn into a nicely formated report.
Again realise that the example below is just a subset of what’s available in the JSON above. If you want more, just add it to the list.
for i in ciscorouter:
print ("Device Name: " + i['deviceName'] + " Device Model: " + i['model'] +
"\nAsset Name is: " + i['name'] + " Asset Serial Number is: " +
i['serialNum']+ "\n")
Why not just write that to disk?
Although we could go directly to the formated report without a lot of extra work, we would be losing a lot of data which we may have use for later. Instead why don’t we export all the available data from the JSON above into a CSV file which can be later opened in your favourite spreadsheet viewer and manipulated to your hearst content.
Pretty cool, no?
keys = ciscorouter[0].keys()
with open('ciscorouter.csv', 'w') as file:
dict_writer = csv.DictWriter(file, keys)
dict_writer.writeheader()
dict_writer.writerows(ciscorouter)
Reading it back
Now we’ll read it back from disk to make sure it worked properly. When working with data like this, I find it useful to think about who’s going to be consuming the data. For example, when looking at this remember this is a CSV file which can be easily opened in python, or something like Microsoft Excel to manipuate further. It’s not realy intended to be read by human beings in this particular format. You’ll need another program to consume and munge the data first to turn it into something human consumable.
with open('ciscorouter.csv') as file:
print (file.read())
What about all my serial numbers at once?
That’s a great question! I’m glad you asked. One of the most beautiful things about learning to automate things like asset gathering through an API is that it’s often not much more work to do something 1000 times than it is to do it a single time.
This time instead of using the get_dev_asset_details function that we used above which gets us all the assets associated with a single device, let’s grab ALL the devices at once.
all_assets = get_dev_asset_details_all(auth.creds, auth.url)
len (all_assets)
That’s a lot of assets!
Exactly why we automate things. Now let’s write the all_assets list to disk as well.
**note for reasons unknown to me at this time, although the majority of the assets have 27 differnet fields, a few of them actually have 28 different attributes. Something I’ll have to dig into later.
keys = all_assets[0].keys()
with open('all_assets.csv', 'w') as file:
dict_writer = csv.DictWriter(file, keys)
dict_writer.writeheader()
dict_writer.writerows(all_assets)
Well That’s not good….
So it looks like there are a few network assets that have a different number of attributes than the first one in the list. We’ll write some quick code to figure out how big of a problem this is.
print ("The length of the first items keys is " + str(len(keys)))
for i in all_assets:
if len(i) != len(all_assets[0].keys()):
print ("The length of index " + str(all_assets.index(i)) + " is " + str(len(i.keys())))
Well that’s not so bad
It looks like the items which don’t have exactly 27 attribues have exactly 28 attributes. So we’ll just pick one of the longer ones to use as the headers for our CSV file and then run the script again.
For this one, I’m going to ask you to trust me that the file is on disk and save us all the trouble of having to print out 1013 seperate assets into this blog post.
keys = all_assets[879].keys()
with open ('all_assets.csv', 'w') as file:
dict_writer = csv.DictWriter(file, keys)
dict_writer.writeheader()
dict_writer.writerows(all_assets)
What’s next?
So now that we’ve got all of our assets into a CSV file which is easily consumable by something like Excel, you can now chose what to do with the data.
For me it’s interesting to see how vendors internally instrument their boxes. Some have serial numbers on power supplies and fans, some don’t. Some use the standard way of doing things. Some don’t.
From an operations perspective, not all gear is created equal and it’s nice to understand what’s supported when trying to make a purchasing choice for something you’re going to have to live with for the next few years.
If you’re looking at your annual SMARTnet upgrade, at least you’ve now got a way to easily audit all of your discovered environment and figure out what line cards need to be tied to a particualr contract.
Or you could just look at another vendor who makes your life easier. Entirely your choice.
Hello Chris,
I stumbled on your posts while trying to get my head around the REST interface of the HP IMC. From your blog posts, you seem to have some deep insight into it’s architecture, and I hope you can help point me to the right direction ..
I am trying to get some basic ACL operations working on the IMC, via the REST interface. However, the API reference is sparse on many important and required details regarding ACL config..
For instance,
* what are all the possible values of aclType ?
* within an aclRule object:
1. what is the range of possible protocols? I have been able to figure out that UDP = 17; IP=256 .. but what are the rest?
2. I have worked out that sourcePortOper / destPortOper should be 1 if a rule is to be applied to specific ports, and -1 if the rule is a non-port specific rule .. but I have also seen a value of 5 used in the API examples .. So, what is the full range of possible values for this attribute?
And so many other such questions on the fields and what to set them to for desired configs .. I am not particularly asking for specific answers to these questions, but where can I obtain a more complete documentation for the fields than what’s available in the web based API reference accessible within an installation ?
Thanks in advance ..
Hey Chux,
Just wanted to add notes to what we talked about offline!
In a nutshell, the swaggerUI and the online docs are really the best place to get started, but for specific API calls which are more complicated, I find that creating example objects of whatever I want to manipulate in the GUI and then using the GET method in the API is really the best way to reverse engineer and ultimately, understand how the API is working for specific objects.
This is one of those “integers are better than names” things that computers like. Primary keys as integers are just more efficient for computers, but the kinda suck for developers when we’re trying to understand what that primary key actually represents.
Hopefully you’ve been able to figure this out! I’d love to see some examples of your code, if shareable, once you’re through with your project!
@netmanchris
Hi Chris,
Thanks for your quick response .. and offline message. Your suggestion to work off GUI changes was very useful .. and I have been able to work out that the protocol numbers are standard IANA assigned numbers (http://www.iana.org/assignments/protocol-numbers/protocol-numbers.xhtml) .. I have also been able to address most of my other issues .. Thanks indeed ..
In terms of my code, I would probably not be able to share it, because it will expose internal details of our network architecture. But the code is not solely focused on the IMC, but integrates into .. and the IMC part of it primarily just does a few basic things:
— list all the ACLs assigned to a given device
— from that list, grab 2 of the ACLs
— for each of the 2 ACLs, add (or remove) some rules depending on command
— delete the original ACLs
— write the changed ACLs in place of the deleted ACLs
best regards
Hey Chux,
Glad to see you figured this out! The. GUI first trick has been invaluable in reverse engineers some of the more complicated APIs. The docs are useful, but they could always use improvements.
This is a great pointer.
I just finished building out an Class for the IP Address management in Resource > Terminal Access > IP Address
I might have to take a shot at the ACLM now. The API has SOOO many capabilities that it’s hard to decide where to start playing next!
Glad you worked it out!
@netmanchris
For IP, I think you will find the python library netaddr (http://netaddr.readthedocs.io/en/latest/) invaluable .. has support for ipv6, ipv4, cidr addresses, hosts-masks (which I see is preferred by IMC for many entries), etc .. give it a look in
Thanks for the pointer!
I actually ended up using the Python ipaddress standard library. I have looked at netaddr before and it looks very useful, but for what I’m doing, the standard library had all the functionality I needed in it.
I try to stick with the standard libraries unless there’s a specific reason for me to look elsewhere. Example – Requests vs URLIB2.
Any comments on comparing the netaddr and the standard ipaddress libraries?
Great strategy to use the standard libraries whenever possible .. Keeps your code leaner, and reduces side effects and dependencies that must be maintained .. As to comparisons with netaddr, there is none really .. Only attraction for netaddr is that ipaddress is not available for python 2, which is what I am using still. Besides that, I think ipaddress has all the features one is likely to need for working with IP addresses ..
How to get serial number of devices ( like CheckPoint, F5) which don’t support Entity MIB in IMC ?
Hey Pradeep,
To the best of my knowledge, IMC only uses the Entity MIB which is the standard way to expose asset information. For vendors who have chosen to not follow that standard… no luck. There are other ways to potentially get this content though using the Compliance Center or potentially to use a combination of python, regular expressions and the eAPI to write scripts to manually parse the text data as it comes back into a report which you could merge with the network assets data. I know that’s not what you’re looking for but it’s what I can think of.
The other thing I would suggest is to request that your vendors support the standard MIB. It’s there for a reason and has existed a long time. 🙂