Monday, July 28, 2014

ec2list -- quickly and easily list ec2 instances across all accounts and regions

Here's a quick tool I cooked up after working in with multiple AWS accounts in many regions. Sometimes I just need a particular piece of information about a host and I only had its instance id. Before this script I would have to run multiple aws calls in different regions until I found the instance.

The script itself is a wrapper around the aws cli tools. It will gather a list of instances in all accounts you specify and in all of the regions. It's multithreaded and I find it finishes in about 4 to 5 seconds. You'll get better performance if you comment out regions that you're not using.

Just clone the repo and execute.

Usage

usage: ec2list [-h] [-i INSTANCES] [-v] [-a] [-f FILTER]

optional arguments:
  -h, --help            show this help message and exit
  -i INSTANCES, --instances INSTANCES
                        Comma-separated list of instances to filter results.
  -v, --verbose         Print all host metadata.
  -a, --all             List all hosts, even those that aren't running.
  -f FILTER, --filter FILTER
                        key value filters to pass directly to command. Eg:
                        Name=instance-type,Value=m1.small

Prerequisites

You must have a $HOME/.aws/config file setup with the proper credentials.
An example profile:
[profile production]
aws_access_key_id=AKIAFOO
aws_secret_access_key=grzOgbar
region=us-west-1

[profile staging]
aws_access_key_id=AKIAIFOO
aws_secret_access_key=BAYS232bar
region=us-west-1
You can test that the different profiles are active by running the aws cli tool
aws ec2 describe-instances --profile=production
aws ec2 describe-instances --profile=staging

Listing all instances in a succinct manner.

The columns when run this way will always be predictable (unlike -v). Useful if you're using awk or other things.
$ ./list
i-7e15d971 stephen_test_001 10.0.0.100 54.183.84.15 m3.large production
i-7e15d971 stephen_test_002 10.0.0.100 54.183.84.16 m3.large staging
...

List instances that are also stopped

By default only running instances will be shown. Use the -a or --all flag to show all instances, even those that are stopped.
$ ./list -a

Be verbose 

Verbose prints everything returned by ec2. The return values are unordered.
./list -v
i-7e15d971:
  Monitoring: {"State": "disabled"}
  PublicDnsName: ec2-54-131-88-95.us-west-2.compute.amazonaws.com
  RootDeviceType: ebs
  State: {"Code": 16, "Name": "running"}
  EbsOptimized: False
  ...
i-bba5d6e6:
  Monitoring: {"State": "disabled"}
  PublicDnsName: ec2-54-210-162-78.us-west-1.compute.amazonaws.com
  RootDeviceType: ebs
  State: {"Code": 16, "Name": "running"}
  EbsOptimized: False
  ...

Filter by a list of instance IDs

To easily filter instances by instance id, they can be passed as a comma-separated list with the -i or --instances flag.
./list -i i-bba5d6e6,i-cad1b0c2,i-afa406f2

Print only instances that are an m1.small, or other things

Note that the filters arg allows you to pass filters down directory to the underlying aws ec2 command. See the ec2 api for information on the types of filtering you can do.
$ ./list -f Name=instance-type,Values=m1.small

Thursday, May 8, 2014

Converting NMEA latitude and longitude coordinates to degrees decimal for use in google maps

I've been tinkering around with my GlobalSat bu-353 USB gps receiver lately.
Initially I was confused about the coordinates I received from the device. I mapped them in google maps and it showed my location in the middle of the pacific ocean. Do I really live in the plastic bag vortex in the pacific??

After some googling I realized I wasn't using the correct formula to convert the lat/long NMEA-specified format to decimal degrees acceptable to google maps.

According to the specs, NMEA specifies latitude as ddmm.mmmm and longitude as dddmm.mmmm. To convert these to decimal degrees, we need to simply divide the minute (mm.mmmm) by 60.

(d)dd + mm.mmmm / 60

The first "d" is optional and only pertains to longitudinal coordinates.

Plotting these coordinates in google maps is easy. Just use the following URL:

http://maps.google.com/maps?q=<latitude>,<longitude>

Friday, February 14, 2014

Move over upstart and make way for systemd

Earlier this week the Debian technical committee voted to make systemd the default init system for Debian. It was a nail-biter to follow, and it essentially came down to a tie-breaker vote cast by Bdale Garbee.

The result is clear: systemd will be the default init system for Debian -- and also Ubuntu -- going forward.

Mark Shuttleworth of Canonical confirmed Ubuntu's decision to follow suit in a blog post called "Losing Graciously". No drama, no attempt to revote or repeal the vote.

If only politics worked that smoothly.

Now it's time to familiarize yourself with systemd. I recommend you start writing all future init scripts in systemd.

The time frame for obsolescence of upstart and sysVinit is unclear for Ubuntu, but I predict around 7 years, so you have time to migrate.

Here's how I got that number (remember that odd number of releases are 2 year support):

13.04 is already out and obviously not part of the decision
14.04 is too far into the development stages
15.04 nobody will use because it's not "true" LTS
16.04: the only logical next release to enforce this decision.

I'll be writing more about systemd in the future. First I'm going to begin my own conversation from upstart so I can figure out how to use the dang thing. I'm glad that now I can write a service that will work across multiple distros.

Saturday, January 25, 2014

Zabbix Server 2.2 with Percona 5.6 Auto Install on Ubuntu



Here's an easy-install for Zabbix server 2.2 running on Percona 5.6.


Zabbix is an enterprise monitoring tool. I've been using it for about two years and find it decent, though it does have shortfalls (especially when dealing with aggregate metrics). If you're dealing with less than two or three hundred hosts, then I think Zabbix is a great tool.

Percona is a high performance mysql fork. This auto-install will download the Zabbix source code and compile it. This is the easiest way I can find to installing Zabbix server without using legacy mysql software.

If you're looking for high-performance metric aggregation, then may I suggest the venerable OpenTSDB or the tried-and-true graphite? These tools can be easily integrated with Zabbix via their respective APIs.

How to install

Simply clone the repo and run:

./bootstrap

Requirements

There are a few things that are assumed. The first is that you don't have apache running or installed. If you do then beware this will nuke your default site vhost and overwrite it with a Zabbix specific one. It also assumes you don't have mysql installed.

As always you should inspect the code before running it on your system. Enjoy and let me know if you have any issues.

Wednesday, January 22, 2014

UDP only rsyslog server and client on Ubuntu

The first thing you'll need to do is install a new version of rsyslog. There's a bug in the version that comes along with Ubuntu 12.04 that prevents rsyslog from listening on a privileged port if you're using drop-privileges. You can read instructions for installing a newer version of rsyslog here. Once you've done that, open up /etc/rsyslog.conf and uncomment the following lines:

#$ModLoad imudp
#$UDPServerRun 514

Restart rsyslog and you're done on the server. Now head over to a client and open up /etc/rsyslog.d/50-default.conf and make the following addition to the top of the file:

*.* @your_server:514

Note that the single @ sign is what designates the sending to UDP only. Restart rsyslog on the server and you can test to see if logs are being sent using the logger command:

logger -t TEST testing

Wednesday, November 20, 2013

Easily convert hex to ints and vice versa with python and the command line

I've started a lot of work lately with the ssd1306 oled display (which you can pick up for a few bucks on ebay) and my raspbery pi. While sending commands I've found it useful to be able to easily convert between ints and hexadecimal strings and back again.

To convert a hexadecimal value to an int on the command line:

python -c 'print int(0x7f)'
127

And back again:

python -c 'print hex(127)
0x7f

For floats you can use the float.hex method:

python -c 'print float.hex(120.5)'
0x1.e200000000000p+6

Maybe you want that trimmed:
python -c 'print float.hex(120.5)[:7]'

0x1.e20

It's worth noting that the '-c' flag being passed to python essentially tells python that the first argument is a script it should execute normally.

Monday, November 18, 2013

Verifying JSON easily on the command line

You can pipe stdout into python -mjson.tool to validate it. It makes for quick and easy json validation on the command line.

We'll create a simple json file:

> somefile.txt
{"someval": "something", "anotherval": 3}

Now pipe this into json.tools and check the output.

$ cat somefile.txt | python -mjson.tool
{
    "anotherval": 3,
    "someval": "something"
}

Cool. It even formatted it nicely for us. Let's break it and see what happens.

Single quotes are not valid according rfc4627.

> somefile.txt
{'someval': 'something', 'anotherval': 3}

$ cat somefile.txt | python -mjson.tool
Expecting property name: line 1 column 2 (char 1)

Not the most useful traceback, but at least you know it's not valid.