SAM & Multi-Statistic Component Scripts

SAM & Multi-Statistic Component Scripts

In September I wrote a few things about how I use SAM Script Templates – specifically with PowerShell.  One of the more powerful things in the SAM arsenal is the ability to return multiple components in a single script.  I’ve used this in any number of ways, but the one that gives me the most satisfaction is a PowerShell script that I wrote for Linux.

First things first – I’m a Windows guy.  I won’t bother defending this because it’s the fault of my upbringing.  My parents used Windows, my first real job used Windows, my current job uses Windows.  It’s what I know.  However, I’m also a non-conformist at times and on the side, I secretly see other operating systems.

In my lab at the office I have multiple flavors of Linux running – including Ubuntu, CentOS, Redhat, SUSE, and even Oracle.  Much of this is because I love to explore.  I’ve always said that I’m a technologist first.

Two years ago, was the first time that I heard about HA Proxy in a THWACKcamp session with some good friends.  After speaking about runbooks with Dave and Leon, I realized that this was a powerful little piece of software that I should know a little more about.

So I set myself up a little test bed.  I’ve got four web servers running Apache.  I’ve got a separate MySQL server, a server for an NFS share, and one to run the HA Proxy.  Basically, it looks like this:

Needless to say it took me a little bit to setup right (I mentioned that I know Windows well, right?).  After I got it running, I could point my SAM templates at it for Apache and MySQL without a problem (providing you remembered to install the drivers for MySQL on your Orion server).  When I tried to do the same for HA Proxy, I ran into some trouble.  I don’t think that anything is wrong with the template – I think it was an issue with my particular build.

I’ll repeat – I’m not someone who knows Perl.  As a frequent scripter, I can read the scripts and understand what they are doing, but I’ve got zero chance of troubleshooting anything more difficult than a typo. Let’s take a quick peek at the script together.

So I can see what’s happening here. HA Proxy is creating a CSV of the status of the program on a web page and port with a specific set of creds.  As I’ve stated before I have NO CLUE how to troubleshoot this, and my mind immediately went to PowerShell.  I wonder if I could just run this from the Orion server as a PowerShell Script?  Then my mind went to a strange place.  Didn’t I just see that PowerShell Core is available for Linux?

I know what you are saying – hasn’t this gone far afield from the title of this post?  Yes, but this is all background, so please be patient.

This is the results that would get returned by just the web call:

It’s a little basic, but pretty easy to parse.  The first line (started with the “#<space>”) gives the field names.  Then each of the rows are raw data associated with that field.  If I were to nix the “#<space>” from the front, it’s pretty much a beautiful formatted, CSV right?

So then I referred back to the original script to decipher what some of the stats meant in English.  There are 62 columns (at present) and I don’t need all of them.  Many are blank or 0’s most of the time.  I picked my 10 favorite because SAM component monitors can only handle 10 statistics from a single call.  This was enough based on what I wanted to use.  If you are following along with the home game, the ones I chose were:

Stat Number Field English Name
1 qcur Current_Requests
2 scur Current_Sessions
3 stot Total_Sessions
4 bin Bytes_In
5 bout Bytes_Out
6 dreq Denied_Requests
7 dresp Denied_Responses
8 econ Connection_Errors
9 eresp Response_Errors
10 status Status

I won’t lie: I had to look up what some of these actually meant.

Then I began crafting my PowerShell template.  I started as I do (95% of the time) with my own personal SAM template and then filled in the blanks.

The pseudo-code goes something like this:

  1. Check to see if I have everything I need (the web farm name, the port, the Uri segment)
  2. Get the results of the web call (get the raw data above, making sure to split it at each end of line)
  3. Nix the “#<space>” from the first line (so it can be the “headers” in my CSV)
  4. Find the farm member I’m interested in (FRONTEND, BACKEND, or a server’s name)
  5. Output the statistics.

Step 5 is the one that gets most people.  Many SAM script templates return only a single value – a single number and (optionally) a message.  In PowerShell it normally takes the form:

Since each of my statistics refer to a single web farm and all those statistics have to do with said farm, it’s better to bundle them together.

The proper formatting for that uses the period to separate elements (dot-delimited).  So, if we take the above and expand it for an additional statistic, it looks like this:

Remember that although there are multiple statistics returned, this is a single component monitor, so it only needs one exit code.

Now SAM will capture both statistics when you run the script once.  This is especially helpful when working with services that may limit the number of calls (like many web-based API’s).

Putting all the parts together yields the following script.

Executing it against my HA Proxy gives me the following results:

This is exactly how the PowerShell script component expects to see these kind of results.

Now that I have it working for the front-end, I can just copy it for each of the additional farm members and change that one parameter.  The results speak for themselves.

So, yes, we went far afield, but if you stuck with me, you probably gained a little bit of insight on how to get multiple statistics working with your SAM component monitors.

If you have any questions, please feel free to ask me.  I’d love to hear your thoughts.

Leave a Comment