Friday, February 10, 2012

Cisco CDR Timestamp and IP Address Conversion

The call detail records (CDRs) used by Cisco Unified Communications Manager (aka Call Manager) can be tricky to interpret. Two fields that are frequently confusing are the IP address fields and the timestamp field.

Continuing my theme of using Python to help with Cisco product administration, here's how to convert timestamps and IP addresses to human-readable format.

Timestamps are pretty easy. They're recorded in UNIX epoch time and there are lots of examples available showing how to convert them using Excel or various scripting tools. In Python:

import time

def time_to_string(time_value):
    """convert Unix epoch time to a human readable string"""
    return time.strftime("%m/%d/%Y %H:%M:%S",time.localtime(float(time_value)))

It took me a while to figure out how to convert IPv4 addresses, since CUCM uses signed 32-bit integers to represent IP addresses and Python's long integers can be of infinite length. First, a review of how to do the conversion manually is in order. Let's say that a CUCM CDR lists an IP address as "-2126438902". First, we convert this signed 32-bit integer to hex using Windows calculator:

-2126438902 = 0x81411E0A

Next, we break the hex number into 1-byte chunks, and reverse the order:


Finally, we convert each byte to decimal and put them together into an IP address:

Here's the Python function to do the conversion:

And here it is in the interpreter showing that it works for reasonable input types:

>>> int_to_ip('-2126438902')
>>> int_to_ip(-2126438902)
>>> int_to_ip(-2126438902L)

 I don't have access to an IPv6-aware CUCM install, so you're on your own for that!