Pattern development for fun and profit.
I have a confession to make. It may shock you, so you had better sit down.
Are you sitting? OK, here goes ……
I enjoy writing patterns!
Phew! There, I have said it. Glad I got that off my chest.
Seriously though, developing patterns for Foundation is something that I enjoy.
Since Foundation 7.0 was released back in late 2007 I have developed a collection of patterns for my own edification, but it is only since the recent release of 7.2 that I have really been able to explore the more interesting ideas that have been on the back-burner. This is because 7.2 added several node kinds specifically intended for the sort of pattern that I was interested in developing (See Detail Node in the Node Lifecycle Document).
Typically the whole process starts of with a question: “Wouldn’t it be cool if Foundation could discover ….?“. The great thing about TPL and Patterns is that if the information is out there to be discovered then you CAN make Foundation model it!
So, an example:
A while ago I wondered whether we could discover if any of our internal systems had USB Storage devices attached to them. You know the kind of thing; USB sticks, external hard drives, iPods and the like. Aside from the curiosity factor, there was always the possibility that there was a business case for knowing this, IT Policies being what they are. To be honest though I mainly wanted to see whether it could be done and to learn something in the process.
Where to start? As is typically the case in corporate IT we mostly run Windows systems here at Tideway; predominantly XP with a smattering of Vista for the unlucky ones ( actually I don’t really mind Vista – shoot me down if you like :) ). To start with therefore I did not worry about Linux etc.
Foundation has four main ways of retrieving information from Windows Hosts:
- WMI queries
- Registry queries
- Executing commands
- Getting files
My first instinct was to go the WMI route and use the great tool Scriptomatic . This allows you to query the various WMI classes on your local host or other remote machines and have a good old dig. If this does not bring any obvious results I also often drop on the the Hey Scripting Guy Archive for inspiration as it is a great resource of information, tips and tricks – check it out ).
In the case of USB Storage devices it turns out there is a WMI class that contains all the right information; Win32_DiskDrive.
My first step is to create a stub pattern that just gets the raw data, this allows me to get a feel for what processing may have to be done. In this case it looked roughly like this:
tpl 1.1 module Misc.USB;
pattern WindowsUSBDrives 1.0
‘’‘
Pattern for getting a list of attached USB drives on Windows devices
‘’‘
overview
tags USB;
end overview;
triggers
on host := Host created, confirmed where os_class = “Windows”;
end triggers;
body
query_results := discovery.wmiQuery(host, “SELECT * FROM Win32_DiskDrive WHERE InterfaceType = ‘USB’”, “root\CIMV2” );
end body;
end pattern;
Having discovered my own machine (with as many USB Storage devices I could find plugged in) I extended the pattern to iterate over the results of the query and create a “Detail” node for each USB Storage device found, link them to the Host they were attached to and Bob is your mother’s brother. In total the pattern took no more than 10 minutes to throw together – great. Some additional work was required to make sure that the newly created nodes were removed at a sensible time after the entities that they represented were no longer found.
Once that was done, I applied the pattern to our internal instance of Foundation (Project Dogfood – that needs a blog post on its own) and waited for the next scheduled scans to occur.
Here are some of things that it found:
- As you might expect there was a smattering of External HDDs of varying capacities (no TB devices yet sadly ).
- Several USB thumb-drives – Charles and I drew curious looks as we went digging around under someones desk to find the “SanDisk Cruzer Micro” device that Foundation said was there but we could not initial see; yes it was there.
- We freaked out our beloved leader Mr Muirhead by asking him if he had a four-port card reader plugged into his laptop. He did, but it had fallen behind his desk.
- You could see who had their Blackberry devices connected, and who had them connected in Mass storage mode.
Our IT manager said that he would find the report useful and remarked that more and more, Foundation is becoming like an IT Swiss Army knife that reduces the tool set he needs day to day (personally I prefer a Leatherman).
That is just one example of the kind of information that Foundation does not store by default but, with a very small amount of effort, can be made to do so.
Should you have a question about your IT infrastructure that Foundation does not answer by default I would encourage you to give a go at writing a pattern to help you. It is not that hard and you will learn a lot in doing so.
Post your experiences to the forum and should you be happy to, the patterns themselves. I have posted the final version of the above pattern here.
Who knows, you may help someone.

Comments have been disabled for this post. 