Category Archives: UNIX & Linux Code Snippets

Nagios Plugin – check_mem

check_mem is a simple Nagios plugin to check memory utilisation on Linux servers. I have written both a bash and a Ruby version of this script.

bash version:

Ruby version:

 

How to Fix Perl Make Errors Under Solaris

When you build Perl modules under Solaris, they are optimised for Sun Studio, which of course, we all use :/ So if you build with gcc, the build will likely fail. To avoid this Perl make error, you can use the following magical one-liner to fix this brain damage, and your modules will build correctly.

GNU and UNIX Commands: Using a while Loop to Populate Configuration File

I recently had a need to generate a lot of Nagios host definitions. The hosts all had a similar naming convention:

foo-nnnnprjenv

Here, nnnn was a zero-padded number from 0 to 9999. In this particular environment, there were 41 hosts. A simple multiline echo command inside a while loop fed by seq produces the appropriate host stanzas we can add to our Nagios configuration. We also look up the servers IP addresses via the host command and insert those too:

The -w option to seq causes it to output a leading-zero padded number. Redirect the output of this loop to a file, and you’ll have your Nagios host configuration. This is another reasons why having a naming convention for a large number of servers is important – it will aid you in automation and administration down the track.

How to Selectively Remove Files

I wanted to remove a large number of files from a directory. However, I did not want to descend into subdirectories, nor did I want to remove any .pdf or .chm files. Some of the files had non-standard characters (such as quotes and spaces) in them.

The solution? A simple one liner using find and xargs:

 

How to Update a File on All Zones at the Same Time

I recently modified /etc/resolv.conf on all of my global zones after building new nameservers. I wanted a quick way to copy this updated configuration to all child zones. A simple one-liner does the trick on each global zone (assuming all your zones are in the /var/zones zonepath):


 

How to Use wget for Recursive get Without Traversing the Parent Directory

When grabbing files with wget, it is useful to sometimes not traverse parent directories. For example, say I want to download everything under http://www.example.com/my/home recursively, but not traverse upwards into parent directories. You can add the –no-parent option for this.


 

How to Generate a Hashed Password

Another OpenSSL related tip.

If you’ve ever wanted to generate a hashed password suitable for inclusion in the /etc/shadow file (for example, during post-install procedures such as sysidcfg), you can use the openssl passwd command:

 

Security: Replacing a Digital Certificate within a Java Keystore

Some notes I prepared whilst renewing a certificate stored in a Java keystore: