Format a list of snapshots coming from a controller

Part of my job is to analyze different data from my customer’s systems and dozen times a day I have to parse and transform regular output from Data ONTAP systems to more organized format, suitable for an Excel spreadsheet for example.

This time I needed a list of  snapshots in a system.

The thing is that Data ONTAP returns in something like this :

yann$ ssh [email protected] snap list -n
Volume vol0
working...

No snapshots exist.

Volume vol_01102013_135658
working...

date          name
------------  --------
Nov 01 18:17  nightly.0      
Oct 31 12:51  nightly.1      
Oct 29 20:00  hourly.0       
Oct 25 16:00  hourly.1       
Oct 22 20:00  hourly.2       
Oct 22 17:03  hourly.3       
Oct 21 20:43  hourly.4       
Oct 21 16:00  hourly.5       

Volume datastore3
working...

date          name
------------  --------
Nov 01 18:17  nightly.0      
Oct 31 12:51  nightly.1      
Oct 29 20:00  hourly.0       
Oct 25 16:00  hourly.1       
Oct 22 20:00  hourly.2       
Oct 22 17:03  hourly.3       
Oct 21 20:43  hourly.4       
Oct 21 16:00  hourly.5       

Volume data
working...

date          name
------------  --------
Nov 01 18:17  nightly.0      
Oct 31 12:51  nightly.1      
Oct 29 20:00  hourly.0       
Oct 25 16:00  hourly.1       
Oct 22 20:00  hourly.2       
Oct 22 17:03  hourly.3       
Oct 21 20:43  hourly.4       
Oct 21 16:00  hourly.5       
Sep 24 08:59  vs1(4079432741)_data.1 (snapmirror)

So I spent an hour working on a sed command that would reformat this in a suitable copy/paste-able text for Excel, and here is the result :

ssh [email protected] snap list -n|tr -d '\r' | sed -n -E '{/^Volume /{N;N;N;s/\n/ /g;s/.*Volume ([^ ]*).*/\1/;h;};/^---/,/^$/{/^---/d;/^$/d;G;s/(.*) (..:..)  (.*)\n(.*)/\4      \1      \2      \3/g;p;};}'

Kinda crazy right ? I love sed, but sometimes you just have to empty your brain and let the inner geek in you take over. Note that I couldn’t reproduce in the blog the “tab” characters between “\4 \1 \2 \3”, to do this on a Mac OS X Terminal, you must type Ctrl-V then the “tab” key.

Here is a sample output :

vol_01102013_135658	Nov 01	18:17	nightly.0      
vol_01102013_135658	Oct 31	12:51	nightly.1      
vol_01102013_135658	Oct 29	20:00	hourly.0       
vol_01102013_135658	Oct 25	16:00	hourly.1       
vol_01102013_135658	Oct 22	20:00	hourly.2       
vol_01102013_135658	Oct 22	17:03	hourly.3       
vol_01102013_135658	Oct 21	20:43	hourly.4       
vol_01102013_135658	Oct 21	16:00	hourly.5       
datastore3	Nov 01	18:17	nightly.0      
datastore3	Oct 31	12:51	nightly.1      
datastore3	Oct 29	20:00	hourly.0       
datastore3	Oct 25	16:00	hourly.1       
datastore3	Oct 22	20:00	hourly.2       
datastore3	Oct 22	17:03	hourly.3       
datastore3	Oct 21	20:43	hourly.4       
datastore3	Oct 21	16:00	hourly.5       
data	Nov 01	18:17	nightly.0      
data	Oct 31	12:51	nightly.1      
data	Oct 29	20:00	hourly.0       
data	Oct 25	16:00	hourly.1       
data	Oct 22	20:00	hourly.2       
data	Oct 22	17:03	hourly.3       
data	Oct 21	20:43	hourly.4       
data	Oct 21	16:00	hourly.5       
data	Sep 24	08:59	vs1(4079432741)_data.1 (snapmirror)

You can paste this right into Excel :

snapshot list in Excel

It might not be perfect, text parsing is usually a delicate and sensible matter, but it is good enough for what I had to do and I thought it was worth sharing.

Most users would use OnCommand Unified Manager to create a report, that’s way easier ! But sometimes you just don’t have access to it or it is quicker to get to an ssh connection.

Leave a Reply

Your email address will not be published. Required fields are marked *