Using Go to write Nagios plugins is a simple, effective way to manage your checks and ensure they are running efficiently.

Although writing a Nagios plugin may vary from language to language in the specifics of it, the basic way in which it works is the same: Nagios will run a check on a host/service and then use the check's return code + its output as the result (OK/Warning/Critical).

This is no different in Go, however there are several advantages to creating your Nagios plugins in Go rather than Python, Perl, Powershell, etc.

Pros:

Portability Go is a compiled language, so you'll get a single executable that can run without dependencies on whatever platform you compile for.

Security Because it is compiled, the code cannot be easily tampered with by other people who should not be editing checks This helps prevent the multiple times in my environment where some particularly cavalier devs & sysadmins would edit checks to return false values in order to avoid having checks alert.

Speed Go is almost always faster than Python & comparable with languages like Java. Go's speed difference with Python (what many of our Nagios checks use) has so far always been orders of magnitude faster (ref: the Conclusion of this post)



Cons:

Unfamiliarity Go is only a little over a decade old, so developers aren't as familiar with it as other languages. Although, it is fairly easy to grasp if you have a background in C, C++, or Java.

Prototyping speed This ties into the "Unfamiliarity" category, but prototyping speed can initally be higher than, say, a language like Python. So, if a new check needs to be implemented ASAP, it would likely better to just use what is most familiar.



What You'll Need to Get Started

Three packages that you will almost always need to import are os , fmt , and flag .

The os package will allow you to call os.Exit(returncode) , which is necessary for Nagios to determine the check result.

The fmt package will allow you to call fmt.Println or fmt.Printf so that you can print the text output that your Nagios check will display.

Finally, the flag package allows you to specify command-line flags, which will allow you to create re-usable checks (e.g. run the same check on 2 machines, but have different thresholds for warning/critical alerts on the two of them).

Conclusion

This article is definitely intended as more of a "here's the tools to get you started" rather than a tutorial, but I've gone ahead and included an example Nagios plugin that I wrote in Go at the end of this article.

The following is a comparison of the execution time between the Go version of said plugin, and the Python version of it.



This plugin is orders of magnitude faster than the Python version of it that I wrote.

Although the speed difference is still in the milliseconds, that difference quickly becomes significant when executing large checks that normally take several seconds in their Python versions.

Example Check

Below is an example check that I wrote in Go that takes a license file, searches for a matching line (contains the expiration date), and sees how many days away the expiration date is.