Published on 29 March 2011 by

Mac Inventory/Warranty Lookups with MCollective

Maintaining an accurate inventory of all of your machines is an oft-neglected evil for IT staff. Knowing when a machine was purchased and, more importantly, if it's still under warranty is often overlooked until something malfunctions. Most vendors provide a way to access the machine's serial number programmatically, and many of them provide a web conduit to provide purchase and warranty information. This article will step you through setting this process up on a Mac running OS X.

Custom Facts for Warranty Information

The first part of this equation is to create custom facts that will provide us with the following:

  1. The purchase date
  2. If our warranty is currently valid
  3. The warranty expiration date
  4. A description of the machine

To get this information, we pull the Serial Number using the /usr/sbin/system_profiler binary and then query Apple's Self Servicing site for all the relevant information. Here's what the Facter fact looks like: Access the full fact here.

        require 'facter'
	require 'open-uri'
	require 'openssl'

	# Constraining to the Darwin Kernel - it's only useful for Macs
	if Facter.value('kernel') == 'Darwin'
	  warranty_array = []
	  hash = {}
	  sn  = %x{system_profiler SPHardwareDataType | awk -F": " '/Serial/{print $2}'}
	  open('https://selfsolve.apple.com/warrantyChecker.do?sn=' + sn.upcase + '&country=USA') {|item|
	         item.each_line {|item|}
	         warranty_array = item.strip.split('"')
	         warranty_array.each {|array_item|
	           hash[array_item] = warranty_array[warranty_array.index(array_item) + 2] if array_item =~ /[A-Z][A-Z\d]+/
	         }
	  }
	end

	Facter.add("purchase_date") do
	  confine :kernel => "Darwin"
	  setcode do
	    hash['PURCHASE_DATE'].gsub("-",".")
	  end
	end

	Facter.add("warranty_out") do
	  confine :kernel => "Darwin"
	  setcode do
	    (hash['HW_COVERAGE_DESC'] == 'Out of Warranty') ? "Yes" : "No"
	  end
	end

	Facter.add("warranty_end") do
	  confine :kernel => "Darwin"
	  setcode do
	    (!hash['COV_END_DATE'].empty?) ? hash['COV_END_DATE'].gsub("-",".") : "Expired"
	  end
	end

	Facter.add("product_description") do
	  confine :kernel => "Darwin"
	  setcode do
	    hash['PROD_DESCR'].chomp
	  end
	end

The four facts that we pull are "purchase_date", "warranty_out", "warranty_end", and "product_description". Just having these facts is beneficial, but now we need a method to query them on active machines. MCollective provides an excellent conduit for these queries using the mc-facts and mc-find-host binaries. Let's take a look and see how this works.

In the next post, I will expand on this custom fact by showing how to use MCollective to receive immediate feedback from all of your nodes (whether online or offline).

Continue with Part 2 of this post here.

Gary Larizza is the Director of Technology for a small K-12 School district in Northern Ohio where he routinely breaks systems in the name of advancement. When he's not trying to automate himself out of a job through the use of Puppet and Ruby, he enjoys seeking out awesome open source tools that will make Mac Management easier. He's been using Puppet in the Education sector for the past two years on hardware that would make Linux admins cry (namely, G5 Xserves and an iMac) and loves to share the knowledge he's gained with anyone who buys him a wheat beer (Weihenstephaner FTW). All of his notes are on his Posterous blog, and he will be speaking at the 2011 Penn State University Macadmins Conference if you'd prefer to chat with him in-person.

Share via:
Posted in:
The content of this field is kept private and will not be shown publicly.

Restricted HTML

  • Allowed HTML tags: <a href hreflang> <em> <strong> <cite> <blockquote cite> <code> <ul type> <ol start type> <li> <dl> <dt> <dd> <h2 id> <h3 id> <h4 id> <h5 id> <h6 id>
  • Lines and paragraphs break automatically.
  • Web page addresses and email addresses turn into links automatically.